1   package org.apache.jcs.auxiliary.remote.server;
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.auxiliary.AuxiliaryCache;
25  import org.apache.jcs.auxiliary.remote.RemoteCacheAttributes;
26  import org.apache.jcs.auxiliary.remote.RemoteCacheListenerMockImpl;
27  import org.apache.jcs.auxiliary.remote.RemoteCacheManager;
28  import org.apache.jcs.engine.CacheElement;
29  import org.apache.jcs.engine.behavior.ICacheElement;
30  import org.apache.jcs.engine.control.CompositeCacheManagerMockImpl;
31  import org.apache.jcs.utils.timing.SleepUtil;
32  
33  /***
34   * These tests startup the remote server and make requests to it.
35   * <p>
36   * @author Aaron Smuts
37   */
38  public class BasicRemoteCacheClientServerUnitTest
39      extends TestCase
40  {
41      RemoteCacheServer server = null;
42  
43      /***
44       * Starts the server. This is not in a setup, since the server is slow to kill right now.
45       */
46      public BasicRemoteCacheClientServerUnitTest()
47      {
48          String configFile = "TestRemoteCacheClientServer.ccf";
49          server = RemoteCacheServerStartupUtil.startServerUsingProperties( configFile );
50      }
51  
52      /***
53       * Verify that we can start the remote cache server. Send an item to the remote. Verify that the
54       * remote put count goes up. If we go through JCS, the manager will be shared and we will get
55       * into an endless loop. We will use a mock cache manager instead.
56       * <p>
57       * The remote server uses the real JCS. We can verify that items are added to JCS behind the
58       * server by calling get. We cannot access it directly via JCS since it is serialized.
59       * <p>
60       * This test uses a mock injected client to test a normal server.
61       * <p>
62       * @throws Exception
63       */
64      public void testSinglePut()
65          throws Exception
66      {
67          // SETUP
68          CompositeCacheManagerMockImpl compositeCacheManager = new CompositeCacheManagerMockImpl();
69  
70          RemoteCacheAttributes attributes = new RemoteCacheAttributes();
71          attributes.setRemoteHost( "localhost" );
72          attributes.setLocalPort( 1202 );
73          attributes.setRemotePort( 1101 );
74  
75          RemoteCacheManager remoteCacheManager = RemoteCacheManager.getInstance( attributes, compositeCacheManager );
76          String regionName = "testSinglePut";
77          AuxiliaryCache cache = remoteCacheManager.getCache( regionName );
78  
79          // DO WORK
80          int numPutsPrior = server.getPutCount();
81          ICacheElement element = new CacheElement( regionName, "key", "value" );
82          cache.update( element );
83          SleepUtil.sleepAtLeast( 50 );
84  
85          // VERIFY
86          System.out.println( server.getStats() );
87          assertEquals( "Wrong number of puts", 1, server.getPutCount() - numPutsPrior );
88  
89          // DO WORK
90          ICacheElement result = cache.get( "key" );
91  
92          // VERIFY
93          assertEquals( "Wrong element.", element.getVal(), result.getVal() );
94      }
95  
96      /***
97       * Verify that we can remove an item via the remote server.
98       * <p>
99       * @throws Exception
100      */
101     public void testPutRemove()
102         throws Exception
103     {
104         // SETUP
105         CompositeCacheManagerMockImpl compositeCacheManager = new CompositeCacheManagerMockImpl();
106 
107         RemoteCacheAttributes attributes = new RemoteCacheAttributes();
108         attributes.setRemoteHost( "localhost" );
109         attributes.setLocalPort( 1202 );
110         attributes.setRemotePort( 1101 );
111 
112         RemoteCacheManager remoteCacheManager = RemoteCacheManager.getInstance( attributes, compositeCacheManager );
113         String regionName = "testPutRemove";
114         AuxiliaryCache cache = remoteCacheManager.getCache( regionName );
115 
116         // DO WORK
117         int numPutsPrior = server.getPutCount();
118         ICacheElement element = new CacheElement( regionName, "key", "value" );
119         cache.update( element );
120         SleepUtil.sleepAtLeast( 50 );
121 
122         // VERIFY
123         System.out.println( server.getStats() );
124         assertEquals( "Wrong number of puts", 1, server.getPutCount() - numPutsPrior );
125 
126         // DO WORK
127         ICacheElement result = cache.get( "key" );
128 
129         // VERIFY
130         assertEquals( "Wrong element.", element.getVal(), result.getVal() );
131 
132         // DO WORK
133         cache.remove( "key" );
134         SleepUtil.sleepAtLeast( 50 );
135         ICacheElement resultAfterRemote = cache.get( "key" );
136 
137         // VERIFY
138         assertNull( "Element resultAfterRemote should be null.", resultAfterRemote );
139     }
140 
141     /***
142      * Register a listener with the server. Send an update. Verify that the listener received it.
143      * @throws Exception
144      */
145     public void testPutAndListen()
146         throws Exception
147     {
148         // SETUP
149         CompositeCacheManagerMockImpl compositeCacheManager = new CompositeCacheManagerMockImpl();
150 
151         RemoteCacheAttributes attributes = new RemoteCacheAttributes();
152         attributes.setRemoteHost( "localhost" );
153         attributes.setLocalPort( 1202 );
154         attributes.setRemotePort( 1101 );
155 
156         RemoteCacheManager remoteCacheManager = RemoteCacheManager.getInstance( attributes, compositeCacheManager );
157         String regionName = "testPutAndListen";
158         AuxiliaryCache cache = remoteCacheManager.getCache( regionName );
159 
160         RemoteCacheListenerMockImpl listener = new RemoteCacheListenerMockImpl();
161         server.addCacheListener( regionName, listener );
162 
163         // DO WORK
164         int numPutsPrior = server.getPutCount();
165         ICacheElement element = new CacheElement( regionName, "key", "value" );
166         cache.update( element );
167         SleepUtil.sleepAtLeast( 50 );
168 
169         // VERIFY
170         try
171         {
172             System.out.println( server.getStats() );
173             assertEquals( "Wrong number of puts", 1, server.getPutCount() - numPutsPrior );
174             assertEquals( "Wrong number of puts to listener.", 1, listener.putCount );
175         }
176         finally
177         {
178             // remove from all regions.
179             server.removeCacheListener( listener );
180         }
181     }
182 
183     /***
184      * Register a listener with the server. Send an update. Verify that the listener received it.
185      * @throws Exception
186      */
187     public void testPutaMultipleAndListen()
188         throws Exception
189     {
190         // SETUP
191         CompositeCacheManagerMockImpl compositeCacheManager = new CompositeCacheManagerMockImpl();
192 
193         RemoteCacheAttributes attributes = new RemoteCacheAttributes();
194         attributes.setRemoteHost( "localhost" );
195         attributes.setLocalPort( 1202 );
196         attributes.setRemotePort( 1101 );
197 
198         RemoteCacheManager remoteCacheManager = RemoteCacheManager.getInstance( attributes, compositeCacheManager );
199         String regionName = "testPutAndListen";
200         AuxiliaryCache cache = remoteCacheManager.getCache( regionName );
201 
202         RemoteCacheListenerMockImpl listener = new RemoteCacheListenerMockImpl();
203         server.addCacheListener( regionName, listener );
204 
205         // DO WORK
206         int numPutsPrior = server.getPutCount();
207         int numToPut = 100;
208         for ( int i = 0; i < numToPut; i++ )
209         {
210             ICacheElement element = new CacheElement( regionName, "key" + 1, "value" + i );
211             cache.update( element );
212         }
213         SleepUtil.sleepAtLeast( 500 );
214 
215         // VERIFY
216         System.out.println( server.getStats() );
217         assertEquals( "Wrong number of puts", numToPut, server.getPutCount() - numPutsPrior );
218         assertEquals( "Wrong number of puts to listener.", numToPut, listener.putCount );
219     }
220 }