Skip to content

Commit d561c17

Browse files
committed
test: instrument expiration
1 parent bdb5ed9 commit d561c17

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

sourceplusplus/control/LiveInstrumentRemote.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from sourceplusplus.models.instrument.LiveBreakpoint import LiveBreakpoint
1111
from sourceplusplus.models.instrument.LiveLog import LiveLog
1212
from sourceplusplus.models.instrument.LiveMeter import LiveMeter
13+
from sourceplusplus.models.instrument.common import LiveInstrument
1314
from sourceplusplus.models.instrument.common.LiveInstrumentType import LiveInstrumentType
1415
from sourceplusplus.models.instrument.common.LiveSourceLocation import LiveSourceLocation
1516

@@ -25,7 +26,7 @@ def __init__(self, eb: EventBus):
2526
LiveInstrumentRemote.dbg = nopdb.get_nopdb()
2627
LiveInstrumentRemote.dbg.start()
2728
threading.settrace(sys.gettrace())
28-
LiveInstrumentRemote.cleanupThread = threading.Thread(target=self.cleanup)
29+
LiveInstrumentRemote.cleanupThread = threading.Thread(target=self.cleanup, daemon=True)
2930
LiveInstrumentRemote.cleanupThread.start()
3031

3132
def add_live_instrument(self, command: LiveInstrumentCommand):
@@ -89,10 +90,11 @@ def cleanup(self):
8990
time.sleep(1)
9091
delete = []
9192
for key, val in LiveInstrumentRemote.instruments.items():
92-
if "expires_at" in val[1] and val[1]["expires_at"] < round(time.time() * 1000):
93+
instrument: LiveInstrument = val[1]
94+
if instrument.expires_at is not None and instrument.expires_at <= round(time.time() * 1000):
9395
delete.append(key)
9496
for key in delete:
95-
instrument = LiveInstrumentRemote.instruments.pop(key)
97+
instrument: LiveInstrument = LiveInstrumentRemote.instruments.pop(key)[1]
9698
LiveInstrumentRemote.eb.send(address="spp.processor.status.live-instrument-removed", body={
9799
"instrument": instrument.to_json(),
98100
"occurredAt": round(time.time() * 1000)

tests/instrument_expiration/__init__.py

Whitespace-only changes.

tests/instrument_expiration/tests.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import json
2+
import time
3+
import unittest
4+
from unittest.mock import MagicMock
5+
6+
from sourceplusplus.control.LiveInstrumentRemote import LiveInstrumentRemote
7+
from sourceplusplus.models.command.LiveInstrumentCommand import LiveInstrumentCommand
8+
9+
10+
class TestSum(unittest.TestCase):
11+
12+
def test_breakpoint_expires(self):
13+
eb_mock = MagicMock()
14+
instrument_remote = LiveInstrumentRemote(eb_mock)
15+
16+
# Add breakpoint
17+
raw_command = {
18+
"commandType": "ADD_LIVE_INSTRUMENT",
19+
"instruments": [
20+
{
21+
"location": {
22+
"source": "E2ETest.py",
23+
"line": 18, "service": None,
24+
"serviceInstance": None,
25+
"commitId": None,
26+
"fileChecksum": None
27+
},
28+
"condition": None,
29+
"expiresAt": round(time.time() * 1000) + 2000,
30+
"hitLimit": 1,
31+
"id": "3145bbee-8d81-4184-8c3d-f97f208a6e15",
32+
"applyImmediately": False,
33+
"applied": False,
34+
"pending": True,
35+
"throttle": {
36+
"limit": 1,
37+
"step": "SECOND"
38+
},
39+
"meta": {},
40+
"type": "BREAKPOINT"
41+
}
42+
],
43+
"locations": []
44+
}
45+
command = LiveInstrumentCommand.from_json(json.dumps(raw_command))
46+
instrument_remote.add_live_instrument(command)
47+
48+
# Ensure breakpoint was added
49+
self.assertEqual(len(instrument_remote.instruments), 1)
50+
51+
# Wait for breakpoint to expire
52+
time.sleep(5)
53+
54+
# Ensure breakpoint was removed
55+
self.assertEqual(len(instrument_remote.instruments), 0)

0 commit comments

Comments
 (0)