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