def test_exception_in_listener(self):
     def listener(e):
         raise RuntimeError("error")
     config = ClientConfig()
     config.lifecycle_listeners = [listener]
     self.cluster.start_member()
     self.create_client(config)
    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)
Esempio n. 3
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)
Esempio n. 4
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)
Esempio n. 5
0
    def test_exception_in_listener(self):
        def listener(e):
            raise RuntimeError("error")

        config = ClientConfig()
        config.lifecycle_listeners = [listener]
        self.cluster.start_member()
        self.create_client(config)
Esempio n. 6
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 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()
    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(self):
        collector = event_collector()
        config = ClientConfig()
        config.lifecycle_listeners = [collector]
        self.cluster.start_member()
        client = self.create_client(config)
        client.shutdown()

        # noinspection PyUnresolvedReferences
        self.assertEqual(collector.events,
                         [LIFECYCLE_STATE_STARTING, LIFECYCLE_STATE_CONNECTED, LIFECYCLE_STATE_SHUTTING_DOWN,
                          LIFECYCLE_STATE_SHUTDOWN])
Esempio n. 11
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()
Esempio n. 12
0
    def test_lifecycle_listener(self):
        collector = event_collector()
        config = ClientConfig()
        config.lifecycle_listeners = [collector]
        self.cluster.start_member()
        client = self.create_client(config)
        client.shutdown()

        # noinspection PyUnresolvedReferences
        self.assertEqual(collector.events, [
            LIFECYCLE_STATE_STARTING, LIFECYCLE_STATE_CONNECTED,
            LIFECYCLE_STATE_SHUTTING_DOWN, LIFECYCLE_STATE_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_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_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()
    def test_async_start_with_no_cluster(self):
        config = ClientConfig()
        config.connection_strategy.async_start = True
        self.client = HazelcastClient(config)

        with self.assertRaises(ClientOfflineError):
            self.client.get_map(random_string())
 def test_start_client_before_member(self):
     t = Thread(target=self.cluster.start_member)
     t.start()
     config = ClientConfig()
     config.network_config.connection_attempt_limit = 10
     self.create_client(config)
     t.join()
Esempio n. 18
0
    def test_listener_re_register(self):
        member = self.cluster.start_member()
        config = ClientConfig()
        config.network_config.connection_attempt_limit = 10
        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.is_live:
                map.put("key-%d" % count.get_and_increment(), "value").result()
                self.assertGreater(len(collector.events), 0)
            else:
                self.fail("Client disconnected...")

        self.assertTrueEventually(assert_events)
    def test_async_start_with_partition_specific_proxies(self):
        config = ClientConfig()
        config.connection_strategy.async_start = True
        self.client = HazelcastClient(config)

        with self.assertRaises(ClientOfflineError):
            self.client.get_list(random_string())
Esempio n. 20
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)
 def test_start_client_with_no_member(self):
     config = ClientConfig()
     config.network_config.addresses.append("127.0.0.1:5701")
     config.network_config.connection_attempt_limit = 2
     config.network_config.connection_attempt_period = 0.1
     with self.assertRaises(HazelcastError):
         self.create_client(config)
    def test_async_start_with_no_cluster_throws_after_shutdown(self):
        config = ClientConfig()
        config.connection_strategy.async_start = True
        self.client = HazelcastClient(config)

        self.client.shutdown()
        with self.assertRaises(HazelcastClientNotActiveError):
            self.client.get_map(random_string())
Esempio n. 23
0
 def test_start_client_with_no_member(self):
     config = ClientConfig()
     config.network.addresses.append("127.0.0.1:5701")
     config.network.addresses.append("127.0.0.1:5702")
     config.network.addresses.append("127.0.0.1:5703")
     config.connection_strategy.connection_retry.cluster_connect_timeout = 2
     with self.assertRaises(HazelcastError):
         self.create_client(config)
Esempio n. 24
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)
 def test_shutdown_not_hang_on_member_closed(self):
     config = ClientConfig()
     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(HazelcastClientNotActiveException):
         while True:
             my_map.get("key").result()
Esempio n. 26
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))
Esempio n. 27
0
    def test_member_list_after_reconnect(self):
        old_member = self.cluster.start_member()
        config = ClientConfig()
        config.network_config.connection_attempt_limit = 10
        client = self.create_client(config)
        old_member.shutdown()

        new_member = self.cluster.start_member()

        def assert_member_list():
            self.assertEqual(1, len(client.cluster.members))
            self.assertEqual(new_member.uuid, client.cluster.members[0].uuid)

        self.assertTrueEventually(assert_member_list)
    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())
Esempio n. 29
0
    def test_reconnect_toNewNode_ViaLastMemberList(self):
        old_member = self.cluster.start_member()
        config = ClientConfig()
        config.network_config.addresses.append("127.0.0.1:5701")
        config.network_config.smart_routing = False
        config.network_config.connection_attempt_limit = 100
        client = self.create_client(config)
        new_member = self.cluster.start_member()
        old_member.shutdown()

        def assert_member_list():
            self.assertEqual(1, len(client.cluster.members))
            self.assertEqual(new_member.uuid, client.cluster.members[0].uuid)

        self.assertTrueEventually(assert_member_list)
Esempio n. 30
0
class CacheManager:
    config = ClientConfig()
    config.network_config.addresses.append("{host}:{port}".format(**hazelcast_params))
    client = HazelcastClient(config)

    def insertResult(self, name, result, key):
        """
        request the next set of results

        :return:
        """

        map = self.client.get_map(name)
        map.put(key=key, value=HazelcastJsonValue(json.dumps(result)))

        logging.debug('inserted object result {}'.format(key))
    def test_restart_member(self):
        member = self.cluster.start_member()
        config = ClientConfig()
        config.network_config.connection_attempt_limit = 10
        client = self.create_client(config)

        state = [None]

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

        client.lifecycle.add_listener(listener)

        member.shutdown()
        self.assertTrueEventually(lambda: self.assertEqual(state[0], LIFECYCLE_STATE_DISCONNECTED))
        self.cluster.start_member()
        self.assertTrueEventually(lambda: self.assertEqual(state[0], LIFECYCLE_STATE_CONNECTED))
Esempio n. 32
0
class Search(FlaskView):
    config = ClientConfig()
    config.network_config.addresses.append(
        "{host}:{port}".format(**hazelcast_params))
    hz = HazelcastClient(config)
    mongoClient = pymongo.MongoClient(**mongo_params)
    querySchemas = mongoClient['search']['schema']

    @route('/getResults/<string:feedName>', methods=['PUT', 'GET'])
    def getResults(self, feedName):
        query = request.get_json()
        map = self.hz.get_map('{}-{}'.format(feedName, query.get("kind")))
        predicate = sql(query.get("value"))
        values = map.values(predicate).result()

        payload = []
        for value in values:
            payload.append(value.loads())
        if payload:
            return Response(json.dumps(payload), mimetype="application/json")
        else:
            return Response("not found", status=404)

    def getQuerySchema(self, querySchema):
        schema = self.querySchemas.find_one({"name": querySchema})
        val = schema['value']
        return Response(json.dumps(val), mimetype="application/json")

    def getSchemaList(self):
        schemas = self.querySchemas.find({})
        names = [{
            "key": schema.get("name"),
            "value": schema.get("name"),
            "text": schema.get("name")
        } for schema in schemas]
        return Response(json.dumps(names), mimetype='application/json')