def test_off_reconnect_mode(self):
        self.cluster = self.rc.createCluster(None, None)
        member = self.rc.startMember(self.cluster.id)
        config = ClientConfig()
        config.cluster_name = self.cluster.id
        config.network.addresses.append("localhost:5701")
        config.connection_strategy.reconnect_mode = RECONNECT_MODE.OFF
        config.connection_strategy.connection_retry.cluster_connect_timeout = six.MAXSIZE

        def collector():
            events = []

            def on_state_change(event):
                if event == LifecycleState.SHUTDOWN:
                    events.append(event)

            on_state_change.events = events
            return on_state_change
        event_collector = collector()
        config.add_lifecycle_listener(event_collector)
        self.client = HazelcastClient(config)
        m = self.client.get_map(random_string()).blocking()
        # no exception at this point
        m.put(1, 1)
        self.rc.shutdownMember(self.cluster.id, member.uuid)
        self.assertTrueEventually(lambda: self.assertEqual(1, len(event_collector.events)))

        with self.assertRaises(HazelcastClientNotActiveError):
            m.put(1, 1)
Пример #2
0
    def test_listener_re_register(self):
        member = self.cluster.start_member()
        config = ClientConfig()
        config.cluster_name = self.cluster.id
        config.connection_strategy.connection_retry.cluster_connect_timeout = 5
        client = self.create_client(config)

        map = client.get_map("map")

        collector = event_collector()
        reg_id = map.add_entry_listener(added_func=collector)
        self.logger.info("Registered listener with id %s", reg_id)
        member.shutdown()
        sleep(3)
        self.cluster.start_member()

        count = AtomicInteger()

        def assert_events():
            if client.lifecycle_service.is_running():
                while True:
                    try:
                        map.put("key-%d" % count.get_and_increment(),
                                "value").result()
                        break
                    except TargetDisconnectedError:
                        pass
                self.assertGreater(len(collector.events), 0)
            else:
                self.fail("Client disconnected...")

        self.assertTrueEventually(assert_events)
Пример #3
0
    def test_invocations_finalised_when_client_shutdowns(self):
        self.cluster.start_member()
        config = ClientConfig()
        config.cluster_name = self.cluster.id
        client = self.create_client(config)
        m = client.get_map("test")
        m.put("key", "value").result()

        def run():
            for _ in range(1000):
                try:
                    m.get("key").result()
                except:
                    pass

        threads = []
        for _ in range(10):
            t = threading.Thread(target=run)
            threads.append(t)
            t.start()

        client.shutdown()

        for i in range(10):
            threads[i].join(5)
Пример #4
0
 def test_start_client_before_member(self):
     t = Thread(target=self.cluster.start_member)
     t.start()
     config = ClientConfig()
     config.cluster_name = self.cluster.id
     config.connection_strategy.connection_retry.cluster_connect_timeout = 5
     self.create_client(config)
     t.join()
    def test_exception_in_listener(self):
        def listener(_):
            raise RuntimeError("error")

        config = ClientConfig()
        config.cluster_name = self.cluster.id
        config.lifecycle_listeners = [listener]
        self.cluster.start_member()
        self.create_client(config)
 def setUp(self):
     self.rc = self.create_rc()
     self.cluster = self.create_cluster(self.rc, self._configure_cluster())
     self.cluster.start_member()
     self.cluster.start_member()
     config = ClientConfig()
     config.cluster_name = self.cluster.id
     self.client = HazelcastClient(config)
     self.pn_counter = self.client.get_pn_counter("pn-counter").blocking()
    def test_lifecycle_listener_receives_disconnected_event(self):
        member = self.cluster.start_member()

        collector = event_collector()
        config = ClientConfig()
        config.cluster_name = self.cluster.id
        client = self.create_client(config)
        client.lifecycle_service.add_listener(collector)
        member.shutdown()
        self.assertEqual(collector.events, [LifecycleState.DISCONNECTED])
        client.shutdown()
