-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathWeakCounterTest.cpp
More file actions
168 lines (148 loc) · 6.18 KB
/
WeakCounterTest.cpp
File metadata and controls
168 lines (148 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* WeakCounterTest.cpp
*
* Created on: May 8, 2018
* Author: rigazilla
*/#include "infinispan/hotrod/ConfigurationBuilder.h"
#include "infinispan/hotrod/RemoteCacheManager.h"
#include "WeakCounterTest.h"
#include <future>
using ::infinispan::hotrod::ConfigurationBuilder;
using ::infinispan::hotrod::Configuration;
std::unique_ptr<infinispan::hotrod::RemoteCacheManager> WeakCounterTest::remoteCacheManager;
WeakCounterTest::WeakCounterTest()
{
}
void WeakCounterTest::SetUp() {
if (WeakCounterTest::remoteCacheManager == nullptr){
ConfigurationBuilder builder;
builder.addServer().host("127.0.0.1").port(11222);
builder.protocolVersion(Configuration::PROTOCOL_VERSION_27);
builder.balancingStrategyProducer(nullptr);
WeakCounterTest::remoteCacheManager.reset(new RemoteCacheManager(builder.build(), false));
}
WeakCounterTest::remoteCacheManager->start();
};
void WeakCounterTest::TearDown() {
WeakCounterTest::remoteCacheManager->stop();
}
TEST_F(WeakCounterTest, testAdd) {
const std::string counterName("weakTestAdd");
const long initialValue = 10;
auto& counterManager = WeakCounterTest::remoteCacheManager->getCounterManager();
auto cc = CounterConfiguration(initialValue,0,0,8,CounterType::WEAK,Storage::VOLATILE);
counterManager.defineCounter(counterName, cc);
auto counter = counterManager.getWeakCounter(counterName);
EXPECT_EQ(counter->getValue(),10);
counter->add(10);
EXPECT_EQ(counter->getValue(),20);
counter->add(-20);
EXPECT_EQ(counter->getValue(),0);
counter->add(-20);
EXPECT_EQ(counter->getValue(),-20);
counter->remove();
}
TEST_F(WeakCounterTest, testReset) {
const std::string counterName("weakTestReset");
const long initialValue = 5;
auto& counterManager = WeakCounterTest::remoteCacheManager->getCounterManager();
auto cc = CounterConfiguration(initialValue, 0, 0, 8, CounterType::WEAK, Storage::VOLATILE);
counterManager.defineCounter(counterName, cc);
auto counter = counterManager.getWeakCounter(counterName);
EXPECT_EQ(counter->getValue(),5);
counter->add(100);
EXPECT_EQ(counter->getValue(),105);
counter->reset();
EXPECT_EQ(counter->getValue(),initialValue);
counter->remove();
}
TEST_F(WeakCounterTest, testConfiguration) {
const std::string counterName("weakTestConfiguration");
auto& counterManager = WeakCounterTest::remoteCacheManager->getCounterManager();
auto cc = CounterConfiguration(0, 0, 0, 64, CounterType::WEAK, Storage::VOLATILE);
counterManager.defineCounter(counterName, cc);
auto counter = counterManager.getWeakCounter(counterName);
CounterConfiguration retCc = counter->getConfiguration();
EXPECT_EQ(cc.getConcurrencyLevel(), retCc.getConcurrencyLevel());
EXPECT_EQ(cc.getType(), retCc.getType());
EXPECT_EQ(cc.getStorage(), retCc.getStorage());
EXPECT_EQ(counter->getName(), counterName);
counterManager.remove(counterName);
counter->remove();
const std::string counterName1("weakTestConfiguration1");
CounterConfiguration cc1 = CounterConfiguration(0, 0, 0, 1, CounterType::WEAK, Storage::PERSISTENT);
counterManager.defineCounter(counterName1, cc1);
counter = counterManager.getWeakCounter(counterName1);
CounterConfiguration retCc1 = counter->getConfiguration();
EXPECT_EQ(cc1.getConcurrencyLevel(), retCc1.getConcurrencyLevel());
EXPECT_EQ(cc1.getType(), retCc1.getType());
EXPECT_EQ(cc1.getStorage(), retCc1.getStorage());
EXPECT_EQ(counter->getName(), counterName1);
counterManager.remove(counterName1);
counter->remove();
}
TEST_F(WeakCounterTest, testRemove) {
const std::string counterName("weakTestRemove");
const long initialValue = 5;
auto& counterManager = WeakCounterTest::remoteCacheManager->getCounterManager();
auto cc = CounterConfiguration(initialValue, 0, 0, 8, CounterType::WEAK, Storage::VOLATILE);
counterManager.defineCounter(counterName, cc);
auto counter = counterManager.getWeakCounter(counterName);
counter->add(100);
EXPECT_EQ(counter->getValue(),105);
counter->remove();
EXPECT_EQ(counter->getValue(),5);
counter->add(-100);
EXPECT_EQ(counter->getValue(),-95);
counterManager.remove(counterName);
EXPECT_EQ(counter->getValue(),5);
counterManager.remove(counterName);
counter->remove();
}
static std::promise<void> valueChangedPromise;
static void action(const event::CounterEvent e) {
EXPECT_EQ(5, e.oldValue);
EXPECT_EQ(15, e.newValue);
EXPECT_EQ(VALID, e.newState);
EXPECT_EQ(VALID, e.oldState);
valueChangedPromise.set_value();
}
static void action1(const event::CounterEvent e) {
EXPECT_EQ(6, e.oldValue);
EXPECT_EQ(16, e.newValue);
EXPECT_EQ(VALID, e.newState);
EXPECT_EQ(VALID, e.oldState);
valueChangedPromise.set_value();
}
TEST_F(WeakCounterTest, testAddRemoveListener) {
const std::string counterName("testAddRemoveListener");
event::CounterListener c(counterName, action);
event::CounterListener c1(counterName, action1);
const long initialValue = 5;
valueChangedPromise = std::promise<void>();
auto& counterManager = WeakCounterTest::remoteCacheManager->getCounterManager();
auto cc = CounterConfiguration(initialValue, 0, 20, 8, CounterType::WEAK, Storage::VOLATILE);
counterManager.defineCounter(counterName, cc);
auto counter = counterManager.getWeakCounter(counterName);
counter->reset();
auto handler = counter->addListener(&c);
counter->add(10);
auto status = valueChangedPromise.get_future().wait_for(std::chrono::seconds(10));
EXPECT_EQ(status, std::future_status::ready);
valueChangedPromise = std::promise<void>();
counter->removeListener(handler);
counter->add(-9);
status = valueChangedPromise.get_future().wait_for(std::chrono::seconds(10));
EXPECT_EQ(status, std::future_status::timeout);
handler = counter->addListener(&c1);
valueChangedPromise = std::promise<void>();
counter->add(10);
status = valueChangedPromise.get_future().wait_for(std::chrono::seconds(10));
EXPECT_EQ(status, std::future_status::ready);
}
/*
@Override
public void testListenerAddAndRemove(Method method) throws InterruptedException {
strategy.testListenerAddAndRemove(method);
}
*/