Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    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')
Ejemplo n.º 3
0
    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')
Ejemplo n.º 4
0
 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')
Ejemplo n.º 5
0
 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')
Ejemplo n.º 6
0
    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')
Ejemplo n.º 7
0
    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')
Ejemplo n.º 8
0
    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')
Ejemplo n.º 9
0
    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')
Ejemplo n.º 10
0
    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')
Ejemplo n.º 11
0
    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')
Ejemplo n.º 12
0
    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)
Ejemplo n.º 13
0
    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()
Ejemplo n.º 14
0
    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()
Ejemplo n.º 15
0
    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)
Ejemplo n.º 16
0
    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)
Ejemplo n.º 17
0
 def test_send_first(self):
     q = Queue()
     q.put('hi')
     self.assertEquals(q.get(), 'hi')
Ejemplo n.º 18
0
 def test_getting_before_sending (self):
     q = Queue()
     gt = spawn(q.put, 'sent')
     self.assertEquals(q.get(), 'sent')
     gt.wait()
Ejemplo n.º 19
0
 def test_getting_before_sending(self):
     q = Queue()
     gt = spawn(q.put, 'sent')
     self.assertEquals(q.get(), 'sent')
     gt.wait()
Ejemplo n.º 20
0
 def test_send_first (self):
     q = Queue()
     q.put('hi')
     self.assertEquals(q.get(), 'hi')