/* * redis.cpp * * Created on: Jun 11, 2015 * Author: mstroom */ #include #include #include #include using namespace Di; using namespace std; TEST(Redis, Connect) { // Test connect with timeout on invalid port shared_ptr r = make_shared("localhost", 9818); EXPECT_FALSE(r->connect(1)); // Test connect with timeout on valid port shared_ptr re = make_shared("localhost", 56379); EXPECT_TRUE(re->connect(3)); } TEST(Redis, ConnectInvalidHostname) { // Test connect with timeout on invalid port shared_ptr r = make_shared("", 9818); EXPECT_FALSE(r->connect(1)); } TEST(Redis, Ping) { shared_ptr r = make_shared("localhost", 56379); // Test ping on invalid connection EXPECT_FALSE(r->ping()); // Open connection for testing if(r->connect(3)) { // Test ping on valid connection EXPECT_TRUE(r->ping()); } else { ADD_FAILURE(); } } TEST(Redis, hset) { shared_ptr r = make_shared("localhost", 56379); string uid; string key; string value; // Test hset while connection is closed EXPECT_FALSE(r->hset(uid,key, value)); // Open connection for testing if(r->connect(3)) { // Test hset while connection is open EXPECT_TRUE(r->hset(uid,key, value)); EXPECT_TRUE(r->disconnect()); // This HSET should reconnect EXPECT_TRUE(r->hset(uid,key, value)); } else { ADD_FAILURE(); } } TEST(Redis, hmget) { shared_ptr r = make_shared("localhost", 56379); string field = "a"; vector keys; vector values; // Test hmget while connection is closed EXPECT_FALSE(r->hmget(field, keys, &values)); // Open connection for testing if(r->connect(3)) { // Set variables for testing EXPECT_TRUE(r->hset(field, "test", "abc")); keys.push_back("key1"); keys.push_back("key2"); // Test hmget while connection is open EXPECT_FALSE(r->hmget(field, keys, nullptr)); EXPECT_TRUE(r->hmget(field, keys, &values)); EXPECT_TRUE(r->disconnect()); EXPECT_TRUE(r->hmget(field, keys, &values)); } else { ADD_FAILURE(); } } TEST(Redis, set) { shared_ptr r = make_shared("localhost", 56379); string key = string("key"); string value; // Test hset while connection is closed EXPECT_FALSE(r->set(key, value)); // Open connection for testing if(r->connect(3)) { // Test hset while connection is open EXPECT_FALSE(r->set(string(), value)); EXPECT_TRUE(r->set(key, value)); EXPECT_TRUE(r->disconnect()); EXPECT_TRUE(r->set(key, value)); } else { ADD_FAILURE(); } } TEST(Redis, get) { shared_ptr r = make_shared("localhost", 56379); string field = "a"; string value; // Test hmget while connection is closed EXPECT_FALSE(r->get(field, &value)); // Open connection for testing if(r->connect(3)) { value = "not-modified"; EXPECT_FALSE(r->get("", &value)); EXPECT_FALSE(r->get(field, nullptr)); EXPECT_STREQ("not-modified", value.c_str()); // Set variables for testing EXPECT_TRUE(r->set(field, "test")); // Test hmget while connection is open EXPECT_FALSE(r->get("non-existing-key.get", &value)); EXPECT_TRUE(r->get(field, &value)); EXPECT_STREQ("test", value.c_str()); value = ""; EXPECT_TRUE(r->disconnect()); EXPECT_TRUE(r->get(field, &value)); EXPECT_STREQ("test", value.c_str()); } else { ADD_FAILURE(); } } TEST(Redis, setex) { shared_ptr r = make_shared("localhost", 56379); string field = "a"; string value; // Test hmget while connection is closed EXPECT_FALSE(r->setex(field, value, 1)); EXPECT_FALSE(r->setex("", value, 1)); // Open connection for testing if(r->connect(3)) { // Set variables for testing EXPECT_TRUE(r->setex(field, "test", 1)); EXPECT_TRUE(r->get(field, &value)); EXPECT_STREQ("test", value.c_str()); usleep(1300000); // 1.3 sec value = ""; EXPECT_FALSE(r->get(field, &value)); EXPECT_STREQ("", value.c_str()); EXPECT_TRUE(r->disconnect()); } else { ADD_FAILURE(); } } TEST(Redis, persist) { shared_ptr r = make_shared("localhost", 56379); string field = "persist"; string value; // Test hmget while connection is closed EXPECT_FALSE(r->persist(field)); EXPECT_FALSE(r->persist("")); // Open connection for testing if(r->connect(3)) { // Set variables for testing EXPECT_TRUE(r->setex(field, "test", 2)); EXPECT_TRUE(r->get(field, &value)); EXPECT_STREQ("test", value.c_str()); sleep(1); EXPECT_TRUE(r->persist(field)); EXPECT_TRUE(r->get(field, &value)); EXPECT_STREQ("test", value.c_str()); usleep(1500 * 1000); EXPECT_TRUE(r->get(field, &value)); EXPECT_STREQ("test", value.c_str()); EXPECT_TRUE(r->disconnect()); } else { ADD_FAILURE(); } } TEST(Redis, hmget_reconnect) { shared_ptr r = make_shared("localhost", 56379); vector value; vector keys; // Open connection for testing if(r->connect(3)) { // Set value EXPECT_TRUE(r->hset("test-hmget_reconnect", "test", "boem")); // Field should be present keys.push_back("test"); EXPECT_TRUE(r->hmget("test-hmget_reconnect", keys, &value)); EXPECT_EQ(1, value.size()); EXPECT_STREQ("boem", value.begin()->c_str()); value.clear(); // Disconnect so that we reconnect at the next command EXPECT_TRUE(r->disconnect()); EXPECT_TRUE(r->hmget("test-hmget_reconnect", keys, &value)); EXPECT_EQ(1, value.size()); EXPECT_STREQ("boem", value.begin()->c_str()); value.clear(); } else { ADD_FAILURE(); } } TEST(Redis, psubscribe) { shared_ptr r = make_shared("localhost", 56379); string pattern; // Test psubscribe while connection is closed EXPECT_FALSE(r->psubscribe(pattern)); // Open connection for testing if(r->connect(3)) { // Test psubscribe while connection is open EXPECT_TRUE(r->psubscribe(pattern)); } else { ADD_FAILURE(); } } void sendEvent() { usleep(100000); shared_ptr r = make_shared("localhost", 56379); if(r->connect(3)) { for (int i = 0; i < 5; i++) { r->hset("test", "test" + to_string(i), "test"); usleep(25000); } } } TEST(Redis, readEvent) { shared_ptr r = make_shared("localhost", 56379); string event; string data; string pattern = "*"; shared_ptr t; // Test readEvent while connection is closed EXPECT_FALSE(r->readEvent(&event, &data)); // Open connection for testing if(r->connect(3)) { // Test psubscribe while connection is open if(r->psubscribe(pattern)) { // Test readEvent while connection is open t = make_shared(sendEvent); EXPECT_TRUE(r->readEvent(&event, &data)); t->join(); } else { ADD_FAILURE(); } } else { ADD_FAILURE(); } } TEST(Redis, readEventTimeout) { shared_ptr r = make_shared("localhost", 56379); string event; string data; string pattern = "*"; shared_ptr t; // Test readEvent while connection is closed EXPECT_EQ(Redis::EV_ERROR, r->readEvent(&event, &data, 1)); // Open connection for testing if(r->connect(3)) { // Test psubscribe while connection is open if(r->psubscribe(pattern)) { // Test readEvent while connection is open EXPECT_EQ(Redis::EV_TIMEOUT, r->readEvent(&event, &data, 1)); t = make_shared(sendEvent); EXPECT_EQ(Redis::EV_OK, r->readEvent(&event, &data, 1)); t->join(); } else { ADD_FAILURE(); } } else { ADD_FAILURE(); } }