def test_channel_wait(self): channel = Queue(0) events = [] def another_greenlet(): events.append('sending hello') channel.put('hello') events.append('sending world') channel.put('world') events.append('sent world') gt = spawn(another_greenlet) events.append('waiting') events.append(channel.get()) events.append(channel.get()) self.assertEqual( ['waiting', 'sending hello', 'hello', 'sending world', 'world'], events) sleep(0) self.assertEqual([ 'waiting', 'sending hello', 'hello', 'sending world', 'world', 'sent world' ], events)
def test_waiters_that_cancel(self): q = Queue() gt = spawn(do_bail, q) self.assertEquals(gt.wait(), 'timed out') q.put('hi') self.assertEquals(q.get(), 'hi')
def test_waiters_that_cancel (self): q = Queue() gt = spawn(do_bail, q) self.assertEquals(gt.wait(), 'timed out') q.put('hi') self.assertEquals(q.get(), 'hi')
def test_two_bogus_waiters(self): q = Queue() gt1 = spawn(do_bail, q) gt2 = spawn(do_bail, q) sleep(0) q.put('sent') self.assertEquals(gt1.wait(), 'timed out') self.assertEquals(gt2.wait(), 'timed out') self.assertEquals(q.get(), 'sent')
def test_two_bogus_waiters (self): q = Queue() gt1 = spawn(do_bail, q) gt2 = spawn(do_bail, q) sleep(0) q.put('sent') self.assertEquals(gt1.wait(), 'timed out') self.assertEquals(gt2.wait(), 'timed out') self.assertEquals(q.get(), 'sent')
def test_max_size (self): q = Queue(2) results = [] def putter (q): q.put('a') results.append('a') q.put('b') results.append('b') q.put('c') results.append('c') spawn(putter, q) sleep(0) self.assertEquals(results, ['a', 'b']) self.assertEquals(q.get(), 'a') sleep(0) self.assertEquals(results, ['a', 'b', 'c']) self.assertEquals(q.get(), 'b') self.assertEquals(q.get(), 'c')
def test_max_size(self): q = Queue(2) results = [] def putter(q): q.put('a') results.append('a') q.put('b') results.append('b') q.put('c') results.append('c') spawn(putter, q) sleep(0) self.assertEquals(results, ['a', 'b']) self.assertEquals(q.get(), 'a') sleep(0) self.assertEquals(results, ['a', 'b', 'c']) self.assertEquals(q.get(), 'b') self.assertEquals(q.get(), 'c')
def test_waiters_that_cancel (self): q = Queue() def do_receive (q, evt): Timeout(0, RuntimeError()) try: result = q.join() evt.send(result) except RuntimeError: evt.send('timed out') evt = Event() spawn(do_receive, q, evt) self.assertEquals(evt.wait(), 'timed out') q.put('hi') self.assertEquals(q.get(), 'hi')
def test_waiters_that_cancel(self): q = Queue() def do_receive(q, evt): Timeout(0, RuntimeError()) try: result = q.join() evt.send(result) except RuntimeError: evt.send('timed out') evt = Event() spawn(do_receive, q, evt) self.assertEquals(evt.wait(), 'timed out') q.put('hi') self.assertEquals(q.get(), 'hi')
def test_two_bogus_waiters(self): def do_receive(q, evt): Timeout(0, RuntimeError()) try: result = q.join() evt.send(result) except RuntimeError: evt.send('timed out') q = Queue() e1 = Event() e2 = Event() spawn(do_receive, q, e1) spawn(do_receive, q, e2) sleep(0) q.put('sent') self.assertEquals(e1.wait(), 'timed out') self.assertEquals(e2.wait(), 'timed out') self.assertEquals(q.get(), 'sent')
def test_two_bogus_waiters (self): def do_receive (q, evt): Timeout(0, RuntimeError()) try: result = q.join() evt.send(result) except RuntimeError: evt.send('timed out') q = Queue() e1 = Event() e2 = Event() spawn(do_receive, q, e1) spawn(do_receive, q, e2) sleep(0) q.put('sent') self.assertEquals(e1.wait(), 'timed out') self.assertEquals(e2.wait(), 'timed out') self.assertEquals(q.get(), 'sent')
def test_channel_wait (self): channel = Queue(0) events = [] def another_greenlet (): events.append('sending hello') channel.put('hello') events.append('sending world') channel.put('world') events.append('sent world') gt = spawn(another_greenlet) events.append('waiting') events.append(channel.get()) events.append(channel.get()) self.assertEqual(['waiting', 'sending hello', 'hello', 'sending world', 'world'], events) sleep(0) self.assertEqual( ['waiting', 'sending hello', 'hello', 'sending world', 'world', 'sent world'], events)
def test_putting_to_queue(self): timer = Timeout(0.1) try: size = 2 self.pool = IntPool(min_size=0, max_size=size) queue = Queue() results = [] def just_put(pool_item, index): self.pool.put(pool_item) queue.put(index) for index in xrange(size + 1): pool_item = self.pool.get() spawn(just_put, pool_item, index) for _ in range(size + 1): x = queue.get() results.append(x) self.assertEqual(sorted(results), range(size + 1)) finally: timer.cancel()
def test_putting_to_queue (self): timer = Timeout(0.1) try: size = 2 self.pool = IntPool(min_size = 0, max_size = size) queue = Queue() results = [] def just_put (pool_item, index): self.pool.put(pool_item) queue.put(index) for index in xrange(size + 1): pool_item = self.pool.get() spawn(just_put, pool_item, index) for _ in range(size + 1): x = queue.get() results.append(x) self.assertEqual(sorted(results), range(size + 1)) finally: timer.cancel()
def test_exhaustion(self): waiter = Queue(0) def consumer(): gotten = None try: gotten = self.pool.get() finally: waiter.put(gotten) spawn(consumer) one, two, three, four = (self.pool.get(), self.pool.get(), self.pool.get(), self.pool.get()) self.assertEquals(self.pool.free(), 0) # Let consumer run; nothing will be in the pool, so he will wait sleep(0) # Wake consumer self.pool.put(one) # wait for the consumer self.assertEquals(waiter.get(), one)
def test_exhaustion (self): waiter = Queue(0) def consumer (): gotten = None try: gotten = self.pool.get() finally: waiter.put(gotten) spawn(consumer) one, two, three, four = ( self.pool.get(), self.pool.get(), self.pool.get(), self.pool.get()) self.assertEquals(self.pool.free(), 0) # Let consumer run; nothing will be in the pool, so he will wait sleep(0) # Wake consumer self.pool.put(one) # wait for the consumer self.assertEquals(waiter.get(), one)
def test_send_first(self): q = Queue() q.put('hi') self.assertEquals(q.get(), 'hi')
def test_getting_before_sending (self): q = Queue() gt = spawn(q.put, 'sent') self.assertEquals(q.get(), 'sent') gt.wait()
def test_getting_before_sending(self): q = Queue() gt = spawn(q.put, 'sent') self.assertEquals(q.get(), 'sent') gt.wait()
def test_send_first (self): q = Queue() q.put('hi') self.assertEquals(q.get(), 'hi')