1   package org.apache.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  
24  import org.apache.jcs.JCS;
25  import org.apache.jcs.auxiliary.lateral.LateralCacheAttributes;
26  import org.apache.jcs.auxiliary.lateral.LateralElementDescriptor;
27  import org.apache.jcs.engine.CacheElement;
28  import org.apache.jcs.engine.behavior.ICacheElement;
29  import org.apache.jcs.engine.behavior.ICompositeCacheManager;
30  import org.apache.jcs.engine.control.CompositeCacheManager;
31  import org.apache.jcs.engine.control.CompositeCacheManagerMockImpl;
32  import org.apache.jcs.utils.timing.SleepUtil;
33  
34  /***
35   * Basic unit tests for the sending and receiving portions of the lateral cache.
36   *
37   * @author Aaron Smuts
38   *
39   */
40  public class TestTCPLateralUnitTest
41      extends TestCase
42  {
43  
44      /***
45       * Test setup
46       */
47      public void setUp()
48      {
49          JCS.setConfigFilename( "/TestTCPLateralCache.ccf" );
50      }
51  
52      /***
53       * Make sure we can send a bunch to the listener. This would be better if we
54       * could plugin a Mock CacheManger. The listener will instantiate on on its
55       * own. We have to configure one before that.
56       * <p>
57       * @throws Exception
58       */
59      public void testSimpleSend()
60          throws Exception
61      {
62  
63          // force initialization
64          JCS.getInstance( "test" );
65  
66          TCPLateralCacheAttributes lac = new TCPLateralCacheAttributes();
67          lac.setTransmissionType( LateralCacheAttributes.TCP );
68          lac.setTcpServer( "localhost" + ":" + 8111 );
69          lac.setTcpListenerPort( 8111 );
70  
71          ICompositeCacheManager cacheMgr = CompositeCacheManager.getInstance();
72  
73          // start the listener
74          LateralTCPListener listener = (LateralTCPListener) LateralTCPListener.getInstance( lac, cacheMgr );
75  
76          // send to the listener
77          LateralTCPSender lur = new LateralTCPSender( lac );
78  
79          int numMes = 10;
80          for ( int i = 0; i < numMes; i++ )
81          {
82              String message = "adsfasasfasfasdasf";
83              CacheElement ce = new CacheElement( "test", "test", message );
84              LateralElementDescriptor led = new LateralElementDescriptor( ce );
85              led.command = LateralElementDescriptor.UPDATE;
86              led.requesterId = 1;
87              lur.send( led );
88          }
89  
90          SleepUtil.sleepAtLeast( numMes * 3 );
91  
92          System.out.println( "PutCount = " + listener.getPutCnt() );
93          assertEquals( "Should have received " + numMes + " by now.", numMes, listener.getPutCnt() );
94  
95      }
96  
97      /***
98       *
99       * @throws Exception
100      */
101     public void testReceive()
102         throws Exception
103     {
104         TCPLateralCacheAttributes lattr = new TCPLateralCacheAttributes();
105         lattr.setTcpListenerPort( 1101 );
106         lattr.setTransmissionTypeName( "TCP" );
107         CompositeCacheManagerMockImpl cacheMgr = new CompositeCacheManagerMockImpl();
108         System.out.println( "mock cache = " + cacheMgr.getCache( "test" ) );
109 
110         // force initialization
111         //LateralTCPCacheFactory fact = new LateralTCPCacheFactory();
112         //.getInstance( lattr, cacheMgr );
113         //LateralCacheNoWait nwait1 = (LateralCacheNoWait)lcMgr1.getCache(
114         // "test" );
115         //AuxiliaryCache nowait1 = fact.createCache( lattr, cacheMgr );
116 
117         //nowait1.update( );
118 
119         // start the listener
120         //LateralTCPListener listener = (LateralTCPListener)
121         LateralTCPListener.getInstance( lattr, cacheMgr );
122 
123         TCPLateralCacheAttributes lattr2 = new TCPLateralCacheAttributes();
124         lattr2.setTcpListenerPort( 1102 );
125         lattr2.setTransmissionTypeName( "TCP" );
126         lattr2.setTcpServer( "localhost:1101" );
127 
128         LateralTCPService service = new LateralTCPService( lattr2 );
129         service.setListenerId( 123456 );
130 
131         int cnt = 100;
132         for ( int i = 0; i < cnt; i++ )
133         {
134             ICacheElement element = new CacheElement( "test", "key" + i, "value1" );
135             service.update( element );
136         }
137 
138         SleepUtil.sleepAtLeast( 1000 );
139 
140         System.out.println( "cache. getPutCount = " + cacheMgr.getCache().getUpdateCount() );
141 
142         assertEquals( "Didn't get the correct number", cnt, cacheMgr.getCache().getUpdateCount() );
143     }
144 
145     /***
146      * Send objects with the same key but different values.
147      * @throws Exception
148      */
149     public void testSameKeyDifferentObject()
150         throws Exception
151     {
152         // setup a listener
153         TCPLateralCacheAttributes lattr = new TCPLateralCacheAttributes();
154         lattr.setTcpListenerPort( 1103 );
155         CompositeCacheManagerMockImpl cacheMgr = new CompositeCacheManagerMockImpl();
156         System.out.println( "mock cache = " + cacheMgr.getCache( "test" ) );
157 
158         // get the listener started
159         // give it our mock cache manager
160         //LateralTCPListener listener = (LateralTCPListener)
161         LateralTCPListener.getInstance( lattr, cacheMgr );
162 
163         // setup a service to talk to the listener started above.
164         TCPLateralCacheAttributes lattr2 = new TCPLateralCacheAttributes();
165         lattr2.setTcpListenerPort( 1104 );
166         lattr2.setTcpServer( "localhost:1103" );
167 
168         LateralTCPService service = new LateralTCPService( lattr2 );
169         service.setListenerId( 123456 );
170 
171         ICacheElement element = new CacheElement( "test", "key", "value1" );
172         service.update( element );
173 
174         SleepUtil.sleepAtLeast( 300 );
175 
176         ICacheElement element2 = new CacheElement( "test", "key", "value2" );
177         service.update( element2 );
178 
179         SleepUtil.sleepAtLeast(  1000 );
180 
181         ICacheElement cacheElement = cacheMgr.getCache().get( "key" );
182         System.out.println( "cacheElement = " + cacheElement );
183         assertEquals( "Didn't get the correct object", element2.getVal(), cacheElement.getVal() );
184     }
185 
186 
187     /***
188      * Send objects with the same key but different values.
189      * @throws Exception
190      */
191     public void testSameKeyObjectDifferentValueObject()
192         throws Exception
193     {
194         TCPLateralCacheAttributes lattr = new TCPLateralCacheAttributes();
195         lattr.setTcpListenerPort( 1105 );
196         lattr.setTransmissionTypeName( "TCP" );
197         CompositeCacheManagerMockImpl cacheMgr = new CompositeCacheManagerMockImpl();
198         System.out.println( "mock cache = " + cacheMgr.getCache( "test" ) );
199 
200         // get the listener started
201         // give it our mock cache manager
202         //LateralTCPListener listener = (LateralTCPListener)
203         LateralTCPListener.getInstance( lattr, cacheMgr );
204 
205         TCPLateralCacheAttributes lattr2 = new TCPLateralCacheAttributes();
206         lattr2.setTcpListenerPort( 1106);
207         lattr2.setTransmissionTypeName( "TCP" );
208         lattr2.setTcpServer( "localhost:1105" );
209 
210         LateralTCPService service = new LateralTCPService( lattr2 );
211         service.setListenerId( 123456 );
212 
213         String key = "key";
214         ICacheElement element = new CacheElement( "test", key, "value1" );
215         service.update( element );
216 
217         SleepUtil.sleepAtLeast( 300 );
218 
219         ICacheElement element2 = new CacheElement( "test", key, "value2" );
220         service.update( element2 );
221 
222         SleepUtil.sleepAtLeast( 1000 );
223 
224         ICacheElement cacheElement = cacheMgr.getCache().get( "key" );
225         System.out.println( "cacheElement = " + cacheElement );
226         assertEquals( "Didn't get the correct object", 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 the service.
231      * <p>
232      * @throws Exception
233      */
234     public void testSendAndReceived()
235         throws Exception
236     {
237         // setup a listener
238         TCPLateralCacheAttributes lattr = new TCPLateralCacheAttributes();
239         lattr.setTcpListenerPort( 1107 );
240         CompositeCacheManagerMockImpl cacheMgr = new CompositeCacheManagerMockImpl();
241         System.out.println( "mock cache = " + cacheMgr.getCache( "test" ) );
242 
243         // get the listener started
244         // give it our mock cache manager
245         //LateralTCPListener listener = (LateralTCPListener)
246         LateralTCPListener.getInstance( lattr, cacheMgr );
247 
248         // add the item to the listeners cache
249         ICacheElement element = new CacheElement( "test", "key", "value1" );
250         cacheMgr.getCache().update( element );
251 
252         // setup a service to talk to the listener started above.
253         TCPLateralCacheAttributes lattr2 = new TCPLateralCacheAttributes();
254         lattr2.setTcpListenerPort( 1108 );
255         lattr2.setTcpServer( "localhost:1107" );
256 
257         LateralTCPService service = new LateralTCPService( lattr2 );
258         service.setListenerId( 123456 );
259 
260         SleepUtil.sleepAtLeast(  300 );
261 
262         // DO WORK
263         ICacheElement result = service.get( "test", "key" );
264 
265         System.out.println( "testSendAndReceived, result = " + result );
266         assertNotNull( "Result should not be null.", result );
267         assertEquals( "Didn't get the correct object", element.getVal(), result.getVal() );
268     }
269 }