wait_for may return before callback

Description

The problem is similar with CPP-514. A unit test is appended in the end.
Maybe it's difficult to guarantee threads waiting for a future see the side effects of the callback
because currently one may call future::wait_for in a callback without deadlocks.

+
+enum class callback_status {
+ WAIT_CALL,
+ CALLING,
+ CALLED,
+};
+
+void on_future_long_callback(CassFuture* future, void* data) {
+ callback_status* callback_status = static_cast<enum callback_status*>(data);
+ *callback_status = callback_status::CALLING;
+ test::Utils::msleep(DELAY_MS);
+ *callback_status = callback_status::CALLED;
+}
+
+TEST(FutureUnitTest, WaitForReturnsAfterCallback) {
+ Future future(Future::FUTURE_TYPE_GENERIC);
+ callback_status status(callback_status::WAIT_CALL);
+ ASSERT_TRUE(future.set_callback(&on_future_long_callback, &status));
+
+ uv_thread_t thread;
+ ASSERT_EQ(0, uv_thread_create(&thread, start_timer, &future));
+
+ while ((volatile callback_status)&status == callback_status::WAIT_CALL) {
+ }
+
+ ASSERT_EQ(status, callback_status::CALLING);
+ ASSERT_FALSE(future.wait_for(1e+7)); // 10 millisecond
+ ASSERT_FALSE(future.ready());
+ ASSERT_NE(status, callback_status::CALLED);
+
+ ASSERT_TRUE(future.wait_for(1e+9)); // 1 second
+ ASSERT_TRUE(future.ready());
+ ASSERT_EQ(status, callback_status::CALLED);
+
+ ASSERT_EQ(0, uv_thread_join(&thread));
+}

Environment

None

Pull Requests

None

Assignee

Unassigned

Reporter

Zhenjie Wang

Labels

None

PM Priority

None

Reproduced in

None

External issue ID

None

Doc Impact

None

Reviewer

None

Pull Request

None

Size

None

Affects versions

Priority

Trivial
Configure