Skip to content

Commit

Permalink
Fix for issue benlau#13 - check canceled before finished
Browse files Browse the repository at this point in the history
  • Loading branch information
vpicaver committed Aug 5, 2019
1 parent 5ca0304 commit ce2d1bc
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
9 changes: 8 additions & 1 deletion asyncfuture.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,18 @@ void watch(QFuture<T> future,

QObject::connect(watcher, &QFutureWatcher<T>::finished,
contextObject, [=]() {
bool watcherCancelled = watcher->isCanceled();

delete watcher;
if (ownerAlive.isNull()) {
return;
}
finished();

if(watcherCancelled) {
canceled();
} else {
finished();
}
});

QObject::connect(watcher, &QFutureWatcher<T>::canceled,
Expand Down
47 changes: 47 additions & 0 deletions tests/asyncfutureunittests/bugtests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,50 @@ void BugTests::test_issue4()

QCOMPARE(actualValue, 78);
}

void BugTests::test_canceled_before_finished() {

class TestClass {
public:
void doWork() {

if(m_doWorkFuture.isRunning() || m_doWorkFuture.isStarted()) {
m_doWorkFuture.cancel();
}

int currentCount = m_count;
auto runFuture = QtConcurrent::run([currentCount](){
return currentCount + 1;
});

m_doWorkFuture = runFuture;

m_subscribeFuture = observe(runFuture).subscribe([this](){
m_finishCount++;
},
[this](){
m_cancelCount++;
}).future();

}

void waitToFinish() {
await(m_subscribeFuture);
}

QFuture<void> m_subscribeFuture;
QFuture<int> m_doWorkFuture;
int m_count = 0;
int m_finishCount = 0;
int m_cancelCount = 0;
};

TestClass myTest;
myTest.doWork();
myTest.doWork();
myTest.doWork();
myTest.waitToFinish();

QCOMPARE(myTest.m_finishCount, 1);
QCOMPARE(myTest.m_cancelCount, 2);
}
2 changes: 2 additions & 0 deletions tests/asyncfutureunittests/bugtests.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ private slots:

void test_issue4();

void test_canceled_before_finished();

};

#endif // BUGTESTS_H

0 comments on commit ce2d1bc

Please sign in to comment.