Пример #8
0
 def test_shutdown_not_hang_on_member_closed(self):
     config = ClientConfig()
     config.cluster_name = self.cluster.id
     config.connection_strategy.connection_retry.cluster_connect_timeout = 5
     member = self.cluster.start_member()
     client = self.create_client(config)
     my_map = client.get_map("test")
     my_map.put("key", "value").result()
     member.shutdown()
     with self.assertRaises(HazelcastClientNotActiveError):
         while True:
             my_map.get("key").result()
    def test_remove_lifecycle_listener(self):
        collector = event_collector()

        self.cluster.start_member()
        config = ClientConfig()
        config.cluster_name = self.cluster.id
        client = self.create_client(config)
        registration_id = client.lifecycle_service.add_listener(collector)
        client.lifecycle_service.remove_listener(registration_id)
        client.shutdown()

        self.assertEqual(collector.events, [])
    def test_lifecycle_listener_receives_events_in_order_after_startup(self):
        self.cluster.start_member()

        collector = event_collector()
        config = ClientConfig()
        config.cluster_name = self.cluster.id
        client = self.create_client(config)
        client.lifecycle_service.add_listener(collector)
        client.shutdown()

        self.assertEqual(collector.events, [
            LifecycleState.SHUTTING_DOWN, LifecycleState.DISCONNECTED,
            LifecycleState.SHUTDOWN
        ])
    def test_lifecycle_listener_receives_events_in_order(self):
        collector = event_collector()
        config = ClientConfig()
        config.cluster_name = self.cluster.id
        config.lifecycle_listeners.append(collector)
        self.cluster.start_member()
        client = self.create_client(config)
        client.shutdown()

        self.assertEqual(collector.events, [
            LifecycleState.STARTING, LifecycleState.STARTED,
            LifecycleState.CONNECTED, LifecycleState.SHUTTING_DOWN,
            LifecycleState.DISCONNECTED, LifecycleState.SHUTDOWN
        ])
    def test_random_load_balancer(self):
        config = ClientConfig()
        config.cluster_name = self.cluster.id
        config.load_balancer = RandomLB()
        client = HazelcastClient(config)
        self.assertTrue(client.lifecycle_service.is_running())

        lb = client._load_balancer
        self.assertTrue(isinstance(lb, RandomLB))

        six.assertCountEqual(self, self.addresses,
                             list(map(lambda m: m.address, lb._members)))
        for _ in range(10):
            self.assertTrue(lb.next().address in self.addresses)

        client.shutdown()
Пример #13
0
    def test_member_list_after_reconnect(self):
        old_member = self.cluster.start_member()
        config = ClientConfig()
        config.cluster_name = self.cluster.id
        config.connection_strategy.connection_retry.cluster_connect_timeout = 5
        client = self.create_client(config)
        old_member.shutdown()

        new_member = self.cluster.start_member()

        def assert_member_list():
            members = client.cluster_service.get_members()
            self.assertEqual(1, len(members))
            self.assertEqual(new_member.uuid, str(members[0].uuid))

        self.assertTrueEventually(assert_member_list)
Пример #14
0
    def test_reconnect_toNewNode_ViaLastMemberList(self):
        old_member = self.cluster.start_member()
        config = ClientConfig()
        config.cluster_name = self.cluster.id
        config.network.addresses.append("127.0.0.1:5701")
        config.network.smart_routing = False
        config.connection_strategy.connection_retry.cluster_connect_timeout = 10
        client = self.create_client(config)
        new_member = self.cluster.start_member()
        old_member.shutdown()

        def assert_member_list():
            members = client.cluster_service.get_members()
            self.assertEqual(1, len(members))
            self.assertEqual(new_member.uuid, str(members[0].uuid))

        self.assertTrueEventually(assert_member_list)
Пример #15
0
    def test_restart_member(self):
        member = self.cluster.start_member()
        config = ClientConfig()
        config.cluster_name = self.cluster.id
        config.connection_strategy.connection_retry.cluster_connect_timeout = 5
        client = self.create_client(config)

        state = [None]

        def listener(s):
            state[0] = s

        client.lifecycle_service.add_listener(listener)

        member.shutdown()
        self.assertTrueEventually(
            lambda: self.assertEqual(state[0], LifecycleState.DISCONNECTED))
        self.cluster.start_member()
        self.assertTrueEventually(
            lambda: self.assertEqual(state[0], LifecycleState.CONNECTED))
    def test_async_start(self):
        self.cluster = self.rc.createCluster(None, None)
        self.rc.startMember(self.cluster.id)
        config = ClientConfig()
        config.cluster_name = self.cluster.id
        config.network.addresses.append("localhost:5701")
        config.connection_strategy.async_start = True

        def collector():
            events = []

            def on_state_change(event):
                if event == LifecycleState.CONNECTED:
                    events.append(event)

            on_state_change.events = events
            return on_state_change
        event_collector = collector()
        config.add_lifecycle_listener(event_collector)
        self.client = HazelcastClient(config)

        self.assertTrueEventually(lambda: self.assertEqual(1, len(event_collector.events)))
        self.client.get_map(random_string())
 def create_config(self):
     config = ClientConfig()
     config.cluster_name = self.cluster.id
     return config
Пример #18
0
 def setUpClass(cls):
     cls.rc = cls.create_rc()
     cls.cluster = cls.create_cluster(cls.rc, cls.configure_cluster())
     config = ClientConfig()
     config.cluster_name = cls.cluster.id
     cls.config = config