Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jmthomas committed Feb 19, 2025
1 parent 33b6d5a commit 5789964
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
46 changes: 30 additions & 16 deletions openc3/python/test/utilities/test_thread_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class TestThreadManager(unittest.TestCase):
def setUp(self):
ThreadManager.MONITOR_SLEEP_SECONDS = 0.01

def tearDown(self):
ThreadManager.MONITOR_SLEEP_SECONDS = 0.25
# Critical to set this to None to clean up the singleton instance
ThreadManager.instance_obj = None

def test_monitors_threads(self):
self.continue2 = True
self.continue3 = True
Expand All @@ -33,43 +38,50 @@ def test_monitors_threads(self):
shutdown_object = Mock()
shutdown_object.shutdown = Mock(side_effect=lambda: setattr(self, "continue3", False))

def thread1_body(name, duration):
time.sleep(duration)
thread1 = threading.Thread(target=thread1_body, args=('thread 1', 0.05))
def thread1_body():
time.sleep(0.1)
thread1 = threading.Thread(target=thread1_body)
thread1.start()
def thread2_body(name, duration):
def thread2_body():
while self.continue2:
time.sleep(duration)
thread2 = threading.Thread(target=thread2_body, args=('thread 1', 0.01))
time.sleep(0.01)
thread2 = threading.Thread(target=thread2_body)
thread2.start()
def thread3_body(name, duration):
def thread3_body():
while self.continue3:
time.sleep(duration)
thread3 = threading.Thread(target=thread3_body, args=('thread 1', 0.01))
time.sleep(0.01)
thread3 = threading.Thread(target=thread3_body)
thread3.start()
# Register all the threads with the ThreadManager
# We add stop_object and shutdown_object to the second and third threads
# so they can be stopped when the ThreadManager monitor detects the first thread has stopped
ThreadManager.instance().register(thread1)
ThreadManager.instance().register(thread2, stop_object=stop_object)
ThreadManager.instance().register(thread3, shutdown_object=shutdown_object)

# Create a new thread to monitor and shutdown the threads
def monitor_and_shutdown():
ThreadManager.instance().monitor()
ThreadManager.instance().shutdown()
manager_thread = threading.Thread(target=monitor_and_shutdown)
manager_thread.start()
time.sleep(0.001)
# Wait for the first thread to finish as the second and third spin
thread1.join()
time.sleep(0.001)
# The monitor should detect the first thread has finished
# shutdown will stop the second and third threads
manager_thread.join()
time.sleep(0.1)
# Allow time for the threads to sleep and stop
time.sleep(0.02)
self.assertFalse(thread1.is_alive())
self.assertFalse(thread2.is_alive())
self.assertFalse(thread3.is_alive())

def test_joins_threads(self):
def task(name, duration):
def task(duration):
time.sleep(duration)
thread1 = threading.Thread(target=task, args=('thread 1', 0.01))
thread2 = threading.Thread(target=task, args=('thread 2', 0.1))
thread3 = threading.Thread(target=task, args=('thread 3', 0.05))
thread1 = threading.Thread(target=task, args=[0.01])
thread2 = threading.Thread(target=task, args=[0.1])
thread3 = threading.Thread(target=task, args=[0.05])
ThreadManager.instance().register(thread1)
ThreadManager.instance().register(thread2)
ThreadManager.instance().register(thread3)
Expand All @@ -82,7 +94,9 @@ def task(name, duration):
start_time = perf_counter()
ThreadManager.instance().join()
stop_time = perf_counter()
# The join should wait for the longest running thread (0.1 seconds)
self.assertAlmostEqual(stop_time - start_time, 0.1, delta=0.01)
# All threads should be stopped
self.assertFalse(thread1.is_alive())
self.assertFalse(thread2.is_alive())
self.assertFalse(thread3.is_alive())
4 changes: 2 additions & 2 deletions openc3/spec/microservices/microservice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# All changes Copyright 2022, OpenC3, Inc.
# All Rights Reserved
#
# This file may also be used under the terms of a commercial license
# This file may also be used under the terms of a commercial license
# if purchased from OpenC3, Inc.

require 'spec_helper'
Expand Down Expand Up @@ -50,7 +50,7 @@ module OpenC3
expect { Microservice.run }.to raise_error(/Name DEFAULT_TYPE_NAME doesn't match convention/)
ENV['OPENC3_MICROSERVICE_NAME'] = "DEFAULT__TYPE__NAME"
Microservice.run
sleep 0.1
sleep 0.3 # Allow the ThreadManager to shut down the microservice
end
end
end
Expand Down

0 comments on commit 5789964

Please sign in to comment.