def display_statistics(): end_time = get_current_timestamp() + hour_limit * 60 * 60 while get_current_timestamp() < end_time: time.sleep(STATS_DISPLAY_SECONDS) if test_failed[0]: # Some thread failed. No need to continue. return logging.info("-" * 40) total_ops = 0 # sum of total ops of all threads hanged_threads = [] for stat in statistics: total = stat.get_total_operation_count_and_reset( ) # per thread total_ops += total if total == 0: hanged_threads.append(stat.id) if not hanged_threads: logging.info("All threads worked without hanging") else: logging.info("%s threads hanged with ids %s", len(hanged_threads), hanged_threads) for thread_id in hanged_threads: thread_hang_counts[thread_id] += 1 logging.info("-" * 40) logging.info("Operations Per Second: %s\n", total_ops / STATS_DISPLAY_SECONDS)
def test_try_acquire_when_not_enough_permits_with_timeout( self, semaphore_type): semaphore = self.get_semaphore(semaphore_type, 1) start = get_current_timestamp() self.assertFalse(semaphore.try_acquire(2, 1)) self.assertGreaterEqual(get_current_timestamp() - start, 1) self.assertEqual(1, semaphore.available_permits())
def test_put_get_large_payload(self): # The fix for reading large payloads is introduced in 4.2.1 # See https://github.com/hazelcast/hazelcast-python-client/pull/436 skip_if_client_version_older_than(self, "4.2.1") payload = bytearray(os.urandom(16 * 1024 * 1024)) start = get_current_timestamp() self.assertIsNone(self.map.put("key", payload)) self.assertEqual(self.map.get("key"), payload) self.assertLessEqual(get_current_timestamp() - start, 5)
def wait_for_statistics_collection(self, client_uuid, timeout=30): timeout_time = get_current_timestamp() + timeout response = self.get_client_stats_from_server(client_uuid) while get_current_timestamp() < timeout_time: try: self.verify_response_not_empty(response) return response except AssertionError: time.sleep(0.1) response = self.get_client_stats_from_server(client_uuid) raise AssertionError
def test_client_only_listens(self): rc = self.create_rc() client_heartbeat_seconds = 8 cluster_config = (""" <hazelcast xmlns="http://www.hazelcast.com/schema/config" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.hazelcast.com/schema/config http://www.hazelcast.com/schema/config/hazelcast-config-4.0.xsd"> <properties> <property name="hazelcast.client.max.no.heartbeat.seconds">%s</property> </properties> </hazelcast>""" % client_heartbeat_seconds) cluster = self.create_cluster(rc, cluster_config) cluster.start_member() client1 = HazelcastClient(cluster_name=cluster.id, heartbeat_interval=1) def lifecycle_event_collector(): events = [] def event_collector(e): print(e) if e == LifecycleState.DISCONNECTED: events.append(e) event_collector.events = events return event_collector collector = lifecycle_event_collector() client1.lifecycle_service.add_listener(collector) client2 = HazelcastClient(cluster_name=cluster.id) key = "topic-name" topic = client1.get_topic(key) def message_listener(_): pass topic.add_listener(message_listener) topic2 = client2.get_topic(key) begin = get_current_timestamp() while (get_current_timestamp() - begin) < 2 * client_heartbeat_seconds: topic2.publish("message") time.sleep(0.5) self.assertEqual(0, len(collector.events)) client1.shutdown() client2.shutdown() rc.exit()
def test_constructor_with_unreachable_addresses(self): addr = Address("192.168.0.1", 5701) config = _Config() start = get_current_timestamp() conn = AsyncoreConnection(MagicMock(map=dict()), MagicMock(), None, addr, config, None) try: # Server is unreachable, but this call should return # before connection timeout self.assertLess(get_current_timestamp() - start, config.connection_timeout) finally: conn.close_connection(None, None)
def assertTrueEventually(self, assertion, timeout=30): timeout_time = get_current_timestamp() + timeout last_exception = None while get_current_timestamp() < timeout_time: try: assertion() return except AssertionError as e: last_exception = e time.sleep(0.1) if last_exception is None: raise Exception("Could not enter the assertion loop!") raise last_exception
def assertTrueEventually(self, assertion, timeout=30): timeout_time = get_current_timestamp() + timeout exc_info = None while get_current_timestamp() < timeout_time: try: assertion() return except AssertionError: exc_info = sys.exc_info() time.sleep(0.1) if exc_info is None: raise Exception("Could not enter the assertion loop!") six.reraise(*exc_info)
def test_publish_all_with_block_policy(self): topic = self.get_topic("block") collector = event_collector() topic.add_listener(collector) topic.publish_all(range(CAPACITY)) begin_time = get_current_timestamp() topic.publish_all(range(CAPACITY, 2 * CAPACITY)) time_passed = get_current_timestamp() - begin_time # TTL is set in the XML config self.assertTrue(time_passed >= 2.0) self.assertTrueEventually(lambda: self.assertEqual(2 * CAPACITY, len(collector.events))) self.assertEqual(list(range(CAPACITY, CAPACITY * 2)), self.get_ringbuffer_data(topic))
def wait_for_statistics_collection(self, client_uuid, timeout=30, get_metric_blob=False): timeout_time = get_current_timestamp() + timeout while get_current_timestamp() < timeout_time: if get_metric_blob: response = self.get_metrics_blob(client_uuid) else: response = self.get_client_stats_from_server(client_uuid) try: self.verify_response_not_empty(response) return response except AssertionError: time.sleep(0.1) raise AssertionError
def test_await_latch_with_timeout(self): timeout = 1 latch = self.get_latch(1) start = get_current_timestamp() self.assertFalse(latch.await_latch(timeout)) time_passed = get_current_timestamp() - start expected_time_passed = timeout if os.name == "nt": # On Windows, we were getting random test failures due to expected # time passed being slightly less than the timeout. This is due to # the low time resolution there (15-16ms). If we are on Windows, we # lower our expectations and settle for a slightly lower value. expected_time_passed *= 0.95 self.assertTrue( time_passed >= expected_time_passed, "Time passed is less than %s, which is %s" % (expected_time_passed, time_passed), )
def test_acquire_blocks_until_someone_releases(self, semaphore_type): semaphore = self.get_semaphore(semaphore_type, 1) event = threading.Event() event2 = threading.Event() def run(): semaphore.acquire(1) event.set() event2.wait() time.sleep(1) semaphore.release() t = threading.Thread(target=run) t.start() event.wait() start = get_current_timestamp() f = semaphore._wrapped.acquire() event2.set() f.result() self.assertGreaterEqual(get_current_timestamp() - start, 1) t.join()
def test_acquire_blocks_until_semaphore_is_destroyed(self, semaphore_type): semaphore = self.get_semaphore(semaphore_type, 1) event = threading.Event() event2 = threading.Event() def run(): semaphore.acquire(1) event.set() event2.wait() time.sleep(1) semaphore.destroy() t = threading.Thread(target=run) t.start() event.wait() start = get_current_timestamp() f = semaphore._wrapped.acquire() event2.set() with self.assertRaises(DistributedObjectDestroyedError): f.result() self.assertGreaterEqual(get_current_timestamp() - start, 1) t.join()
def run(stats): end_time = get_current_timestamp() + hour_limit * 60 * 60 while get_current_timestamp() < end_time: if test_failed[0]: return # Some other thread failed, no need to continue the test key = str(random.randint(0, ENTRY_COUNT)) value = str(random.randint(0, ENTRY_COUNT)) operation = random.randint(0, 100) try: if operation < 30: test_map.get(key) elif operation < 60: test_map.put(key, value) elif operation < 80: test_map.values(between("this", 0, 10)) else: test_map.execute_on_key(key, processor) stats.increment_operation_count() except: test_failed[0] = True logging.exception("Unexpected error occurred") return
def test_await_latch_with_timeout(self): latch = self.get_latch(1) start = get_current_timestamp() self.assertFalse(latch.await_latch(0.1)) time_passed = get_current_timestamp() - start self.assertTrue(time_passed > 0.1)