Updates to run microbenchmarks against S3 us-west.
[bluesky.git] / kvstore / kvtest.cc
1 #include <boost/shared_ptr.hpp>
2 #include <gtest/gtest.h>
3 #include "workqueue.h"
4 #include "kvservice.h"
5 #include "kvclient.h"
6 #include "protobufrpc.h"
7
8 using namespace boost;
9 using namespace kvstore;
10 using namespace bicker;
11 using namespace kvrpc;
12
13 /* http://code.google.com/p/googletest/wiki/GoogleTestPrimer */
14
15 class KVTest : public ::testing::Test
16 {
17 public:
18     KVTest()
19         :_kv_client("127.0.0.1", "9090")
20     {
21         vector<string> data_dirs;
22         data_dirs.push_back("/tmp");
23
24         shared_ptr<Service> service(new KeyValueRpcService(new BDBBackend(data_dirs)));
25         _rpc_server.registerService(9090, service);
26
27         _server_thread.reset(
28                         new thread(
29                                 boost::bind(
30                                         &ProtoBufRpcServer::run, 
31                                         &_rpc_server)));
32
33     }
34
35     virtual ~KVTest()
36     {
37         _rpc_server.shutdown();
38         _server_thread->join();
39     }
40 protected:
41
42     ProtoBufRpcServer _rpc_server;
43     KeyValueClient _kv_client;
44     shared_ptr<thread> _server_thread;
45 };
46
47
48 TEST_F(KVTest, PutTest)
49 {
50     ASSERT_TRUE(_kv_client.Put("test", "value"));
51     string value;
52     ASSERT_TRUE(_kv_client.Get("test", &value));
53     ASSERT_EQ(value, "value");
54 }
55
56 TEST_F(KVTest, MedPutTest)
57 {
58     string test_value(1024*10, '6');
59     ASSERT_TRUE(_kv_client.Put("test", test_value));
60     string value;
61     ASSERT_TRUE(_kv_client.Get("test", &value));
62     ASSERT_TRUE(value == test_value);
63 }
64
65 TEST_F(KVTest, BigPutTest)
66 {
67     string test_value(1024*1024*10, '6');
68     ASSERT_TRUE(_kv_client.Put("test", test_value));
69     string value;
70     ASSERT_TRUE(_kv_client.Get("test", &value));
71     ASSERT_EQ(value.size(), test_value.size());
72     ASSERT_TRUE(value == test_value);
73 }
74
75 TEST_F(KVTest, LoopPutTest)
76 {
77     string test_value(1024*1024, '6');
78
79     for (int i = 0; i < 10; ++i)
80     {
81         ostringstream key;
82         key << "test" << i;
83         ASSERT_TRUE(_kv_client.Put(key.str(), test_value));
84         string value;
85         ASSERT_TRUE(_kv_client.Get(key.str(), &value));
86         ASSERT_TRUE(value == test_value);
87     }
88 }
89
90 TEST_F(KVTest, EmptyTest)
91 {
92     string value;
93     ASSERT_FALSE(_kv_client.Get("test", &value));
94 }
95
96 int
97 main(
98      int argc,
99      char **argv
100     )
101 {
102     ::testing::InitGoogleTest(&argc, argv);
103     return RUN_ALL_TESTS();
104 }