def test_non_smart_listener_add_member(self):
        self.client_config["smart_routing"] = False
        client = self.create_client(self.client_config)
        map = client.get_map(random_string()).blocking()
        map.add_entry_listener(added_func=self.collector)
        m3 = self.cluster.start_member()
        wait_for_partition_table(client)
        key_m3 = generate_key_owned_by_instance(client, m3.uuid)
        map.put(key_m3, 'value')

        def assert_event():
            self.assertEqual(1, len(self.collector.events))
        self.assertTrueEventually(assert_event)
    def test_smart_listener_remove_member(self):
        self.client_config["smart_routing"] = True
        client = self.create_client(self.client_config)
        wait_for_partition_table(client)
        key_m1 = generate_key_owned_by_instance(client, self.m1.uuid)
        map = client.get_map(random_string()).blocking()
        map.put(key_m1, 'value1')
        map.add_entry_listener(updated_func=self.collector)
        self.m1.shutdown()
        map.put(key_m1, 'value2')

        def assert_event():
            self.assertEqual(1, len(self.collector.events))
        self.assertTrueEventually(assert_event)
    def test_non_smart_listener_remove_member(self):
        self.client_config.network.smart_routing = False
        client = self.create_client(self.client_config)
        map = client.get_map(random_string()).blocking()
        map.add_entry_listener(added_func=self.collector)
        self.m2.shutdown()
        wait_for_partition_table(client)

        generated_key = generate_key_owned_by_instance(client, self.m1.uuid)
        map.put(generated_key, 'value')

        def assert_event():
            self.assertEqual(1, len(self.collector.events))

        self.assertTrueEventually(assert_event)
Esempio n. 4
0
    def test_add_member(self, _, is_smart):
        self.client_config["smart_routing"] = is_smart
        client = self.create_client(self.client_config)
        random_map = client.get_map(random_string()).blocking()
        random_map.add_entry_listener(added_func=self.collector,
                                      updated_func=self.collector)
        m2 = self.cluster.start_member()
        wait_for_partition_table(client)
        key_m2 = generate_key_owned_by_instance(client, m2.uuid)

        assertion_succeeded = False

        def run():
            nonlocal assertion_succeeded
            # When a new connection is added, we add the existing
            # listeners to it, but we do it non-blocking. So, it might
            # be the case that, the listener registration request is
            # sent to the new member, but not completed yet.
            # So, we might not get an event for the put. To avoid this,
            # we will put multiple times.
            for i in range(10):
                if assertion_succeeded:
                    # We have successfully got an event
                    return

                random_map.put(key_m2, f"value-{i}")

                time.sleep((i + 1) * 0.1)

        put_thread = threading.Thread(target=run)
        put_thread.start()

        def assert_event():
            nonlocal assertion_succeeded
            self.assertGreaterEqual(len(self.collector.events), 1)
            assertion_succeeded = True

        self.assertTrueEventually(assert_event)
        put_thread.join()
    def test_key_owner_shutdowns_after_invocation_timeout(self):
        key_owner = self.cluster.start_member()

        invocation_timeout_seconds = 1
        self.client.invoker.invocation_timeout = invocation_timeout_seconds

        key = generate_key_owned_by_instance(self.client, key_owner.address)
        server_lock = self.client.get_lock(key).blocking()
        server_lock.lock()

        e = Event()

        def lock_thread_func():
            lock = self.client.get_lock(key).blocking()
            lock.lock()
            lock.unlock()
            e.set()

        self.start_new_thread(lock_thread_func)

        time.sleep(2 * invocation_timeout_seconds)
        key_owner.shutdown()

        partition_id = self.client.partition_service.get_partition_id(key)
        while not (
                self.client.partition_service.get_partition_owner(partition_id)
                == self.member.address):
            time.sleep(0.1)
        try:
            self.assertTrue(server_lock.is_locked())
            self.assertTrue(server_lock.is_locked_by_current_thread())
            self.assertTrue(server_lock.try_lock())
            server_lock.unlock()
            server_lock.unlock()
            self.assertSetEventually(e)
        finally:
            # revert the invocation timeout change for other tests since client instance is only created once.
            self.client.invoker.invocation_timeout = self.client.properties.INVOCATION_TIMEOUT_SECONDS.default_value