def test_align_adapts_t0_during_active_batch(self):
        self.buf = sensor_stream.Buffer(
            self.bufdir,
            self.on_emit,
        )

        self.buf.batch_size = 200

        t0 = datetime(2017, 6, 8, 13, 27, 0, 4900)
        t1 = datetime(2017, 6, 8, 13, 27, 0, 10003)
        t2 = datetime(2017, 6, 8, 13, 27, 0, 15100)

        self.buf.align(0, t0, timedelta(milliseconds=5))

        self.buf.submit(0, list(range(190)))

        self.buf.align(1, t1, timedelta(milliseconds=5))

        self.buf.align(2, t2, timedelta(milliseconds=5))

        self.buf.submit(190, list(range(190, 200)))

        self.on_emit.assert_called_once_with(
            t0.replace(microsecond=5001),
            0,
            self.period,
            list(range(200)),
            unittest.mock.ANY,
        )
    def test_data_stays_until_closed(self):
        self.buf.batch_size = 200
        self.buf.submit(0, list(range(200)))
        self.on_emit.assert_called_once_with(
            self.t0,
            0,
            self.period,
            list(range(200)),
            unittest.mock.ANY,
        )

        on_emit = unittest.mock.Mock()
        on_emit.return_value = None

        sensor_stream.Buffer(
            self.bufdir,
            on_emit,
        )

        on_emit.assert_called_once_with(
            self.t0,
            0,
            self.period,
            list(range(200)),
            unittest.mock.ANY,
        )

        on_emit = unittest.mock.Mock()
        on_emit.return_value = None

        sensor_stream.Buffer(
            self.bufdir,
            on_emit,
        )

        on_emit.assert_called_once_with(
            self.t0,
            0,
            self.period,
            list(range(200)),
            unittest.mock.ANY,
        )
 def setUp(self):
     self._context = contextlib.ExitStack()
     self.bufdir = pathlib.Path(
         self._context.enter_context(tempfile.TemporaryDirectory()))
     self.on_emit = unittest.mock.Mock()
     self.on_emit.return_value = None
     self.buf = sensor_stream.Buffer(
         self.bufdir,
         self.on_emit,
     )
     self.t0 = datetime.utcnow()
     self.period = timedelta(milliseconds=5)
     self.buf.align(
         0,
         self.t0,
         self.period,
     )
    def test_emit_existing_data_from_persistent_storage_on_construction(self):
        self.buf.submit(0, list(range(190)))

        on_emit = unittest.mock.Mock()
        on_emit.return_value = None

        sensor_stream.Buffer(
            self.bufdir,
            on_emit,
        )

        on_emit.assert_called_once_with(
            self.t0,
            0,
            self.period,
            list(range(190)),
            unittest.mock.ANY,
        )
    def test_no_reemission_of_closed_data(self):
        self.buf.batch_size = 200
        self.buf.submit(0, list(range(200)))
        self.on_emit.assert_called_once_with(
            self.t0,
            0,
            self.period,
            list(range(200)),
            unittest.mock.ANY,
        )
        self.on_emit.mock_calls[0][1][-1].close()

        on_emit = unittest.mock.Mock()
        on_emit.return_value = None

        sensor_stream.Buffer(
            self.bufdir,
            on_emit,
        )

        on_emit.assert_not_called()
    def test_emit_emitted_but_not_closed_data_on_construction(self):
        self.buf.batch_size = 200

        self.buf.submit(0, list(range(400)))

        self.on_emit.mock_calls[0][1][-1].close()

        on_emit = unittest.mock.Mock()
        on_emit.return_value = None

        sensor_stream.Buffer(
            self.bufdir,
            on_emit,
        )

        on_emit.assert_called_once_with(
            self.t0 + self.period * 200,
            200,
            self.period,
            list(range(200, 400)),
            unittest.mock.ANY,
        )