1 package org.apache.commons.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 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
32
33 public class LateralTCPConcurrentRandomTestUtil
34 extends TestCase
35 {
36
37 private static boolean isSysOut = false;
38
39
40
41
42
43
44
45 public LateralTCPConcurrentRandomTestUtil( String testName )
46 {
47 super( testName );
48 }
49
50
51
52
53 @Override
54 public void setUp()
55 {
56 JCS.setConfigFilename( "/TestTCPLateralCacheConcurrent.ccf" );
57 }
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 public void runTestForRegion( String region, int range, int numOps, int testNum )
74 throws Exception
75 {
76 boolean show = true;
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
86
87
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
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
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
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
172
173
174
175
176
177
178
179
180
181
182
183 }
184
185
186
187
188 public static void p( String s )
189 {
190 if ( isSysOut )
191 {
192 System.out.println( s );
193 }
194 }
195 }