def setUp(self): self.rq = Queue.PriorityQueue() self.es = EventSequence('match_mock', ['e1', 'e2', 'e3'], self.rq, 0.1, False) self.es_strict = EventSequence('match_mock', ['e1', 'e2', 'e3'], self.rq, 0.1, True) self.es_wait = EventSequence('match_mock', ['e1', 'e2', 'e3'], self.rq, 0.1, False, 'w1') self.event_sequences = [self.es, self.es_strict, self.es_wait] track_mock = mock.Mock(spec=models.Track) track_mock.uri = 'pandora:track:id_mock:token_mock' self.tl_track_mock = mock.Mock(spec=models.TlTrack) self.tl_track_mock.track = track_mock
def event_sequence_wait(rq): return EventSequence("match_mock", ["e1", "e2", "e3"], rq, 0.1, False, "w1")
def event_sequence_strict(rq): return EventSequence("match_mock", ["e1", "e2", "e3"], rq, 0.1, True)
def event_sequence_wait(rq): return EventSequence('match_mock', ['e1', 'e2', 'e3'], rq, 0.1, False, 'w1')
def event_sequence_strict(rq): return EventSequence('match_mock', ['e1', 'e2', 'e3'], rq, 0.1, True)
class EventSequenceTests(unittest.TestCase): def setUp(self): self.rq = Queue.PriorityQueue() self.es = EventSequence('match_mock', ['e1', 'e2', 'e3'], self.rq, 0.1, False) self.es_strict = EventSequence('match_mock', ['e1', 'e2', 'e3'], self.rq, 0.1, True) self.es_wait = EventSequence('match_mock', ['e1', 'e2', 'e3'], self.rq, 0.1, False, 'w1') self.event_sequences = [self.es, self.es_strict, self.es_wait] track_mock = mock.Mock(spec=models.Track) track_mock.uri = 'pandora:track:id_mock:token_mock' self.tl_track_mock = mock.Mock(spec=models.TlTrack) self.tl_track_mock.track = track_mock def test_events_ignored_if_time_position_is_zero(self): for es in self.event_sequences: es.notify('e1', tl_track=self.tl_track_mock) for es in self.event_sequences: assert not es.is_monitoring() def test_start_monitor_on_event(self): for es in self.event_sequences: es.notify('e1', tl_track=self.tl_track_mock, time_position=100) for es in self.event_sequences: assert es.is_monitoring() def test_start_monitor_handles_no_tl_track(self): for es in self.event_sequences: es.notify('e1', tl_track=self.tl_track_mock, time_position=100) for es in self.event_sequences: assert es.is_monitoring() def test_stop_monitor_adds_result_to_queue(self): for es in self.event_sequences[0:2]: es.notify('e1', tl_track=self.tl_track_mock, time_position=100) es.notify('e2', time_position=100) es.notify('e3', time_position=100) for es in self.event_sequences[0:2]: es.wait(1.0) assert not es.is_monitoring() assert self.rq.qsize() == 2 def test_stop_monitor_only_waits_for_matched_events(self): self.es_wait.notify('e1', time_position=100) self.es_wait.notify('e_not_in_monitored_sequence', time_position=100) time.sleep(0.1 * 1.1) assert not self.es_wait.is_monitoring() assert self.rq.qsize() == 0 def test_stop_monitor_waits_for_event(self): self.es_wait.notify('e1', time_position=100) self.es_wait.notify('e2', time_position=100) self.es_wait.notify('e3', time_position=100) assert self.es_wait.is_monitoring() assert self.rq.qsize() == 0 self.es_wait.notify('w1', time_position=100) self.es_wait.wait(timeout=1.0) assert not self.es_wait.is_monitoring() assert self.rq.qsize() == 1 def test_get_stop_monitor_ensures_that_all_events_occurred(self): self.es.notify('e1', tl_track=self.tl_track_mock, time_position=100) self.es.notify('e2', time_position=100) self.es.notify('e3', time_position=100) assert self.rq.qsize() == 0 self.es.wait(timeout=1.0) self.es.events_seen = ['e1', 'e2', 'e3'] assert self.rq.qsize() > 0 def test_get_stop_monitor_strict_ensures_that_events_were_seen_in_order( self): self.es_strict.notify('e1', tl_track=self.tl_track_mock, time_position=100) self.es_strict.notify('e3', time_position=100) self.es_strict.notify('e2', time_position=100) self.es_strict.wait(timeout=1.0) assert self.rq.qsize() == 0 self.es_strict.notify('e1', tl_track=self.tl_track_mock, time_position=100) self.es_strict.notify('e2', time_position=100) self.es_strict.notify('e3', time_position=100) self.es_strict.wait(timeout=1.0) assert self.rq.qsize() > 0 def test_get_ratio_handles_repeating_events(self): self.es.target_sequence = ['e1', 'e2', 'e3', 'e1'] self.es.events_seen = ['e1', 'e2', 'e3', 'e1'] assert self.es.get_ratio() > 0 def test_get_ratio_enforces_strict_matching(self): self.es_strict.events_seen = ['e1', 'e2', 'e3', 'e4'] assert self.es_strict.get_ratio() == 0 self.es_strict.events_seen = ['e1', 'e2', 'e3'] assert self.es_strict.get_ratio() == 1