test: timer-thread: Destroy Object from correct thread context

The TimeoutHandler used in the test is destroyed from the main thread,
which is invalid for a thread-bound object bound to a different thread.
Fix it by destroying it with deleteLater().

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Milan Zamazal <mzamazal@redhat.com>
This commit is contained in:
Laurent Pinchart 2024-01-19 03:43:55 +02:00
parent 4f76beae86
commit fdcea5ad79
2 changed files with 21 additions and 9 deletions

View file

@ -54,7 +54,9 @@ protected:
int init() int init()
{ {
thread_.start(); thread_.start();
timeout_.moveToThread(&thread_);
timeout_ = new TimeoutHandler();
timeout_->moveToThread(&thread_);
return TestPass; return TestPass;
} }
@ -67,7 +69,7 @@ protected:
* event dispatcher to make sure we don't succeed simply because * event dispatcher to make sure we don't succeed simply because
* the event dispatcher hasn't noticed the timer restart. * the event dispatcher hasn't noticed the timer restart.
*/ */
timeout_.start(); timeout_->start();
thread_.eventDispatcher()->interrupt(); thread_.eventDispatcher()->interrupt();
this_thread::sleep_for(chrono::milliseconds(200)); this_thread::sleep_for(chrono::milliseconds(200));
@ -79,7 +81,7 @@ protected:
* to return TestPass in the unexpected (usually known as * to return TestPass in the unexpected (usually known as
* "fail") case, and TestFail otherwise. * "fail") case, and TestFail otherwise.
*/ */
if (timeout_.timeout()) { if (timeout_->timeout()) {
cout << "Timer start from wrong thread succeeded unexpectedly" cout << "Timer start from wrong thread succeeded unexpectedly"
<< endl; << endl;
return TestPass; return TestPass;
@ -90,13 +92,17 @@ protected:
void cleanup() void cleanup()
{ {
/* Must stop thread before destroying timeout. */ /*
* Object class instances must be destroyed from the thread
* they live in.
*/
timeout_->deleteLater();
thread_.exit(0); thread_.exit(0);
thread_.wait(); thread_.wait();
} }
private: private:
TimeoutHandler timeout_; TimeoutHandler *timeout_;
Thread thread_; Thread thread_;
}; };

View file

@ -50,7 +50,9 @@ protected:
int init() int init()
{ {
thread_.start(); thread_.start();
timeout_.moveToThread(&thread_);
timeout_ = new TimeoutHandler();
timeout_->moveToThread(&thread_);
return TestPass; return TestPass;
} }
@ -63,7 +65,7 @@ protected:
*/ */
this_thread::sleep_for(chrono::milliseconds(200)); this_thread::sleep_for(chrono::milliseconds(200));
if (!timeout_.timeout()) { if (!timeout_->timeout()) {
cout << "Timer expiration test failed" << endl; cout << "Timer expiration test failed" << endl;
return TestFail; return TestFail;
} }
@ -73,13 +75,17 @@ protected:
void cleanup() void cleanup()
{ {
/* Must stop thread before destroying timeout. */ /*
* Object class instances must be destroyed from the thread
* they live in.
*/
timeout_->deleteLater();
thread_.exit(0); thread_.exit(0);
thread_.wait(); thread_.wait();
} }
private: private:
TimeoutHandler timeout_; TimeoutHandler *timeout_;
Thread thread_; Thread thread_;
}; };