-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
164 lines (112 loc) · 4.26 KB
/
run.py
File metadata and controls
164 lines (112 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from distributed_sales_system.customer import Customer
from distributed_sales_system.producer import Producer
from distributed_sales_system import stop_producer
from distributed_sales_system.product_register import product_register
from random import randint, sample
def BasicCommunicationTest():
producer1 = Producer('producer_1', products=["apple", "pear", "banana"])
customer1 = Customer('customer_1', 1, {"apple": 2})
#flow
customer1.start()
producer1.start()
customer1.join()
stop_producer.set()
def CustomerPreferenceTest():
producer1 = Producer('producer_1', products=["apple", "pear", "banana"])
producer2 = Producer('producer_2', products={"apple": {'price': 0.9}})
customer1 = Customer('customer_1', 1, {"apple": 2})
customer2 = Customer('customer_2', 1, {"apple": 3, "pear": 2})
producer1.start()
producer2.start()
# testcase #1: customer prefers lower prices
customer1.start()
customer1.join()
# testcase #2: customer prefers more types of items, even if they're more expensive
customer2.start()
customer2.join()
stop_producer.set()
def AcceptRefuseOrderTest():
producer1 = Producer('producer_1', products=["apple", "pear", "banana"])
customer1 = Customer('customer_1', 1, {"apple": 4})
customer2 = Customer('customer_2', 1, {"apple": 4})
producer1.start()
customer1.start()
customer2.start()
customer1.join()
customer2.join()
stop_producer.set()
def ProducerWithZeroStock():
producer1 = Producer('producer_1', products={"apple": {'amount': 0, 'create_amount': 0}})
customer1 = Customer('customer_1', 1, {"apple": 4})
producer1.start()
customer1.start()
customer1.join()
stop_producer.set()
def PartialOfferTest():
producer1 = Producer('producer_1', products=["apple", "pear", "banana"])
producer2 = Producer('producer_2', products=["apple", "banana", "x"])
customer1 = Customer('customer_1', 1, {"apple": 10, "pear":3, "x": 5})
producer1.start()
producer2.start()
customer1.start()
customer1.join()
stop_producer.set()
def NonExistentProductOrderTest():
producer1 = Producer('producer_1', products=["apple", "pear", "banana"])
customer1 = Customer('customer_1', 1, {"pjoter": 10})
producer1.start()
customer1.start()
customer1.join()
stop_producer.set()
def DiscountTest():
producer1 = Producer('producer_1', products={"apple": {'price': 10, 'amount': 30}, "pear": {}, "banana": {}, "orange": {}})
customer1 = Customer('customer_1', 2, {"apple": 10})
producer1.start()
customer1.start()
customer1.join()
stop_producer.set()
def ProductGeneratorTest():
import time
producer1 = Producer('producer_1', products={"apple": {'create_time': 1, 'create_amount': 10},
"pear": {'create_time': 2, 'create_amount': 20},
"banana": {'create_time': 4, 'create_amount': 30},
"orange": {'create_time': 5, 'create_amount': 40}})
producer1.start()
time.sleep(10)
stop_producer.set()
def AddNewProductsTest():
import time
producer1 = Producer('producer_1', products={"apple": {'create_time': 2, 'create_amount': 10}})
producer1.start()
time.sleep(5)
with producer1.warehouse_lock:
producer1.add_product("banana", create_time=1)
time.sleep(5)
with producer1.warehouse_lock:
producer1.delete_product("apple")
time.sleep(5)
stop_producer.set()
def EnduranceTest():
customers = []
# producers = []
for i in range(10):
prod = Producer(f"producer_{i}", products=sample(product_register, randint(1,len(product_register))))
prod.start()
for i in range(20):
cust = Customer(f"customer_{i}", 1)
customers.append(cust)
cust.start()
for cust in customers:
cust.join()
stop_producer.set()
if __name__ == "__main__":
# BasicCommunicationTest()
# CustomerPreferenceTest()
# AcceptRefuseOrderTest()
# ProducerWithZeroStock()
# PartialOfferTest()
# NonExistentProductOrderTest()
# DiscountTest()
# ProductGeneratorTest()
# AddNewProductsTest()
EnduranceTest()