agent: Refactor BatchingTelemetryMessenger tests to destroy threads

This commit is contained in:
Mike Salvatore 2021-06-29 07:48:18 -04:00
parent 8cf316b64a
commit d5a26ca6eb
1 changed files with 28 additions and 15 deletions

View File

@ -1,7 +1,5 @@
import time
import pytest
from infection_monkey.telemetry.base_telem import BaseTelem
from infection_monkey.telemetry.batchable_telem_mixin import BatchableTelemMixin
from infection_monkey.telemetry.i_batchable_telem import IBatchableTelem
@ -53,15 +51,21 @@ class BatchableTelemStub(BatchableTelemMixin, BaseTelem, IBatchableTelem):
return {"entries": self._telemetry_entries}
@pytest.fixture
def batching_telemetry_messenger(monkeypatch, telemetry_messenger_spy):
# Note that this function is not a fixture. This is because BatchingTelemetyMessenger
# stops its thread when it is destructed. If this were a fixture, it may live
# past the end of the test, which would allow the in the BatchingTelemetryMessenger
# instance to keep running instead of stopping
def build_batching_telemetry_messenger(monkeypatch, telemetry_messenger_spy):
patch_time(monkeypatch, 0)
return BatchingTelemetryMessenger(telemetry_messenger_spy, period=0.001)
return BatchingTelemetryMessenger(telemetry_messenger_spy, period=PERIOD)
def test_send_immediately(batching_telemetry_messenger, telemetry_messenger_spy):
def test_send_immediately(monkeypatch, telemetry_messenger_spy):
batching_telemetry_messenger = build_batching_telemetry_messenger(
monkeypatch, telemetry_messenger_spy
)
telem = NonBatchableTelemStub()
batching_telemetry_messenger.send_telemetry(telem)
release_GIL()
@ -69,7 +73,11 @@ def test_send_immediately(batching_telemetry_messenger, telemetry_messenger_spy)
assert telemetry_messenger_spy.telemetries[0] == telem
def test_send_telem_batch(monkeypatch, batching_telemetry_messenger, telemetry_messenger_spy):
def test_send_telem_batch(monkeypatch, telemetry_messenger_spy):
batching_telemetry_messenger = build_batching_telemetry_messenger(
monkeypatch, telemetry_messenger_spy
)
expected_data = {"entries": [1, 2]}
telem1 = BatchableTelemStub(1)
telem2 = BatchableTelemStub(2)
@ -86,9 +94,11 @@ def test_send_telem_batch(monkeypatch, batching_telemetry_messenger, telemetry_m
assert telemetry_messenger_spy.telemetries[0].get_data() == expected_data
def test_send_different_telem_types(
monkeypatch, batching_telemetry_messenger, telemetry_messenger_spy
):
def test_send_different_telem_types(monkeypatch, telemetry_messenger_spy):
batching_telemetry_messenger = build_batching_telemetry_messenger(
monkeypatch, telemetry_messenger_spy
)
telem1 = BatchableTelemStub(1, "cat1")
telem2 = BatchableTelemStub(2, "cat2")
@ -105,7 +115,11 @@ def test_send_different_telem_types(
assert telemetry_messenger_spy.telemetries[1] == telem2
def test_send_two_batches(monkeypatch, batching_telemetry_messenger, telemetry_messenger_spy):
def test_send_two_batches(monkeypatch, telemetry_messenger_spy):
batching_telemetry_messenger = build_batching_telemetry_messenger(
monkeypatch, telemetry_messenger_spy
)
telem1 = BatchableTelemStub(1, "cat1")
telem2 = BatchableTelemStub(2, "cat1")
@ -125,9 +139,8 @@ def test_send_two_batches(monkeypatch, batching_telemetry_messenger, telemetry_m
def test_send_remaining_telem_after_stop(monkeypatch, telemetry_messenger_spy):
patch_time(monkeypatch, 0)
batching_telemetry_messenger = BatchingTelemetryMessenger(
telemetry_messenger_spy, period=PERIOD
batching_telemetry_messenger = build_batching_telemetry_messenger(
monkeypatch, telemetry_messenger_spy
)
expected_data = {"entries": [1]}