def test_event_cannot_assign_callback_when_assigned(buffer_connection): def fake_callback(): pass def fake_callback2(): pass from moler.events.unix.wait4prompt import Wait4prompt event = Wait4prompt(connection=buffer_connection.moler_connection, prompt="bash", till_occurs_times=1) event.add_event_occurred_callback(fake_callback, callback_params=dict()) event.enable_log_occurrence() with pytest.raises(MolerException) as ex: event.add_event_occurred_callback(fake_callback) assert "is already assigned" in str(ex) with pytest.raises(MolerException) as ex: event.add_event_occurred_callback(fake_callback2) assert "is already assigned" in str(ex) event.remove_event_occurred_callback() event.add_event_occurred_callback(fake_callback2) ret = event.get_last_occurrence() assert ret is None
def test_event_whole_output(buffer_connection): from moler.events.unix.wait4prompt import Wait4prompt output = "bash\n" event = Wait4prompt(connection=buffer_connection.moler_connection, prompt="bash", till_occurs_times=1) event.start(timeout=0.1) buffer_connection.moler_connection.data_received(output.encode("utf-8")) event.await_done() assert event.done() is True
def test_events_false_any(): connection = ObservableConnection() events = list() patterns = ("aaa", "bbb") for pattern in patterns: event = Wait4prompt(connection=connection, till_occurs_times=1, prompt=pattern) event.start() events.append(event) assert EventAwaiter.wait_for_any(timeout=0.1, events=events) is False done, not_done = EventAwaiter.separate_done_events(events) assert 0 == len(done) assert 2 == len(not_done) EventAwaiter.cancel_all_events(events)
def test_event_output_in_parts(buffer_connection): from moler.events.unix.wait4prompt import Wait4prompt outputs = ["ba", "sh\n"] event = Wait4prompt(connection=buffer_connection.moler_connection, prompt="bash", till_occurs_times=1) event.start(timeout=0.1) for output in outputs: buffer_connection.moler_connection.data_received( output.encode("utf-8")) event.await_done() assert event.done() is True with pytest.raises(ResultAlreadySet): event.event_occurred("data")
def test_events_true_any_one(): connection = ThreadedMolerConnection() events = list() patterns = ("aaa", "bbb") for pattern in patterns: event = Wait4prompt(connection=connection, till_occurs_times=1, prompt=pattern) event.start() events.append(event) connection.data_received(patterns[0], datetime.datetime.now()) assert EventAwaiter.wait_for_any(timeout=0.1, events=events) is True done, not_done = EventAwaiter.separate_done_events(events) assert 1 == len(done) assert 1 == len(not_done) EventAwaiter.cancel_all_events(events)
def test_event_get_last_occurrence(buffer_connection): from moler.events.unix.wait4prompt import Wait4prompt output = "bash\n" dict_output = { 'line': u'bash', 'matched': u'bash', 'named_groups': {}, 'groups': (), 'time': 0 } event = Wait4prompt(connection=buffer_connection.moler_connection, prompt="bash", till_occurs_times=1) event.start(timeout=0.1) buffer_connection.moler_connection.data_received(output.encode("utf-8")) event.await_done() occurrence = event.get_last_occurrence() occurrence['time'] = 0 assert occurrence == dict_output