def test_when_empty(env): def proc(env, queue): yield queue.when_empty() assert env.now == 0 yield queue.put('a') yield queue.put('b') with queue.when_empty() as when_empty_ev: assert not when_empty_ev.triggered yield env.timeout(1) item = yield queue.get() assert item == 'a' assert not when_empty_ev.triggered with queue.when_empty() as when_empty_ev: assert not when_empty_ev.triggered yield env.timeout(1) with queue.get() as get_ev: item = yield get_ev assert item == 'b' assert when_empty_ev.triggered yield when_empty_ev env.process(proc(env, Queue(env))) env.run()
def test_when_at_most(env): def proc(env, queue): for item in 'abc': with queue.put(item) as put_ev: yield put_ev at_most = {} at_most[0] = queue.when_at_most(0) at_most[3] = queue.when_at_most(3) at_most[1] = queue.when_at_most(1) at_most[2] = queue.when_at_most(2) assert not at_most[0].triggered assert not at_most[1].triggered assert not at_most[2].triggered assert at_most[3].triggered item = yield queue.get() assert item == 'a' assert not at_most[0].triggered assert not at_most[1].triggered assert at_most[2].triggered item = yield queue.get() assert item == 'b' assert not at_most[0].triggered assert at_most[1].triggered item = yield queue.get() assert item == 'c' assert at_most[0].triggered env.process(proc(env, Queue(env))) env.run()
def test_mq_when_full(env): queue = Queue(env, capacity=2) result = [] def producer(env): yield env.timeout(1) for i in range(5): yield queue.put(i) yield env.timeout(1) def consumer(env): yield env.timeout(5) for i in range(3): msg = yield queue.get() assert msg == i def full_waiter(env): yield queue.when_full() result.append('full') def any_waiter(env): yield queue.when_any() assert env.now == 1 result.append('any') env.process(producer(env)) env.process(consumer(env)) env.process(full_waiter(env)) env.process(any_waiter(env)) env.process(any_waiter(env)) env.run() assert queue.items assert queue.is_full assert 'full' in result assert result.count('any') == 2
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.pump_rate = self.env.config.get('tanker.pump_rate', 10) self.avg_travel = self.env.config.get('tanker.travel_time', 600) tank_capacity = self.env.config.get('tanker.capacity', 200) self.tank = Pool(self.env, tank_capacity) # This auto_probe() call uses the self.tank Pool get/put hooks so that # whenever it's level changes, the new level is noted in the log. self.auto_probe('tank', log={}) # The parent TankerCompany enqueues instructions to this queue. self._instructions = Queue(self.env) # Declare a persistant process to be started at simulation-time. self.add_process(self._dispatch_loop)
def test_when_at_least(env): def proc(env, queue): at_least = {} at_least[3] = queue.when_at_least(3) at_least[0] = queue.when_at_least(0) at_least[2] = queue.when_at_least(2) at_least[1] = queue.when_at_least(1) assert at_least[0].triggered assert not at_least[1].triggered assert not at_least[2].triggered assert not at_least[3].triggered yield queue.put('a') assert at_least[1].triggered assert not at_least[2].triggered assert not at_least[3].triggered yield queue.get() assert not at_least[2].triggered assert not at_least[3].triggered yield queue.put('b') assert not at_least[2].triggered assert not at_least[3].triggered yield queue.put('c') assert at_least[2].triggered assert not at_least[3].triggered yield queue.put('d') assert at_least[3].triggered env.process(proc(env, Queue(env))) env.run()
def __init__(self, *args, **kwargs): super(TopTest, self).__init__(*args, **kwargs) self.container = simpy.Container(self.env) self.resource = simpy.Resource(self.env) self.queue = Queue(self.env) self.pool = Pool(self.env) self.a = CompA(self) self.b = CompB(self) hints = {} if self.env.config['sim.log.enable']: hints['log'] = {'level': 'INFO'} if self.env.config['sim.vcd.enable']: hints['vcd'] = {} if self.env.config['sim.db.enable']: hints['db'] = {} self.auto_probe('container', **hints) self.auto_probe('resource', **hints) self.auto_probe('queue', **hints) self.auto_probe('pool', **hints) self.trace_some = self.get_trace_function('something', vcd={'var_type': 'real'}, log={'level': 'INFO'}) self.trace_other = self.get_trace_function('otherthing', vcd={ 'var_type': 'integer', 'init': ('z', 'z'), 'size': (8, 8) }) self.add_process(self.loop)
def __init__(self, *args, **kwargs): super(TankerTruck, self).__init__(*args, **kwargs) self.pump_rate = self.env.config.get('tanker.pump_rate', 10) self.avg_travel = self.env.config.get('tanker.travel_time', 600) tank_capacity = self.env.config.get('tanker.capacity', 200) self.tank = Container(self.env, tank_capacity) # This auto_probe() call causes the self.tank Container to be # monkey-patched such that whenever it's level changes, the new level # is noted in the log. self.auto_probe('tank', log={}) # The parent TankerCompany enqueues instructions to this queue. self._instructions = Queue(self.env) # Declare a persistent process to be started at simulation-time. self.add_process(self._dispatch_loop)
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.cashier = Cashier(self) self.customer_queue = Resource(self.env) self.auto_probe('customer_queue', trace_queue=True, vcd={'init': 0}) feed_capacity = self.env.config['checkout.feed_capacity'] self.feed_belt = Queue(self.env, capacity=feed_capacity) self.auto_probe('feed_belt', vcd={}) bag_area_capacity = self.env.config['checkout.bag_area_capacity'] self.bag_area = Queue(self.env, capacity=bag_area_capacity) self.auto_probe('bag_area', vcd={}) self.baggers = Container(self.env) self.auto_probe('baggers', vcd={})
def test_queue_peek(env): queue = Queue(env) assert queue.is_empty with raises(IndexError): queue.peek() queue2 = Queue(env, items=[9, 8, 7]) assert not queue2.is_empty assert queue2.peek() == 9
def test_queue_cancel(env): queue = Queue(env, capacity=2) def producer(env): for i in range(5): yield env.timeout(5) yield queue.put(i) def consumer(env): for i in range(3): yield env.timeout(10) msg = yield queue.get() assert msg == i def canceller(env): any_ev = queue.when_any() get_ev = queue.get() full_ev = queue.when_full() yield env.timeout(1) assert not get_ev.triggered assert not any_ev.triggered assert not full_ev.triggered get_ev.cancel() any_ev.cancel() full_ev.cancel() assert not queue.is_full with queue.when_full() as when_full: yield when_full with queue.put(1) as put_ev: not_full_ev = queue.when_not_full() yield env.timeout(1) assert not put_ev.triggered assert not not_full_ev.triggered not_full_ev.cancel() yield env.timeout(100) assert not get_ev.triggered assert not any_ev.triggered assert not put_ev.triggered assert not put_ev.triggered assert not not_full_ev.triggered env.process(producer(env)) env.process(consumer(env)) env.process(canceller(env)) env.run()
def test_queue_overflow(env): def proc(env, queue): yield queue.put(1) yield env.timeout(1) yield queue.put(1) yield env.timeout(1) with raises(OverflowError): yield queue.put(1) queue = Queue(env, capacity=2, hard_cap=True) env.process(proc(env, queue)) env.run()
class CheckoutLane(Component): base_name = 'lane' def __init__(self, *args, **kwargs): super(CheckoutLane, self).__init__(*args, **kwargs) self.queue = Queue(self.env) self.auto_probe('queue', vcd={}) self.add_process(self.checkout) def __lt__(self, other): return self.queue.size < other.queue.size def enqueue(self, num_items, done_event): return self.queue.put((num_items, done_event)) def checkout(self): scan_dist = partial(self.env.rand.expovariate, 1 / self.env.config['grocery.scan_time']) while True: num_items, done_event = yield self.queue.get() for n in range(num_items): yield self.env.timeout(scan_dist()) done_event.succeed()
def test_attach_queue_size(env): values = [] queue = Queue(env) attach('scope', queue, [values.append]) def proc(): yield queue.put('item0') yield queue.put('item1') yield queue.put('item2') item = yield queue.get() assert item == 'item0' env.process(proc()) env.run() assert values == [1, 2, 3, 2]
def __init__(self, *args, **kwargs): super(TankerTruck, self).__init__(*args, **kwargs) self.pump_rate = self.env.config.get('tanker.pump_rate', 10) self.avg_travel = self.env.config.get('tanker.travel_time', 600) tank_capacity = self.env.config.get('tanker.capacity', 200) self.tank = Pool(self.env, tank_capacity) # This auto_probe() call uses the self.tank Pool get/put hooks so that # whenever it's level changes, the new level is noted in the log. self.auto_probe('tank', log={}) # The parent TankerCompany enqueues instructions to this queue. self._instructions = Queue(self.env) # Declare a persistant process to be started at simulation-time. self.add_process(self._dispatch_loop)
def test_attach_queue_remaining(env): values = [] queue = Queue(env, capacity=10) attach('scope', queue, [values.append], trace_remaining=True) def proc(): yield queue.put('item0') yield queue.put('item1') yield queue.put('item2') item = yield queue.get() assert item == 'item0' env.process(proc()) env.run() assert values == [9, 8, 7, 8]
def test_mq(env): queue = Queue(env, capacity=2) def producer(msg, wait): yield env.timeout(wait) yield queue.put(msg) def consumer(expected_msg, wait): yield env.timeout(wait) msg = yield queue.get() assert msg == expected_msg env.process(producer('1st', 0)) env.process(producer('2nd', 1)) env.process(consumer('1st', 0)) env.process(consumer('2nd', 1)) env.run()
def test_when_not_full(env): queue = Queue(env, capacity=2, items=[0, 1]) def consumer(env): for i in range(2): yield env.timeout(3) msg = yield queue.get() assert msg == i def not_full_waiter(env): yield queue.when_not_full() assert env.now == 3 yield queue.when_not_full() assert env.now == 3 env.process(consumer(env)) env.process(not_full_waiter(env)) env.run()
class TankerTruck(Component): """Tanker trucks carry fuel to gas stations. Each tanker truck has a queue of gas stations it must visit. When the truck's tank becomes empty, it must go refill itself. """ base_name = 'truck' def __init__(self, *args, **kwargs): super(TankerTruck, self).__init__(*args, **kwargs) self.pump_rate = self.env.config.get('tanker.pump_rate', 10) self.avg_travel = self.env.config.get('tanker.travel_time', 600) tank_capacity = self.env.config.get('tanker.capacity', 200) self.tank = Container(self.env, tank_capacity) # This auto_probe() call causes the self.tank Container to be # monkey-patched such that whenever it's level changes, the new level # is noted in the log. self.auto_probe('tank', log={}) # The parent TankerCompany enqueues instructions to this queue. self._instructions = Queue(self.env) # Declare a persistant process to be started at simulation-time. self.add_process(self._dispatch_loop) def dispatch(self, gas_station, done_event): """Append dispatch instructions to the truck's queue.""" return self._instructions.put((gas_station, done_event)) def _dispatch_loop(self): """This is the tanker truck's main behavior. Travel, pump, refill...""" while True: if not self.tank.level: self.info('going for refill') # Desmod simulation environments come equipped with a # random.Random() instance seeded based on the 'sim.seed' # configuration key. travel_time = self.env.rand.expovariate(1 / self.avg_travel) yield self.env.timeout(travel_time) self.info('refilling') pump_time = self.tank.capacity / self.pump_rate yield self.env.timeout(pump_time) yield self.tank.put(self.tank.capacity) self.info('refilled {}L in {:.0f}s'.format( self.tank.capacity, pump_time)) gas_station, done_event = yield self._instructions.get() self.info('traveling to {}'.format(gas_station.name)) travel_time = self.env.rand.expovariate(1 / self.avg_travel) yield self.env.timeout(travel_time) self.info('arrived at {}'.format(gas_station.name)) while self.tank.level and (gas_station.reservoir.level < gas_station.reservoir.capacity): yield self.env.timeout(1 / self.pump_rate) yield gas_station.reservoir.put(1) yield self.tank.get(1) self.info('done pumping') done_event.succeed()
class TankerTruck(Component): """Tanker trucks carry fuel to gas stations. Each tanker truck has a queue of gas stations it must visit. When the truck's tank becomes empty, it must go refill itself. """ base_name = 'truck' def __init__(self, *args, **kwargs): super(TankerTruck, self).__init__(*args, **kwargs) self.pump_rate = self.env.config.get('tanker.pump_rate', 10) self.avg_travel = self.env.config.get('tanker.travel_time', 600) tank_capacity = self.env.config.get('tanker.capacity', 200) self.tank = Container(self.env, tank_capacity) # This auto_probe() call causes the self.tank Container to be # monkey-patched such that whenever it's level changes, the new level # is noted in the log. self.auto_probe('tank', log={}) # The parent TankerCompany enqueues instructions to this queue. self._instructions = Queue(self.env) # Declare a persistant process to be started at simulation-time. self.add_process(self._dispatch_loop) def dispatch(self, gas_station, done_event): """Append dispatch instructions to the truck's queue.""" return self._instructions.put((gas_station, done_event)) def _dispatch_loop(self): """This is the tanker truck's main behavior. Travel, pump, refill...""" while True: if not self.tank.level: self.info('going for refill') # Desmod simulation environments come equipped with a # random.Random() instance seeded based on the 'sim.seed' # configuration key. travel_time = self.env.rand.expovariate(1 / self.avg_travel) yield self.env.timeout(travel_time) self.info('refilling') pump_time = self.tank.capacity / self.pump_rate yield self.env.timeout(pump_time) yield self.tank.put(self.tank.capacity) self.info('refilled {}L in {:.0f}s'.format( self.tank.capacity, pump_time)) gas_station, done_event = yield self._instructions.get() self.info('traveling to {}'.format(gas_station.name)) travel_time = self.env.rand.expovariate(1 / self.avg_travel) yield self.env.timeout(travel_time) self.info('arrived at {}'.format(gas_station.name)) while self.tank.level and (gas_station.reservoir.level < gas_station.reservoir.capacity): yield self.env.timeout(1 / self.pump_rate) yield gas_station.reservoir.put(1) yield self.tank.get(1) self.info('done pumping') done_event.succeed()
def test_queue_repr(env): queue = Queue(env, name='hi', items=[3, 2, 1]) assert str(queue) == "Queue(name='hi' size=3 capacity=inf)" pri_queue = PriorityQueue(env, capacity=3) assert str(pri_queue) == 'PriorityQueue(name=None size=0 capacity=3)'
def __init__(self, *args, **kwargs): super(CheckoutLane, self).__init__(*args, **kwargs) self.queue = Queue(self.env) self.auto_probe('queue', vcd={}) self.add_process(self.checkout)