Skip to content

Commit

Permalink
Fix hang on disconnect and change tests to handle this case (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
MisterTea authored Oct 9, 2018
1 parent ae1ae13 commit 4084659
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/base/BackedReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ int BackedReader::read(string* buf) {
// In EternalTCP, the server needs to explictly tell the client that
// the session is over.
errno = EPIPE;
bytesRead = -1;
return -1;
} else if (bytesRead > 0) {
partialMessage.append(tmpBuf, bytesRead);
} else if (bytesRead == -1 && errno == EAGAIN) {
Expand Down
22 changes: 13 additions & 9 deletions test/ConnectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class Collector {
if (status == 1) {
if (s == string("DONE")) {
fifo.push_back(s);
break;
}
if (s != string("HEARTBEAT")) {
fifo.push_back(s);
Expand All @@ -49,7 +48,10 @@ class Collector {
FATAL_FAIL(status);
}
}
::usleep(1000);
if (connection->isShuttingDown()) {
done = true;
}
::usleep(10 * 1000);
if (lastSecond <= time(NULL) - 5) {
lock_guard<std::mutex> guard(collectorMutex);
lastSecond = time(NULL);
Expand All @@ -58,6 +60,8 @@ class Collector {
}
}

void join() { collectorThread->join(); }

void finish() {
connection->shutdown();
lock_guard<std::mutex> guard(collectorMutex);
Expand All @@ -82,13 +86,15 @@ class Collector {

string read() {
while (!hasData()) {
::usleep(1000);
::usleep(10 * 1000);
}
return pop();
}

void write(const string& s) { return connection->writeMessage(s); }

shared_ptr<Connection> getConnection() { return connection; }

protected:
shared_ptr<Connection> connection;
deque<string> fifo;
Expand All @@ -100,13 +106,11 @@ class Collector {

void listenFn(bool* stopListening, int serverFd,
shared_ptr<ServerConnection> serverConnection) {
// Only works when there is 1:1 mapping between endpoint and fds. Will fix in
// future api
while (*stopListening == false) {
if (serverConnection->getSocketHandler()->hasData(serverFd)) {
serverConnection->acceptNewConnection(serverFd);
}
::usleep(1000 * 1000);
::usleep(10 * 1000);
}
}

Expand Down Expand Up @@ -205,8 +209,9 @@ class ConnectionTest : public testing::Test {
result = clientCollector->read();
EXPECT_EQ(result, "DONE");

serverCollector->finish();
clientCollector->finish();
serverConnection->removeClient(serverCollector->getConnection()->getId());
serverCollector->join();
clientCollector->join();

EXPECT_EQ(resultConcat, s);
}
Expand Down Expand Up @@ -282,7 +287,6 @@ TEST_F(FlakyConnectionTest, MultiReadWrite) {
new_id[0] = 'A' + a;
pool.push([&, this](int id, string clientId) { readWriteTest(clientId); },
new_id);
::usleep((500 + rand() % 1000) * 1000);
}
pool.stop(true);
}
2 changes: 1 addition & 1 deletion test/FlakySocketHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class FlakySocketHandler : public SocketHandler {
}

int writeAllOrReturn(int fd, const void* buf, size_t count) {
if (rand() % 20 == 0) {
if (rand() % 30 == 0) {
errno = EPIPE;
return -1;
}
Expand Down

0 comments on commit 4084659

Please sign in to comment.