View Javadoc
1   package org.apache.commons.jcs3.auxiliary.lateral.socket.tcp;
2   
3   import java.io.Serializable;
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   */
35  public class LateralTCPFilterRemoveHashCodeUnitTest
36      extends TestCase
37  {
38      /** Does the test print to system out. */
39      private static final boolean isSysOut = false;
40  
41      /** The port the server will listen to. */
42      private final int serverPort = 2001;
43  
44      /**
45       * Constructor for the TestDiskCache object.
46       *
47       * @param testName
48       */
49      public LateralTCPFilterRemoveHashCodeUnitTest( final String testName )
50      {
51          super( testName );
52      }
53  
54      /**
55       * Test setup
56       */
57      @Override
58      public void setUp()
59      {
60          System.setProperty( "jcs.auxiliary.LTCP.attributes.TcpServers", "localhost:" + serverPort );
61          JCS.setConfigFilename( "/TestTCPLateralRemoveFilter.ccf" );
62      }
63  
64      /**
65       *
66       * @throws Exception
67       */
68      public void test()
69          throws Exception
70      {
71          this.runTestForRegion( "region1", 200, 1 );
72      }
73  
74      /**
75       * This tests issues tons of puts. It also check to see that a key that was
76       * put in was removed by the clients remove command.
77       *
78       * @param region
79       *            Name of the region to access
80       * @param numOps
81       * @param testNum
82       *
83       * @throws Exception
84       *                If an error occurs
85       */
86      public void runTestForRegion( final String region, final int numOps, final int testNum )
87          throws Exception
88      {
89          final CacheAccess<String, Serializable> cache = JCS.getInstance( region );
90  
91          Thread.sleep( 100 );
92  
93          final TCPLateralCacheAttributes lattr2 = new TCPLateralCacheAttributes();
94          lattr2.setTcpListenerPort( 1102 );
95          lattr2.setTransmissionType(LateralCacheAttributes.Type.TCP);
96          lattr2.setTcpServer( "localhost:" + serverPort );
97          lattr2.setIssueRemoveOnPut( true );
98          // should still try to remove
99          lattr2.setAllowPut( false );
100 
101         // this service will put and remove using the lateral to
102         // the cache instance above
103         // the cache thinks it is different since the listenerid is different
104         final LateralTCPService<String, Serializable> service =
105                 new LateralTCPService<>(lattr2,  new StandardSerializer());
106         service.setListenerId( 123456 );
107 
108         final String keyToBeRemovedOnPut = "test1";
109 
110         final String keyToNotBeRemovedOnPut = "test2";
111 
112         final Serializable dataToPassHashCodeCompare = new Serializable()
113         {
114             private static final long serialVersionUID = 1L;
115 
116             @Override
117             public int hashCode()
118             {
119                 return 1;
120             }
121         };
122         //String dataToPassHashCodeCompare = "this should be the same and not
123         // get removed.";
124         //p( "dataToPassHashCodeCompare hashCode = " + +
125         // dataToPassHashCodeCompare.hashCode() );
126 
127         cache.put( keyToBeRemovedOnPut, "this should get removed." );
128         final ICacheElement<String, Serializable> element1 = new CacheElement<>( region, keyToBeRemovedOnPut, region
129             + ":data-this shouldn't get there" );
130         service.update( element1 );
131 
132         cache.put( keyToNotBeRemovedOnPut, dataToPassHashCodeCompare );
133         final ICacheElement<String, Serializable> element2 = new CacheElement<>( region, keyToNotBeRemovedOnPut, dataToPassHashCodeCompare );
134         service.update( element2 );
135 
136         /*
137          * try { for ( int i = 1; i < numOps; i++ ) { Random ran = new Random( i );
138          * int n = ran.nextInt( 4 ); int kn = ran.nextInt( range ); String key =
139          * "key" + kn;
140          *
141          * ICacheElement<String, String> element = new CacheElement( region, key, region +
142          * ":data" + i + " junk asdfffffffadfasdfasf " + kn + ":" + n );
143          * service.update( element ); if ( show ) { p( "put " + key ); }
144          *
145          * if ( i % 100 == 0 ) { System.out.println( cache.getStats() ); }
146          *  } p( "Finished cycle of " + numOps ); } catch ( Exception e ) { p(
147          * e.toString() ); e.printStackTrace( System.out ); throw e; }
148          */
149 
150         final CacheAccess<String, String> jcs = JCS.getInstance( region );
151         final String key = "testKey" + testNum;
152         final String data = "testData" + testNum;
153         jcs.put( key, data );
154         final String value = jcs.get( key );
155         assertEquals( "Couldn't put normally.", data, value );
156 
157         // make sure the items we can find are in the correct region.
158         for ( int i = 1; i < numOps; i++ )
159         {
160             final String keyL = "key" + i;
161             final String dataL = jcs.get( keyL );
162             if ( dataL != null )
163             {
164                 assertTrue( "Incorrect region detected.", dataL.startsWith( region ) );
165             }
166 
167         }
168 
169         Thread.sleep( 200 );
170 
171         final Object testObj1 = cache.get( keyToBeRemovedOnPut );
172         p( "test object1 = " + testObj1 );
173         assertNull( "The test object should have been remvoed by a put.", testObj1 );
174 
175         final Object testObj2 = cache.get( keyToNotBeRemovedOnPut );
176         p( "test object2 = " + testObj2 + " hashCode = " );
177         if ( testObj2 != null )
178         {
179             p( "test2 hashCode = " + +testObj2.hashCode() );
180         }
181         assertNotNull( "This should not have been removed, since the hascode were the same.", testObj2 );
182 
183     }
184 
185     /**
186      * @param s String to print
187      */
188     public static void p( final String s )
189     {
190         if ( isSysOut )
191         {
192             System.out.println( s );
193         }
194     }
195 }