Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jmthomas committed Feb 19, 2025
1 parent 5789964 commit 52ca5e9
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
3 changes: 2 additions & 1 deletion openc3/python/openc3/microservices/interface_microservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ def run(self):

class InterfaceMicroservice(Microservice):
UNKNOWN_BYTES_TO_PRINT = 16
DISCONNECT_WAIT_TIME = 1

def __init__(self, name):
self.mutex = threading.Lock()
Expand Down Expand Up @@ -585,7 +586,7 @@ def run(self):
match self.interface.state:
case "DISCONNECTED":
# Just wait to see if we should connect later
self.interface_thread_sleeper.sleep(1)
self.interface_thread_sleeper.sleep(InterfaceMicroservice.DISCONNECT_WAIT_TIME)
case "ATTEMPTING":
try:
with self.mutex:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ def test_connect_handles_parameters(self):
# im.shutdown()

def test_handles_a_clean_disconnect(self):
InterfaceMicroservice.DISCONNECT_WAIT_TIME = 0.01
im = InterfaceMicroservice("DEFAULT__INTERFACE__INST_INT")
all_interfaces = InterfaceStatusModel.all(scope="DEFAULT")
self.assertEqual(all_interfaces["INST_INT"]["state"], "ATTEMPTING")
Expand All @@ -299,7 +300,7 @@ def test_handles_a_clean_disconnect(self):
self.assertIn(TestInterfaceMicroservice.CONN_SUCCESS_MSG, stdout.getvalue())

InterfaceTopic.disconnect_interface("INST_INT")
time.sleep(1.01) # Allow disconnect
time.sleep(0.02) # Allow disconnect
all_interfaces = InterfaceStatusModel.all(scope="DEFAULT")
self.assertEqual(all_interfaces["INST_INT"]["state"], "DISCONNECTED")
self.assertIn("Disconnect requested", stdout.getvalue())
Expand Down
2 changes: 2 additions & 0 deletions openc3/python/test/script/test_api_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# This file may also be used under the terms of a commercial license
# if purchased from OpenC3, Inc.

import os
import unittest
from unittest.mock import *
from test.test_helper import *
Expand Down Expand Up @@ -855,6 +856,7 @@ def test_starts_a_script(self):
for stdout in capture_io():
start("tester.py")
self.assertIn("Hello World", stdout.getvalue())
os.remove("tester.py")

def test_load_utility_raises(self):
with self.assertRaisesRegex(
Expand Down
8 changes: 8 additions & 0 deletions openc3/python/test/utilities/test_thread_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from unittest.mock import Mock
from openc3.utilities.thread_manager import ThreadManager


class TestThreadManager(unittest.TestCase):
def setUp(self):
ThreadManager.MONITOR_SLEEP_SECONDS = 0.01
Expand All @@ -40,16 +41,21 @@ def test_monitors_threads(self):

def thread1_body():
time.sleep(0.1)

thread1 = threading.Thread(target=thread1_body)
thread1.start()

def thread2_body():
while self.continue2:
time.sleep(0.01)

thread2 = threading.Thread(target=thread2_body)
thread2.start()

def thread3_body():
while self.continue3:
time.sleep(0.01)

thread3 = threading.Thread(target=thread3_body)
thread3.start()
# Register all the threads with the ThreadManager
Expand All @@ -63,6 +69,7 @@ def thread3_body():
def monitor_and_shutdown():
ThreadManager.instance().monitor()
ThreadManager.instance().shutdown()

manager_thread = threading.Thread(target=monitor_and_shutdown)
manager_thread.start()
# Wait for the first thread to finish as the second and third spin
Expand All @@ -79,6 +86,7 @@ def monitor_and_shutdown():
def test_joins_threads(self):
def task(duration):
time.sleep(duration)

thread1 = threading.Thread(target=task, args=[0.01])
thread2 = threading.Thread(target=task, args=[0.1])
thread3 = threading.Thread(target=task, args=[0.05])
Expand Down
8 changes: 6 additions & 2 deletions openc3/spec/utilities/thread_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
module OpenC3
describe ThreadManager do
before(:all) do
ThreadManager.class_variable_set("@@instance", nil)
@sleep_seconds = ThreadManager::MONITOR_SLEEP_SECONDS
OpenC3.disable_warnings { ThreadManager::MONITOR_SLEEP_SECONDS = 0.01 }
end
after(:all) do
OpenC3.disable_warnings { ThreadManager::MONITOR_SLEEP_SECONDS = @sleep_seconds }
ThreadManager.class_variable_set("@@instance", nil)
end

it "monitors threads until 1 dies then stops and shutdowns" do
Expand Down Expand Up @@ -54,12 +56,14 @@ module OpenC3
ThreadManager.instance.register(thread1)
ThreadManager.instance.register(thread2, stop_object: stop_object)
ThreadManager.instance.register(thread3, shutdown_object: shutdown_object)
Thread.new do
manager_thread = Thread.new do
ThreadManager.instance.monitor()
ThreadManager.instance.shutdown()
end
thread1.join()
sleep 0.03
sleep 0.01
manager_thread.join()
sleep 0.02 # Allow the other threads to finish
expect(thread1.alive?).to be false
expect(thread2.alive?).to be false
expect(thread3.alive?).to be false
Expand Down

0 comments on commit 52ca5e9

Please sign in to comment.