def test_request_multiplexing(self):
        req_id = next_req_id()
        req_id_2 = next_req_id()
        req_id_3 = next_req_id()
        role = FCGI_RESPONDER
        flags = 0
        records = (
            (FCGI_BEGIN_REQUEST, pack_begin_request(role, flags), req_id),
            (FCGI_PARAMS, pack_env(), req_id),
            (FCGI_BEGIN_REQUEST, pack_begin_request(role, flags), req_id_2),
            (FCGI_BEGIN_REQUEST, pack_begin_request(role, flags), req_id_3),
            (FCGI_PARAMS, pack_env(), req_id_3),
            (FCGI_PARAMS, pack_env(), req_id_2),
            (FCGI_PARAMS, '', req_id_2),
            (FCGI_PARAMS, '', req_id),
            (FCGI_ABORT_REQUEST, '', req_id_3),
        )

        handler = run_handler(records, role=role)

        assert not handler.requests
        assert handler.request_handler.call_count == 2
        assert handler.conn.close.called

        for r_id in req_id, req_id_2, req_id_3:
            rec = find_rec(handler, FCGI_END_REQUEST, r_id)
            assert rec and unpack_end_request(rec.content)

            for stream in FCGI_STDOUT, FCGI_STDERR:
                assert '' == read_stream(handler, stream, r_id)
    def test_abort_request(self):
        req_id = next_req_id()
        role = FCGI_RESPONDER
        flags = 0
        records = (
            (FCGI_BEGIN_REQUEST, pack_begin_request(role, flags), req_id),
            (FCGI_PARAMS, pack_env(), req_id),
            (FCGI_PARAMS, '', req_id),
            # request_handler gets spawned after PARAMS is "closed"
            # lets give it a chance to run
            0.1,
            # then abort it
            (FCGI_ABORT_REQUEST, '', req_id),
        )

        # use request_handler that waits on STDIN so we can abort
        # it while it's running
        handler = run_handler(records, role=role,
                              request_handler=copy_stdin_to_stdout)

        rec = find_rec(handler, FCGI_END_REQUEST, req_id)
        assert rec and unpack_end_request(rec.content)

        for stream in FCGI_STDOUT, FCGI_STDERR:
            assert '' == read_stream(handler, stream, req_id)
Esempio n. 3
0
    def _handle_requests(self, request_ids, records, **server_params):
        responses = dict(
            (request_id, Response(request_id)) for request_id in request_ids)

        with start_wsgi_server(**server_params) as server:
            with make_connection(server.address) as conn:
                map(conn.write_record, records)
                conn.done_writing()
                for record in conn:
                    self.assertIn(record.request_id, responses)
                    response = responses[record.request_id]
                    self.assertIs(response.request_status, None, str(record))
                    if record.type == FCGI_STDOUT:
                        response.stdout.feed(record.content)
                    elif record.type == FCGI_STDERR:
                        response.stderr.feed(record.content)
                    elif record.type == FCGI_END_REQUEST:
                        response.app_status, response.request_status = (
                            unpack_end_request(record.content))
                    else:
                        self.fail('Unexpected record type %s' % record.type)

        return responses.values()
    def _handle_requests(self, request_ids, records, **server_params):
        responses = dict(
            (request_id, Response(request_id)) for request_id in request_ids)

        with start_wsgi_server(**server_params) as server:
            with make_connection(server.address) as conn:
                map(conn.write_record, records)
                conn.done_writing()
                for record in conn:
                    self.assertIn(record.request_id, responses)
                    response = responses[record.request_id]
                    self.assertIs(response.request_status, None, str(record))
                    if record.type == FCGI_STDOUT:
                        response.stdout.feed(record.content)
                    elif record.type == FCGI_STDERR:
                        response.stderr.feed(record.content)
                    elif record.type == FCGI_END_REQUEST:
                        response.app_status, response.request_status = (
                            unpack_end_request(record.content))
                    else:
                        self.fail('Unexpected record type %s' % record.type)

        return responses.values()