class ThriftClientPoolTest(EzThriftServerTestHarness):

    def setUp(self):
        super(ThriftClientPoolTest, self).setUp()

        ez_props = EzConfiguration().getProperties()
        ez_props["thrift.use.ssl"] = "false"
        ez_props["zookeeper.connection.string"] = self.hosts
        application_name = ApplicationConfiguration(ez_props).getApplicationName()

        for endpoint in ENDPOINTS:
            host, port = endpoint.split(':')
            self.add_server(application_name, "ezpz", host, int(port), EzPz.Processor(EzPzHandler()))

        self.sd_client.register_endpoint(application_name, "service_one", 'localhost', 8083)
        self.sd_client.register_endpoint(application_name, "service_two", 'localhost', 8084)
        self.sd_client.register_endpoint(application_name, "service_three", 'localhost', 8085)

        self.sd_client.register_common_endpoint('common_service_one', 'localhost', 8080)
        self.sd_client.register_common_endpoint('common_service_two', 'localhost', 8081)
        self.sd_client.register_common_endpoint('common_service_three', 'localhost', 8082)
        self.sd_client.register_common_endpoint('common_service_multi', '192.168.1.1', 6060)
        self.sd_client.register_common_endpoint('common_service_multi', '192.168.1.2', 6161)

        self.sd_client.register_endpoint("NotThriftClientPool", "unknown_service_three", 'localhost', 8091)
        self.sd_client.register_endpoint("NotThriftClientPool", "unknown_service_three", 'localhost', 8092)
        self.sd_client.register_endpoint("NotThriftClientPool", "unknown_service_three", 'localhost', 8093)

        self.clientPool = ThriftClientPool(ez_props)

    def tearDown(self):
        self.clientPool.close()
        nt.assert_false(self.clientPool._get_service_map())
        nt.assert_false(self.clientPool._get_client_map())
        super(ThriftClientPoolTest, self).tearDown()

    def test_endpoints(self):
        service_map = self.clientPool._get_service_map()
        self.assertTrue("common_service_one" in service_map)
        self.assertTrue("common_service_two" in service_map)
        self.assertTrue("common_service_three" in service_map)
        self.assertTrue("service_one" in service_map)
        self.assertTrue("service_two" in service_map)
        self.assertTrue("service_three" in service_map)
        self.assertFalse("unknown_service_one" in service_map)
        self.assertFalse("unknown_service_two" in service_map)
        self.assertFalse("unknown_service_three" in service_map)

        self.assertTrue("common_service_multi" in service_map)
        self.assertEqual(len(service_map["common_service_multi"]), 2)

        self.assertTrue("ezpz" in service_map)
        self.assertEqual(len(service_map["ezpz"]), len(ENDPOINTS))

    def test_get_client(self):
        client = self.clientPool.get_client(service_name='ezpz', clazz=EzPz.Client)
        try:
            nt.assert_equal('pz', client.ez())
        finally:
            client.close()

        client = self.clientPool.get_client(service_name='ezpz1', clazz=EzPz.Client)            # None existing service
        nt.assert_false(client)

    def test_multi_get_client(self):
        client1 = self.clientPool.get_client(service_name='ezpz', clazz=EzPz.Client)
        client2 = self.clientPool.get_client(service_name='ezpz', clazz=EzPz.Client)
        try:
            nt.assert_equal('pz', client1.ez())
            nt.assert_equal('pz', client2.ez())
        finally:
            self.clientPool.close()

    def test_get_client_app(self):
        client = self.clientPool.get_client(app_name="testApp", service_name="ezpz", clazz=EzPz.Client)
        try:
            nt.assert_equal("pz", client.ez())
        finally:
            client.close()

        client = self.clientPool.get_client(app_name="testApp1", service_name="ezpz0", clazz=EzPz.Client) # None existing app
        nt.assert_false(client)
