2023-04-20 17:06:16 +08:00
|
|
|
import time
|
2023-05-16 21:49:05 +08:00
|
|
|
import eventloop
|
2023-04-20 17:06:16 +08:00
|
|
|
from eventloop import EventLoop
|
|
|
|
|
|
|
|
finished = False
|
|
|
|
|
2023-04-20 19:26:04 +08:00
|
|
|
eventloop.set_debug(True)
|
|
|
|
|
|
|
|
|
2023-04-20 17:06:16 +08:00
|
|
|
def test_func(arg1, arg2):
|
|
|
|
print("Running test function with arguments:", arg1, arg2)
|
|
|
|
return arg1 + arg2
|
|
|
|
|
2023-04-20 19:26:04 +08:00
|
|
|
|
2023-04-20 17:06:16 +08:00
|
|
|
def test_callback(res):
|
|
|
|
global finished
|
|
|
|
print("Running test callback function", res)
|
|
|
|
assert res == "Hello World"
|
|
|
|
finished = True
|
|
|
|
|
2023-04-20 19:26:04 +08:00
|
|
|
|
2023-04-20 17:06:16 +08:00
|
|
|
# Test case 1: Add and run a one-time task
|
|
|
|
event_loop = EventLoop(period_ms=100)
|
|
|
|
|
2023-04-20 19:26:04 +08:00
|
|
|
event_loop.start_new_task_once(
|
|
|
|
test_func, ("Hello", " World"), callback=test_callback)
|
2023-04-20 17:06:16 +08:00
|
|
|
event_loop.start()
|
|
|
|
|
|
|
|
# Sleep for enough time to allow the one-time task to run
|
|
|
|
while not finished:
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
|
|
|
event_loop.stop()
|