Skip to content

Commit

Permalink
Replace override_current_time with proper patching. Resolves #141.
Browse files Browse the repository at this point in the history
  • Loading branch information
dnephin committed May 20, 2012
1 parent baeb845 commit 89a227c
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 157 deletions.
15 changes: 4 additions & 11 deletions tests/core/actionrun_test.py
Expand Up @@ -2,9 +2,9 @@
import shutil
import tempfile

from testify import run, setup, TestCase, assert_equal, turtle
from testify import run, setup, TestCase, assert_equal, turtle, teardown
from testify.assertions import assert_raises, assert_in
from testify.test_case import class_setup, class_teardown, teardown
from tests import testingutils
from tests.assertions import assert_length
from tests.mocks import MockNode
from tests.testingutils import Turtle
Expand All @@ -17,16 +17,9 @@
from tron.serialize import filehandler
from tron.utils import timeutils

class ActionRunContextTestCase(TestCase):
class ActionRunContextTestCase(testingutils.MockTimeTestCase):

@class_setup
def freeze_time(self):
timeutils.override_current_time(datetime.datetime.now())
self.now = timeutils.current_time()

@class_teardown
def unfreeze_time(self):
timeutils.override_current_time(None)
now = datetime.datetime.now()

@setup
def build_context(self):
Expand Down
17 changes: 6 additions & 11 deletions tests/core/job_test.py
Expand Up @@ -4,11 +4,11 @@
from tests import mocks
from tests.assertions import assert_length, assert_call
from tests.mocks import MockNode
from tests.testingutils import MockReactorTestCase, Turtle
from tests.testingutils import Turtle
from tests import testingutils
from tron import node, event
from tron.core import job, jobrun
from tron.core.actionrun import ActionRun
from tron.utils import timeutils


class JobContextTestCase(TestCase):
Expand Down Expand Up @@ -358,7 +358,9 @@ def __call__(self, *args, **kwargs):
return [self.manual_run]


class JobSchedulerManualStartTestCase(TestCase):
class JobSchedulerManualStartTestCase(testingutils.MockTimeTestCase):

now = datetime.datetime.now()

@setup
def setup_job(self):
Expand All @@ -375,13 +377,6 @@ def setup_job(self):
self.manual_run = Turtle()
self.job.build_new_runs = MockRunBuilder(manual_run=self.manual_run)

self.now = datetime.datetime.now()
timeutils.override_current_time(self.now)

@teardown
def teardown_timeutils(self):
timeutils.override_current_time(None)

def test_manual_start(self):
manual_runs = self.job_scheduler.manual_start()

Expand All @@ -398,7 +393,7 @@ def test_manual_start_with_run_time(self):
assert_length(self.manual_run.start.calls, 1)


class JobSchedulerScheduleTestCase(MockReactorTestCase):
class JobSchedulerScheduleTestCase(testingutils.MockReactorTestCase):

module_to_mock = job

Expand Down
26 changes: 9 additions & 17 deletions tests/core/jobrun_test.py
@@ -1,12 +1,12 @@
import datetime
import pytz
from testify import TestCase, setup, assert_equal, teardown
from testify import TestCase, setup, assert_equal
from testify.assertions import assert_in
from tests.assertions import assert_length, assert_raises, assert_call
from tests.mocks import MockNode
from tron.core import jobrun, actionrun
from tests.testingutils import Turtle
from tron.utils import timeutils
from tests import testingutils


class JobRunContextTestCase(TestCase):
Expand All @@ -23,11 +23,11 @@ def test_cleanup_job_status(self):

def test_cleanup_job_status_failure(self):
self.jobrun.action_runs.is_failed = True
assert_equal(self.context.cleanup_job_status, 'FAILURE')


class JobRunTestCase(testingutils.MockTimeTestCase):

class JobRunTestCase(TestCase):
now = datetime.datetime(2012, 3, 14, 15, 9, 20)

@setup
def setup_jobrun(self):
Expand All @@ -44,10 +44,6 @@ def setup_jobrun(self):
self.job_run.watch = Turtle()
self.job_run.notify = Turtle()

