def test_scheduler(): sent_msgs = {} def sender(rcpt, msg): if rcpt not in sent_msgs: sent_msgs[rcpt] = [] sent_msgs[rcpt].append((msg, time.time())) scheduler = Scheduler(sender) scheduler.add_event( **{ "msg": "This is a test event", "recipients": ["*****@*****.**", "*****@*****.**", "*****@*****.**"], "start": time.time(), "repeat": 3, # how many times to send the event "interval": 1, # how many seconds between sends } ) # ensure that we haven't sent any until we start the scheduler assert not sent_msgs scheduler.start() time.sleep(5) scheduler.stop() assert len(sent_msgs) == 3, "We have messages for different recipients" for rcpt in ["*****@*****.**", "*****@*****.**", "*****@*****.**"]: assert len(sent_msgs[rcpt]) == 3, "number of msgs was %d" % len(sent_msgs[rcpt]) last_msg = None for msg in sent_msgs[rcpt]: assert msg if last_msg: assert msg[1] - last_msg[1] > 1 last_msg = msg
def test_scheduler_overlap(): sent_msgs = {} def sender(rcpt, msg): if msg not in sent_msgs: sent_msgs[msg] = [] assert rcpt == "*****@*****.**" sent_msgs[msg].append(time.time()) scheduler = Scheduler(sender) scheduler.add_event( **{ "msg": "This is a two sec repeat", "recipients": ["*****@*****.**"], "start": time.time(), "repeat": 2, # how many times to send the event "interval": 2, # how many seconds between sends } ) scheduler.add_event( **{ "msg": "This is a one sec repeat", "recipients": ["*****@*****.**"], "start": time.time(), "repeat": 3, # how many times to send the event "interval": 1, # how many seconds between sends } ) # ensure that we haven't sent any until we start the scheduler assert not sent_msgs scheduler.start() time.sleep(4) scheduler.stop() assert len(sent_msgs) == 2 assert len(sent_msgs["This is a two sec repeat"]) == 2 assert len(sent_msgs["This is a one sec repeat"]) == 3