Skip to content

Commit a3a15bb

Browse files
authored
Merge pull request #13 from MrMineO5/master
Add expired instrument checking thread
2 parents f8872a2 + d561c17 commit a3a15bb

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

sourceplusplus/control/LiveInstrumentRemote.py

+20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sys
22
import threading
3+
import time
34

45
from nopdb import nopdb
56
from vertx import EventBus
@@ -9,6 +10,7 @@
910
from sourceplusplus.models.instrument.LiveBreakpoint import LiveBreakpoint
1011
from sourceplusplus.models.instrument.LiveLog import LiveLog
1112
from sourceplusplus.models.instrument.LiveMeter import LiveMeter
13+
from sourceplusplus.models.instrument.common import LiveInstrument
1214
from sourceplusplus.models.instrument.common.LiveInstrumentType import LiveInstrumentType
1315
from sourceplusplus.models.instrument.common.LiveSourceLocation import LiveSourceLocation
1416

@@ -17,12 +19,15 @@ class LiveInstrumentRemote(object):
1719
instruments = {}
1820
eb = None
1921
dbg = None
22+
cleanupThread = None
2023

2124
def __init__(self, eb: EventBus):
2225
LiveInstrumentRemote.eb = eb
2326
LiveInstrumentRemote.dbg = nopdb.get_nopdb()
2427
LiveInstrumentRemote.dbg.start()
2528
threading.settrace(sys.gettrace())
29+
LiveInstrumentRemote.cleanupThread = threading.Thread(target=self.cleanup, daemon=True)
30+
LiveInstrumentRemote.cleanupThread.start()
2631

2732
def add_live_instrument(self, command: LiveInstrumentCommand):
2833
for inst_dict in command.instruments:
@@ -79,3 +84,18 @@ def handle_instrument_command(self, command: LiveInstrumentCommand):
7984
self.add_live_instrument(command)
8085
elif command.command_type == CommandType.REMOVE_LIVE_INSTRUMENT:
8186
self.remove_live_instrument(command)
87+
88+
def cleanup(self):
89+
while True:
90+
time.sleep(1)
91+
delete = []
92+
for key, val in LiveInstrumentRemote.instruments.items():
93+
instrument: LiveInstrument = val[1]
94+
if instrument.expires_at is not None and instrument.expires_at <= round(time.time() * 1000):
95+
delete.append(key)
96+
for key in delete:
97+
instrument: LiveInstrument = LiveInstrumentRemote.instruments.pop(key)[1]
98+
LiveInstrumentRemote.eb.send(address="spp.processor.status.live-instrument-removed", body={
99+
"instrument": instrument.to_json(),
100+
"occurredAt": round(time.time() * 1000)
101+
})

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)