diff weather_server/logfile_test.py @ 24:20c8ec56e447

logfile: Pull logfile thread out of Logger. This enables automatic garbage collection of Logger instances, since a running thread no longer has a reference to a Logger's self. It separates exclusive management of logfile state into the _writer_thread function, which now opens the file and writes it until it is told to stop by receiving the poison pill.
author Paul Fisher <paul@pfish.zone>
date Sun, 10 Nov 2019 23:07:11 -0500
parents beb42c835c52
children 9bc3687e1e5e
line wrap: on
line diff
--- a/weather_server/logfile_test.py	Sun Nov 10 19:42:04 2019 -0500
+++ b/weather_server/logfile_test.py	Sun Nov 10 23:07:11 2019 -0500
@@ -1,7 +1,9 @@
 import contextlib
 import datetime
+import os.path
 import pathlib
 import tempfile
+import threading
 import unittest
 
 import bson
@@ -9,7 +11,6 @@
 
 from . import common
 from . import logfile
-from . import types
 
 
 def ts(n):
@@ -35,6 +36,20 @@
         with contextlib.closing(lg) as logger:
             self.assertEqual(logger.data, ())
 
+    def test_fails_to_open(self):
+        with self.assertRaises(OSError):
+            logfile.Logger(
+                os.path.join(
+                    self.temp_dir.name,
+                    'nonexistent-directory',
+                    'bogus-filename'),
+                sample_field='unimportant')
+
+    def test_del(self):
+        lg = logfile.Logger(
+            str(self.log_path), sample_field='x')
+        del lg
+
     def test_loading(self):
         with self.log_path.open('wb') as outfile:
             outfile.write(common.bson_encode(dict(
@@ -72,14 +87,34 @@
         )) as logger:
             logger.write_rows([
                 # Ignored, since it's older than the newest entry.
-                types.Reading(ts(100), 999, 666, ts(101)).as_dict(),
-                types.Reading(ts(125), 333, 777, ts(200)).as_dict(),
+                dict(
+                    sample_time=ts(100),
+                    temp_c=999,
+                    rh_pct=666,
+                    ingest_time=ts(101),
+                ),
+                dict(
+                    sample_time=ts(125),
+                    temp_c=333,
+                    rh_pct=777,
+                    ingest_time=ts(200),
+                ),
             ])
             self.assertEqual(
                 logger.data,
                 (
-                    types.Reading(ts(123), 420, 69, ts(125)).as_dict(),
-                    types.Reading(ts(125), 333, 777, ts(200)).as_dict(),
+                    dict(
+                        sample_time=ts(123),
+                        temp_c=420,
+                        rh_pct=69,
+                        ingest_time=ts(125),
+                    ),
+                    dict(
+                        sample_time=ts(125),
+                        temp_c=333,
+                        rh_pct=777,
+                        ingest_time=ts(200),
+                    ),
                 )
             )
 
@@ -104,8 +139,18 @@
             sample_field='sample_time',
         )) as logger:
             logger.write_rows([
-                types.Reading(ts(100), 999, 666, ts(101)).as_dict(),
-                types.Reading(ts(125), 333, 777, ts(200)).as_dict(),
+                dict(
+                    sample_time=ts(100),
+                    temp_c=999,
+                    rh_pct=666,
+                    ingest_time=ts(101),
+                ),
+                dict(
+                    sample_time=ts(125),
+                    temp_c=333,
+                    rh_pct=777,
+                    ingest_time=ts(200),
+                ),
             ])
             with self.log_path.open('ab') as outfile:
                 outfile.write(common.bson_encode(dict(
@@ -116,9 +161,24 @@
                 )))
                 outfile.flush()
             self.assertEqual(logger.data, (
-                types.Reading(ts(100), 999, 666, ts(101)).as_dict(),
-                types.Reading(ts(125), 333, 777, ts(200)).as_dict(),
-                types.Reading(ts(1024), 256, 128, ts(4096)).as_dict(),
+                dict(
+                    sample_time=ts(100),
+                    temp_c=999,
+                    rh_pct=666,
+                    ingest_time=ts(101),
+                ),
+                dict(
+                    sample_time=ts(125),
+                    temp_c=333,
+                    rh_pct=777,
+                    ingest_time=ts(200),
+                ),
+                dict(
+                    sample_time=ts(1024),
+                    temp_c=256,
+                    rh_pct=128,
+                    ingest_time=ts(4096),
+                ),
             ))
 
     def read_bsons(self):