def test_dispatch_before_execution(monkeypatch): value = [original_value] def mock_gen_dispatch_timestamp(self, *args, **kwargs): exec_time = datetime.datetime.now().timestamp() + 3 self.dispatch_seconds = [exec_time] monkeypatch.setattr(Dispatch, 'gen_dispatch_timestamp', mock_gen_dispatch_timestamp) _unused_scheduler = Dispatch(1, hook_modify_value_once, value) time.sleep(3 - 2) assert value[0] == original_value
def test_dispatch_twice(monkeypatch): value = [original_value] def mock_gen_dispatch_timestamp(self, *args, **kwargs): exec_time = datetime.datetime.now().timestamp() + 3 self.dispatch_seconds = [exec_time, exec_time + 3] monkeypatch.setattr(Dispatch, 'gen_dispatch_timestamp', mock_gen_dispatch_timestamp) _unused_scheduler = Dispatch(2, hook_modify_value_schedule, value) time.sleep(6 + 2) assert value[0] == original_value + 2
def test_launch_on_sunday(setup_dispatch_time): # Arrange sunday = datetime.datetime(2020, 11, 1, 12, 30, 0) offset = (1 * 60 * 60) setup_dispatch_time(sunday, offset) value = [original_value] # Act scheduler = Dispatch(1, hook_modify_value_once, value) # Assert # dispatch on monday assert scheduler.dispatch_seconds[0] == datetime.datetime( 2020, 11, 2, 10, 0, 0).timestamp()
def test_launch_on_thursday_night(setup_dispatch_time): # Arrange thursday = datetime.datetime(2020, 10, 29, 19, 30, 0) offset = (1 * 60 * 60) setup_dispatch_time(thursday, offset) value = [original_value] # Act scheduler = Dispatch(1, hook_modify_value_once, value) # Assert # dispatch on friday assert scheduler.dispatch_seconds[0] == datetime.datetime( 2020, 10, 30, 10, 0, 0).timestamp()
def test_dispatch_on_next_monday(setup_dispatch_time): # Arrange thursday = datetime.datetime(2020, 10, 29, 9, 0, 0) offset_smaller_than_one_day = (4 * 9 * 60 * 60) setup_dispatch_time(thursday, offset_smaller_than_one_day) value = [original_value] # Act scheduler = Dispatch(1, hook_modify_value_once, value) # Assert # dispatch on thursday assert scheduler.dispatch_seconds[0] == datetime.datetime( 2020, 11, 5, 9, 0, 0).timestamp()
def main(argv): config = {} try: opts, _ = getopt.getopt(argv, "hu:p:f:n:t:d:", \ ["username="******"password="******"frequency=","number=","timeout=","delay="]) except getopt.GetoptError: usage() sys.exit(2) config['username'] = None config['password'] = None config['frequency'] = 0 config['number'] = 1 config['timeout'] = 0 config['delay'] = 1 for opt, arg in opts: if opt == '-h': usage() sys.exit() elif opt in ("-u", "--username"): config['username'] = arg elif opt in ("-p", "--password"): config['password'] = arg elif opt in ("-f", "--frequency"): config['frequency'] = int(arg) elif opt in ("-n", "--number"): config['number'] = int(arg) elif opt in ("-t", "--timeout"): config['timeout'] = int(arg) elif opt in ("-d", "--delay"): config['delay'] = int(arg) if not config['username'] or not config['password']: usage() sys.exit(2) for key in config: print('key:', key) print('value: %s' % str(config[key])) _unused_dispatch = Dispatch(config['frequency'], play, config) if config['frequency'] != 0: while True: time.sleep(100)
def test_run_once(): value = [original_value] _unused_scheduler = Dispatch(0, hook_modify_value_once, value) assert value[0] == original_value + 1