def test_works_ok(self): def func_no_issues(): time.sleep(1) print("OK - NodeCount %d" % mocknet.node_count) mocknet = MockNet(func_no_issues, timeout_secs=10, node_count=0) mocknet.run()
def test_timeout(self): def func_blocks(): while mocknet.running: time.sleep(1) mocknet = MockNet(func_blocks, timeout_secs=2, node_count=0) with self.assertRaises(TimeoutError): mocknet.run()
def test_exception(self): def func_raises_value_error(): time.sleep(1) raise ValueError("Some random exception") mocknet = MockNet(func_raises_value_error, timeout_secs=10, node_count=0) with self.assertRaises(ValueError): mocknet.run()
def test_grpc_fuzzing(self): timeout = 60 * 15 def func_monitor_log(): node_tracker = NodeLogTracker(mocknet) while mocknet.uptime < 30: node_tracker.track() time.sleep(0.01) if node_tracker.synced_count() < mocknet.node_count: raise Exception("Nodes did not sync") protobuf_strategies = modules_to_strategies(qrl_pb2) publicapi = qrl_pb2._PUBLICAPI self.assertTrue(publicapi.methods, "methods is empty") req_attrs = [] for m in publicapi.methods: req_attrs.append(ReqAttrNames(m.name, m.input_type.name)) stubs = [ qrl_pb2_grpc.PublicAPIStub( grpc.insecure_channel("127.0.0.1:10002")) ] while mocknet.uptime < 180: try: node_tracker.track() if node_tracker.synced_count() == mocknet.node_count: for i in range(1, PASS_NUM): rand_stub = choice(stubs) rand_req = choice(req_attrs) req_strategy = protobuf_strategies[getattr( qrl_pb2, rand_req.arg)] req_arg = req_strategy.example() req_method = getattr(rand_stub, rand_req.method) try: resp = req_method(req_arg) except grpc.RpcError as err: print('*******************************') print("\nERROR: %s \n" % rand_req.method) print(req_arg) print('*******************************') print(err) # raise except Exception as e: pass time.sleep(1) except Empty: pass # Launch mocknet mocknet = MockNet(func_monitor_log, timeout_secs=timeout, node_count=2) mocknet.prepare_source() mocknet.run()
def test_launch_nodes_no_monitor(self): def func_monitor_log(): # exit fast time.sleep(2) mocknet = MockNet(func_monitor_log, timeout_secs=30, node_count=10) mocknet.prepare_source() mocknet.run()
def test_launch_1_node(self): def func_monitor_log(): node_logtracker = NodeLogTracker(mocknet) while mocknet.uptime < 5: node_logtracker.track() mocknet = MockNet(func_monitor_log, timeout_secs=10, node_count=1) mocknet.prepare_source() mocknet.run()
def test_launch_log_nodes(self): def func_monitor_log(): node_logtracker = NodeLogTracker(mocknet) while mocknet.running: node_logtracker.track() if len(mocknet.nodes) >= 10: return mocknet = MockNet(func_monitor_log, timeout_secs=30, node_count=10) mocknet.prepare_source() mocknet.run()
def test_launch_log_nodes(self): def func_monitor_log(): node_logtracker = NodeLogTracker(mocknet) while mocknet.running: node_logtracker.track() if node_logtracker.synced_count() == mocknet.node_count: return mocknet = MockNet(func_monitor_log, timeout_secs=60, node_count=3) mocknet.prepare_source() mocknet.run()
def test_launch_nodes_fast_timeout(self): def func_monitor_log(): mocknet.writeout("Monitor START") while mocknet.running: # Block pass mocknet.writeout("Monitor STOP") mocknet = MockNet(func_monitor_log, timeout_secs=30, node_count=10) mocknet.prepare_source() with self.assertRaises(TimeoutError): mocknet.run()
def test_fork_recovery(self): timeout = 120 def state_check(): public_api_addresses = mocknet.public_addresses for public_api_address in public_api_addresses: channel_public = grpc.insecure_channel(public_api_address) stub = qrl_pb2_grpc.PublicAPIStub(channel_public) # TODO: Check coins emitted, coins total supply, epoch, block_last_reward # response = stub.GetStats(request=qrl_pb2.GetStatsReq()) response = stub.GetNodeState(request=qrl_pb2.GetNodeStateReq()) if response.info.block_height != LAST_BLOCK_NUMBER: raise Exception( 'Expected Blockheight %s \n Found blockheight %s', LAST_BLOCK_NUMBER, response.info.block_height) if response.info.block_last_hash != bytes( hstr2bin(LAST_BLOCK_HEADERHASH)): raise Exception( 'Last Block Headerhash mismatch\n' 'Expected : %s\n', bin2hstr(response.info.block_last_hash), 'Found : %s ', LAST_BLOCK_HEADERHASH) return True def func_monitor_log(): node_tracker = NodeLogTracker(mocknet) while mocknet.running: msg = node_tracker.track() if "Added Block #{0} {1}".format(LAST_BLOCK_NUMBER, LAST_BLOCK_HEADERHASH) in msg: state_check() return mocknet = MockNet(func_monitor_log, timeout_secs=timeout, node_count=2, node_args="--mockGetMeasurement 1000000000", remove_data=False) mocknet.prepare_source() mocknet.run()