Skip to content

Commit 0465dee

Browse files
client: handle connections with ready responses in waitAny
Currently, `waitAny` does not account for connections that already have ready responses. Let's handle this by going through the set of owned connections and checking them for ready responses. We should do this before the timer start, since the checking overhead should not be accounted for the waiting time. Closes #132
1 parent 10f25e9 commit 0465dee

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

src/Client/Connection.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ struct ConnectionImpl
8585
void setError(const std::string &msg, int errno_ = 0);
8686
bool hasError() const;
8787

88+
size_t getFutureCount() const;
89+
8890
BUFFER &getInBuf();
8991
BUFFER &getOutBuf();
9092

@@ -161,6 +163,13 @@ ConnectionImpl<BUFFER, NetProvider>::hasError() const
161163
return error.has_value();
162164
}
163165

166+
template <class BUFFER, class NetProvider>
167+
size_t
168+
ConnectionImpl<BUFFER, NetProvider>::getFutureCount() const
169+
{
170+
return futures.size();
171+
}
172+
164173
template <class BUFFER, class NetProvider>
165174
BUFFER &
166175
ConnectionImpl<BUFFER, NetProvider>::getInBuf()
@@ -459,7 +468,7 @@ template<class BUFFER, class NetProvider>
459468
size_t
460469
Connection<BUFFER, NetProvider>::getFutureCount() const
461470
{
462-
return impl->futures.size();
471+
return impl->getFutureCount();
463472
}
464473

465474
template<class BUFFER, class NetProvider>

src/Client/Connector.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,10 @@ Connector<BUFFER, NetProvider>::waitAny(int timeout)
366366
LOG_DEBUG("waitAny() called on connector without connections");
367367
return std::nullopt;
368368
}
369+
for (auto *conn : m_Connections) {
370+
if (conn->getFutureCount() != 0)
371+
return conn;
372+
}
369373
Timer timer{timeout};
370374
timer.start();
371375
while (m_ReadyToDecode.empty()) {

test/ClientTest.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,13 +1405,10 @@ test_wait(Connector<BUFFER, NetProvider> &client)
14051405
/* FIXME(gh-143): test solely that we check future readiness before waiting. */
14061406
fail_unless(client.waitCount(conn, 0) == 0);
14071407
conn.getResponse(f);
1408-
/* FIXME(gh-132): waitAny does not check connections for ready futures. */
1409-
#if 0
14101408
f = conn.ping();
14111409
fail_unless(client.wait(conn, f, WAIT_TIMEOUT) == 0);
1412-
fail_unless(client.waitAny(conn).has_value());
1410+
fail_unless(client.waitAny().has_value());
14131411
conn.getResponse(f);
1414-
#endif
14151412

14161413
#ifdef __linux__
14171414
TEST_CASE("wait methods internal wait failure (gh-121)");

0 commit comments

Comments
 (0)