コード例 #1
0
ファイル: replayer.py プロジェクト: dougives/indy-plenum
def replay_patched_node(looper, replaying_node, node_recorder, cr):
    node_run_no = 0
    looper.add(replaying_node)
    cr.start_playing()
    next_stop_at = time.perf_counter() + (cr.start_times[node_run_no][1] -
                                          cr.start_times[node_run_no][0])
    while cr.is_playing:
        vals = cr.get_next()
        if next_stop_at is not None and time.perf_counter() >= next_stop_at:
            node_run_no += 1
            if node_run_no < len(cr.start_times):
                # The node stopped here
                sleep_for = cr.start_times[node_run_no][0] - cr.start_times[
                    node_run_no - 1][1]
                replaying_node.stop()
                looper.removeProdable(replaying_node)
                # Create new node since node is destroyed on stop
                replaying_node = replaying_node.__class__(
                    replaying_node.name,
                    config_helper=replaying_node.config_helper,
                    ha=replaying_node.nodestack.ha,
                    cliha=replaying_node.clientstack.ha)
                patch_replaying_node(replaying_node, node_recorder,
                                     cr.start_times)
                print(
                    'sleeping for {} to simulate node stop'.format(sleep_for))
                time.sleep(sleep_for)
                after = cr.start_times[node_run_no][1] - cr.start_times[
                    node_run_no][0]
                next_stop_at = time.perf_counter() + after
                print('Next stop after {}'.format(after))
                looper.add(replaying_node)
            else:
                next_stop_at = None

        if not vals:
            continue

        n_msgs, c_msgs = vals
        if n_msgs:
            for inc in n_msgs:
                if Recorder.is_incoming(inc):
                    msg, frm = to_bytes(inc[1]), to_bytes(inc[2])
                    replaying_node.nodestack._verifyAndAppend(msg, frm)
                if Recorder.is_disconn(inc):
                    disconnecteds = inc[1:]
                    replaying_node.nodestack._connsChanged(
                        set(), disconnecteds)

        if c_msgs:
            incomings = Recorder.filter_incoming(c_msgs)
            for inc in incomings:
                msg, frm = to_bytes(inc[0]), to_bytes(inc[1])
                replaying_node.clientstack._verifyAndAppend(msg, frm)

        looper.run(replaying_node.prod())

    return replaying_node
コード例 #2
0
def test_recorder_get_next(recorder):
    incoming_count = 100
    outgoing_count = 50

    incoming = [(randomString(100), randomString(6))
                for _ in range(incoming_count)]
    outgoing = [(randomString(100), randomString(6))
                for _ in range(outgoing_count)]

    while incoming or outgoing:
        if random.choice([0, 1]) and outgoing:
            recorder.add_outgoing(*outgoing.pop())
            time.sleep(random.choice([0, 1]) + random.random())
        elif incoming:
            recorder.add_incoming(*incoming.pop())
            time.sleep(random.choice([0, 1]) + random.random())
        else:
            continue

    recorded_incomings = OrderedDict()
    for k, v in recorder.store.iterator(include_value=True):
        v = Recorder.get_parsed(v, only_incoming=True)
        if v:
            recorded_incomings[int(k)] = v

    assert len(recorded_incomings) == incoming_count

    max_time_to_run = incoming_count * 2 + 10

    recorder.start_playing()
    start = time.perf_counter()

    while recorder.is_playing and (time.perf_counter() <
                                   start + max_time_to_run):
        vals = recorder.get_next()
        if vals:
            inc = Recorder.filter_incoming(vals)
            if inc:
                assert recorded_incomings.popitem(last=False)[1] == inc
        else:
            time.sleep(0.01)

    assert len(recorded_incomings) == 0
    assert not recorder.is_playing
コード例 #3
0
def test_recorder_get_next(recorder):
    incoming_count = 100
    outgoing_count = 50

    incoming = [(randomString(100), randomString(6)) for _ in range(incoming_count)]
    outgoing = [(randomString(100), randomString(6)) for _ in range(outgoing_count)]

    while incoming or outgoing:
        if random.choice([0, 1]) and outgoing:
            recorder.add_outgoing(*outgoing.pop())
            time.sleep(random.choice([0, 1]) + random.random())
        elif incoming:
            recorder.add_incoming(*incoming.pop())
            time.sleep(random.choice([0, 1]) + random.random())
        else:
            continue

    recorded_incomings = OrderedDict()
    for k, v in recorder.store.iterator(include_value=True):
        v = Recorder.get_parsed(v, only_incoming=True)
        if v:
            recorded_incomings[int(k)] = v

    assert len(recorded_incomings) == incoming_count

    max_time_to_run = incoming_count * 2 + 10

    recorder.start_playing()
    start = time.perf_counter()

    while recorder.is_playing and (time.perf_counter() < start + max_time_to_run):
        vals = recorder.get_next()
        if vals:
            inc = Recorder.filter_incoming(vals)
            if inc:
                assert recorded_incomings.popitem(last=False)[1] == inc
        else:
            time.sleep(0.01)

    assert len(recorded_incomings) == 0
    assert not recorder.is_playing
コード例 #4
0
ファイル: replayer.py プロジェクト: michaeldboyd/indy-plenum
def replay_patched_node(looper, replaying_node, node_recorder, cr):
    node_run_no = 0
    looper.add(replaying_node)
    cr.start_playing()
    next_stop_at = time.perf_counter() + (cr.start_times[node_run_no][1] -
                                          cr.start_times[node_run_no][0])

    progress_data = _create_progress_data(replaying_node.replay_msg_count)
    #
    # n_msg_count = 0
    # start_time = time.perf_counter()
    # next_progress_note = start_time + 5
    while cr.is_playing:
        _print_progress(progress_data)

        vals = cr.get_next()
        if next_stop_at is not None and time.perf_counter() >= next_stop_at:
            node_run_no += 1
            if node_run_no < len(cr.start_times):
                # The node stopped here
                sleep_for, after = _cal_run_times(node_run_no, cr.start_times)
                replaying_node.stop()
                looper.removeProdable(replaying_node)
                # Create new node since node is destroyed on stop
                replaying_node = replaying_node.__class__(replaying_node.name,
                                                          config_helper=replaying_node.config_helper,
                                                          ha=replaying_node.nodestack.ha,
                                                          cliha=replaying_node.clientstack.ha)
                patch_replaying_node(replaying_node, node_recorder, cr.start_times)
                print('Sleeping for {}s to simulate node stop'.format(sleep_for))
                time.sleep(sleep_for)

                if after is None:
                    next_stop_at = None
                else:
                    next_stop_at = time.perf_counter() + after
                    print('Next stop after {}s'.format(after))

                looper.add(replaying_node)
            else:
                next_stop_at = None

        if not vals:
            looper.run(replaying_node.prod())
            continue

        n_msgs, c_msgs = vals
        progress_data = _update_progress_msg_count(progress_data, len(n_msgs))
        if n_msgs:
            for inc in n_msgs:
                if Recorder.is_incoming(inc):
                    msg, frm = to_bytes(inc[1]), to_bytes(inc[2])
                    replaying_node.nodestack._verifyAndAppend(msg, frm)
                if Recorder.is_disconn(inc):
                    disconnecteds = inc[1:]
                    replaying_node.nodestack._connsChanged(set(), disconnecteds)

        if c_msgs:
            incomings = Recorder.filter_incoming(c_msgs)
            for inc in incomings:
                msg, frm = to_bytes(inc[0]), to_bytes(inc[1])
                replaying_node.clientstack._verifyAndAppend(msg, frm)

        looper.run(replaying_node.prod())

    return replaying_node