Skip to content

Commit c3bc541

Browse files
committed
More fixture logging improvements
- Add test logging NullHandler - Remove default logging level filtering in testutil - Log render_template info - More fixture logging cleanups - wait_for() should not handle child shutdown
1 parent 0330036 commit c3bc541

File tree

4 files changed

+19
-14
lines changed

4 files changed

+19
-14
lines changed

test/__init__.py

+11
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,14 @@
44
import unittest2 as unittest # pylint: disable=import-error
55
else:
66
import unittest
7+
8+
# Set default logging handler to avoid "No handler found" warnings.
9+
import logging
10+
try: # Python 2.7+
11+
from logging import NullHandler
12+
except ImportError:
13+
class NullHandler(logging.Handler):
14+
def emit(self, record):
15+
pass
16+
17+
logging.getLogger(__name__).addHandler(NullHandler())

test/fixtures.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@ def kafka_run_class_env(self):
7979

8080
@classmethod
8181
def render_template(cls, source_file, target_file, binding):
82+
log.info('Rendering %s from template %s', target_file, source_file)
8283
with open(source_file, "r") as handle:
8384
template = handle.read()
85+
assert len(template) > 0, 'Empty template %s' % source_file
8486
with open(target_file, "w") as handle:
8587
handle.write(template.format(**binding))
8688
handle.flush()
@@ -139,22 +141,22 @@ def open(self):
139141
env = self.kafka_run_class_env()
140142

141143
# Party!
142-
self.out("Starting...")
143144
timeout = 5
144145
max_timeout = 30
145146
backoff = 1
146147
end_at = time.time() + max_timeout
148+
tries = 1
147149
while time.time() < end_at:
148-
log.critical('Starting Zookeeper instance')
150+
self.out('Attempting to start (try #%d)' % tries)
149151
self.child = SpawnedService(args, env)
150152
self.child.start()
151153
timeout = min(timeout, max(end_at - time.time(), 0))
152154
if self.child.wait_for(r"binding to port", timeout=timeout):
153155
break
154-
log.critical('Zookeeper did not start within timeout %s secs', timeout)
155156
self.child.stop()
156157
timeout *= 2
157158
time.sleep(backoff)
159+
tries += 1
158160
else:
159161
raise Exception('Failed to start Zookeeper before max_timeout')
160162
self.out("Done!")
@@ -260,8 +262,6 @@ def open(self):
260262
raise RuntimeError("Failed to create Zookeeper chroot node")
261263
self.out("Done!")
262264

263-
self.out("Starting...")
264-
265265
# Configure Kafka child process
266266
args = self.kafka_run_class_args("kafka.Kafka", properties)
267267
env = self.kafka_run_class_env()
@@ -270,18 +270,19 @@ def open(self):
270270
max_timeout = 30
271271
backoff = 1
272272
end_at = time.time() + max_timeout
273+
tries = 1
273274
while time.time() < end_at:
274-
log.critical('Starting Kafka instance')
275+
self.out('Attempting to start (try #%d)' % tries)
275276
self.child = SpawnedService(args, env)
276277
self.child.start()
277278
timeout = min(timeout, max(end_at - time.time(), 0))
278279
if self.child.wait_for(r"\[Kafka Server %d\], Started" %
279280
self.broker_id, timeout=timeout):
280281
break
281-
log.critical('Kafka did not start within timeout %s secs', timeout)
282282
self.child.stop()
283283
timeout *= 2
284284
time.sleep(backoff)
285+
tries += 1
285286
else:
286287
raise Exception('Failed to start KafkaInstance before max_timeout')
287288
self.out("Done!")

test/service.py

-4
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,6 @@ def wait_for(self, pattern, timeout=30):
9999
while True:
100100
elapsed = time.time() - start
101101
if elapsed >= timeout:
102-
try:
103-
self.child.kill()
104-
except:
105-
log.exception("Received exception when killing child process")
106102
log.error("Waiting for %r timed out after %d seconds", pattern, timeout)
107103
return False
108104

test/testutil.py

-3
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,3 @@ def __enter__(self):
142142
def __exit__(self, *args):
143143
self.end = time.time()
144144
self.interval = self.end - self.start
145-
146-
logging.getLogger('test.fixtures').setLevel(logging.ERROR)
147-
logging.getLogger('test.service').setLevel(logging.ERROR)

0 commit comments

Comments
 (0)