Skip to content

Commit ee5b0d5

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 568095a commit ee5b0d5

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
@@ -212,8 +212,15 @@ Connector<BUFFER, NetProvider>::wait(Connection<BUFFER, NetProvider> &conn,
212212
LOG_DEBUG("Waiting for the future ", future, " with timeout ", timeout);
213213
Timer timer{timeout};
214214
timer.start();
215+
static constexpr int INVALID_SYNC = -1;
216+
if (result != NULL)
217+
result->header.sync = INVALID_SYNC;
215218
if (connectionDecodeResponses(conn, result) != 0)
216219
return -1;
220+
if (result != NULL && result->header.sync != INVALID_SYNC) {
221+
LOG_DEBUG("Future ", future, " is ready and decoded");
222+
return 0;
223+
}
217224
while (!conn.hasError() && !conn.futureIsReady(future)) {
218225
if (m_NetProvider.wait(timer.timeLeft()) != 0) {
219226
conn.setError(std::string("Failed to poll: ") +
@@ -222,6 +229,10 @@ Connector<BUFFER, NetProvider>::wait(Connection<BUFFER, NetProvider> &conn,
222229
}
223230
if (connectionDecodeResponses(conn, result) != 0)
224231
return -1;
232+
if (result != NULL && result->header.sync != INVALID_SYNC) {
233+
LOG_DEBUG("Future ", future, " is ready and decoded");
234+
return 0;
235+
}
225236
if (timer.isExpired())
226237
break;
227238
}
@@ -233,6 +244,8 @@ Connector<BUFFER, NetProvider>::wait(Connection<BUFFER, NetProvider> &conn,
233244
LOG_DEBUG("Connection has been timed out: future ", future,
234245
" is not ready");
235246
return -1;
247+
} else if (result != NULL) {
248+
*result = std::move(conn.getResponse(future));
236249
}
237250
LOG_DEBUG("Feature ", future, " is ready and decoded");
238251
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)