1 package org.apache.commons.jcs3.auxiliary.lateral.socket.tcp;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import java.util.Map;
24 import java.util.Set;
25
26 import org.apache.commons.jcs3.JCS;
27 import org.apache.commons.jcs3.auxiliary.lateral.LateralCacheAttributes;
28 import org.apache.commons.jcs3.auxiliary.lateral.LateralCommand;
29 import org.apache.commons.jcs3.auxiliary.lateral.LateralElementDescriptor;
30 import org.apache.commons.jcs3.engine.CacheElement;
31 import org.apache.commons.jcs3.engine.behavior.ICacheElement;
32 import org.apache.commons.jcs3.engine.behavior.ICompositeCacheManager;
33 import org.apache.commons.jcs3.engine.behavior.IElementSerializer;
34 import org.apache.commons.jcs3.engine.control.CompositeCache;
35 import org.apache.commons.jcs3.engine.control.CompositeCacheManager;
36 import org.apache.commons.jcs3.engine.control.MockCompositeCacheManager;
37 import org.apache.commons.jcs3.engine.control.group.GroupAttrName;
38 import org.apache.commons.jcs3.engine.control.group.GroupId;
39 import org.apache.commons.jcs3.utils.serialization.EncryptingSerializer;
40 import org.apache.commons.jcs3.utils.serialization.StandardSerializer;
41 import org.apache.commons.jcs3.utils.timing.SleepUtil;
42
43 import junit.framework.TestCase;
44
45
46
47
48 public class TestTCPLateralUnitTest
49 extends TestCase
50 {
51 private final MockCompositeCacheManager cacheMgr = new MockCompositeCacheManager();
52
53
54
55
56 @Override
57 public void setUp()
58 {
59 JCS.setConfigFilename( "/TestTCPLateralCache.ccf" );
60 }
61
62 private <K,V> CompositeCache<K, V> createCache(int port)
63 {
64 final TCPLateralCacheAttributes lattr = new TCPLateralCacheAttributes();
65 lattr.setTcpListenerPort(port);
66 lattr.setTransmissionType(LateralCacheAttributes.Type.TCP);
67
68 final CompositeCache<K, V> cache = cacheMgr.getCache( "test" );
69
70
71
72
73 LateralTCPListener.getInstance( lattr, cacheMgr, new StandardSerializer());
74
75 return cache;
76 }
77
78 private <K, V> LateralTCPService<K, V> createService(int listenerPort, int serverPort, long listenerId) throws IOException
79 {
80 final TCPLateralCacheAttributes lattr2 = new TCPLateralCacheAttributes();
81 lattr2.setTcpListenerPort(listenerPort);
82 lattr2.setTransmissionType(LateralCacheAttributes.Type.TCP);
83 lattr2.setTcpServer("localhost:" + serverPort);
84
85 final LateralTCPService<K, V> service = new LateralTCPService<>(lattr2, new StandardSerializer());
86 service.setListenerId(listenerId);
87
88 return service;
89 }
90
91
92
93
94
95
96
97
98 public void testSimpleSend()
99 throws Exception
100 {
101 simpleSend(new StandardSerializer(), 8111);
102 }
103
104
105
106
107 public void testSimpleEncriptedSend()
108 throws Exception
109 {
110 EncryptingSerializer serializer = new EncryptingSerializer();
111 serializer.setPreSharedKey("my_key");
112 simpleSend(serializer, 8112);
113 }
114
115 private void simpleSend(final IElementSerializer serializer, final int port ) throws IOException {
116
117
118 JCS.getInstance( "test" );
119
120 final TCPLateralCacheAttributes lac = new TCPLateralCacheAttributes();
121 lac.setTransmissionType(LateralCacheAttributes.Type.TCP);
122 lac.setTcpServer( "localhost:" + port );
123 lac.setTcpListenerPort( port );
124
125 final ICompositeCacheManager cacheMgr = CompositeCacheManager.getInstance();
126
127
128
129 final LateralTCPListener<String, String> listener = LateralTCPListener.getInstance( lac, cacheMgr, serializer );
130
131
132 final LateralTCPSender lur = new LateralTCPSender(lac, serializer);
133
134
135 final int numMes = 10;
136 for ( int i = 0; i < numMes; i++ )
137 {
138 final String message = "adsfasasfasfasdasf";
139 final CacheElement<String, String> ce = new CacheElement<>( "test", "test", message );
140 final LateralElementDescriptor<String, String> led =
141 new LateralElementDescriptor<>(ce, LateralCommand.UPDATE, 1);
142 lur.send( led );
143 }
144
145 SleepUtil.sleepAtLeast( numMes * 4 );
146
147
148 assertEquals( "Should have received " + numMes + " by now.", numMes, listener.getPutCnt() );
149 }
150
151
152
153
154 public void testReceive()
155 throws Exception
156 {
157
158 createCache(1101);
159
160 final LateralTCPService<String, String> service = createService(1102, 1101, 123456);
161
162
163 final int cnt = 100;
164 for ( int i = 0; i < cnt; i++ )
165 {
166 final ICacheElement<String, String> element = new CacheElement<>( "test", "key" + i, "value1" );
167 service.update( element );
168 }
169
170 SleepUtil.sleepAtLeast( 1000 );
171
172
173 assertEquals( "Didn't get the correct number", cnt, cacheMgr.getCache().getUpdateCount() );
174 }
175
176
177
178
179
180
181 public void testSameKeyDifferentObject()
182 throws Exception
183 {
184
185 final CompositeCache<String, String> cache = createCache(1103);
186
187
188 final LateralTCPService<String, String> service = createService(1104, 1103, 123456);
189
190
191 final ICacheElement<String, String> element = new CacheElement<>( "test", "key", "value1" );
192 service.update( element );
193
194 SleepUtil.sleepAtLeast( 300 );
195
196 final ICacheElement<String, String> element2 = new CacheElement<>( "test", "key", "value2" );
197 service.update( element2 );
198
199 SleepUtil.sleepAtLeast( 1000 );
200
201
202 final ICacheElement<String, String> cacheElement = cache.get( "key" );
203 assertEquals( "Didn't get the correct object "+ cacheElement, element2.getVal(), cacheElement.getVal() );
204 }
205
206
207
208
209
210
211 public void testSameKeyObjectDifferentValueObject()
212 throws Exception
213 {
214 final CompositeCache<String, String> cache = createCache(1105);
215
216 final LateralTCPService<String, String> service = createService(1106, 1105, 123456);
217
218
219 final String key = "key";
220 final ICacheElement<String, String> element = new CacheElement<>( "test", key, "value1" );
221 service.update( element );
222
223 SleepUtil.sleepAtLeast( 300 );
224
225 final ICacheElement<String, String> element2 = new CacheElement<>( "test", key, "value2" );
226 service.update( element2 );
227
228 SleepUtil.sleepAtLeast( 1000 );
229
230
231 final ICacheElement<String, String> cacheElement = cache.get( "key" );
232 assertEquals( "Didn't get the correct object: " + cacheElement , element2.getVal(), cacheElement.getVal() );
233 }
234
235
236
237
238
239
240
241 public void testGet_SendAndReceived()
242 throws Exception
243 {
244
245 final CompositeCache<String, String> cache = createCache(1107);
246
247
248 final ICacheElement<String, String> element = new CacheElement<>( "test", "key", "value1" );
249 cache.update( element );
250
251
252 final LateralTCPService<String, String> service = createService(1108, 1107, 123456);
253
254 SleepUtil.sleepAtLeast( 300 );
255
256
257 final ICacheElement<String, String> result = service.get( "test", "key" );
258
259
260 assertNotNull( "Result should not be null.", result );
261 assertEquals( "Didn't get the correct object", element.getVal(), result.getVal() );
262 }
263
264
265
266
267
268
269
270 public void testGetGroupKeys_SendAndReceived() throws Exception
271 {
272
273 final CompositeCache<GroupAttrName<String>, String> cache = createCache(1150);
274
275
276 final GroupAttrName<String> groupKey = new GroupAttrName<>(new GroupId("test", "group"), "key");
277 final ICacheElement<GroupAttrName<String>, String> element =
278 new CacheElement<>( "test", groupKey, "value1" );
279 cache.update( element );
280
281
282 final LateralTCPService<GroupAttrName<String>, String>service = createService(1151, 1150, 123459);
283
284 SleepUtil.sleepAtLeast( 500 );
285
286
287 final Set<GroupAttrName<String>> result = service.getKeySet("test");
288
289
290
291
292 assertNotNull( "Result should not be null.", result );
293 assertEquals( "Didn't get the correct object", "key", result.iterator().next().attrName );
294 }
295
296
297
298
299
300
301
302 public void testGetMatching_WithData()
303 throws Exception
304 {
305
306 final CompositeCache<String, Integer> cache = createCache(1108);
307
308 final String keyprefix1 = "MyPrefix1";
309 final int numToInsertPrefix1 = 10;
310
311 for ( int i = 0; i < numToInsertPrefix1; i++ )
312 {
313
314 final ICacheElement<String, Integer> element = new CacheElement<>( "test", keyprefix1 + String.valueOf( i ), Integer.valueOf( i ) );
315 cache.update( element );
316 }
317
318
319 final LateralTCPService<String, Integer> service = createService(1108, 1108, 123456);
320
321 SleepUtil.sleepAtLeast( 300 );
322
323
324 final Map<String, ICacheElement<String, Integer>> result = service.getMatching( "test", keyprefix1 + ".+" );
325
326
327 assertNotNull( "Result should not be null.", result );
328 assertEquals( "Wrong number returned 1:", numToInsertPrefix1, result.size() );
329 }
330 }