class ThriftClientPoolTest(KazooTestCase):

    def setUp(self):
        """
        """
        super(ThriftClientPoolTest, self).setUp()
        ezd_client = ServiceDiscoveryClient(self.hosts)

        ez_props = EzConfiguration().getProperties()
        ez_props["thrift.use.ssl"] = "false"
        ez_props["zookeeper.connection.string"] = self.hosts
        application_name = ApplicationConfiguration(ez_props).getApplicationName()

        self.serverProcesses = []
        for endpoint in ENDPOINTS:
            host, port = endpoint.split(':')
            port = int(port)
            server_process = Process(target=start_ezpz, args=(EzPzHandler(), port,))
            server_process.start()
            time.sleep(1)
            self.serverProcesses.append(server_process)
            ezd_client.register_endpoint(application_name, "ezpz", host, port)

        ezd_client.register_endpoint(application_name, "service_one", 'localhost', 8083)
        ezd_client.register_endpoint(application_name, "service_two", 'localhost', 8084)
        ezd_client.register_endpoint(application_name, "service_three", 'localhost', 8085)

        ezd_client.register_common_endpoint('common_service_one', 'localhost', 8080)
        ezd_client.register_common_endpoint('common_service_two', 'localhost', 8081)
        ezd_client.register_common_endpoint('common_service_three', 'localhost', 8082)
        ezd_client.register_common_endpoint('common_service_multi', '192.168.1.1', 6060)
        ezd_client.register_common_endpoint('common_service_multi', '192.168.1.2', 6161)

        ezd_client.register_endpoint("NotThriftClientPool", "unknown_service_three", 'localhost', 8091)
        ezd_client.register_endpoint("NotThriftClientPool", "unknown_service_three", 'localhost', 8092)
        ezd_client.register_endpoint("NotThriftClientPool", "unknown_service_three", 'localhost', 8093)

        self.clientPool = ThriftClientPool(ez_props)

    def tearDown(self):
        super(ThriftClientPoolTest, self).tearDown()
        self.clientPool.close()
        nt.assert_false(self.clientPool._get_service_map())
        nt.assert_false(self.clientPool._get_client_map())
        for server_process in self.serverProcesses:
            if server_process.is_alive():
                server_process.terminate()

    def test_endpoints(self):
        service_map = self.clientPool._get_service_map()
        self.assertTrue("common_service_one" in service_map)
        self.assertTrue("common_service_two" in service_map)
        self.assertTrue("common_service_three" in service_map)
        self.assertTrue("service_one" in service_map)
        self.assertTrue("service_two" in service_map)
        self.assertTrue("service_three" in service_map)
        self.assertFalse("unknown_service_one" in service_map)
        self.assertFalse("unknown_service_two" in service_map)
        self.assertFalse("unknown_service_three" in service_map)

        self.assertTrue("common_service_multi" in service_map)
        self.assertEqual(len(service_map["common_service_multi"]), 2)

        self.assertTrue("ezpz" in service_map)
        self.assertEqual(len(service_map["ezpz"]), len(ENDPOINTS))

    def test_get_client(self):
        client = self.clientPool.get_client(service_name='ezpz', clazz=EzPz.Client)
        try:
            nt.assert_equal('pz', client.ez())
        finally:
            client.close()

        client = self.clientPool.get_client(service_name='ezpz1', clazz=EzPz.Client)            # None existing service
        nt.assert_false(client)

    def test_get_client_for_app(self):
        client = self.clientPool.get_client(app_name='testApp', service_name='ezpz', clazz=EzPz.Client)
        try:
            nt.assert_equal('pz', client.ez())
        finally:
            client.close()

    def test_multi_get_client(self):
        client1 = self.clientPool.get_client(service_name='ezpz', clazz=EzPz.Client)
        client2 = self.clientPool.get_client(service_name='ezpz', clazz=EzPz.Client)
        try:
            nt.assert_equal('pz', client1.ez())
            nt.assert_equal('pz', client2.ez())
        finally:
            client1.close()
            client2.close()
