1 package org.apache.jcs.auxiliary.lateral.socket.tcp;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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;
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
85
86
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
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
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
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
171
172
173
174
175
176
177
178
179
180
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
196
197
198
199 }
200 }
201 }