View Javadoc
1   package org.apache.commons.jcs3.auxiliary.lateral.socket.tcp;
2   
3   import java.util.Random;
4   
5   import org.apache.commons.jcs3.JCS;
6   import org.apache.commons.jcs3.access.CacheAccess;
7   import org.apache.commons.jcs3.auxiliary.lateral.LateralCacheAttributes;
8   import org.apache.commons.jcs3.engine.CacheElement;
9   import org.apache.commons.jcs3.engine.behavior.ICacheElement;
10  import org.apache.commons.jcs3.utils.serialization.StandardSerializer;
11  
12  /*
13   * Licensed to the Apache Software Foundation (ASF) under one
14   * or more contributor license agreements.  See the NOTICE file
15   * distributed with this work for additional information
16   * regarding copyright ownership.  The ASF licenses this file
17   * to you under the Apache License, Version 2.0 (the
18   * "License"); you may not use this file except in compliance
19   * with the License.  You may obtain a copy of the License at
20   *
21   *   http://www.apache.org/licenses/LICENSE-2.0
22   *
23   * Unless required by applicable law or agreed to in writing,
24   * software distributed under the License is distributed on an
25   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
26   * KIND, either express or implied.  See the License for the
27   * specific language governing permissions and limitations
28   * under the License.
29   */
30  
31  import junit.framework.TestCase;
32  
33  /**
34   * Tests the issue remove on put fuctionality.
35   */
36  public class LateralTCPIssueRemoveOnPutUnitTest
37      extends TestCase
38  {
39      /** Should log data go to system out. */
40      private static final boolean isSysOut = false;
41  
42      /** The port the server will listen to. */
43      private final int serverPort = 1118;
44  
45      /**
46       * Constructor for the TestDiskCache object.
47       * <p>
48       * @param testName
49       */
50      public LateralTCPIssueRemoveOnPutUnitTest( final String testName )
51      {
52          super( testName );
53      }
54  
55      /**
56       * Test setup
57       */
58      @Override
59      public void setUp()
60      {
61          System.setProperty( "jcs.auxiliary.LTCP.attributes.TcpServers", "localhost:" + serverPort );
62  
63          JCS.setConfigFilename( "/TestTCPLateralIssueRemoveCache.ccf" );
64      }
65  
66      /**
67       * @throws Exception
68       */
69      public void testPutLocalPutRemoteGetBusyVerifyRemoved()
70          throws Exception
71      {
72          this.runTestForRegion( "region1", 1, 200, 1 );
73      }
74  
75      /**
76       * Verify that a standard put works. Get the cache configured from a file. Create a tcp service
77       * to talk to that cache. Put via the service. Verify that the cache got the data.
78       * <p>
79       * @throws Exception
80       */
81      public void testStandardPut()
82          throws Exception
83      {
84          final String region = "region1";
85  
86          final CacheAccess<String, String> cache = JCS.getInstance( region );
87  
88          Thread.sleep( 100 );
89  
90          final TCPLateralCacheAttributes lattr2 = new TCPLateralCacheAttributes();
91          lattr2.setTcpListenerPort( 1102 );
92          lattr2.setTransmissionType(LateralCacheAttributes.Type.TCP);
93          lattr2.setTcpServer( "localhost:" + serverPort );
94          lattr2.setIssueRemoveOnPut( false );
95          // should still try to remove
96          // lattr2.setAllowPut( false );
97  
98          // Using the lateral, this service will put to and remove from
99          // the cache instance above.
100         // The cache thinks it is different since the listenerid is different
101         final LateralTCPService<String, String> service =
102                 new LateralTCPService<>(lattr2,  new StandardSerializer());
103         service.setListenerId( 123456 );
104 
105         final String keyToBeRemovedOnPut = "test1_notremoved";
106 
107         final ICacheElement<String, String> element1 = new CacheElement<>( region, keyToBeRemovedOnPut, region
108             + ":data-this shouldn't get removed, it should get to the cache." );
109         service.update( element1 );
110 
111         Thread.sleep( 1000 );
112 
113         final Object testObj = cache.get( keyToBeRemovedOnPut );
114         p( "testStandardPut, test object = " + testObj );
115         assertNotNull( "The test object should not have been removed by a put.", testObj );
116     }
117 
118     /**
119      * This tests issues tons of puts. It also check to see that a key that was put in was removed
120      * by the clients remove command.
121      * <p>
122      * @param region Name of the region to access
123      * @param range
124      * @param numOps
125      * @param testNum
126      * @throws Exception If an error occurs
127      */
128     public void runTestForRegion( final String region, final int range, final int numOps, final int testNum )
129         throws Exception
130     {
131         final CacheAccess<String, String> cache = JCS.getInstance( region );
132 
133         Thread.sleep( 100 );
134 
135         final TCPLateralCacheAttributes lattr2 = new TCPLateralCacheAttributes();
136         lattr2.setTcpListenerPort( 1102 );
137         lattr2.setTransmissionType(LateralCacheAttributes.Type.TCP);
138         lattr2.setTcpServer( "localhost:" + serverPort );
139         lattr2.setIssueRemoveOnPut( true );
140         // should still try to remove
141         lattr2.setAllowPut( false );
142 
143         // Using the lateral, this service will put to and remove from
144         // the cache instance above.
145         // The cache thinks it is different since the listenerid is different
146         final LateralTCPService<String, String> service =
147                 new LateralTCPService<>( lattr2,  new StandardSerializer());
148         service.setListenerId( 123456 );
149 
150         final String keyToBeRemovedOnPut = "test1";
151         cache.put( keyToBeRemovedOnPut, "this should get removed." );
152 
153         final ICacheElement<String, String> element1 = new CacheElement<>( region, keyToBeRemovedOnPut, region
154             + ":data-this shouldn't get there" );
155         service.update( element1 );
156 
157         try
158         {
159             for ( int i = 1; i < numOps; i++ )
160             {
161                 final Random ran = new Random( i );
162                 final int n = ran.nextInt( 4 );
163                 final int kn = ran.nextInt( range );
164                 final String key = "key" + kn;
165 
166                 final ICacheElement<String, String> element = new CacheElement<>( region, key, region + ":data" + i
167                     + " junk asdfffffffadfasdfasf " + kn + ":" + n );
168                 service.update( element );
169                 p("put " + key);
170 
171                 if (i % 100 == 0)
172                 {
173                     p(cache.getStats());
174                 }
175 
176             }
177             p("Finished cycle of " + numOps);
178         }
179         catch ( final Exception e )
180         {
181             p( e.toString() );
182             throw e;
183         }
184 
185         final CacheAccess<String, String> jcs = JCS.getInstance( region );
186         final String key = "testKey" + testNum;
187         final String data = "testData" + testNum;
188         jcs.put( key, data );
189         final String value = jcs.get( key );
190         assertEquals( "Couldn't put normally.", data, value );
191 
192         // make sure the items we can find are in the correct region.
193         for ( int i = 1; i < numOps; i++ )
194         {
195             final String keyL = "key" + i;
196             final String dataL = jcs.get( keyL );
197             if ( dataL != null )
198             {
199                 assertTrue( "Incorrect region detected.", dataL.startsWith( region ) );
200             }
201 
202         }
203 
204         Thread.sleep( 200 );
205 
206         final Object testObj = cache.get( keyToBeRemovedOnPut );
207         p( "runTestForRegion, test object = " + testObj );
208         assertNull( "The test object should have been removed by a put.", testObj );
209 
210     }
211 
212     /**
213      * @param s String to be printed
214      */
215     public static void p( final String s )
216     {
217         if ( isSysOut )
218         {
219             System.out.println( s );
220         }
221     }
222 }