示例#1
0
    def test_acquire(self):
        pool = Pool(url="ws://localhost:8182/",
                    maxsize=2,
                    username="******",
                    password="******",
                    loop=self.loop)

        @asyncio.coroutine
        def go():
            connection = yield from pool.acquire()
            conn = connection.conn
            self.assertFalse(conn.closed)
            self.assertIsInstance(conn, Response)
            self.assertEqual(pool.size, 1)
            self.assertTrue(connection in pool._acquired)
            connection2 = yield from pool.acquire()
            conn2 = connection2.conn
            self.assertFalse(conn2.closed)
            self.assertIsInstance(conn2, Response)
            self.assertEqual(pool.size, 2)
            self.assertTrue(connection2 in pool._acquired)
            yield from connection.close()
            yield from connection2.close()

        self.loop.run_until_complete(go())
 async def go():
     pool1 = Pool(url="ws://localhost:8182/",
                  maxsize=2,
                  username="******",
                  password="******",
                  loop=self.loop)
     pool2 = Pool(url="ws://localhost:8182/",
                  username="******",
                  password="******",
                  loop=self.loop)
     conn1 = await pool1.acquire()
     conn2 = await pool2.acquire()
     conn3 = await pool2.acquire()
     conn4 = await pool2.acquire()
     pool1.pool.append(conn2)
     pool1.pool.append(conn3)
     pool1.pool.append(conn4)
     await pool1.release(conn1)
     self.assertTrue(conn1.closed)
     await conn1.close()
     await pool1.close()
    def test_release_acquire(self):
        pool = Pool(url="ws://localhost:8182/",
                    maxsize=2,
                    username="******",
                    password="******",
                    loop=self.loop)

        async def go():
            c1 = await pool.acquire()
            await pool.release(c1)
            c2 = await pool.acquire()
            self.assertEqual(c1, c2)
            await c2.close()

        self.loop.run_until_complete(go())
示例#4
0
    def test_release_acquire(self):
        pool = Pool(url="ws://localhost:8182/",
                    maxsize=2,
                    username="******",
                    password="******",
                    loop=self.loop)

        @asyncio.coroutine
        def go():
            c1 = yield from pool.acquire()
            yield from pool.release(c1)
            c2 = yield from pool.acquire()
            self.assertEqual(c1, c2)
            yield from c2.close()

        self.loop.run_until_complete(go())
    def test_release(self):
        pool = Pool(url="ws://localhost:8182/",
                    maxsize=2,
                    username="******",
                    password="******",
                    loop=self.loop)

        async def go():
            self.assertEqual(len(pool.pool), 0)
            c1 = await pool.acquire()
            self.assertEqual(len(pool._acquired), 1)
            await pool.release(c1)
            self.assertEqual(len(pool.pool), 1)
            self.assertEqual(len(pool._acquired), 0)
            await c1.close()
        self.loop.run_until_complete(go())
    def test_cancelled(self):
        pool = Pool(url="ws://localhost:8182/",
                    maxsize=2,
                    username="******",
                    password="******",
                    loop=self.loop)

        async def go():
            c1 = await pool.acquire()
            c2 = await pool.acquire()
            c3 = pool.acquire()
            await pool.close()
            self.assertTrue(c3.cancelled())
            await c1.close()
            await c2.close()

        self.loop.run_until_complete(go())
示例#7
0
    def test_cancelled(self):
        pool = Pool(url="ws://localhost:8182/",
                    maxsize=2,
                    username="******",
                    password="******",
                    loop=self.loop)

        @asyncio.coroutine
        def go():
            c1 = yield from pool.acquire()
            c2 = yield from pool.acquire()
            c3 = pool.acquire()
            yield from pool.close()
            self.assertTrue(c3.cancelled())
            yield from c1.close()
            yield from c2.close()

        self.loop.run_until_complete(go())
    def test_maxsize(self):
        pool = Pool(url="ws://localhost:8182/",
                    maxsize=2,
                    username="******",
                    password="******",
                    loop=self.loop)

        async def go():
            c1 = await pool.acquire()
            c2 = await pool.acquire()
            c3 = pool.acquire()
            self.assertIsInstance(c3, asyncio.Future)
            with self.assertRaises(asyncio.TimeoutError):
                await asyncio.wait_for(c3, 0.1, loop=self.loop)
            await c1.close()
            await c2.close()

        self.loop.run_until_complete(go())
    def test_self_release(self):
        pool = Pool(url="ws://localhost:8182/",
                    maxsize=2,
                    username="******",
                    password="******",
                    force_release=True,
                    loop=self.loop)

        async def go():
            self.assertEqual(len(pool.pool), 0)
            c1 = await pool.acquire()
            self.assertEqual(len(pool._acquired), 1)
            stream = c1.send("1 + 1")
            resp = await stream.read()
            self.assertEqual(len(pool.pool), 1)
            self.assertEqual(len(pool._acquired), 0)
            await c1.close()

        self.loop.run_until_complete(go())
    def test_acquire_send(self):
        pool = Pool(url="ws://localhost:8182/",
                    maxsize=2,
                    username="******",
                    password="******",
                    loop=self.loop)

        async def go():
            connection = await pool.acquire()
            resp = connection.send("1 + 1")
            while True:
                msg = await resp.read()
                if msg is None:
                    break
                self.assertEqual(msg.status_code, 200)
                self.assertEqual(msg.data[0], 2)
            await connection.close()

        self.loop.run_until_complete(go())
示例#11
0
 def go():
     pool1 = Pool(url="ws://localhost:8182/",
                  maxsize=2,
                  username="******",
                  password="******",
                  loop=self.loop)
     pool2 = Pool(url="ws://localhost:8182/",
                  username="******",
                  password="******",
                  loop=self.loop)
     conn1 = yield from pool1.acquire()
     conn2 = yield from pool2.acquire()
     conn3 = yield from pool2.acquire()
     conn4 = yield from pool2.acquire()
     pool1.pool.append(conn2)
     pool1.pool.append(conn3)
     pool1.pool.append(conn4)
     yield from pool1.release(conn1)
     self.assertTrue(conn1.closed)
     yield from conn1.close()
     yield from pool1.close()