def clientstack(tdir, looper, alpha_handler): names = ['ClientA', 'Alpha'] genKeys(tdir, names) aseed = randomSeed() cseed = randomSeed() stackParams = { "name": names[0], "ha": genHa(), "auto": 2, "basedirpath": tdir } timer = MockTimer(0) client = ClientZStack(stackParams, alpha_handler.handle, cseed, False, timer=timer) stackParams = { "name": names[1], "ha": genHa(), "auto": 2, "basedirpath": tdir } timer = MockTimer(0) alpha = SimpleZStack(stackParams, alpha_handler, aseed, True, timer=timer) amotor = SMotor(alpha) looper.add(amotor) cmotor = SMotor(client) looper.add(cmotor) return alpha, client
def __init__(self, node_count: int = 4, random: Optional[SimRandom] = None): self._random = random if random else DefaultSimRandom() self._timer = MockTimer() self._network = SimNetwork(self._timer, self._random, self._serialize_deserialize) self._nodes = [] self._genesis_txns = None self._genesis_validators = genNodeNames(node_count) self.validators = self._genesis_validators # ToDo: maybe it should be a random too? self._primary_name = self._genesis_validators[0] self._internal_buses = {} self._node_votes = {} self._ports = self._random.sample(range(9000, 9999), 2 * len(self._genesis_validators)) # ToDo: need to remove after implementation catchup_service (INDY-2148) # and when we can change pool after NODE txn self._expected_node_reg = self.validators # Actions self._generate_genensis_txns() # Create nodes from genesis for name in self._genesis_validators: self.add_new_node(name)
def __init__(self, node_count: int = 4, random: Optional[SimRandom] = None): self._random = random if random else DefaultSimRandom() self._timer = MockTimer() self._network = SimNetwork(self._timer, self._random, self._serialize_deserialize) self._nodes = [] validators = genNodeNames(node_count) # ToDo: maybe it should be a random too? primary_name = validators[0] genesis_txns = create_pool_txn_data( node_names=validators, crypto_factory=create_default_bls_crypto_factory(), get_free_port=partial(random.integer, 9000, 9999))['txns'] for name in validators: # TODO: emulate it the same way as in Replica, that is sender must have 'node_name:inst_id' form replica_name = generateName(name, 0) handler = partial(self.network._send_message, replica_name) write_manager = create_test_write_req_manager(name, genesis_txns) write_manager.node_reg_handler.node_reg_at_beginning_of_view[ 0] = validators replica = ReplicaService(replica_name, validators, primary_name, self._timer, InternalBus(), self.network.create_peer(name, handler), write_manager=write_manager, bls_bft_replica=MockBlsBftReplica()) replica.config.NEW_VIEW_TIMEOUT = 30 * 1000 self._nodes.append(replica)
def test_repeating_timer_is_started_active(): timer = MockTimer() cb = Callback() RepeatingTimer(timer, 5, cb) assert cb.call_count == 0 timer.advance(3) assert cb.call_count == 0 timer.advance(3) assert cb.call_count == 1 timer.advance(3) assert cb.call_count == 1 timer.advance(3) assert cb.call_count == 2
def test_repeating_timer_can_be_started_inactive(): timer = MockTimer() cb = Callback() RepeatingTimer(timer, 5, cb, active=False) assert cb.call_count == 0 timer.advance(3) assert cb.call_count == 0 timer.advance(3) assert cb.call_count == 0 timer.advance(3) assert cb.call_count == 0 timer.advance(3) assert cb.call_count == 0
def __init__(self, node_count: int = 4, random: Optional[SimRandom] = None): self._random = random if random else DefaultSimRandom() self._timer = MockTimer() self._network = SimNetwork(self._timer, self._random) self._nodes = [ ReplicaService(name, self.network.create_peer(name)) for name in genNodeNames(node_count) ]
def test_repeating_timer_can_be_stopped(): timer = MockTimer() cb = Callback() repeating_timer = RepeatingTimer(timer, 5, cb) assert cb.call_count == 0 timer.advance(4) assert cb.call_count == 0 timer.advance(4) assert cb.call_count == 1 repeating_timer.stop() timer.advance(4) assert cb.call_count == 1 timer.advance(4) assert cb.call_count == 1
def __init__(self, node_count: int = 4, random: Optional[SimRandom] = None): self._random = random if random else DefaultSimRandom() self._timer = MockTimer() self._network = SimNetwork(self._timer, self._random) validators = genNodeNames(node_count) primary_name = validators[0] self._nodes = [ ReplicaService(name, validators, primary_name, self._timer, InternalBus(), self.network.create_peer(name)) for name in validators ]
def test_multiple_repeating_timers_can_work_together(): timer = MockTimer() cb1 = Callback() cb2 = Callback() RepeatingTimer(timer, 5, cb1) RepeatingTimer(timer, 2, cb2) timer.advance(3) assert cb1.call_count == 0 assert cb2.call_count == 1 timer.advance(3) assert cb1.call_count == 1 assert cb2.call_count == 2 timer.advance(3) assert cb1.call_count == 1 assert cb2.call_count == 3 timer.advance(3) assert cb1.call_count == 2 assert cb2.call_count == 4
def stacks(tdir, looper, alpha_handler): names = ['Alpha', 'Beta'] genKeys(tdir, names) aseed = randomSeed() bseed = randomSeed() def bHandler(m): msg, a = m beta.send(msg, a) stackParams = { "name": names[0], "ha": genHa(), "auto": 2, "basedirpath": tdir } timer = MockTimer(0) alpha = SimpleZStack(stackParams, alpha_handler.handle, aseed, False, timer=timer) stackParams = { "name": names[1], "ha": genHa(), "auto": 2, "basedirpath": tdir } timer = MockTimer(0) beta = SimpleZStack(stackParams, bHandler, bseed, True, timer=timer) amotor = SMotor(alpha) looper.add(amotor) bmotor = SMotor(beta) looper.add(bmotor) return alpha, beta
def test_repeating_timer_doesnt_repeat_too_much(): timer = MockTimer() cb = Callback() RepeatingTimer(timer, 5, cb) timer.advance(12) assert cb.call_count == 1 timer.advance(12) assert cb.call_count == 2
def __init__(self, node_count: int = 4, random: Optional[SimRandom] = None): self._random = random if random else DefaultSimRandom() self._timer = MockTimer() self._network = SimNetwork(self._timer, self._random) validators = genNodeNames(node_count) primary_name = validators[0] genesis_txns = create_pool_txn_data( node_names=validators, crypto_factory=create_default_bls_crypto_factory(), get_free_port=partial(random.integer, 9000, 9999))['txns'] self._nodes = [ReplicaService(name, validators, primary_name, self._timer, InternalBus(), self.network.create_peer(name), write_manager=create_test_write_req_manager(name, genesis_txns)) for name in validators]
def test_repeating_timer_triggers_callback_on_time(): timer = MockTimer() cb = Callback() RepeatingTimer(timer, 5, cb) assert cb.call_count == 0 timer.advance(5) assert cb.call_count == 1 timer.advance(5) assert cb.call_count == 2
def __init__(self, node_count: int = 4, random: Optional[SimRandom] = None): self._random = random if random else DefaultSimRandom() self._timer = MockTimer() self._network = SimNetwork(self._timer, self._random, self._serialize_deserialize) self._nodes = [] validators = genNodeNames(node_count) # ToDo: maybe it should be a random too? primary_name = validators[0] genesis_txns = create_pool_txn_data( node_names=validators, crypto_factory=create_default_bls_crypto_factory(), get_free_port=partial(random.integer, 9000, 9999))['txns'] for name in validators: # TODO: emulate it the same way as in Replica, that is sender must have 'node_name:inst_id' form replica_name = generateName(name, 0) handler = partial(self.network._send_message, replica_name) write_manager = create_test_write_req_manager(name, genesis_txns) replica = ReplicaService(replica_name, validators, primary_name, self._timer, InternalBus(), self.network.create_peer(name, handler), write_manager=write_manager, bls_bft_replica=MockBlsBftReplica()) # ToDo: For now, future_primary_handler is depended from the node. # And for now we need to patching set_node_state functionality future_primaries_handler = FuturePrimariesBatchHandler( write_manager.database_manager, FakeSomething(nodeReg={}, nodeIds=[])) future_primaries_handler._get_primaries = lambda *args, **kwargs: replica._data.primaries write_manager.register_batch_handler(future_primaries_handler) # ToDo: also, it should be done at the zero-view stage. write_manager.future_primary_handler.set_node_state() replica.config.NEW_VIEW_TIMEOUT = 30 * 1000 self._nodes.append(replica)
def test_multiple_classes_with_repeating_timers_can_work_together_with_restarts(): timer = MockTimer() class TestService: def __init__(self, interval: int, timer: TimerService): self.counter = 0 self._timer = RepeatingTimer(timer, interval, self._on_timer) self._timer.stop() self._timer.start() def _on_timer(self): self.counter += 1 service1 = TestService(5, timer) service2 = TestService(7, timer) timer.run_for(6) assert service1.counter == 1 assert service2.counter == 0 timer.run_for(2) assert service1.counter == 1 assert service2.counter == 1
def view_change_service(): data = ConsensusSharedData("some_name", genNodeNames(4), 0) return ViewChangeService(data, MockTimer(0), InternalBus(), MockNetwork())
def timer(): return MockTimer(0)
def view_change_service(internal_bus, external_bus, stasher): data = ConsensusSharedData("some_name", genNodeNames(4), 0) return ViewChangeService(data, MockTimer(0), internal_bus, external_bus, stasher)
def mock_timer(): return MockTimer()
def mock_timer(initial_time): return MockTimer(initial_time)