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