From ff23b5b2cefb994bbbb27704579e7c7984366024 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 9 Jul 2016 22:48:11 +0100 Subject: [PATCH] Clean up thread in python test --- tests/python-test.py | 29 ++++++++++++++++++++++++----- tests/test-c-plugin/test-c-plugin.c | 1 + 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/tests/python-test.py b/tests/python-test.py index 2210c0d..ff12457 100644 --- a/tests/python-test.py +++ b/tests/python-test.py @@ -7,11 +7,14 @@ plugin_win = "Python Test" count = 0 ping_id = 1 - +count_thread = None +thread_stop = None def _inc_counter(): global count - while True: + global thread_stop + + while not thread_stop.is_set(): time.sleep(5) count = count + 1 @@ -346,9 +349,17 @@ def timed_callback(): def prof_init(version, status, account_name, fulljid): - t = threading.Thread(target=_inc_counter) - t.daemon = True - t.start() + global count + global ping_id + global thread_stop + + count = 0 + ping_id = 1 + + thread_stop = threading.Event() + count_thread = threading.Thread(target=_inc_counter) + count_thread.daemon = True + count_thread.start() prof.disco_add_feature("urn:xmpp:profanity:python_test_plugin"); @@ -466,13 +477,21 @@ def prof_on_start(): def prof_on_shutdown(): + global thread_stop + _create_win() prof.win_show(plugin_win, "fired -> prof_on_shutdown") + thread_stop.set() + count_thread.join() def prof_on_unload(): + global thread_stop + _create_win() prof.win_show(plugin_win, "fired -> prof_on_unload") + thread_stop.set() + count_thread.join() def prof_on_connect(account_name, fulljid): diff --git a/tests/test-c-plugin/test-c-plugin.c b/tests/test-c-plugin/test-c-plugin.c index 479e335..d0da88b 100644 --- a/tests/test-c-plugin/test-c-plugin.c +++ b/tests/test-c-plugin/test-c-plugin.c @@ -624,6 +624,7 @@ prof_on_shutdown(void) { create_win(); prof_win_show(plugin_win, "fired -> prof_on_shutdown"); + pthread_cancel(worker_thread); } void