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