def startScheduler(self, sources): r = random.Random() now = datetime.datetime.now(eventcalendar.LOCAL) def eventInstanceStarted(eventInstance): self.pollData(*eventInstance.event.content) def eventStopped(eventInstance): pass self.scheduler.subscribe(eventInstanceStarted, eventInstanceStopped) for source in sources: freq = sourceGetSampleFrequency(source) # randomly offset the polling offset = datetime.timedelta(seconds=r.randint(0, freq)) data = (str(sourceGetConnectionInfo(source)), sourceGetComponentId(source), sourceGetUIStateKey(source), sourceGetName(source), sourceGetFileName(source)) # FIXME: Event never actually allowed a timedelta as rrule, # so I doubt this refactoring of scheduler ever worked calendar = eventcalendar.Calendar() calendar.addEvent(now.isoformat(), now + offset, now + offset + datetime.timedelta(seconds=1), data, rrule=datetime.timedelta(seconds=freq)) self.scheduler.setCalendar(calendar)
def testScheduledNotACallbackWhenCancelled(self): now = _now() start = now end = now + timedelta(hours=1) calendar = eventcalendar.Calendar() calendar.addEvent(eventcalendar.Event('uid', start, end, 'content')) self._scheduler.setCalendar(calendar)
def testScheduledEndCallbackWhenEndInWindowSize(self): now = _now() start = now - timedelta(hours=1) end = now + timedelta(hours=1) calendar = eventcalendar.Calendar() calendar.addEvent(eventcalendar.Event('uid', start, end, 'content')) self._scheduler.setCalendar(calendar) resultSeconds = self._scheduler._nextStart expectedSeconds = _toSeconds(end - now) self.assertEquals(round(resultSeconds / 10.0), round(expectedSeconds / 10.0))
def testDefaultScheduledCallbackWhenBeforeAndAfterTheWindowSize(self): now = _now() start = now - timedelta(days=1) end = now + timedelta(days=2) calendar = eventcalendar.Calendar() calendar.addEvent(eventcalendar.Event('uid', start, end, 'content')) self._scheduler.setCalendar(calendar) resultSeconds = self._scheduler._nextStart expectedSeconds = _toSeconds(self._scheduler.windowSize) / 2 self.assertEquals(round(resultSeconds / 10.0), round(expectedSeconds / 10.0))
def testSubscribe(self): # create a list to store subscription call results calls = [] started = lambda ei: calls.append(('started', ei.event.content)) ended = lambda ei: calls.append(('ended', ei.event.content)) sid = self._scheduler.subscribe(started, ended) self.assertEquals(calls, []) # create a calendar and put a currently active event on it calendar = eventcalendar.Calendar() now = _now() start = now - timedelta(hours=1) end = now + timedelta(minutes=1) calendar.addEvent(eventcalendar.Event('uid', start, end, 'foo')) self._scheduler.setCalendar(calendar) # verify that we gat a start call for the active event instance self.assertEquals(calls, [('started', 'foo')]) eis = self._scheduler.getCalendar().getActiveEventInstances() self.assertEventAttribsEqual(eis[0].event, start, end, 'foo', None, None) # now set a new empty calendar on the scheduler calendar = eventcalendar.Calendar() self._scheduler.setCalendar(calendar) # make sure we got an end call for the previously active event instance self.assertEquals(calls, [('started', 'foo'), ('ended', 'foo')]) eis = self._scheduler.getCalendar().getActiveEventInstances() self.failIf(eis, 'Empty calendar so should not have active event instances') self._scheduler.unsubscribe(sid)
def testUnsubscribe(self): # create a list to store subscription call results calls = [] started = lambda c: calls.append(('started', c.content)) ended = lambda c: calls.append(('ended', c.content)) sid = self._scheduler.subscribe(started, ended) self._scheduler.unsubscribe(sid) now = _now() start = now - timedelta(hours=1) end = now + timedelta(minutes=1) calendar = eventcalendar.Calendar() self.assertEquals(calls, []) calendar.addEvent(eventcalendar.Event('uid', start, end, 'content')) self._scheduler.setCalendar(calendar) self.assertEquals(calls, []) eis = self._scheduler.getCalendar().getActiveEventInstances() self.failUnless(eis) self.assertEquals(eis[0].event.content, 'content')