View Javadoc
1   package org.apache.commons.jcs.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 junit.framework.TestCase;
23  import org.apache.commons.jcs.JCS;
24  import org.apache.commons.jcs.auxiliary.lateral.LateralCacheAttributes;
25  import org.apache.commons.jcs.auxiliary.lateral.LateralCommand;
26  import org.apache.commons.jcs.auxiliary.lateral.LateralElementDescriptor;
27  import org.apache.commons.jcs.engine.CacheElement;
28  import org.apache.commons.jcs.engine.behavior.ICacheElement;
29  import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager;
30  import org.apache.commons.jcs.engine.control.CompositeCache;
31  import org.apache.commons.jcs.engine.control.CompositeCacheManager;
32  import org.apache.commons.jcs.engine.control.MockCompositeCacheManager;
33  import org.apache.commons.jcs.engine.control.group.GroupAttrName;
34  import org.apache.commons.jcs.engine.control.group.GroupId;
35  import org.apache.commons.jcs.utils.timing.SleepUtil;
36  
37  import java.util.Map;
38  import java.util.Set;
39  
40  /**
41   * Basic unit tests for the sending and receiving portions of the lateral cache.
42   * <p>
43   * @author Aaron Smuts
44   */
45  public class TestTCPLateralUnitTest
46      extends TestCase
47  {
48      /**
49       * Test setup
50       */
51      @Override
52      public void setUp()
53      {
54          JCS.setConfigFilename( "/TestTCPLateralCache.ccf" );
55      }
56  
57      /**
58       * Make sure we can send a bunch to the listener. This would be better if we could plugin a Mock
59       * CacheManger. The listener will instantiate it on its own. We have to configure one before
60       * that.
61       * <p>
62       * @throws Exception
63       */
64      public void testSimpleSend()
65          throws Exception
66      {
67          // SETUP
68          // force initialization
69          JCS.getInstance( "test" );
70  
71          TCPLateralCacheAttributes lac = new TCPLateralCacheAttributes();
72          lac.setTransmissionType( LateralCacheAttributes.Type.TCP );
73          lac.setTcpServer( "localhost" + ":" + 8111 );
74          lac.setTcpListenerPort( 8111 );
75  
76          ICompositeCacheManager cacheMgr = CompositeCacheManager.getInstance();
77  
78          // start the listener
79          LateralTCPListener<String, String> listener = LateralTCPListener.getInstance( lac, cacheMgr );
80  
81          // send to the listener
82          LateralTCPSender lur = new LateralTCPSender( lac );
83  
84          // DO WORK
85          int numMes = 10;
86          for ( int i = 0; i < numMes; i++ )
87          {
88              String message = "adsfasasfasfasdasf";
89              CacheElement<String, String> ce = new CacheElement<String, String>( "test", "test", message );
90              LateralElementDescriptor<String, String> led = new LateralElementDescriptor<String, String>( ce );
91              led.command = LateralCommand.UPDATE;
92              led.requesterId = 1;
93              lur.send( led );
94          }
95  
96          SleepUtil.sleepAtLeast( numMes * 3 );
97  
98          // VERIFY
99          assertEquals( "Should have received " + numMes + " by now.", numMes, listener.getPutCnt() );
100     }
101 
102     /**
103      * @throws Exception
104      */
105     public void testReceive()
106         throws Exception
107     {
108         // VERIFY
109         TCPLateralCacheAttributes lattr = new TCPLateralCacheAttributes();
110         lattr.setTcpListenerPort( 1101 );
111         lattr.setTransmissionTypeName( "TCP" );
112         MockCompositeCacheManager cacheMgr = new MockCompositeCacheManager();
113 //        System.out.println( "mock cache = " + cacheMgr.getCache( "test" ) );
114 
115         LateralTCPListener.getInstance( lattr, cacheMgr );
116 
117         TCPLateralCacheAttributes lattr2 = new TCPLateralCacheAttributes();
118         lattr2.setTcpListenerPort( 1102 );
119         lattr2.setTransmissionTypeName( "TCP" );
120         lattr2.setTcpServer( "localhost:1101" );
121 
122         LateralTCPService<String, String> service = new LateralTCPService<String, String>( lattr2 );
123         service.setListenerId( 123456 );
124 
125         // DO WORK
126         int cnt = 100;
127         for ( int i = 0; i < cnt; i++ )
128         {
129             ICacheElement<String, String> element = new CacheElement<String, String>( "test", "key" + i, "value1" );
130             service.update( element );
131         }
132 
133         SleepUtil.sleepAtLeast( 1000 );
134 
135         // VERIFY
136         assertEquals( "Didn't get the correct number", cnt, cacheMgr.getCache().getUpdateCount() );
137     }
138 
139     /**
140      * Send objects with the same key but different values.
141      * <p>
142      * @throws Exception
143      */
144     public void testSameKeyDifferentObject()
145         throws Exception
146     {
147         // SETUP
148         // setup a listener
149         TCPLateralCacheAttributes lattr = new TCPLateralCacheAttributes();
150         lattr.setTcpListenerPort( 1103 );
151         MockCompositeCacheManager cacheMgr = new MockCompositeCacheManager();
152         CompositeCache<String, String> cache = cacheMgr.getCache( "test" );
153 //        System.out.println( "mock cache = " + cache );
154 
155         // get the listener started
156         // give it our mock cache manager
157         //LateralTCPListener listener = (LateralTCPListener)
158         LateralTCPListener.getInstance( lattr, cacheMgr );
159 
160         // setup a service to talk to the listener started above.
161         TCPLateralCacheAttributes lattr2 = new TCPLateralCacheAttributes();
162         lattr2.setTcpListenerPort( 1104 );
163         lattr2.setTcpServer( "localhost:1103" );
164 
165         LateralTCPService<String, String> service = new LateralTCPService<String, String>( lattr2 );
166         service.setListenerId( 123456 );
167 
168         // DO WORK
169         ICacheElement<String, String> element = new CacheElement<String, String>( "test", "key", "value1" );
170         service.update( element );
171 
172         SleepUtil.sleepAtLeast( 300 );
173 
174         ICacheElement<String, String> element2 = new CacheElement<String, String>( "test", "key", "value2" );
175         service.update( element2 );
176 
177         SleepUtil.sleepAtLeast( 1000 );
178 
179         // VERIFY
180         ICacheElement<String, String> cacheElement = cache.get( "key" );
181         assertEquals( "Didn't get the correct object "+ cacheElement, element2.getVal(), cacheElement.getVal() );
182     }
183 
184     /**
185      * Send objects with the same key but different values.
186      * <p>
187      * @throws Exception
188      */
189     public void testSameKeyObjectDifferentValueObject()
190         throws Exception
191     {
192         TCPLateralCacheAttributes lattr = new TCPLateralCacheAttributes();
193         lattr.setTcpListenerPort( 1105 );
194         lattr.setTransmissionTypeName( "TCP" );
195         MockCompositeCacheManager cacheMgr = new MockCompositeCacheManager();
196         CompositeCache<String, String> cache = cacheMgr.getCache( "test" );
197 //        System.out.println( "mock cache = " + cache );
198 
199         // get the listener started
200         // give it our mock cache manager
201         //LateralTCPListener listener = (LateralTCPListener)
202         LateralTCPListener.getInstance( lattr, cacheMgr );
203 
204         TCPLateralCacheAttributes lattr2 = new TCPLateralCacheAttributes();
205         lattr2.setTcpListenerPort( 1106 );
206         lattr2.setTransmissionTypeName( "TCP" );
207         lattr2.setTcpServer( "localhost:1105" );
208 
209         LateralTCPService<String, String> service = new LateralTCPService<String, String>( lattr2 );
210         service.setListenerId( 123456 );
211 
212         // DO WORK
213         String key = "key";
214         ICacheElement<String, String> element = new CacheElement<String, String>( "test", key, "value1" );
215         service.update( element );
216 
217         SleepUtil.sleepAtLeast( 300 );
218 
219         ICacheElement<String, String> element2 = new CacheElement<String, String>( "test", key, "value2" );
220         service.update( element2 );
221 
222         SleepUtil.sleepAtLeast( 1000 );
223 
224         // VERIFY
225         ICacheElement<String, String> cacheElement = cache.get( "key" );
226         assertEquals( "Didn't get the correct object: " + cacheElement , element2.getVal(), cacheElement.getVal() );
227     }
228 
229     /**
230      * Create a listener. Add an element to the listeners cache. Setup a service. Try to get from
231      * the service.
232      * <p>
233      * @throws Exception
234      */
235     public void testGet_SendAndReceived()
236         throws Exception
237     {
238         // SETUP
239         // setup a listener
240         TCPLateralCacheAttributes lattr = new TCPLateralCacheAttributes();
241         lattr.setTcpListenerPort( 1107 );
242         MockCompositeCacheManager cacheMgr = new MockCompositeCacheManager();
243         CompositeCache<String, String> cache = cacheMgr.getCache( "test" );
244 //        System.out.println( "mock cache = " + cache );
245 
246         // get the listener started
247         // give it our mock cache manager
248         LateralTCPListener.getInstance( lattr, cacheMgr );
249 
250         // add the item to the listeners cache
251         ICacheElement<String, String> element = new CacheElement<String, String>( "test", "key", "value1" );
252         cache.update( element );
253 
254         // setup a service to talk to the listener started above.
255         TCPLateralCacheAttributes lattr2 = new TCPLateralCacheAttributes();
256         lattr2.setTcpListenerPort( 1108 );
257         lattr2.setTcpServer( "localhost:1107" );
258 
259         LateralTCPService<String, String> service = new LateralTCPService<String, String>( lattr2 );
260         service.setListenerId( 123456 );
261 
262         SleepUtil.sleepAtLeast( 300 );
263 
264         // DO WORK
265         ICacheElement<String, String> result = service.get( "test", "key" );
266 
267         // VERIFY
268         assertNotNull( "Result should not be null.", result );
269         assertEquals( "Didn't get the correct object", element.getVal(), result.getVal() );
270     }
271 
272     /**
273      * Create a listener. Add an element to the listeners cache. Setup a service. Try to get keys from
274      * the service.
275      * <p>
276      * @throws Exception
277      */
278     public void testGetGroupKeys_SendAndReceived()  throws Exception
279     {
280         // SETUP
281         // setup a listener
282         TCPLateralCacheAttributes lattr = new TCPLateralCacheAttributes();
283         lattr.setTcpListenerPort( 1150 );
284         MockCompositeCacheManager cacheMgr = new MockCompositeCacheManager();
285         CompositeCache<GroupAttrName<String>, String> cache = cacheMgr.getCache( "test" );
286 //        System.out.println( "mock cache = " + cache );
287 
288         // get the listener started
289         // give it our mock cache manager
290         LateralTCPListener.getInstance( lattr, cacheMgr );
291 
292         // add the item to the listeners cache
293         GroupAttrName<String> groupKey = new GroupAttrName<String>(new GroupId("test", "group"), "key");
294         ICacheElement<GroupAttrName<String>, String> element =
295             new CacheElement<GroupAttrName<String>, String>( "test", groupKey, "value1" );
296         cache.update( element );
297 
298         // setup a service to talk to the listener started above.
299         TCPLateralCacheAttributes lattr2 = new TCPLateralCacheAttributes();
300         lattr2.setTcpListenerPort( 1151 );
301         lattr2.setTcpServer( "localhost:1150" );
302 
303         LateralTCPService<GroupAttrName<String>, String> service =
304             new LateralTCPService<GroupAttrName<String>, String>( lattr2 );
305         service.setListenerId( 123459 );
306 
307         SleepUtil.sleepAtLeast( 500 );
308 
309         // DO WORK
310         Set<GroupAttrName<String>> result = service.getKeySet("test");
311 
312        // SleepUtil.sleepAtLeast( 5000000 );
313 
314         // VERIFY
315         assertNotNull( "Result should not be null.", result );
316         assertEquals( "Didn't get the correct object", "key", result.toArray()[0] );
317     }
318 
319     /**
320      * Create a listener. Add an element to the listeners cache. Setup a service. Try to get from
321      * the service.
322      * <p>
323      * @throws Exception
324      */
325     public void testGetMatching_WithData()
326         throws Exception
327     {
328         // SETUP
329         // setup a listener
330         TCPLateralCacheAttributes lattr = new TCPLateralCacheAttributes();
331         lattr.setTcpListenerPort( 1108 );
332         MockCompositeCacheManager cacheMgr = new MockCompositeCacheManager();
333         CompositeCache<String, Integer> cache = cacheMgr.getCache( "test" );
334 //        System.out.println( "mock cache = " + cache );
335 
336         // get the listener started
337         // give it our mock cache manager
338         LateralTCPListener.getInstance( lattr, cacheMgr );
339 
340         String keyprefix1 = "MyPrefix1";
341         int numToInsertPrefix1 = 10;
342         // insert with prefix1
343         for ( int i = 0; i < numToInsertPrefix1; i++ )
344         {
345             // add the item to the listeners cache
346             ICacheElement<String, Integer> element = new CacheElement<String, Integer>( "test", keyprefix1 + String.valueOf( i ), Integer.valueOf( i ) );
347             cache.update( element );
348         }
349 
350         // setup a service to talk to the listener started above.
351         TCPLateralCacheAttributes lattr2 = new TCPLateralCacheAttributes();
352         lattr2.setTcpListenerPort( 1108 );
353         lattr2.setTcpServer( "localhost:1108" );
354 
355         LateralTCPService<String, Integer> service = new LateralTCPService<String, Integer>( lattr2 );
356         service.setListenerId( 123456 );
357 
358         SleepUtil.sleepAtLeast( 300 );
359 
360         // DO WORK
361         Map<String, ICacheElement<String, Integer>> result = service.getMatching( "test", keyprefix1 + ".+" );
362 
363         // VERIFY
364         assertNotNull( "Result should not be null.", result );
365         assertEquals( "Wrong number returned 1:", numToInsertPrefix1, result.size() );
366     }
367 }