From 2e0c97cea810d8ac3fe91db3ec68e93638bada49 Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Wed, 13 Sep 2017 18:14:41 -0700 Subject: [PATCH] test_stats: start improving coverage --- src/wormhole_transit_relay/test/test_stats.py | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/wormhole_transit_relay/test/test_stats.py diff --git a/src/wormhole_transit_relay/test/test_stats.py b/src/wormhole_transit_relay/test/test_stats.py new file mode 100644 index 0000000..f7a6365 --- /dev/null +++ b/src/wormhole_transit_relay/test/test_stats.py @@ -0,0 +1,42 @@ +from __future__ import print_function, unicode_literals +import os, json +from twisted.trial import unittest +from ..transit_server import Transit + +class UsageLog(unittest.TestCase): + def test_log(self): + d = self.mktemp() + os.mkdir(d) + usage_logfile = os.path.join(d, "usage.log") + def read(): + with open(usage_logfile, "rb") as f: + return [json.loads(line) for line in f.readlines()] + t = Transit(None, usage_logfile, None) + t.recordUsage(started=123, result="happy", total_bytes=100, + total_time=10, waiting_time=2) + self.assertEqual(read(), [dict(started=123, mood="happy", + total_time=10, waiting_time=2, + total_bytes=100)]) + + t.recordUsage(started=150, result="errory", total_bytes=200, + total_time=11, waiting_time=3) + self.assertEqual(read(), [dict(started=123, mood="happy", + total_time=10, waiting_time=2, + total_bytes=100), + dict(started=150, mood="errory", + total_time=11, waiting_time=3, + total_bytes=200), + ]) + + if False: + # the current design opens the logfile exactly once, at process + # start, in the faint hopes of surviving an exhaustion of available + # file descriptors. This should be rethought. + os.unlink(usage_logfile) + + t.recordUsage(started=200, result="lonely", total_bytes=300, + total_time=12, waiting_time=4) + self.assertEqual(read(), [dict(started=200, mood="lonely", + total_time=12, waiting_time=4, + total_bytes=300)]) +