View Javadoc
1   package org.apache.commons.jcs3.auxiliary.lateral.socket.tcp;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Basic unit tests for the sending and receiving portions of the lateral cache.
47   */
48  public class TestTCPLateralUnitTest
49      extends TestCase
50  {
51      private final MockCompositeCacheManager cacheMgr = new MockCompositeCacheManager();
52  
53      /**
54       * Test setup
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          // get the listener started
71          // give it our mock cache manager
72          //LateralTCPListener listener = (LateralTCPListener)
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       * Make sure we can send a bunch to the listener. This would be better if we could plugin a Mock
93       * CacheManger. The listener will instantiate it on its own. We have to configure one before
94       * that.
95       * <p>
96       * @throws Exception
97       */
98      public void testSimpleSend()
99          throws Exception
100     {
101     	simpleSend(new StandardSerializer(), 8111);
102     }
103     
104     /**
105      * @throws Exception
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     	// SETUP
117         // force initialization
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         // start the listener
129         final LateralTCPListener<String, String> listener = LateralTCPListener.getInstance( lac, cacheMgr, serializer );
130 
131         // send to the listener
132         final LateralTCPSender lur = new LateralTCPSender(lac,  serializer);
133 
134         // DO WORK
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 ); // this may need to be adjusted ...
146 
147         // VERIFY
148         assertEquals( "Should have received " + numMes + " by now.", numMes, listener.getPutCnt() );
149     }
150 
151     /**
152      * @throws Exception
153      */
154     public void testReceive()
155         throws Exception
156     {
157         // VERIFY
158         createCache(1101);
159 
160         final LateralTCPService<String, String> service = createService(1102, 1101, 123456);
161 
162         // DO WORK
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         // VERIFY
173         assertEquals( "Didn't get the correct number", cnt, cacheMgr.getCache().getUpdateCount() );
174     }
175 
176     /**
177      * Send objects with the same key but different values.
178      * <p>
179      * @throws Exception
180      */
181     public void testSameKeyDifferentObject()
182         throws Exception
183     {
184         // SETUP
185         final CompositeCache<String, String> cache = createCache(1103);
186 
187         // setup a service to talk to the listener started above.
188         final LateralTCPService<String, String> service = createService(1104, 1103, 123456);
189 
190         // DO WORK
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         // VERIFY
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      * Send objects with the same key but different values.
208      * <p>
209      * @throws Exception
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         // DO WORK
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         // VERIFY
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      * Create a listener. Add an element to the listeners cache. Setup a service. Try to get from
237      * the service.
238      * <p>
239      * @throws Exception
240      */
241     public void testGet_SendAndReceived()
242         throws Exception
243     {
244         // SETUP
245         final CompositeCache<String, String> cache = createCache(1107);
246 
247         // add the item to the listeners cache
248         final ICacheElement<String, String> element = new CacheElement<>( "test", "key", "value1" );
249         cache.update( element );
250 
251         // setup a service to talk to the listener started above.
252         final LateralTCPService<String, String> service = createService(1108, 1107, 123456);
253 
254         SleepUtil.sleepAtLeast( 300 );
255 
256         // DO WORK
257         final ICacheElement<String, String> result = service.get( "test", "key" );
258 
259         // VERIFY
260         assertNotNull( "Result should not be null.", result );
261         assertEquals( "Didn't get the correct object", element.getVal(), result.getVal() );
262     }
263 
264     /**
265      * Create a listener. Add an element to the listeners cache. Setup a service. Try to get keys from
266      * the service.
267      * <p>
268      * @throws Exception
269      */
270     public void testGetGroupKeys_SendAndReceived()  throws Exception
271     {
272         // SETUP
273         final CompositeCache<GroupAttrName<String>, String> cache = createCache(1150);
274 
275         // add the item to the listeners cache
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         // setup a service to talk to the listener started above.
282         final LateralTCPService<GroupAttrName<String>, String>service = createService(1151, 1150, 123459);
283 
284         SleepUtil.sleepAtLeast( 500 );
285 
286         // DO WORK
287         final Set<GroupAttrName<String>> result = service.getKeySet("test");
288 
289        // SleepUtil.sleepAtLeast( 5000000 );
290 
291         // VERIFY
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      * Create a listener. Add an element to the listeners cache. Setup a service. Try to get from
298      * the service.
299      * <p>
300      * @throws Exception
301      */
302     public void testGetMatching_WithData()
303         throws Exception
304     {
305         // SETUP
306         final CompositeCache<String, Integer> cache = createCache(1108);
307 
308         final String keyprefix1 = "MyPrefix1";
309         final int numToInsertPrefix1 = 10;
310         // insert with prefix1
311         for ( int i = 0; i < numToInsertPrefix1; i++ )
312         {
313             // add the item to the listeners cache
314             final ICacheElement<String, Integer> element = new CacheElement<>( "test", keyprefix1 + String.valueOf( i ), Integer.valueOf( i ) );
315             cache.update( element );
316         }
317 
318         // setup a service to talk to the listener started above.
319         final LateralTCPService<String, Integer> service = createService(1108, 1108, 123456);
320 
321         SleepUtil.sleepAtLeast( 300 );
322 
323         // DO WORK
324         final Map<String, ICacheElement<String, Integer>> result = service.getMatching( "test", keyprefix1 + ".+" );
325 
326         // VERIFY
327         assertNotNull( "Result should not be null.", result );
328         assertEquals( "Wrong number returned 1:", numToInsertPrefix1, result.size() );
329     }
330 }