Tests: Ensure cleanup of batching_telemetry_messenger

Since an "assert" halts a test, the BatchingTelemetryMessenger may not
be deconstructed. This results in pytest hanging. By adding a finally
clause, we ensure the cleanup of the BatchingTelemetryMessenger.
This commit is contained in:
Mike Salvatore 2021-07-20 12:23:13 -04:00
parent 42dcb697e9
commit 4affb11128
1 changed files with 52 additions and 38 deletions

View File

@ -69,8 +69,11 @@ def test_send_immediately(monkeypatch, telemetry_messenger_spy):
batching_telemetry_messenger.send_telemetry(telem) batching_telemetry_messenger.send_telemetry(telem)
release_GIL() release_GIL()
assert len(telemetry_messenger_spy.telemetries) == 1 try:
assert telemetry_messenger_spy.telemetries[0] == telem assert len(telemetry_messenger_spy.telemetries) == 1
assert telemetry_messenger_spy.telemetries[0] == telem
finally:
del batching_telemetry_messenger
def test_send_telem_batch(monkeypatch, telemetry_messenger_spy): def test_send_telem_batch(monkeypatch, telemetry_messenger_spy):
@ -78,20 +81,23 @@ def test_send_telem_batch(monkeypatch, telemetry_messenger_spy):
monkeypatch, telemetry_messenger_spy monkeypatch, telemetry_messenger_spy
) )
expected_data = {"entries": [1, 2]} try:
telem1 = BatchableTelemStub(1) expected_data = {"entries": [1, 2]}
telem2 = BatchableTelemStub(2) telem1 = BatchableTelemStub(1)
telem2 = BatchableTelemStub(2)
batching_telemetry_messenger.send_telemetry(telem1) batching_telemetry_messenger.send_telemetry(telem1)
batching_telemetry_messenger.send_telemetry(telem2) batching_telemetry_messenger.send_telemetry(telem2)
release_GIL() release_GIL()
assert len(telemetry_messenger_spy.telemetries) == 0 assert len(telemetry_messenger_spy.telemetries) == 0
advance_clock_to_next_period(monkeypatch) advance_clock_to_next_period(monkeypatch)
release_GIL() release_GIL()
assert len(telemetry_messenger_spy.telemetries) == 1 assert len(telemetry_messenger_spy.telemetries) == 1
assert telemetry_messenger_spy.telemetries[0].get_data() == expected_data assert telemetry_messenger_spy.telemetries[0].get_data() == expected_data
finally:
del batching_telemetry_messenger
def test_send_different_telem_types(monkeypatch, telemetry_messenger_spy): def test_send_different_telem_types(monkeypatch, telemetry_messenger_spy):
@ -99,20 +105,23 @@ def test_send_different_telem_types(monkeypatch, telemetry_messenger_spy):
monkeypatch, telemetry_messenger_spy monkeypatch, telemetry_messenger_spy
) )
telem1 = BatchableTelemStub(1, "cat1") try:
telem2 = BatchableTelemStub(2, "cat2") telem1 = BatchableTelemStub(1, "cat1")
telem2 = BatchableTelemStub(2, "cat2")
batching_telemetry_messenger.send_telemetry(telem1) batching_telemetry_messenger.send_telemetry(telem1)
batching_telemetry_messenger.send_telemetry(telem2) batching_telemetry_messenger.send_telemetry(telem2)
release_GIL() release_GIL()
assert len(telemetry_messenger_spy.telemetries) == 0 assert len(telemetry_messenger_spy.telemetries) == 0
advance_clock_to_next_period(monkeypatch) advance_clock_to_next_period(monkeypatch)
release_GIL() release_GIL()
assert len(telemetry_messenger_spy.telemetries) == 2 assert len(telemetry_messenger_spy.telemetries) == 2
assert telemetry_messenger_spy.telemetries[0] == telem1 assert telemetry_messenger_spy.telemetries[0] == telem1
assert telemetry_messenger_spy.telemetries[1] == telem2 assert telemetry_messenger_spy.telemetries[1] == telem2
finally:
del batching_telemetry_messenger
def test_send_two_batches(monkeypatch, telemetry_messenger_spy): def test_send_two_batches(monkeypatch, telemetry_messenger_spy):
@ -120,22 +129,25 @@ def test_send_two_batches(monkeypatch, telemetry_messenger_spy):
monkeypatch, telemetry_messenger_spy monkeypatch, telemetry_messenger_spy
) )
telem1 = BatchableTelemStub(1, "cat1") try:
telem2 = BatchableTelemStub(2, "cat1") telem1 = BatchableTelemStub(1, "cat1")
telem2 = BatchableTelemStub(2, "cat1")
batching_telemetry_messenger.send_telemetry(telem1) batching_telemetry_messenger.send_telemetry(telem1)
advance_clock_to_next_period(monkeypatch) advance_clock_to_next_period(monkeypatch)
release_GIL() release_GIL()
batching_telemetry_messenger.send_telemetry(telem2) batching_telemetry_messenger.send_telemetry(telem2)
release_GIL() release_GIL()
assert len(telemetry_messenger_spy.telemetries) == 1 assert len(telemetry_messenger_spy.telemetries) == 1
advance_clock_to_next_period(monkeypatch) advance_clock_to_next_period(monkeypatch)
release_GIL() release_GIL()
assert len(telemetry_messenger_spy.telemetries) == 2 assert len(telemetry_messenger_spy.telemetries) == 2
assert telemetry_messenger_spy.telemetries[1] == telem2 assert telemetry_messenger_spy.telemetries[1] == telem2
finally:
del batching_telemetry_messenger
def test_send_remaining_telem_after_stop(monkeypatch, telemetry_messenger_spy): def test_send_remaining_telem_after_stop(monkeypatch, telemetry_messenger_spy):
@ -149,8 +161,10 @@ def test_send_remaining_telem_after_stop(monkeypatch, telemetry_messenger_spy):
batching_telemetry_messenger.send_telemetry(telem) batching_telemetry_messenger.send_telemetry(telem)
release_GIL() release_GIL()
assert len(telemetry_messenger_spy.telemetries) == 0 try:
del batching_telemetry_messenger assert len(telemetry_messenger_spy.telemetries) == 0
finally:
del batching_telemetry_messenger
assert len(telemetry_messenger_spy.telemetries) == 1 assert len(telemetry_messenger_spy.telemetries) == 1
assert telemetry_messenger_spy.telemetries[0].get_data() == expected_data assert telemetry_messenger_spy.telemetries[0].get_data() == expected_data