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