From 8e932704b59fc26ced6b34e4d3e221d5e0f9347a Mon Sep 17 00:00:00 2001 From: Bram Veldhoen Date: Mon, 1 Jun 2015 14:13:21 +0200 Subject: [PATCH] Fixed bug in eventloop upon connection failure. --- src/client.cpp | 2 ++ test/test.cpp | 27 +++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/client.cpp b/src/client.cpp index e4700de..0b1a901 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -224,6 +224,8 @@ void Redox::runEventLoop() { // Handle connection error if(connect_state_ != CONNECTED) { logger_.warning() << "Did not connect, event loop exiting."; + exited_ = true; + running_ = false; running_waiter_.notify_one(); return; } diff --git a/test/test.cpp b/test/test.cpp index 56612b2..e14fec9 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -38,7 +38,9 @@ protected: Redox rdx; - RedoxTest() { + RedoxTest() {} + + void connect() { // Connect to the server rdx.connect("localhost", 6379); @@ -47,7 +49,10 @@ protected: rdx.command({"DEL", "redox_test:a"}); } - virtual ~RedoxTest() { } + virtual ~RedoxTest() + { + rdx.disconnect(); + } // CV and counter to wait for async commands to complete atomic_int cmd_count = {0}; @@ -149,13 +154,25 @@ protected: // Core unit tests - asynchronous // ------------------------------------------- +TEST_F(RedoxTest, TestConnection) +{ + EXPECT_TRUE(rdx.connect("localhost", 6379)); +} + +TEST_F(RedoxTest, TestConnectionFailure) +{ + EXPECT_FALSE(rdx.connect("localhost", 6380)); +} + TEST_F(RedoxTest, GetSet) { + connect(); rdx.command({"SET", "redox_test:a", "apple"}, print_and_check("OK")); rdx.command({"GET", "redox_test:a"}, print_and_check("apple")); wait_for_replies(); } TEST_F(RedoxTest, Delete) { + connect(); rdx.command({"SET", "redox_test:a", "apple"}, print_and_check("OK")); rdx.command({"DEL", "redox_test:a"}, print_and_check(1)); rdx.command({"GET", "redox_test:a"}, check(nullptr)); @@ -163,6 +180,7 @@ TEST_F(RedoxTest, Delete) { } TEST_F(RedoxTest, Incr) { + connect(); int count = 100; for(int i = 0; i < count; i++) { rdx.command({"INCR", "redox_test:a"}, check(i+1)); @@ -172,6 +190,7 @@ TEST_F(RedoxTest, Incr) { } TEST_F(RedoxTest, Delayed) { + connect(); rdx.commandDelayed({"INCR", "redox_test:a"}, check(1), 0.1); this_thread::sleep_for(chrono::milliseconds(150)); rdx.command({"GET", "redox_test:a"}, print_and_check(to_string(1))); @@ -179,6 +198,7 @@ TEST_F(RedoxTest, Delayed) { } TEST_F(RedoxTest, Loop) { + connect(); int count = 0; int target_count = 20; double dt = 0.005; @@ -208,12 +228,14 @@ TEST_F(RedoxTest, GetSetError) { // ------------------------------------------- TEST_F(RedoxTest, GetSetSync) { + connect(); print_and_check_sync(rdx.commandSync({"SET", "redox_test:a", "apple"}), "OK"); print_and_check_sync(rdx.commandSync({"GET", "redox_test:a"}), "apple"); rdx.disconnect(); } TEST_F(RedoxTest, DeleteSync) { + connect(); print_and_check_sync(rdx.commandSync({"SET", "redox_test:a", "apple"}), "OK"); print_and_check_sync(rdx.commandSync({"DEL", "redox_test:a"}), 1); check_sync(rdx.commandSync({"GET", "redox_test:a"}), nullptr); @@ -221,6 +243,7 @@ TEST_F(RedoxTest, DeleteSync) { } TEST_F(RedoxTest, IncrSync) { + connect(); int count = 100; for(int i = 0; i < count; i++) { check_sync(rdx.commandSync({"INCR", "redox_test:a"}), i+1); -- libgit2 0.21.4