1 package org.apache.jcs.auxiliary.lateral.socket.tcp.discovery;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23
24 import junit.framework.TestCase;
25
26 import org.apache.jcs.JCS;
27 import org.apache.jcs.auxiliary.lateral.LateralCache;
28 import org.apache.jcs.auxiliary.lateral.LateralCacheAttributes;
29 import org.apache.jcs.auxiliary.lateral.LateralCacheNoWait;
30 import org.apache.jcs.auxiliary.lateral.LateralCacheNoWaitFacade;
31 import org.apache.jcs.auxiliary.lateral.behavior.ILateralCacheAttributes;
32 import org.apache.jcs.auxiliary.lateral.socket.tcp.TCPLateralCacheAttributes;
33 import org.apache.jcs.engine.behavior.ICompositeCacheManager;
34 import org.apache.jcs.engine.control.CompositeCacheManager;
35
36 /***
37 *
38 * @author Aaron Smuts
39 *
40 */
41 public class UDPDiscoveryUnitTest
42 extends TestCase
43 {
44
45 /***
46 * Test setup
47 */
48 public void setUp()
49 {
50 JCS.setConfigFilename( "/TestUDPDiscovery.ccf" );
51 }
52
53 /***
54 * 1. create the attributes for the service
55 * <p>
56 * 2. create the service
57 * <p>
58 * 3. create a no wait facade for the service
59 * <p>
60 * 4. add the facade to the service under the name testCache1
61 * <p>
62 * 5. create a receiver with the service
63 * <p>
64 * 6. create a sender
65 * <p>
66 * 7.create more names than we have no wait facades for the only one that
67 * gets added should be testCache1
68 * <p>
69 * 8. send 10 messages
70 * <p>
71 * 9. check to see that we got 10 messages
72 * <p>
73 * 10. check to see if the testCache1 facade got a nowait.
74 *
75 * @throws Exception
76 */
77 public void testSimpleUDPDiscovery()
78 throws Exception
79 {
80
81 TCPLateralCacheAttributes lac = new TCPLateralCacheAttributes();
82 lac.setTransmissionType( LateralCacheAttributes.TCP );
83 lac.setTcpServer( "localhost" + ":" + 1111 );
84
85 ICompositeCacheManager cacheMgr = CompositeCacheManager.getInstance();
86
87
88 UDPDiscoveryService service = new UDPDiscoveryService( lac.getUdpDiscoveryAddr(), lac.getUdpDiscoveryPort(), lac.getTcpListenerPort(), cacheMgr );
89 service.setTcpLateralCacheAttributes( lac );
90
91
92 ArrayList noWaits = new ArrayList();
93 ILateralCacheAttributes attr = new LateralCacheAttributes();
94 attr.setCacheName( "testCache1" );
95
96 LateralCacheNoWaitFacade lcnwf = new LateralCacheNoWaitFacade( (LateralCacheNoWait[]) noWaits
97 .toArray( new LateralCacheNoWait[0] ), attr );
98
99
100 service.addNoWaitFacade( lcnwf, "testCache1" );
101
102
103 UDPDiscoveryReceiver receiver = new UDPDiscoveryReceiver( service, "228.5.6.7", 6789, cacheMgr );
104 Thread t = new Thread( receiver );
105 t.start();
106
107
108 UDPDiscoverySender sender = new UDPDiscoverySender( "228.5.6.7", 6789 );
109
110
111
112 ArrayList cacheNames = new ArrayList();
113 int numJunk = 10;
114 for ( int i = 0; i < numJunk; i++ )
115 {
116 cacheNames.add( "junkCacheName" + i );
117 }
118 cacheNames.add( "testCache1" );
119
120
121 int max = 10;
122 int cnt = 0;
123 for ( ; cnt < max; cnt++ )
124 {
125 sender.passiveBroadcast( "localhost", 1111, cacheNames, 1 );
126 Thread.sleep( 3 );
127 }
128
129
130 System.out.println( "Receiver count = " + receiver.getCnt() );
131
132
133
134
135 assertTrue( "Receiver count should be the at least the number sent.", cnt <= receiver.getCnt() );
136
137 Thread.sleep( 2000 );
138
139
140 assertEquals( "Should have 1", 1, lcnwf.noWaits.length );
141
142
143
144
145
146
147
148
149 }
150
151 /***
152 * Verify that the config does not throw any errors.
153 *
154 * @throws Exception
155 */
156 public void testUDPDiscoveryConfig()
157 throws Exception
158 {
159 JCS jcs = JCS.getInstance( "testCache1" );
160
161 System.out.println( jcs.getStats() );
162
163 JCS jcs2 = JCS.getInstance( "testCache2" );
164
165 System.out.println( jcs2.getStats() );
166
167 }
168
169 /***
170 * Make sure the no wait facade doesn't add dupes.
171 * <p>
172 * @throws Exception
173 */
174 public void testNoWaitFacadeAdd()
175 throws Exception
176 {
177 ArrayList noWaits = new ArrayList();
178 ILateralCacheAttributes attr = new LateralCacheAttributes();
179 attr.setCacheName( "testCache1" );
180 LateralCacheNoWaitFacade lcnwf = new LateralCacheNoWaitFacade( (LateralCacheNoWait[]) noWaits
181 .toArray( new LateralCacheNoWait[0] ), attr );
182
183 TCPLateralCacheAttributes lac = new TCPLateralCacheAttributes();
184 lac.setTransmissionType( LateralCacheAttributes.TCP );
185 lac.setTcpServer( "localhost" + ":" + 1111 );
186
187 LateralCache cache = new MockLateralCache( lac );
188
189
190 LateralCacheNoWait noWait = new LateralCacheNoWait( cache );
191 lcnwf.addNoWait( noWait );
192 assertEquals( "Facade should have 1 no wait", 1, lcnwf.noWaits.length );
193
194
195 LateralCacheNoWait noWait2 = new LateralCacheNoWait( cache );
196 lcnwf.addNoWait( noWait2 );
197 assertEquals( "Facade should have 2 no waits", 2, lcnwf.noWaits.length );
198
199
200 lcnwf.addNoWait( noWait2 );
201 assertEquals( "Facade should still have 2 no waits", 2, lcnwf.noWaits.length );
202
203 }
204 }