Commit 8e932704b59fc26ced6b34e4d3e221d5e0f9347a

Authored by Bram Veldhoen
1 parent 53cd7adf

Fixed bug in eventloop upon connection failure.

In case of a connection failure (i.e. specify incorrect port number),
the main thread would be blocked. This is fixed by setting the condition
variables correctly in case of a connection error. The test
TestConnectionFailure will hang without this fix.
src/client.cpp
... ... @@ -224,6 +224,8 @@ void Redox::runEventLoop() {
224 224 // Handle connection error
225 225 if(connect_state_ != CONNECTED) {
226 226 logger_.warning() << "Did not connect, event loop exiting.";
  227 + exited_ = true;
  228 + running_ = false;
227 229 running_waiter_.notify_one();
228 230 return;
229 231 }
... ...
test/test.cpp
... ... @@ -38,7 +38,9 @@ protected:
38 38  
39 39 Redox rdx;
40 40  
41   - RedoxTest() {
  41 + RedoxTest() {}
  42 +
  43 + void connect() {
42 44  
43 45 // Connect to the server
44 46 rdx.connect("localhost", 6379);
... ... @@ -47,7 +49,10 @@ protected:
47 49 rdx.command({"DEL", "redox_test:a"});
48 50 }
49 51  
50   - virtual ~RedoxTest() { }
  52 + virtual ~RedoxTest()
  53 + {
  54 + rdx.disconnect();
  55 + }
51 56  
52 57 // CV and counter to wait for async commands to complete
53 58 atomic_int cmd_count = {0};
... ... @@ -149,13 +154,25 @@ protected:
149 154 // Core unit tests - asynchronous
150 155 // -------------------------------------------
151 156  
  157 +TEST_F(RedoxTest, TestConnection)
  158 +{
  159 + EXPECT_TRUE(rdx.connect("localhost", 6379));
  160 +}
  161 +
  162 +TEST_F(RedoxTest, TestConnectionFailure)
  163 +{
  164 + EXPECT_FALSE(rdx.connect("localhost", 6380));
  165 +}
  166 +
152 167 TEST_F(RedoxTest, GetSet) {
  168 + connect();
153 169 rdx.command<string>({"SET", "redox_test:a", "apple"}, print_and_check<string>("OK"));
154 170 rdx.command<string>({"GET", "redox_test:a"}, print_and_check<string>("apple"));
155 171 wait_for_replies();
156 172 }
157 173  
158 174 TEST_F(RedoxTest, Delete) {
  175 + connect();
159 176 rdx.command<string>({"SET", "redox_test:a", "apple"}, print_and_check<string>("OK"));
160 177 rdx.command<int>({"DEL", "redox_test:a"}, print_and_check(1));
161 178 rdx.command<nullptr_t>({"GET", "redox_test:a"}, check(nullptr));
... ... @@ -163,6 +180,7 @@ TEST_F(RedoxTest, Delete) {
163 180 }
164 181  
165 182 TEST_F(RedoxTest, Incr) {
  183 + connect();
166 184 int count = 100;
167 185 for(int i = 0; i < count; i++) {
168 186 rdx.command<int>({"INCR", "redox_test:a"}, check(i+1));
... ... @@ -172,6 +190,7 @@ TEST_F(RedoxTest, Incr) {
172 190 }
173 191  
174 192 TEST_F(RedoxTest, Delayed) {
  193 + connect();
175 194 rdx.commandDelayed<int>({"INCR", "redox_test:a"}, check(1), 0.1);
176 195 this_thread::sleep_for(chrono::milliseconds(150));
177 196 rdx.command<string>({"GET", "redox_test:a"}, print_and_check(to_string(1)));
... ... @@ -179,6 +198,7 @@ TEST_F(RedoxTest, Delayed) {
179 198 }
180 199  
181 200 TEST_F(RedoxTest, Loop) {
  201 + connect();
182 202 int count = 0;
183 203 int target_count = 20;
184 204 double dt = 0.005;
... ... @@ -208,12 +228,14 @@ TEST_F(RedoxTest, GetSetError) {
208 228 // -------------------------------------------
209 229  
210 230 TEST_F(RedoxTest, GetSetSync) {
  231 + connect();
211 232 print_and_check_sync<string>(rdx.commandSync<string>({"SET", "redox_test:a", "apple"}), "OK");
212 233 print_and_check_sync<string>(rdx.commandSync<string>({"GET", "redox_test:a"}), "apple");
213 234 rdx.disconnect();
214 235 }
215 236  
216 237 TEST_F(RedoxTest, DeleteSync) {
  238 + connect();
217 239 print_and_check_sync<string>(rdx.commandSync<string>({"SET", "redox_test:a", "apple"}), "OK");
218 240 print_and_check_sync(rdx.commandSync<int>({"DEL", "redox_test:a"}), 1);
219 241 check_sync(rdx.commandSync<nullptr_t>({"GET", "redox_test:a"}), nullptr);
... ... @@ -221,6 +243,7 @@ TEST_F(RedoxTest, DeleteSync) {
221 243 }
222 244  
223 245 TEST_F(RedoxTest, IncrSync) {
  246 + connect();
224 247 int count = 100;
225 248 for(int i = 0; i < count; i++) {
226 249 check_sync(rdx.commandSync<int>({"INCR", "redox_test:a"}), i+1);
... ...