Skip to content

Commit 00f1a67

Browse files
committed
client: handle result argument of wait properly
Method `wait` of `Connector` allows to obtain result directly with `result` argument - it can be useful when the user cares about performance and don't want to perform an unneeded insertion to a map of ready futures. However, we check that the future is ready by looking to the map of ready futures, and the future is not inserted there when the argument is used. The commit fixes the problem by checking the argument as well. Also, the commit handles a situation when user waits for already decoded future using `result` argument. Before the commit, `wait` would return zero return code (success) but the `result` wouldn't be set - user still had to check if it's not in the map of futures. After the commit, already decoded future is moved to `result` as well, so if the function returns successfully, `result` is guaranteed to be set. Part of #112
1 parent 3aab6da commit 00f1a67

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/Client/Connector.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,15 @@ Connector<BUFFER, NetProvider>::wait(Connection<BUFFER, NetProvider> &conn,
210210
LOG_DEBUG("Waiting for the future ", future, " with timeout ", timeout);
211211
Timer timer{timeout};
212212
timer.start();
213+
static constexpr int INVALID_SYNC = -1;
214+
if (result != NULL)
215+
result->header.sync = INVALID_SYNC;
213216
if (connectionDecodeResponses(conn, result) != 0)
214217
return -1;
218+
if (result != NULL && result->header.sync != INVALID_SYNC) {
219+
LOG_DEBUG("Future ", future, " is ready and decoded");
220+
return 0;
221+
}
215222
while (!conn.hasError() && !conn.futureIsReady(future)) {
216223
if (m_NetProvider.wait(timer.timeLeft()) != 0) {
217224
conn.setError(std::string("Failed to poll: ") +
@@ -220,6 +227,10 @@ Connector<BUFFER, NetProvider>::wait(Connection<BUFFER, NetProvider> &conn,
220227
}
221228
if (connectionDecodeResponses(conn, result) != 0)
222229
return -1;
230+
if (result != NULL && result->header.sync != INVALID_SYNC) {
231+
LOG_DEBUG("Future ", future, " is ready and decoded");
232+
return 0;
233+
}
223234
if (timer.isExpired())
224235
break;
225236
}
@@ -231,6 +242,8 @@ Connector<BUFFER, NetProvider>::wait(Connection<BUFFER, NetProvider> &conn,
231242
LOG_DEBUG("Connection has been timed out: future ", future,
232243
" is not ready");
233244
return -1;
245+
} else if (result != NULL) {
246+
*result = std::move(conn.getResponse(future));
234247
}
235248
LOG_DEBUG("Feature ", future, " is ready and decoded");
236249
return 0;

test/ClientTest.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,29 @@ test_wait(Connector<BUFFER, NetProvider> &client)
12801280
client.close(conn2);
12811281
client.close(conn3);
12821282

1283+
TEST_CASE("wait with argument result");
1284+
f = conn.ping();
1285+
fail_unless(!conn.futureIsReady(f));
1286+
Response<BUFFER> result;
1287+
fail_unless(client.wait(conn, f, WAIT_TIMEOUT, &result) == 0);
1288+
/* The result was consumed, so the future is not ready. */
1289+
fail_unless(!conn.futureIsReady(f));
1290+
/* The future is actually request sync - check if the result is valid. */
1291+
fail_unless(result.header.sync == static_cast<int>(f));
1292+
fail_unless(result.header.code == 0);
1293+
1294+
TEST_CASE("wait with argument result for decoded future");
1295+
f = conn.ping();
1296+
fail_unless(!conn.futureIsReady(f));
1297+
fail_unless(client.wait(conn, f, WAIT_TIMEOUT) == 0);
1298+
fail_unless(conn.futureIsReady(f));
1299+
fail_unless(client.wait(conn, f, WAIT_TIMEOUT, &result) == 0);
1300+
/* The result was consumed, so the future is not ready. */
1301+
fail_unless(!conn.futureIsReady(f));
1302+
/* The future is actually request sync - check if the result is valid. */
1303+
fail_unless(result.header.sync == static_cast<int>(f));
1304+
fail_unless(result.header.code == 0);
1305+
12831306
client.close(conn);
12841307
}
12851308

0 commit comments

Comments
 (0)