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