Skip to content

Commit

Permalink
Added condition variable to sockets unit test
Browse files Browse the repository at this point in the history
The sockets unit test had the possibility of the client trying to
connect before the server actually has started listening. The condition
variable should ensure this doesn't happen.
  • Loading branch information
eddic committed Apr 30, 2016
1 parent 54596da commit 9c747d4
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tests/sockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <mutex>
#include <thread>
#include <string>
#include <condition_variable>

const unsigned int chunkSize=1024;
const unsigned int tranCount=768;
Expand Down Expand Up @@ -197,8 +198,12 @@ void client()
serverGroup->wake();
}

std::condition_variable cv;
std::mutex cvMutex;

void server()
{
std::unique_lock<std::mutex> cvLock(cvMutex);
struct Buffer
{
std::vector<char> data;
Expand All @@ -210,6 +215,8 @@ void server()
serverGroup = &group;
if(!group.listen("127.0.0.1", port.c_str()))
FAIL_LOG("Unable to listen")
cv.notify_all();
cvLock.unlock();
std::map<Fastcgipp::Socket, Buffer> buffers;
std::unique_lock<std::mutex> lock(doneMutex);
bool flushed;
Expand Down Expand Up @@ -342,6 +349,10 @@ int main()

done=false;
std::thread serverThread(server);
{
std::unique_lock<std::mutex> cvLock(cvMutex);
cv.wait(cvLock);
}
client();
serverThread.join();

Expand Down

0 comments on commit 9c747d4

Please sign in to comment.