Esempio n. 1
0
 def test_bad_status_codes_cause_no_go_flag(self):
     through_to_you = crosstown_traffic(
         no_go_status_codes=[418]
     )
     through_to_you.status_code = 418
     through_to_you.check_status_code_against_no_go_list()
     self.assertTrue(through_to_you.no_go)
Esempio n. 2
0
 def test_no_bad_status_codes_are_cool(self):
     through_to_you = crosstown_traffic(
         no_go_status_codes=[418]
     )
     through_to_you.status_code = 404
     through_to_you.check_status_code_against_no_go_list()
     self.assertFalse(through_to_you.no_go)
Esempio n. 3
0
def test_fail_without_response():
    '''
    Same test as above, but with fail_without_response, we get an error.
    '''
    some_task = lambda: None

    through_to_you = crosstown_traffic(same_thread=True, fail_without_response=True)

    with pytest.raises(ThreadHasNoResponse):
        through_to_you(some_task)
Esempio n. 4
0
def test_with_no_request():
    success_string = "Without a request in progress, the default behavior for crosstown_traffic is to run the task immediately."

    def append_me_to_pass():
        pass_flags.append(success_string)

    through_to_you = crosstown_traffic(same_thread=True)

    assert success_string not in pass_flags
    through_to_you(append_me_to_pass)
    assert success_string in pass_flags
    def test_with_no_request(self):

        self.has_run = False

        def append_me_to_pass():
            self.has_run = True

        through_to_you = crosstown_traffic(same_thread=True)
        through_to_you(append_me_to_pass)

        self.assertTrue(self.has_run)
Esempio n. 6
0
    def test_with_no_request(self):

        self.has_run = False

        def append_me_to_pass():
            self.has_run = True

        through_to_you = crosstown_traffic(same_thread=True)
        through_to_you(append_me_to_pass)

        self.assertTrue(self.has_run)
Esempio n. 7
0
    def test_fail_without_response(self):
        '''
        Same test as above, but with fail_without_response, we get an error.
        '''
        self.has_run = False

        def append_me_to_pass():
            self.has_run = True

        through_to_you = crosstown_traffic(same_thread=True, fail_without_response=True)

        self.assertRaises(ThreadHasNoResponse, through_to_you, append_me_to_pass)
Esempio n. 8
0
def test_assert_num_tasks():
    a_mixin = AsyncTestMixin()
    a_mixin.sub_setUp()
    a_mixin.assertNumCrosstownTasks(0)

    def some_task():
        pass

    through_to_you = crosstown_traffic()
    through_to_you(some_task)

    a_mixin.assertNumCrosstownTasks(1)
    def test_next_task(self):

        a_mixin = AsyncTestMixin()
        a_mixin.sub_setUp()
        a_mixin.assertNumCrosstownTasks(0)

        def some_task():
            pass

        through_to_you = crosstown_traffic()
        through_to_you(some_task)

        self.assertIs(some_task, a_mixin.next_task())
Esempio n. 10
0
    def test_next_task(self):

        a_mixin = AsyncTestMixin()
        a_mixin.sub_setUp()
        a_mixin.assertNumCrosstownTasks(0)

        def some_task():
            pass

        through_to_you = crosstown_traffic()
        through_to_you(some_task)

        self.assertIs(some_task, a_mixin.next_task())
    def test_fail_without_response(self):
        '''
        Same test as above, but with fail_without_response, we get an error.
        '''
        self.has_run = False

        def append_me_to_pass():
            self.has_run = True

        through_to_you = crosstown_traffic(same_thread=True,
                                           fail_without_response=True)

        self.assertRaises(ThreadHasNoResponse, through_to_you,
                          append_me_to_pass)
    def test_no_more_tasks(self):

        a_mixin = AsyncTestMixin()
        a_mixin.sub_setUp()
        a_mixin.assertNumCrosstownTasks(0)

        def some_task():
            pass

        through_to_you = crosstown_traffic()
        through_to_you(some_task)

        same_task = a_mixin.next_task()

        # That will be the only (and thus last) task.
        self.assertRaises(StopIteration, a_mixin.next_task)
Esempio n. 13
0
    def test_no_more_tasks(self):

        a_mixin = AsyncTestMixin()
        a_mixin.sub_setUp()
        a_mixin.assertNumCrosstownTasks(0)

        def some_task():
            pass

        through_to_you = crosstown_traffic()
        through_to_you(some_task)

        same_task = a_mixin.next_task()

        # That will be the only (and thus last) task.
        self.assertRaises(StopIteration, a_mixin.next_task)