@teardown
def teardown_jobrun(self):
timeutils.override_current_time(None)

def test__init__(self):
assert_equal(self.job_run.job_name, 'jobname')
assert_equal(self.job_run.run_time, self.run_time)
Expand Down Expand Up @@ -103,15 +99,11 @@ def test_set_action_runs_duplicate(self):
self.job_run._set_action_runs, run_collection)

def test_seconds_until_run_time(self):
now = datetime.datetime(2012, 3, 14, 15, 9, 20)
timeutils.override_current_time(now)
seconds = self.job_run.seconds_until_run_time()
assert_equal(seconds, 6)

def test_seconds_until_run_time_with_tz(self):
self.job_run.run_time = self.run_time.replace(tzinfo=pytz.utc)
now = datetime.datetime(2012, 3, 14, 15, 9, 20)
timeutils.override_current_time(now)
seconds = self.job_run.seconds_until_run_time()
assert_equal(seconds, 6)

Expand Down Expand Up @@ -139,7 +131,7 @@ def test_start_no_startable_action_runs(self):
assert_length(self.job_run.notify.calls, 1)

def test_do_start(self):
timeutils.override_current_time(self.run_time)
self.now = self.run_time
startable_runs = [Turtle(), Turtle(), Turtle()]
self.job_run.action_runs.get_startable_action_runs = lambda: startable_runs

Expand All @@ -153,15 +145,15 @@ def test_do_start(self):
assert_call(self.job_run.notify, 0, self.job_run.EVENT_STARTED)

def test_do_start_all_failed(self):
timeutils.override_current_time(self.run_time)
self.now = self.run_time
self.job_run._start_action_runs = lambda: [None]

assert not self.job_run._do_start()
assert_equal(self.job_run.start_time, self.run_time)
assert_length(self.job_run.notify.calls, 0)

def test_do_start_some_failed(self):
timeutils.override_current_time(self.run_time)
self.now = self.run_time
self.job_run._start_action_runs = lambda: [True, None]

assert self.job_run._do_start()
Expand Down Expand Up @@ -251,15 +243,15 @@ def test_state_with_no_action_runs(self):
assert_equal(self.job_run.state, actionrun.ActionRun.STATE_UNKNOWN)

def test_finalize(self):
timeutils.override_current_time(self.run_time)
self.now = self.run_time
self.job_run.action_runs.is_failed = False
self.job_run.finalize()
assert_call(self.job_run.notify, 0, self.job_run.EVENT_SUCCEEDED)
assert_call(self.job_run.notify, 1, self.job_run.NOTIFY_DONE)
assert_equal(self.job_run.end_time, self.run_time)

def test_finalize_failure(self):
timeutils.override_current_time(self.run_time)
self.now = self.run_time
self.job_run.finalize()
assert_call(self.job_run.notify, 0, self.job_run.EVENT_FAILED)
assert_call(self.job_run.notify, 1, self.job_run.NOTIFY_DONE)
Expand Down
21 changes: 8 additions & 13 deletions tests/mcp_test.py
Expand Up @@ -3,31 +3,26 @@
import shutil
import StringIO
import tempfile
import time
import yaml

from testify import TestCase, class_setup, class_teardown, setup, teardown
from testify import TestCase, setup, teardown
from testify import assert_raises, assert_equal, suite, run
from testify.utils import turtle
import time
import yaml

from tests import testingutils
from tests.assertions import assert_length
from tests.mocks import MockNode

import tron
from tron.config import config_parse

from tron.core import job, actionrun
from tron import mcp, scheduler, event, node, service
from tron.utils import timeutils


class StateHandlerIntegrationTestCase(TestCase):
@class_setup
def class_setup_time(self):
timeutils.override_current_time(datetime.datetime.now())
self.now = timeutils.current_time()
class StateHandlerIntegrationTestCase(testingutils.MockTimeTestCase):

@class_teardown
def class_teardown_time(self):
timeutils.override_current_time(None)
now = datetime.datetime.now()

def _advance_run(self, job_run, state):
for action_run in job_run.action_runs:
Expand Down

0 comments on commit 89a227c

Please sign in to comment.