Exemplo n.º 1
0
    def test_put_get(self):
        """
            Simple put/get test
        """
        testitem = {"foo": "bar"}
        channel = Channel(1, loop=self.loop)
        self.ruc(channel.put(testitem))

        self.assertEqual(channel.qsize(), 1)
        self.assertTrue(channel.full())
        self.assertFalse(channel.empty())

        item = self.ruc(channel.get())
        self.assertEqual(item, testitem)
        self.assertEqual(channel.qsize(), 0)
        self.assertFalse(channel.full())
        self.assertTrue(channel.empty())
Exemplo n.º 2
0
    def test_put_get(self):
        """
            Simple put/get test
        """
        testitem = {"foo": "bar"}
        channel = Channel(1, loop=self.loop)
        self.ruc(channel.put(testitem))

        self.assertEqual(channel.qsize(), 1)
        self.assertTrue(channel.full())
        self.assertFalse(channel.empty())

        item = self.ruc(channel.get())
        self.assertEqual(item, testitem)
        self.assertEqual(channel.qsize(), 0)
        self.assertFalse(channel.full())
        self.assertTrue(channel.empty())
Exemplo n.º 3
0
    def test_putter_cancel(self):
        channel = Channel(1, loop=self.loop)
        self.ruc(channel.put("foo"))
        # next put will block as channel is full
        self.assertTrue(channel.full())

        @asyncio.coroutine
        def test_put():
            yield from channel.put("bar")

        @asyncio.coroutine
        def test_cancel():
            yield from asyncio.sleep(0.01, loop=self.loop)
            channel._putters[0].cancel()

        result = self.rucgather(test_put(), test_cancel(), return_exceptions=True)
        self.assertIsInstance(result[0], asyncio.CancelledError)
Exemplo n.º 4
0
    def test_put_throws_channel_closed(self):
        """
            Test that when a put blocks, and a channel is closed, the
            put will throw a ChannelClosed instead of waiting to add to channel
        """
        channel = Channel(1, loop=self.loop)
        channel.put_nowait("foo")
        self.assertTrue(channel.full())

        @asyncio.coroutine
        def wait_close():
            yield from asyncio.sleep(0.01, loop=self.loop)
            channel.close()

        (put_return, _) = self.rucgather(channel.put("bar"), wait_close(), return_exceptions=True)
        self.assertIsInstance(put_return, ChannelClosed)
        self.assertTrue(channel.closed())
Exemplo n.º 5
0
    def test_putter_exception(self):
        channel = Channel(1, loop=self.loop)
        self.ruc(channel.put("foo"))
        # next put will block as channel is full
        self.assertTrue(channel.full())

        @asyncio.coroutine
        def test_put():
            yield from channel.put("bar")

        @asyncio.coroutine
        def test_cancel():
            yield from asyncio.sleep(0.01, loop=self.loop)
            channel._maxsize = 2  # For hitting a different code branch in Channel
            channel._putters[0].set_exception(TypeError('random type error'))

        result = self.rucgather(test_put(), test_cancel(), return_exceptions=True)
        self.assertIsInstance(result[0], TypeError)
Exemplo n.º 6
0
    def test_putter_cancel(self):
        channel = Channel(1, loop=self.loop)
        self.ruc(channel.put("foo"))
        # next put will block as channel is full
        self.assertTrue(channel.full())

        @asyncio.coroutine
        def test_put():
            yield from channel.put("bar")

        @asyncio.coroutine
        def test_cancel():
            yield from asyncio.sleep(0.01, loop=self.loop)
            channel._putters[0].cancel()

        result = self.rucgather(test_put(),
                                test_cancel(),
                                return_exceptions=True)
        self.assertIsInstance(result[0], asyncio.CancelledError)
Exemplo n.º 7
0
    def test_put_throws_channel_closed(self):
        """
            Test that when a put blocks, and a channel is closed, the
            put will throw a ChannelClosed instead of waiting to add to channel
        """
        channel = Channel(1, loop=self.loop)
        channel.put_nowait("foo")
        self.assertTrue(channel.full())

        @asyncio.coroutine
        def wait_close():
            yield from asyncio.sleep(0.01, loop=self.loop)
            channel.close()

        (put_return, _) = self.rucgather(channel.put("bar"),
                                         wait_close(),
                                         return_exceptions=True)
        self.assertIsInstance(put_return, ChannelClosed)
        self.assertTrue(channel.closed())
Exemplo n.º 8
0
    def test_putter_exception(self):
        channel = Channel(1, loop=self.loop)
        self.ruc(channel.put("foo"))
        # next put will block as channel is full
        self.assertTrue(channel.full())

        @asyncio.coroutine
        def test_put():
            yield from channel.put("bar")

        @asyncio.coroutine
        def test_cancel():
            yield from asyncio.sleep(0.01, loop=self.loop)
            channel._maxsize = 2  # For hitting a different code branch in Channel
            channel._putters[0].set_exception(TypeError('random type error'))

        result = self.rucgather(test_put(),
                                test_cancel(),
                                return_exceptions=True)
        self.assertIsInstance(result[0], TypeError)
Exemplo n.º 9
0
    def test_multiple_blocking_puts(self):
        """
            Test that a channel with multiple running put() still works
            out fine when the channel is closed
        """
        channel = Channel(1, loop=self.loop)
        channel.put_nowait("foo")
        self.assertTrue(channel.full())

        @asyncio.coroutine
        def wait_close():
            yield from asyncio.sleep(0.01, loop=self.loop)
            channel.close()

        futures = [channel.put(i) for i in range(100)]
        futures.insert(50, wait_close())

        result = self.rucgather(*futures, return_exceptions=True)
        result.pop(50)  # pop the result for wait_close()
        for res in result:
            self.assertIsInstance(res, ChannelClosed)
Exemplo n.º 10
0
    def test_multiple_blocking_puts(self):
        """
            Test that a channel with multiple running put() still works
            out fine when the channel is closed
        """
        channel = Channel(1, loop=self.loop)
        channel.put_nowait("foo")
        self.assertTrue(channel.full())

        @asyncio.coroutine
        def wait_close():
            yield from asyncio.sleep(0.01, loop=self.loop)
            channel.close()

        futures = [channel.put(i) for i in range(100)]
        futures.insert(50, wait_close())

        result = self.rucgather(*futures, return_exceptions=True)
        result.pop(50)  # pop the result for wait_close()
        for res in result:
            self.assertIsInstance(res, ChannelClosed)
Exemplo n.º 11
0
 def test_put_when_closed(self):
     channel = Channel(1, loop=self.loop)
     channel.close()
     self.assertRaises(ChannelClosed, lambda: self.ruc(channel.put("foo")))
Exemplo n.º 12
0
 def test_put_when_closed(self):
     channel = Channel(1, loop=self.loop)
     channel.close()
     self.assertRaises(ChannelClosed, lambda: self.ruc(channel.put("foo")))