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.util.Random;
29  
30  /**
31   * Tests the issue remove on put fuctionality.
32   * @author asmuts
33   */
34  public class LateralTCPIssueRemoveOnPutUnitTest
35      extends TestCase
36  {
37      /** Should log data go to system out. */
38      private static boolean isSysOut = false;
39  
40      /** The port the server will listen to. */
41      private final int serverPort = 1118;
42  
43      /**
44       * Constructor for the TestDiskCache object.
45       * <p>
46       * @param testName
47       */
48      public LateralTCPIssueRemoveOnPutUnitTest( String testName )
49      {
50          super( testName );
51      }
52  
53      /**
54       * Test setup
55       */
56      @Override
57      public void setUp()
58      {
59          System.setProperty( "jcs.auxiliary.LTCP.attributes.TcpServers", "localhost:" + serverPort );
60  
61          JCS.setConfigFilename( "/TestTCPLateralIssueRemoveCache.ccf" );
62      }
63  
64      /**
65       * @throws Exception
66       */
67      public void testPutLocalPutRemoteGetBusyVerifyRemoved()
68          throws Exception
69      {
70          this.runTestForRegion( "region1", 1, 200, 1 );
71      }
72  
73      /**
74       * Verify that a standard put works. Get the cache configured from a file. Create a tcp service
75       * to talk to that cache. Put via the service. Verify that the cache got the data.
76       * <p>
77       * @throws Exception
78       */
79      public void testStandardPut()
80          throws Exception
81      {
82          String region = "region1";
83  
84          CacheAccess<String, String> cache = JCS.getInstance( region );
85  
86          Thread.sleep( 100 );
87  
88          TCPLateralCacheAttributes lattr2 = new TCPLateralCacheAttributes();
89          lattr2.setTcpListenerPort( 1102 );
90          lattr2.setTransmissionTypeName( "TCP" );
91          lattr2.setTcpServer( "localhost:" + serverPort );
92          lattr2.setIssueRemoveOnPut( false );
93          // should still try to remove
94          // lattr2.setAllowPut( false );
95  
96          // Using the lateral, this service will put to and remove from
97          // the cache instance above.
98          // The cache thinks it is different since the listenerid is different
99          LateralTCPService<String, String> service = new LateralTCPService<String, String>( lattr2 );
100         service.setListenerId( 123456 );
101 
102         String keyToBeRemovedOnPut = "test1_notremoved";
103 
104         ICacheElement<String, String> element1 = new CacheElement<String, String>( region, keyToBeRemovedOnPut, region
105             + ":data-this shouldn't get removed, it should get to the cache." );
106         service.update( element1 );
107 
108         Thread.sleep( 1000 );
109 
110         Object testObj = cache.get( keyToBeRemovedOnPut );
111         p( "testStandardPut, test object = " + testObj );
112         assertNotNull( "The test object should not have been removed by a put.", testObj );
113     }
114 
115     /**
116      * This tests issues tons of puts. It also check to see that a key that was put in was removed
117      * by the clients remove command.
118      * <p>
119      * @param region Name of the region to access
120      * @param range
121      * @param numOps
122      * @param testNum
123      * @throws Exception If an error occurs
124      */
125     public void runTestForRegion( String region, int range, int numOps, int testNum )
126         throws Exception
127     {
128 
129         boolean show = false;
130 
131         CacheAccess<String, String> cache = JCS.getInstance( region );
132 
133         Thread.sleep( 100 );
134 
135         TCPLateralCacheAttributes lattr2 = new TCPLateralCacheAttributes();
136         lattr2.setTcpListenerPort( 1102 );
137         lattr2.setTransmissionTypeName( "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         LateralTCPService<String, String> service = new LateralTCPService<String, String>( lattr2 );
147         service.setListenerId( 123456 );
148 
149         String keyToBeRemovedOnPut = "test1";
150         cache.put( keyToBeRemovedOnPut, "this should get removed." );
151 
152         ICacheElement<String, String> element1 = new CacheElement<String, String>( region, keyToBeRemovedOnPut, region
153             + ":data-this shouldn't get there" );
154         service.update( element1 );
155 
156         try
157         {
158             for ( int i = 1; i < numOps; i++ )
159             {
160                 Random ran = new Random( i );
161                 int n = ran.nextInt( 4 );
162                 int kn = ran.nextInt( range );
163                 String key = "key" + kn;
164 
165                 ICacheElement<String, String> element = new CacheElement<String, String>( region, key, region + ":data" + i
166                     + " junk asdfffffffadfasdfasf " + kn + ":" + n );
167                 service.update( element );
168                 if ( show )
169                 {
170                     p( "put " + key );
171                 }
172 
173                 if (show && i % 100 == 0 )
174                 {
175                     System.out.println( cache.getStats() );
176                 }
177 
178             }
179             p( "Finished cycle of " + numOps );
180         }
181         catch ( Exception e )
182         {
183             p( e.toString() );
184             e.printStackTrace( System.out );
185             throw e;
186         }
187 
188         CacheAccess<String, String> jcs = JCS.getInstance( region );
189         String key = "testKey" + testNum;
190         String data = "testData" + testNum;
191         jcs.put( key, data );
192         String value = jcs.get( key );
193         assertEquals( "Couldn't put normally.", data, value );
194 
195         // make sure the items we can find are in the correct region.
196         for ( int i = 1; i < numOps; i++ )
197         {
198             String keyL = "key" + i;
199             String dataL = jcs.get( keyL );
200             if ( dataL != null )
201             {
202                 assertTrue( "Incorrect region detected.", dataL.startsWith( region ) );
203             }
204 
205         }
206 
207         Thread.sleep( 200 );
208 
209         Object testObj = cache.get( keyToBeRemovedOnPut );
210         p( "runTestForRegion, test object = " + testObj );
211         assertNull( "The test object should have been removed by a put.", testObj );
212 
213     }
214 
215     /**
216      * @param s String to be printed
217      */
218     public static void p( String s )
219     {
220         if ( isSysOut )
221         {
222             System.out.println( s );
223         }
224     }
225 }