1   package org.apache.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 java.util.Random;
23  
24  import junit.framework.TestCase;
25  
26  import org.apache.jcs.JCS;
27  import org.apache.jcs.engine.CacheElement;
28  import org.apache.jcs.engine.behavior.ICacheElement;
29  
30  /***
31   * @author asmuts
32   */
33  public class LateralTCPConcurrentRandomTestUtil
34      extends TestCase
35  {
36  
37      private static boolean isSysOut = false;
38      //private static boolean isSysOut = true;
39  
40      /***
41       * Constructor for the TestDiskCache object.
42       *
43       * @param testName
44       */
45      public LateralTCPConcurrentRandomTestUtil( String testName )
46      {
47          super( testName );
48      }
49  
50      /***
51       * Test setup
52       */
53      public void setUp()
54      {
55          JCS.setConfigFilename( "/TestTCPLateralCacheConcurrent.ccf" );
56      }
57  
58      /***
59       * Randomly adds items to cache, gets them, and removes them. The range
60       * count is more than the size of the memory cache, so items should spool to
61       * disk.
62       * <p>
63       * @param region
64       *            Name of the region to access
65       * @param range
66       * @param numOps
67       * @param testNum
68       *
69       * @exception Exception
70       *                If an error occurs
71       */
72      public void runTestForRegion( String region, int range, int numOps, int testNum )
73          throws Exception
74      {
75          boolean show = true;//false;
76  
77          JCS cache = JCS.getInstance( region );
78  
79          TCPLateralCacheAttributes lattr2 = new TCPLateralCacheAttributes();
80          lattr2.setTcpListenerPort( 1103 );
81          lattr2.setTransmissionTypeName( "TCP" );
82          lattr2.setTcpServer( "localhost:1102" );
83  
84          // this service will put and remove using the lateral to
85          // the cache instance above
86          // the cache thinks it is different since the listenerid is different
87          LateralTCPService service = new LateralTCPService( lattr2 );
88          service.setListenerId( 123456 );
89  
90          try
91          {
92              for ( int i = 1; i < numOps; i++ )
93              {
94                  Random ran = new Random( i );
95                  int n = ran.nextInt( 4 );
96                  int kn = ran.nextInt( range );
97                  String key = "key" + kn;
98                  if ( n == 1 )
99                  {
100                     ICacheElement element = new CacheElement( region, key, region + ":data" + i
101                         + " junk asdfffffffadfasdfasf " + kn + ":" + n );
102                     service.update( element );
103                     if ( show )
104                     {
105                         p( "put " + key );
106                     }
107                 }
108                 /***/
109                 else if ( n == 2 )
110                 {
111                     service.remove( region, key );
112                     if ( show )
113                     {
114                         p( "removed " + key );
115                     }
116                 }
117                 /***/
118                 else
119                 {
120                     // slightly greater chance of get
121                     try
122                     {
123                         Object obj = service.get( region, key );
124                         if ( show && obj != null )
125                         {
126                             p( obj.toString() );
127                         }
128                     }
129                     catch ( Exception e )
130                     {
131                         // consider failing, some timeouts are expected
132                         e.printStackTrace();
133                     }
134                 }
135 
136                 if ( i % 100 == 0 )
137                 {
138                     System.out.println( cache.getStats() );
139                 }
140 
141             }
142             p( "Finished random cycle of " + numOps );
143         }
144         catch ( Exception e )
145         {
146             p( e.toString() );
147             e.printStackTrace( System.out );
148             throw e;
149         }
150 
151         JCS jcs = JCS.getInstance( region );
152         String key = "testKey" + testNum;
153         String data = "testData" + testNum;
154         jcs.put( key, data );
155         String value = (String) jcs.get( key );
156         assertEquals( "Couldn't put normally.", data, value );
157 
158         // make sure the items we can find are in the correct region.
159         for ( int i = 1; i < numOps; i++ )
160         {
161             String keyL = "key" + i;
162             String dataL = (String) jcs.get( keyL );
163             if ( dataL != null )
164             {
165                 assertTrue( "Incorrect region detected.", dataL.startsWith( region ) );
166             }
167 
168         }
169 
170         //Thread.sleep( 1000 );
171 
172         //ICacheElement element = new CacheElement( region, "abc", "testdata");
173         //service.update( element );
174 
175         //Thread.sleep( 2500 );
176         // could be too mcuh going on right now to get ti through, sot he test
177         // might fail.
178         //String value2 = (String) jcs.get( "abc" );
179         //assertEquals( "Couldn't put laterally, could be too much traffic in
180         // queue.", "testdata", value2 );
181 
182     }
183 
184     /***
185      * @param s string to print
186      */
187     public static void p( String s )
188     {
189         if ( isSysOut )
190         {
191             System.out.println( s );
192         }
193         else
194         {
195             //if ( log.isInfoEnabled() )
196             //{
197             //    log.info( s );
198             //}
199         }
200     }
201 }