Skip to content

Commit

Permalink
Fixed textual referenced bound methods issue on persistent job stores #…
Browse files Browse the repository at this point in the history
  • Loading branch information
spengjie committed Oct 7, 2020
1 parent 15477e6 commit a34204e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
5 changes: 3 additions & 2 deletions apscheduler/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,9 @@ def __getstate__(self):

# Instance methods cannot survive serialization as-is, so store the "self" argument
# explicitly
if ismethod(self.func) and not isclass(self.func.__self__):
args = (self.func.__self__,) + tuple(self.args)
func = self.func
if ismethod(func) and not isclass(func.__self__) and obj_to_ref(func) == self.func_ref:
args = (func.__self__,) + tuple(self.args)
else:
args = self.args

Expand Down
21 changes: 19 additions & 2 deletions tests/test_jobstores.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ def dummy_classmethod(cls, a, b):
return a + b


dummy_instance = DummyClass()


@pytest.fixture
def memjobstore():
yield MemoryJobStore()
Expand Down Expand Up @@ -112,19 +115,33 @@ def create(jobstore, func=dummy_job, run_date=datetime(2999, 1, 1), id=None, pau
return create


def test_add_instance_method_job(jobstore, create_add_job):
def test_add_callable_instance_method_job(jobstore, create_add_job):
instance = DummyClass()
initial_job = create_add_job(jobstore, instance.dummy_method, kwargs={'a': 1, 'b': 2})
job = jobstore.lookup_job(initial_job.id)
assert job.func(*job.args, **job.kwargs) == 3


def test_add_class_method_job(jobstore, create_add_job):
def test_add_callable_class_method_job(jobstore, create_add_job):
initial_job = create_add_job(jobstore, DummyClass.dummy_classmethod, kwargs={'a': 1, 'b': 2})
job = jobstore.lookup_job(initial_job.id)
assert job.func(*job.args, **job.kwargs) == 3


def test_add_textual_instance_method_job(jobstore, create_add_job):
initial_job = create_add_job(jobstore, 'tests.test_jobstores:dummy_instance.dummy_method',
kwargs={'a': 1, 'b': 2})
job = jobstore.lookup_job(initial_job.id)
assert job.func(*job.args, **job.kwargs) == 3


def test_add_textual_class_method_job(jobstore, create_add_job):
initial_job = create_add_job(jobstore, 'tests.test_jobstores:DummyClass.dummy_classmethod',
kwargs={'a': 1, 'b': 2})
job = jobstore.lookup_job(initial_job.id)
assert job.func(*job.args, **job.kwargs) == 3


def test_lookup_job(jobstore, create_add_job):
initial_job = create_add_job(jobstore)
job = jobstore.lookup_job(initial_job.id)
Expand Down

0 comments on commit a34204e

Please sign in to comment.