From 163710cb1abc602b15d2a3b49aa52f3f7c84a65a Mon Sep 17 00:00:00 2001 From: Mehdi Abaakouk Date: Tue, 3 Jun 2014 18:46:06 +0200 Subject: [PATCH] Iterates swift response earlier to get the correct status We must iterate the next middleware app until we get a response before asking for the headers and status. Closes-bug: #1326250 Change-Id: I8947bc88754db93e3ebe01ea864c4b489d1bd870 --- ceilometer/objectstore/swift_middleware.py | 14 +++++++++++--- .../tests/objectstore/test_swift_middleware.py | 4 +++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/ceilometer/objectstore/swift_middleware.py b/ceilometer/objectstore/swift_middleware.py index c1541b8f7..9cc70ecfb 100644 --- a/ceilometer/objectstore/swift_middleware.py +++ b/ceilometer/objectstore/swift_middleware.py @@ -93,14 +93,22 @@ class CeilometerMiddleware(object): start_response_args[0] = (status, list(headers), exc_info) def iter_response(iterable): + iterator = iter(iterable) + try: + chunk = iterator.next() + while not chunk: + chunk = iterator.next() + except StopIteration: + chunk = '' + if start_response_args[0]: start_response(*start_response_args[0]) bytes_sent = 0 try: - for chunk in iterable: - if chunk: - bytes_sent += len(chunk) + while chunk: + bytes_sent += len(chunk) yield chunk + chunk = iterator.next() finally: try: self.publish_sample(env, diff --git a/ceilometer/tests/objectstore/test_swift_middleware.py b/ceilometer/tests/objectstore/test_swift_middleware.py index 64b6d931e..8ffe44e37 100644 --- a/ceilometer/tests/objectstore/test_swift_middleware.py +++ b/ceilometer/tests/objectstore/test_swift_middleware.py @@ -39,13 +39,15 @@ class FakeApp(object): self.body = body def __call__(self, env, start_response): + yield start_response('200 OK', [ ('Content-Type', 'text/plain'), ('Content-Length', str(sum(map(len, self.body)))) ]) while env['wsgi.input'].read(5): pass - return self.body + for line in self.body: + yield line class TestSwiftMiddleware(tests_base.BaseTestCase):