def test_event_loop__multiple_events(self): farcy = self._farcy_instance() # Must have its time set first. event_1 = Struct(actor=Struct(login=None), type='PushEvent', created_at=datetime.now(UTC()), id=1) event_2 = Struct(actor=Struct(login=None), type='ForkEvent', created_at=datetime.now(UTC()), id=2) event_3 = Struct(actor=Struct(login=None), type='PullRequestEvent', created_at=datetime.now(UTC()), id=3) event_4 = Struct(actor=Struct(login=None), type='MemberEvent', created_at=datetime.now(UTC()), id=4) events = [] newest_id = farcy._event_loop([event_4, event_3, event_2, event_1], events) self.assertEqual(4, newest_id) self.assertEqual([event_1, event_3], events)
def test_event_loop__single_event(self): farcy = self._farcy_instance() # Must have its time set first. event = Struct(actor=Struct(login=None), type='PushEvent', created_at=datetime.now(UTC()), id=0xDEADBEEF) events = [] newest_id = farcy._event_loop([event], events) self.assertEqual(0xDEADBEEF, newest_id) self.assertEqual([event], events)
def test_event_loop__ignore_events_before_start(self): event = Struct(actor=Struct(login=None), type='PushEvent', created_at=datetime.now(UTC()), id=1) farcy = self._farcy_instance() # Must have its time set second. events = [] newest_id = farcy._event_loop([event], events) self.assertEqual(None, newest_id) self.assertEqual([], events)
def test_event_loop__ignore_old_events(self): farcy = self._farcy_instance() # Must have its time set first. farcy.last_event_id = 1 event = Struct(actor=Struct(login=None), type='PushEvent', created_at=datetime.now(UTC()), id=1) events = [] newest_id = farcy._event_loop([event], events) self.assertEqual(None, newest_id) self.assertEqual([], events)
def test_events__prevent_duplicate_calls(self): farcy = self._farcy_instance() self.assertEqual(None, farcy.last_event_id) event = Struct(actor=Struct(login=None), type='PushEvent', created_at=datetime.now(UTC()), id=0xDEADBEEF) farcy.repo.events.return_value = Struct([event], etag='DUMMY_ETAG') self.assertEqual(event, next(farcy.events())) self.assertEqual(0xDEADBEEF, farcy.last_event_id) self.assertRaises(FarcyException, next, farcy.events())
def test_events__end_loop(self): farcy = self._farcy_instance() self.assertEqual(None, farcy.last_event_id) event = Struct(actor=Struct(login=None), type='PushEvent', created_at=datetime.now(UTC()), id=0xDEADBEEF) farcy.repo.events.return_value = Struct( [event], etag='DUMMY_ETAG', last_response=Struct(headers={'X-Poll-Interval': 100})) event_itr = farcy.events() self.assertEqual(event, next(event_itr)) farcy.running = False self.assertRaises(StopIteration, next, event_itr)
def test_events__network_exception(self, mock_sleep, mock_event_loop): farcy = self._farcy_instance() event = Struct(actor=Struct(login=None), type='PushEvent', created_at=datetime.now(UTC()), id=0xDEADBEEF) call_count = [0] def side_effect(_, events): call_count[0] += 1 if call_count[0] == 1: raise ConnectionError('Foo') else: events.append(event) return event.id mock_event_loop.side_effect = side_effect self.assertEqual(event, next(farcy.events())) self.assertEqual(0xDEADBEEF, farcy.last_event_id) self.assertTrue(mock_sleep.called_with(1))