Skip to content

Commit

Permalink
Fix scheduler.once() running multiple times (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
unode authored Jun 14, 2021
1 parent bd0eeab commit d4f78c5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
12 changes: 12 additions & 0 deletions mmpy_bot/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,20 @@ def set_next_run(self, next_time: datetime):
raise AssertionError("The next_time parameter should be a datetime object.")
self.at_time = next_time
self.next_run = next_time
self.should_run = True

@property
def should_run(self):
return self._keep_running and super().should_run

@should_run.setter
def should_run(self, value):
self._keep_running = value

def run(self):
# This prevents the job from running more than once
self.should_run = False

super().run()
return schedule.CancelJob()

Expand Down
14 changes: 14 additions & 0 deletions tests/unit_tests/scheduler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import datetime
from pathlib import Path
from typing import Dict
from unittest.mock import Mock

import pytest

Expand Down Expand Up @@ -31,6 +32,19 @@ def job(path: str):
assert float(file.readline()) - 2 == pytest.approx(start, abs=0.3)


def test_once_single_call():
mock = Mock()
mock.side_effect = lambda: time.sleep(0.2)

schedule.once().do(mock)

for _ in range(10):
schedule.run_pending()
time.sleep(0.05)

mock.assert_called_once()


def test_recurring_thread():
def job(modifiable_arg: Dict):
# Modify the variable, which should be shared with the main thread.
Expand Down

0 comments on commit d4f78c5

Please sign in to comment.