Esempio n. 14
0
    def test_postiive_decorator_coherence(self):
        self.pass_flag = False

        def run_me_to_pass():
            self.pass_flag = True

        class FakeResponse(object):
            crosstown_tasks = []
            status = "200 OK"

        through_to_you = crosstown_traffic(same_thread=True)
        threading.current_thread().response_object = FakeResponse()
        through_to_you(run_me_to_pass)
        # threadpool doesn't matter because same_thread is True.
        through_to_you.run(reactor.threadpool)

        self.assertFalse(through_to_you.no_go)  # If the no_go is False...
        self.assertTrue(self.pass_flag)  # Then run_me_to_pass will have run.
    def test_postiive_decorator_coherence(self):
        self.pass_flag = False

        def run_me_to_pass():
            self.pass_flag = True

        class FakeResponse(object):
            crosstown_tasks = []
            status = "200 OK"

        through_to_you = crosstown_traffic(same_thread=True)
        threading.current_thread().response_object = FakeResponse()
        through_to_you(run_me_to_pass)
        # threadpool doesn't matter because same_thread is True.
        through_to_you.run(reactor.threadpool)

        self.assertFalse(through_to_you.no_go)  # If the no_go is False...
        self.assertTrue(self.pass_flag)  # Then run_me_to_pass will have run.
Esempio n. 16
0
def test_no_go_causes_task_not_to_fire():
    some_task = lambda: None

    class FakeResponse(object):
        crosstown_tasks = []
        status = "418 I'm a teapot.  Seriously."

    threading.current_thread().response_object = FakeResponse()
    through_to_you = crosstown_traffic(same_thread=True)

    through_to_you.no_go = True  # If no_go is True...
    through_to_you(some_task)  # and we call it...
    assert not through_to_you.response.crosstown_tasks  # We won't have added any tasks.

    through_to_you.no_go = False  # However if no_go is False...
    through_to_you(some_task)  # and we call it...

    # We will have added the function.
    assert through_to_you.response.crosstown_tasks[0].crosstown_task == some_task
Esempio n. 17
0
def test_typical_same_thread_operation():
    success_string = "With a Response affixed to this thread, the task will run if the status code is not a 'no-go'."

    def run_me_to_pass():
        pass_flags.append(success_string)

    class FakeResponse(object):
        crosstown_tasks = []
        status = "200 OK"

    through_to_you = crosstown_traffic(same_thread=True)
    threading.current_thread().response_object = FakeResponse()
    through_to_you(run_me_to_pass)
    # threadpool doesn't matter because same_thread is True.

    # The function hasn't run yet.
    assert success_string not in pass_flags
    through_to_you.run(reactor.threadpool)

    assert not through_to_you.no_go  # Since the no_go is False...
    assert success_string in pass_flags  # Then run_me_to_pass will have run.
Esempio n. 18
0
    def test_negative_decorator_coherence(self):

        def append_me_to_pass():
            pass

        class FakeResponse(object):
            crosstown_tasks = []
            status = "418 I'm a teapot.  Seriously."

        threading.current_thread().response_object = FakeResponse()
        through_to_you = crosstown_traffic(same_thread=True)

        through_to_you.no_go = True  # If no_go is True...
        through_to_you(append_me_to_pass)  # and we call it...
        self.assertFalse(through_to_you.response.crosstown_tasks)  # We won't have added any tasks.

        through_to_you.no_go = False  # However if no_go is False...
        through_to_you(append_me_to_pass)  # and we call it...
        self.assertEqual(through_to_you.response.crosstown_tasks[0].crosstown_task,
                         append_me_to_pass
                         )  # We will have added the function.
    def test_negative_decorator_coherence(self):
        def append_me_to_pass():
            pass

        class FakeResponse(object):
            crosstown_tasks = []
            status = "418 I'm a teapot.  Seriously."

        threading.current_thread().response_object = FakeResponse()
        through_to_you = crosstown_traffic(same_thread=True)

        through_to_you.no_go = True  # If no_go is True...
        through_to_you(append_me_to_pass)  # and we call it...
        self.assertFalse(through_to_you.response.crosstown_tasks
                         )  # We won't have added any tasks.

        through_to_you.no_go = False  # However if no_go is False...
        through_to_you(append_me_to_pass)  # and we call it...
        self.assertEqual(
            through_to_you.response.crosstown_tasks[0].crosstown_task,
            append_me_to_pass)  # We will have added the function.
Esempio n. 20
0
 def sub_setUp(self):
     self.recorded_tasks = []
     crosstown_traffic.decorator = crosstownTaskListDecoratorFactory(self.recorded_tasks)
     crosstown_traffic()
     self.archived_tasks = []