diff --git a/test/test.cpp b/test/test.cpp index 57423e8..2c84905 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -16,15 +16,24 @@ using namespace std; // The fixture for testing class Redox. // ------------------------------------------ class RedoxTest : public ::testing::Test { + protected: Redox rdx = {"localhost", 6379}; RedoxTest() { + + // Connect to the server rdx.start(); + + // Clear all keys used by the tests here + rdx.command("DEL redox_test:a"); } virtual ~RedoxTest() { + + // Block until the event loop exits. + // Each test is responsible for calling the stop_signal() rdx.block(); } @@ -57,7 +66,7 @@ protected: template Callback print(Callback callback) { return [callback](const string& cmd, const ReplyT& reply) { - cout << cmd << ": " << reply << endl; + cout << "[ASYNC] " << cmd << ": " << reply << endl; callback(cmd, reply); }; } @@ -79,10 +88,25 @@ protected: cmd_waiter.wait(ul, [this] { return (cmd_count == 0); }); rdx.stop_signal(); }; + + template + void check_sync(Command* c, const ReplyT& value) { + ASSERT_TRUE(c->ok()); + EXPECT_EQ(c->reply(), value); + c->free(); + } + + template + void print_and_check_sync(Command* c, const ReplyT& value) { + ASSERT_TRUE(c->ok()); + EXPECT_EQ(c->reply(), value); + cout << "[SYNC] " << c->cmd << ": " << c->reply() << endl; + c->free(); + } }; // ------------------------------------------- -// Unit tests - asynchronous +// Core unit tests - asynchronous // ------------------------------------------- TEST_F(RedoxTest, GetSet) { @@ -92,12 +116,72 @@ TEST_F(RedoxTest, GetSet) { } TEST_F(RedoxTest, Delete) { - rdx.command("SET redox_test:b banana", print_and_check("OK")); - rdx.command("DEL redox_test:b", print_and_check(1)); - rdx.command("GET redox_test:b", check(nullptr)); + 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)); + wait_and_stop(); +} + +TEST_F(RedoxTest, Incr) { + int count = 100; + for(int i = 0; i < count; i++) { + rdx.command("INCR redox_test:a", check(i+1)); + } + rdx.command("GET redox_test:a", print_and_check(to_string(count))); wait_and_stop(); } +TEST_F(RedoxTest, Delayed) { + Command* c = rdx.command("INCR redox_test:a", check(1), nullptr, 0, 0.1); + this_thread::sleep_for(chrono::milliseconds(150)); + c->cancel(); + rdx.command("GET redox_test:a", print_and_check(to_string(1))); + wait_and_stop(); +} + +TEST_F(RedoxTest, Loop) { + int count = 0; + int target_count = 100; + double dt = 0.001; + Command* c = rdx.command("INCR redox_test:a", + [this, &count](const string& cmd, const int& reply) { + check(++count)(cmd, reply); + }, nullptr, dt); + + double wait_time = dt * (target_count - 0.5); + this_thread::sleep_for(std::chrono::duration(wait_time)); + c->cancel(); + + rdx.command("GET redox_test:a", print_and_check(to_string(target_count))); + wait_and_stop(); +} + +// ------------------------------------------- +// Core unit tests - synchronous +// ------------------------------------------- + +TEST_F(RedoxTest, GetSetSync) { + print_and_check_sync(rdx.command_blocking("SET redox_test:a apple"), "OK"); + print_and_check_sync(rdx.command_blocking("GET redox_test:a"), "apple"); + rdx.stop_signal(); +} + +TEST_F(RedoxTest, DeleteSync) { + print_and_check_sync(rdx.command_blocking("SET redox_test:a apple"), "OK"); + print_and_check_sync(rdx.command_blocking("DEL redox_test:a"), 1); + check_sync(rdx.command_blocking("GET redox_test:a"), nullptr); + rdx.stop_signal(); +} + +TEST_F(RedoxTest, IncrSync) { + int count = 100; + for(int i = 0; i < count; i++) { + check_sync(rdx.command_blocking("INCR redox_test:a"), i+1); + } + print_and_check_sync(rdx.command_blocking("GET redox_test:a"), to_string(count)); + rdx.stop_signal(); +} + // ------------------------------------------- // End tests // -------------------------------------------