Commit a2138a659c772e28fbe939ac87fb98a4b7b9ed08

Authored by Hayk Martirosyan
1 parent e51d7b99

Syncronous tests, looping tests

Showing 1 changed file with 89 additions and 5 deletions
test/test.cpp
... ... @@ -16,15 +16,24 @@ using namespace std;
16 16 // The fixture for testing class Redox.
17 17 // ------------------------------------------
18 18 class RedoxTest : public ::testing::Test {
  19 +
19 20 protected:
20 21  
21 22 Redox rdx = {"localhost", 6379};
22 23  
23 24 RedoxTest() {
  25 +
  26 + // Connect to the server
24 27 rdx.start();
  28 +
  29 + // Clear all keys used by the tests here
  30 + rdx.command("DEL redox_test:a");
25 31 }
26 32  
27 33 virtual ~RedoxTest() {
  34 +
  35 + // Block until the event loop exits.
  36 + // Each test is responsible for calling the stop_signal()
28 37 rdx.block();
29 38 }
30 39  
... ... @@ -57,7 +66,7 @@ protected:
57 66 template<class ReplyT>
58 67 Callback<ReplyT> print(Callback<ReplyT> callback) {
59 68 return [callback](const string& cmd, const ReplyT& reply) {
60   - cout << cmd << ": " << reply << endl;
  69 + cout << "[ASYNC] " << cmd << ": " << reply << endl;
61 70 callback(cmd, reply);
62 71 };
63 72 }
... ... @@ -79,10 +88,25 @@ protected:
79 88 cmd_waiter.wait(ul, [this] { return (cmd_count == 0); });
80 89 rdx.stop_signal();
81 90 };
  91 +
  92 + template<class ReplyT>
  93 + void check_sync(Command<ReplyT>* c, const ReplyT& value) {
  94 + ASSERT_TRUE(c->ok());
  95 + EXPECT_EQ(c->reply(), value);
  96 + c->free();
  97 + }
  98 +
  99 + template<class ReplyT>
  100 + void print_and_check_sync(Command<ReplyT>* c, const ReplyT& value) {
  101 + ASSERT_TRUE(c->ok());
  102 + EXPECT_EQ(c->reply(), value);
  103 + cout << "[SYNC] " << c->cmd << ": " << c->reply() << endl;
  104 + c->free();
  105 + }
82 106 };
83 107  
84 108 // -------------------------------------------
85   -// Unit tests - asynchronous
  109 +// Core unit tests - asynchronous
86 110 // -------------------------------------------
87 111  
88 112 TEST_F(RedoxTest, GetSet) {
... ... @@ -92,12 +116,72 @@ TEST_F(RedoxTest, GetSet) {
92 116 }
93 117  
94 118 TEST_F(RedoxTest, Delete) {
95   - rdx.command<string>("SET redox_test:b banana", print_and_check<string>("OK"));
96   - rdx.command<int>("DEL redox_test:b", print_and_check(1));
97   - rdx.command<nullptr_t>("GET redox_test:b", check(nullptr));
  119 + rdx.command<string>("SET redox_test:a apple", print_and_check<string>("OK"));
  120 + rdx.command<int>("DEL redox_test:a", print_and_check(1));
  121 + rdx.command<nullptr_t>("GET redox_test:a", check(nullptr));
  122 + wait_and_stop();
  123 +}
  124 +
  125 +TEST_F(RedoxTest, Incr) {
  126 + int count = 100;
  127 + for(int i = 0; i < count; i++) {
  128 + rdx.command<int>("INCR redox_test:a", check(i+1));
  129 + }
  130 + rdx.command<string>("GET redox_test:a", print_and_check(to_string(count)));
98 131 wait_and_stop();
99 132 }
100 133  
  134 +TEST_F(RedoxTest, Delayed) {
  135 + Command<int>* c = rdx.command<int>("INCR redox_test:a", check(1), nullptr, 0, 0.1);
  136 + this_thread::sleep_for(chrono::milliseconds(150));
  137 + c->cancel();
  138 + rdx.command<string>("GET redox_test:a", print_and_check(to_string(1)));
  139 + wait_and_stop();
  140 +}
  141 +
  142 +TEST_F(RedoxTest, Loop) {
  143 + int count = 0;
  144 + int target_count = 100;
  145 + double dt = 0.001;
  146 + Command<int>* c = rdx.command<int>("INCR redox_test:a",
  147 + [this, &count](const string& cmd, const int& reply) {
  148 + check(++count)(cmd, reply);
  149 + }, nullptr, dt);
  150 +
  151 + double wait_time = dt * (target_count - 0.5);
  152 + this_thread::sleep_for(std::chrono::duration<double>(wait_time));
  153 + c->cancel();
  154 +
  155 + rdx.command<string>("GET redox_test:a", print_and_check(to_string(target_count)));
  156 + wait_and_stop();
  157 +}
  158 +
  159 +// -------------------------------------------
  160 +// Core unit tests - synchronous
  161 +// -------------------------------------------
  162 +
  163 +TEST_F(RedoxTest, GetSetSync) {
  164 + print_and_check_sync<string>(rdx.command_blocking<string>("SET redox_test:a apple"), "OK");
  165 + print_and_check_sync<string>(rdx.command_blocking<string>("GET redox_test:a"), "apple");
  166 + rdx.stop_signal();
  167 +}
  168 +
  169 +TEST_F(RedoxTest, DeleteSync) {
  170 + print_and_check_sync<string>(rdx.command_blocking<string>("SET redox_test:a apple"), "OK");
  171 + print_and_check_sync(rdx.command_blocking<int>("DEL redox_test:a"), 1);
  172 + check_sync(rdx.command_blocking<nullptr_t>("GET redox_test:a"), nullptr);
  173 + rdx.stop_signal();
  174 +}
  175 +
  176 +TEST_F(RedoxTest, IncrSync) {
  177 + int count = 100;
  178 + for(int i = 0; i < count; i++) {
  179 + check_sync(rdx.command_blocking<int>("INCR redox_test:a"), i+1);
  180 + }
  181 + print_and_check_sync(rdx.command_blocking<string>("GET redox_test:a"), to_string(count));
  182 + rdx.stop_signal();
  183 +}
  184 +
101 185 // -------------------------------------------
102 186 // End tests
103 187 // -------------------------------------------
... ...