def test_event_file_writer(self):
        run_path = tempfile.mkdtemp()
        ew = EventFileWriter(run_path)
        events = [
            LoggedEventSpec(name="test",
                            kind="metric",
                            event=V1Event.make(step=1, metric=1.12)),
            LoggedEventSpec(name="test",
                            kind="metric",
                            event=V1Event.make(step=12, metric=1.12)),
        ]
        for e in events:
            ew.add_event(e)
        ew.flush()
        assert len(os.listdir(run_path + "/events")) == 1
        assert len(os.listdir(run_path + "/events/metric")) == 1

        results = V1Events.read(name="test",
                                kind="metric",
                                data=run_path + "/events/metric/test.plx")
        assert results.name == "test"
        assert results.kind == "metric"
        assert len(results.df.values) == 2
        assert results.get_event_at(0).to_dict() == events[0].event.to_dict()
        assert results.get_event_at(1).to_dict() == events[1].event.to_dict()

        # Adding more events
        new_events = [
            LoggedEventSpec(name="test",
                            kind="metric",
                            event=V1Event.make(step=13, metric=1.12)),
            LoggedEventSpec(name="test",
                            kind="metric",
                            event=V1Event.make(step=14, metric=1.12)),
            LoggedEventSpec(name="test2",
                            kind="metric",
                            event=V1Event.make(step=14, metric=1.12)),
            LoggedEventSpec(name="test",
                            kind="html",
                            event=V1Event.make(step=12, html="some div")),
        ]
        for e in new_events:
            ew.add_event(e)
        ew.flush()

        assert len(os.listdir(run_path + "/events")) == 2  # metric and html
        assert len(os.listdir(run_path + "/events/metric")) == 2

        results = V1Events.read(name="test",
                                kind="metric",
                                data=run_path + "/events/metric/test.plx")
        assert results.name == "test"
        assert results.kind == "metric"
        assert len(results.df.values) == 4
        assert results.get_event_at(0).to_dict() == events[0].event.to_dict()
        assert results.get_event_at(1).to_dict() == events[1].event.to_dict()
        assert results.get_event_at(
            2).to_dict() == new_events[0].event.to_dict()
        assert results.get_event_at(
            3).to_dict() == new_events[1].event.to_dict()
    def test_init_events(self):
        events = LoggedEventListSpec(name="test", kind="metric", events=[])

        run_path = tempfile.mkdtemp()
        ew = EventWriter(run_path=run_path, backend=EventWriter.EVENTS_BACKEND)

        event_file = ew._get_event_path(name=events.name, kind=events.kind)

        assert os.path.exists(event_file) is False
        ew._init_events(events)
        assert os.path.exists(event_file) is True
        expected_events = V1Events.read(kind="metric", name="test", data=event_file)
        assert expected_events.name == events.name
        assert expected_events.kind == events.kind

        # Init same file
        ew._init_events(events)
        assert os.path.exists(event_file) is True

        # New file
        events = LoggedEventListSpec(name="new", kind="text", events=[])

        new_event_file = ew._get_event_path(name=events.name, kind=events.kind)

        assert os.path.exists(new_event_file) is False
        ew._init_events(events)
        assert os.path.exists(new_event_file) is True
        expected_events = V1Events.read(kind="text", name="new", data=new_event_file)
        assert expected_events.name == events.name
        assert expected_events.kind == events.kind

        # Previous file should still be there
        assert os.path.exists(event_file) is True
    def test_event_file_writer_raise_after_close(self):
        run_path = tempfile.mkdtemp()
        ew = EventFileWriter(run_path)
        events = [
            LoggedEventSpec(name="test",
                            kind="metric",
                            event=V1Event.make(step=1, metric=1.12)),
            LoggedEventSpec(name="test",
                            kind="metric",
                            event=V1Event.make(step=12, metric=1.12)),
        ]
        for e in events:
            ew.add_event(e)
        ew.close()
        assert len(os.listdir(run_path + "/events")) == 1
        assert len(os.listdir(run_path + "/events/metric")) == 1

        results = V1Events.read(name="test",
                                kind="metric",
                                data=run_path + "/events/metric/test.plx")
        assert results.name == "test"
        assert results.kind == "metric"
        assert len(results.df.values) == 2
        assert results.get_event_at(0).to_dict() == events[0].event.to_dict()
        assert results.get_event_at(1).to_dict() == events[1].event.to_dict()

        # Adding event raises
        with self.assertRaises(OSError):
            ew.add_event(
                LoggedEventSpec(name="test",
                                kind="metric",
                                event=V1Event.make(step=13, metric=1.12)))
 def test_async_writer_write_once(self):
     run_path = tempfile.mkdtemp()
     ew = EventAsyncManager(event_writer=EventWriter(
         run_path, backend=EventWriter.EVENTS_BACKEND))
     event = LoggedEventSpec(name="test",
                             kind="metric",
                             event=V1Event.make(step=13, metric=1.12))
     ew.write(event)
     ew.close()
     assert len(os.listdir(run_path + "/events")) == 1
     assert len(os.listdir(run_path + "/events/metric")) == 1
     results = V1Events.read(name="test",
                             kind="metric",
                             data=run_path + "/events/metric/test.plx")
     assert results.name == "test"
     assert results.kind == "metric"
     assert len(results.df.values) == 1
     assert results.get_event_at(0).to_dict() == event.event.to_dict()
    def test_write_batch_events_file_writer(self):
        run_path = tempfile.mkdtemp()
        ew = EventFileWriter(run_path)
        events = [
            LoggedEventSpec(name="test",
                            kind="metric",
                            event=V1Event.make(step=1, metric=1.12)),
            LoggedEventSpec(name="test",
                            kind="metric",
                            event=V1Event.make(step=12, metric=1.12)),
        ]
        ew.add_events(events)
        ew.flush()
        assert len(os.listdir(run_path + "/events")) == 1
        assert len(os.listdir(run_path + "/events/metric")) == 1

        results = V1Events.read(name="test",
                                kind="metric",
                                data=run_path + "/events/metric/test.plx")
        assert results.name == "test"
        assert results.kind == "metric"
        assert len(results.df.values) == 2
        assert results.get_event_at(0).to_dict() == events[0].event.to_dict()
        assert results.get_event_at(1).to_dict() == events[1].event.to_dict()
    def test_expect_closing_flushes_data_to_files(self):
        run_path = tempfile.mkdtemp()
        ew = EventWriter(run_path=run_path, backend=EventWriter.EVENTS_BACKEND)
        events = [
            LoggedEventSpec(name="test",
                            kind="metric",
                            event=V1Event.make(step=1, metric=1.12)),
            LoggedEventSpec(name="test",
                            kind="metric",
                            event=V1Event.make(step=12, metric=1.12)),
        ]
        ew.write(events)
        ew.close()

        assert len(os.listdir(run_path + "/events")) == 1
        assert len(os.listdir(run_path + "/events/metric")) == 1

        results = V1Events.read(
            name="test",
            kind="metric",
            data=ew._get_event_path(kind="metric", name="test"),
        )
        assert results.name == "test"
        assert results.kind == "metric"
        assert len(results.df.values) == 2
        assert results.get_event_at(0).to_dict() == events[0].event.to_dict()
        assert results.get_event_at(1).to_dict() == events[1].event.to_dict()

        # all flushed
        assert ew._files["metric.test"].events == []

        # Adding more events
        new_events = [
            LoggedEventSpec(name="test",
                            kind="metric",
                            event=V1Event.make(step=13, metric=1.12)),
            LoggedEventSpec(name="test",
                            kind="metric",
                            event=V1Event.make(step=14, metric=1.12)),
            LoggedEventSpec(name="test2",
                            kind="metric",
                            event=V1Event.make(step=14, metric=1.12)),
            LoggedEventSpec(name="test",
                            kind="html",
                            event=V1Event.make(step=12, html="some div")),
        ]
        ew.write(new_events)
        ew.close()

        assert len(os.listdir(run_path + "/events")) == 2  # metric and html
        assert len(os.listdir(run_path + "/events/metric")) == 2

        results = V1Events.read(
            name="test",
            kind="metric",
            data=ew._get_event_path(kind="metric", name="test"),
        )
        assert results.name == "test"
        assert results.kind == "metric"
        assert len(results.df.values) == 4
        assert results.get_event_at(0).to_dict() == events[0].event.to_dict()
        assert results.get_event_at(1).to_dict() == events[1].event.to_dict()
        assert results.get_event_at(
            2).to_dict() == new_events[0].event.to_dict()
        assert results.get_event_at(
            3).to_dict() == new_events[1].event.to_dict()