diff options
author | Kent Hansen <kent.hansen@nokia.com> | 2012-08-01 12:59:28 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-08-08 14:13:53 +0200 |
commit | 8efcfda41f98af51c5d7ab72b5bc38926e9c3f11 (patch) | |
tree | e57f2ba371efb37a302f6113d7a89fe77869967f /tests/auto/corelib/statemachine/qstatemachine | |
parent | f7272b4587a4a0a44fcf36a3c1e163f8040a2d48 (diff) |
statemachine: Really fix signal transition handling in multi-threaded setup
Commit f9a17d7f0f02f7af849afdf653a763ffdaf78a1b fixed it for the case
where the sender object is in a different thread at transition setup
time. However, it still didn't work if either the sender object or the
state machine was moved to a different thread at some later time,
before the machine was started.
Therefore: Bite the sour grape and traverse all the machine's
transitions when the machine is being started, registering those
signal transitions whose sender objects are in other threads.
This will increase the machine's startup time (proportional to the
number of transitions), but at least it works in all known scenarios,
meaning we don't have to document weird restrictions regarding the
order in which the user's operations have to be done.
Task-number: QTBUG-19789
Change-Id: I5f1dd1321994e49635f52be65cf56d2678ed1253
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@nokia.com>
Diffstat (limited to 'tests/auto/corelib/statemachine/qstatemachine')
-rw-r--r-- | tests/auto/corelib/statemachine/qstatemachine/tst_qstatemachine.cpp | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/tests/auto/corelib/statemachine/qstatemachine/tst_qstatemachine.cpp b/tests/auto/corelib/statemachine/qstatemachine/tst_qstatemachine.cpp index dd036641b2..15f23e05cd 100644 --- a/tests/auto/corelib/statemachine/qstatemachine/tst_qstatemachine.cpp +++ b/tests/auto/corelib/statemachine/qstatemachine/tst_qstatemachine.cpp @@ -209,6 +209,7 @@ private slots: void createSignalTransitionWhenRunning(); void createEventTransitionWhenRunning(); void signalTransitionSenderInDifferentThread(); + void signalTransitionSenderInDifferentThread2(); void signalTransitionRegistrationThreadSafety(); void childModeConstructor(); }; @@ -4895,6 +4896,43 @@ void tst_QStateMachine::signalTransitionSenderInDifferentThread() QTRY_VERIFY(thread.wait()); } +void tst_QStateMachine::signalTransitionSenderInDifferentThread2() +{ + QStateMachine machine; + QState *s1 = new QState(&machine); + machine.setInitialState(s1); + + QState *s2 = new QState(&machine); + SignalEmitter emitter; + // At the time of the transition creation, the machine and the emitter + // are both in the same thread. + s1->addTransition(&emitter, SIGNAL(signalWithNoArg()), s2); + + QFinalState *s3 = new QFinalState(&machine); + s2->addTransition(&emitter, SIGNAL(signalWithDefaultArg()), s3); + + QThread thread; + // Move the machine and its states to a secondary thread, but let the + // SignalEmitter stay in the main thread. + machine.moveToThread(&thread); + + thread.start(); + QTRY_VERIFY(thread.isRunning()); + + QSignalSpy startedSpy(&machine, SIGNAL(started())); + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.start(); + QTRY_COMPARE(startedSpy.count(), 1); + + emitter.emitSignalWithNoArg(); + // The second emission should not get "lost". + emitter.emitSignalWithDefaultArg(); + QTRY_COMPARE(finishedSpy.count(), 1); + + thread.quit(); + QTRY_VERIFY(thread.wait()); +} + class SignalTransitionMutatorThread : public QThread { public: |