class ThriftClientPoolTest(EzThriftServerTestHarness):

    def setUp(self):
        super(ThriftClientPoolTest, self).setUp()

        ez_props = EzConfiguration().getProperties()
        ez_props["thrift.use.ssl"] = "false"
        ez_props["zookeeper.connection.string"] = self.hosts
        ez_props["thrift.max.idle.clients"] = 6
        # ez_props["thrift.max.pool.clients"] = 6
        ez_props["thrift.millis.between.client.eviction.checks"] = 1000
        ez_props["thrift.millis.idle.before.eviction"] = 1.5 * 1000
        application_name = ApplicationConfiguration(ez_props).getApplicationName()

        for endpoint in ENDPOINTS:
            host, port = endpoint.split(':')
            self.add_server(application_name, "ezpz", host, int(port),
                            EzPz.Processor(EzPzHandler()), use_simple_server=False)

        self.clientPool = ThriftClientPool(ez_props)

    def tearDown(self):
        self.clientPool.close()
        nt.assert_false(self.clientPool._get_service_map())
        nt.assert_false(self.clientPool._get_client_map())
        super(ThriftClientPoolTest, self).tearDown()

    def client_thread(self, tid):
        print tid
        client = self.clientPool.get_client(service_name='ezpz', clazz=EzPz.Client)
        res = client.ez2(random.randint(10, 20) * 0.1)
        print chr(ord('a') + tid)
        return res

    def test_eviction(self):

        threads = []
        for i in range(10):
            thread = threading.Thread(target=self.client_thread, args=(i,))
            thread.start()
            threads.append(thread)

        for thread in threads:
            thread.join()

        time.sleep(3)                                   # sleep longer enough to evict all connections.
        client = self.clientPool.get_client(service_name='ezpz', clazz=EzPz.Client)
        #client._pool._connection_queue.join()           # test to see if join function on queue still work
        nt.assert_equal(0, client._pool._connection_queue.qsize())

    def client_thread_for_apps(self, tid):
        print tid
        client = self.clientPool.get_client(app_name='testApp', service_name='ezpz', clazz=EzPz.Client)
        res = client.ez2(random.randint(10, 20) * 0.1)
        print chr(ord('a') + tid)
        return res

    def test_eviction_for_apps(self):


        threads = []
        for i in range(10):
            thread = threading.Thread(target=self.client_thread, args=(i,))
            thread.start()
            threads.append(thread)

        for thread in threads:
            thread.join()

        time.sleep(3)                                   # sleep longer enough to evict all connections.
        client = self.clientPool.get_client(app_name='testApp', service_name='ezpz', clazz=EzPz.Client)
        #client._pool._connection_queue.join()           # test to see if join function on queue still work
        nt.assert_equal(0, client._pool._connection_queue.qsize())
class ThriftClientPoolTest(EzThriftServerTestHarness):

    def setUp(self):
        super(ThriftClientPoolTest, self).setUp()

        ez_props = EzConfiguration().getProperties()
        ez_props["thrift.use.ssl"] = "true"
        ez_props["zookeeper.connection.string"] = self.hosts
        application_name = ApplicationConfiguration(ez_props).getApplicationName()

        for endpoint in ENDPOINTS:
            host, port = endpoint.split(':')
            self.add_server(application_name, "ezpz_ssl", host, int(port), EzPz.Processor(EzPzHandler()),
                            use_ssl=True, ca_certs=servercapath, cert=servercertpath, key=serverprivpath)

        self.clientPool = ThriftClientPool(ez_props)

    def tearDown(self):
        self.clientPool.close()
        nt.assert_false(self.clientPool._get_service_map())
        nt.assert_false(self.clientPool._get_client_map())
        super(ThriftClientPoolTest, self).tearDown()

    def test_get_client(self):
        client = self.clientPool.get_client(service_name='ezpz_ssl', clazz=EzPz.Client)
        try:
            resp = client.ez()
            nt.assert_equal('pz', resp)
        finally:
            client.close()

        client = self.clientPool.get_client(service_name='ezpz1', clazz=EzPz.Client)            # None existing service
        nt.assert_false(client)

    def test_get_client_for_apps(self):
        client = self.clientPool.get_client(app_name='testApp', service_name='ezpz_ssl', clazz=EzPz.Client)
        try:
            resp = client.ez()
            nt.assert_equal('pz', resp)
        finally:
            client.close()

        client = self.clientPool.get_client(app_name='testApp', service_name='ezpz1', clazz=EzPz.Client)            # None existing service
        nt.assert_false(client)


    def test_multi_get_client(self):
        client1 = self.clientPool.get_client(service_name='ezpz_ssl', clazz=EzPz.Client)
        client2 = self.clientPool.get_client(service_name='ezpz_ssl', clazz=EzPz.Client)
        try:
            nt.assert_equal('pz', client1.ez())
            nt.assert_equal('pz', client2.ez())
        finally:
            self.clientPool.close()

    def test_multi_get_client_for_apps(self):
        client1 = self.clientPool.get_client(app_name='testApp', service_name='ezpz_ssl', clazz=EzPz.Client)
        client2 = self.clientPool.get_client(app_name='testApp', service_name='ezpz_ssl', clazz=EzPz.Client)
        try:
            nt.assert_equal('pz', client1.ez())
            nt.assert_equal('pz', client2.ez())
        finally:
            self.clientPool.close()