class NoGoStatusCodes(TestCase):

    def __init__(self, *args, **kwargs):
        self.tp = ThreadPool(maxthreads=20)
        self.tp.start()

        self.resource = HendrixWSGIResource(reactor,
                                            self.tp,
                                            self.wsgi_thing)

        self.nameSpace = TestNameSpace()
        self.nameSpace.async_thing_complete = Queue()
        return super(NoGoStatusCodes, self).__init__(*args, **kwargs)

    def setUp(self, *args, **kwargs):
        self.addCleanup(self.tp.stop)
        super(NoGoStatusCodes, self).setUp(*args, **kwargs)

    def wsgi_thing(self, environ, start_response):
            start_response('404 NOT FOUND', [('Content-type','text/plain')])

            @crosstown_traffic.follow_response(
                no_go_status_codes=self.no_go_status_codes,
                same_thread=True
            )
            def long_thing_on_same_thread():
                self.nameSpace.async_task_was_run = True
                logger.info("No bad status codes; went ahead with async thing.")

            return "Nothing."

    def test_bad_status_codes_cause_no_go_in_wsgi_response(self):
        self.no_go_status_codes = [404, '6xx']

        request = DummyRequest('r1')
        request.isSecure = lambda: False
        request.content = "llamas"

        finished = request.notifyFinish()

        self.resource.render(request)

        # This must wait until the WSGI response is closed.
        finished.addCallback(
            lambda _: self.assertFalse(
                self.nameSpace.async_task_was_run
            )
        )

    def test_bad_status_codes_cause_no_go_flag(self):
        through_to_you = crosstown_traffic.follow_response(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)

    def test_no_bad_status_codes_are_cool(self):
        through_to_you = crosstown_traffic.follow_response(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)
    def __init__(self, *args, **kwargs):
        self.tp = ThreadPool(maxthreads=20)
        self.tp.start()

        self.resource = HendrixWSGIResource(reactor,
                                            self.tp,
                                            self.wsgi_thing)

        self.nameSpace = TestNameSpace()
        self.nameSpace.async_thing_complete = Queue()
        return super(NoGoStatusCodes, self).__init__(*args, **kwargs)