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 * Tests the issue remove on put fuctionality.
32 *
33 * @author asmuts
34 */
35 public class LateralTCPIssueRemoveOnPutUnitTest
36 extends TestCase
37 {
38 private static boolean isSysOut = true;
39
40 /***
41 * Constructor for the TestDiskCache object.
42 *
43 * @param testName
44 */
45 public LateralTCPIssueRemoveOnPutUnitTest( String testName )
46 {
47 super( testName );
48 }
49
50 /***
51 * Test setup
52 */
53 public void setUp()
54 {
55 JCS.setConfigFilename( "/TestTCPLateralIssueRemoveCache.ccf" );
56 }
57
58 /***
59 *
60 * @throws Exception
61 */
62 public void testPutLocalPutRemoteGetBusyVerifyRemoved()
63 throws Exception
64 {
65 this.runTestForRegion( "region1", 1, 200, 1 );
66 }
67
68 /***
69 * Verify that a standard put works.
70 *
71 * Get the cache configured from a file. Create a tcp service to talk to
72 * that cache. Put via the servive. Verify that the cache got the data.
73 *
74 * @throws Exception
75 *
76 */
77 public void testStandardPut()
78 throws Exception
79 {
80 String region = "region1";
81
82 JCS cache = JCS.getInstance( region );
83
84 Thread.sleep( 100 );
85
86 TCPLateralCacheAttributes lattr2 = new TCPLateralCacheAttributes();
87 lattr2.setTcpListenerPort( 1102 );
88 lattr2.setTransmissionTypeName( "TCP" );
89 lattr2.setTcpServer( "localhost:1110" );
90 lattr2.setIssueRemoveOnPut( false );
91
92
93
94
95
96
97 LateralTCPService service = new LateralTCPService( lattr2 );
98 service.setListenerId( 123456 );
99
100 String keyToBeRemovedOnPut = "test1_notremoved";
101
102 ICacheElement element1 = new CacheElement( region, keyToBeRemovedOnPut, region
103 + ":data-this shouldn't get removed, it should get to the cache." );
104 service.update( element1 );
105
106 Thread.sleep( 1000 );
107
108 Object testObj = cache.get( keyToBeRemovedOnPut );
109 p( "testStandardPut, test object = " + testObj );
110 assertNotNull( "The test object should not have been removed by a put.", testObj );
111 }
112
113 /***
114 * This tests issues tons of puts. It also check to see that a key that was
115 * put in was removed by the clients remove command.
116 *
117 * @param region
118 * Name of the region to access
119 * @param range
120 * @param numOps
121 * @param testNum
122 *
123 * @exception Exception
124 * If an error occurs
125 */
126 public void runTestForRegion( String region, int range, int numOps, int testNum )
127 throws Exception
128 {
129
130 boolean show = true;
131
132 JCS cache = JCS.getInstance( region );
133
134 Thread.sleep( 100 );
135
136 TCPLateralCacheAttributes lattr2 = new TCPLateralCacheAttributes();
137 lattr2.setTcpListenerPort( 1102 );
138 lattr2.setTransmissionTypeName( "TCP" );
139 lattr2.setTcpServer( "localhost:1110" );
140 lattr2.setIssueRemoveOnPut( true );
141
142 lattr2.setAllowPut( false );
143
144
145
146
147 LateralTCPService service = new LateralTCPService( lattr2 );
148 service.setListenerId( 123456 );
149
150 String keyToBeRemovedOnPut = "test1";
151 cache.put( keyToBeRemovedOnPut, "this should get removed." );
152
153 ICacheElement element1 = new CacheElement( region, keyToBeRemovedOnPut, region
154 + ":data-this shouldn't get there" );
155 service.update( element1 );
156
157 try
158 {
159 for ( int i = 1; i < numOps; i++ )
160 {
161 Random ran = new Random( i );
162 int n = ran.nextInt( 4 );
163 int kn = ran.nextInt( range );
164 String key = "key" + kn;
165
166 ICacheElement element = new CacheElement( region, key, region + ":data" + i
167 + " junk asdfffffffadfasdfasf " + kn + ":" + n );
168 service.update( element );
169 if ( show )
170 {
171 p( "put " + key );
172 }
173
174 if ( i % 100 == 0 )
175 {
176 System.out.println( cache.getStats() );
177 }
178
179 }
180 p( "Finished cycle of " + numOps );
181 }
182 catch ( Exception e )
183 {
184 p( e.toString() );
185 e.printStackTrace( System.out );
186 throw e;
187 }
188
189 JCS jcs = JCS.getInstance( region );
190 String key = "testKey" + testNum;
191 String data = "testData" + testNum;
192 jcs.put( key, data );
193 String value = (String) jcs.get( key );
194 assertEquals( "Couldn't put normally.", data, value );
195
196
197 for ( int i = 1; i < numOps; i++ )
198 {
199 String keyL = "key" + i;
200 String dataL = (String) jcs.get( keyL );
201 if ( dataL != null )
202 {
203 assertTrue( "Incorrect region detected.", dataL.startsWith( region ) );
204 }
205
206 }
207
208 Thread.sleep( 200 );
209
210 Object testObj = cache.get( keyToBeRemovedOnPut );
211 p( "runTestForRegion, test object = " + testObj );
212 assertNull( "The test object should have been removed by a put.", testObj );
213
214 }
215
216 /***
217 * @param s
218 * String to be printed
219 */
220 public static void p( String s )
221 {
222 if ( isSysOut )
223 {
224 System.out.println( s );
225 }
226 }
227 }