Example #1
0
    def test_not_setted_connection_pool_connection_keepalive(self):
        from thrift_connector.tornado import TornadoClientPool
        pool = TornadoClientPool(
            self.pingpong_thrift_client.pool.service,
            self.pingpong_thrift_client.host,
            self.pingpong_thrift_client.port,
            name=self.pingpong_thrift_client.pool.name,
            raise_empty=False,
            max_conn=3,
            connection_class=self.pingpong_thrift_client.pool.connection_class)
        self.assertIs(pool.keepalive, None)
        with (yield pool.connection_ctx()) as conn:
            now = time.time()
            self.assertIs(conn.alive_until, None)
            self.assertTrue((yield conn.test_connection()))
            old_connection = conn

        with fake_datetime(now + 0.1):
            with (yield pool.connection_ctx()) as conn:
                self.assertIs(conn, old_connection)

        with fake_datetime(now + timedelta(days=100).seconds):
            self.assertTrue((yield old_connection.test_connection()))

            with (yield pool.connection_ctx()) as conn:
                self.assertIs(old_connection, conn)
Example #2
0
    def test_setted_connection_pool_connection_keepalive(self):
        keep_alive = 1
        from thrift_connector.tornado import TornadoClientPool
        pool = TornadoClientPool(
            self.pingpong_thrift_client.pool.service,
            self.pingpong_thrift_client.host,
            self.pingpong_thrift_client.port,
            name=self.pingpong_thrift_client.pool.name,
            raise_empty=False,
            max_conn=3,
            connection_class=self.pingpong_thrift_client.pool.connection_class,
            keepalive=keep_alive)
        self.assertEqual(pool.keepalive, keep_alive)
        with (yield pool.connection_ctx()) as conn:
            now = time.time()
            self.assertEqual(int(conn.alive_until), int(now + keep_alive))
            self.assertTrue((yield conn.test_connection()))
            old_connection = conn

        with fake_datetime(now + 0.1):
            with (yield pool.connection_ctx()) as conn:
                self.assertIs(conn, old_connection)

        with fake_datetime(now + keep_alive + 2):
            self.assertFalse((yield old_connection.test_connection()))

            with (yield pool.connection_ctx()) as conn:
                self.assertIsNot(old_connection, conn)
Example #3
0
    def test_not_setted_connection_pool_connection_keepalive(self):
        from thrift_connector.tornado import TornadoClientPool
        pool = TornadoClientPool(
            self.pingpong_thrift_client.pool.service,
            self.pingpong_thrift_client.host,
            self.pingpong_thrift_client.port,
            name=self.pingpong_thrift_client.pool.name,
            raise_empty=False, max_conn=3,
            connection_class=self.pingpong_thrift_client.pool.connection_class
        )
        self.assertIs(pool.keepalive, None)
        with (yield pool.connection_ctx()) as conn:
            now = time.time()
            self.assertIs(conn.alive_until, None)
            self.assertTrue((yield conn.test_connection()))
            old_connection = conn

        with fake_datetime(now + 0.1):
            with (yield pool.connection_ctx()) as conn:
                self.assertIs(conn, old_connection)

        with fake_datetime(now + timedelta(days=100).seconds):
            self.assertTrue((yield old_connection.test_connection()))

            with (yield pool.connection_ctx()) as conn:
                self.assertIs(old_connection, conn)
Example #4
0
    def test_setted_connection_pool_connection_keepalive(self):
        keep_alive = 1
        from thrift_connector.tornado import TornadoClientPool
        pool = TornadoClientPool(
            self.pingpong_thrift_client.pool.service,
            self.pingpong_thrift_client.host,
            self.pingpong_thrift_client.port,
            name=self.pingpong_thrift_client.pool.name,
            raise_empty=False, max_conn=3,
            connection_class=self.pingpong_thrift_client.pool.connection_class,
            keepalive=keep_alive
        )
        self.assertEqual(pool.keepalive, keep_alive)
        with (yield pool.connection_ctx()) as conn:
            now = time.time()
            self.assertEqual(int(conn.alive_until), int(now + keep_alive))
            self.assertTrue((yield conn.test_connection()))
            old_connection = conn

        with fake_datetime(now + 0.1):
            with (yield pool.connection_ctx()) as conn:
                self.assertIs(conn, old_connection)

        with fake_datetime(now + keep_alive + 2):
            self.assertFalse((yield old_connection.test_connection()))

            with (yield pool.connection_ctx()) as conn:
                self.assertIsNot(old_connection, conn)
Example #5
0
def tornado_pingpong_thrift_client(request, pingpong_service_key,
                                   pingpong_thrift_service,
                                   tornado_pingpong_thrift_server):
    port, port2, gunicorn_server = tornado_pingpong_thrift_server

    from thrift_connector.tornado import TornadoClientPool, \
        TornadoThriftPyClient

    pool = TornadoClientPool(pingpong_thrift_service,
                             'localhost',
                             port,
                             name=pingpong_service_key,
                             connection_class=TornadoThriftPyClient)

    request.cls.pingpong_thrift_client = TestServerInfo(
        'localhost',
        port,
        gunicorn_server,
        pool,
        pingpong_thrift_service,
        port2=port2)
Example #6
0
# -*- coding: utf-8 -*-
from __future__ import print_function

import thriftpy2
from tornado import gen
from tornado.ioloop import IOLoop

from thrift_connector.tornado import TornadoClientPool, TornadoThriftPyClient

service = thriftpy2.load("pingpong_app/pingpong.thrift")
pool = TornadoClientPool(service.PingService,
                         'localhost',
                         8880,
                         connection_class=TornadoThriftPyClient)


def callback(future):
    print(future.result())
    IOLoop.current().stop()


@gen.coroutine
def main():
    print("Sending Ping...")
    print("Receive:", (yield pool.ping()))
    print("Sleeping...")
    with (yield pool.connection_ctx()) as conn:
        yield conn.sleep(1)
    print("Waked!")
    print("Winning the match...")
    print("Receive:")