1 package org.apache.commons.jcs.utils.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.commons.jcs.utils.timing.SleepUtil;
27
28
29
30
31 public class UDPDiscoveryUnitTest
32 extends TestCase
33 {
34
35
36
37
38 public void testSimpleUDPDiscovery()
39 throws Exception
40 {
41 UDPDiscoveryAttributes attributes = new UDPDiscoveryAttributes();
42 attributes.setUdpDiscoveryAddr( "228.5.6.7" );
43 attributes.setUdpDiscoveryPort( 6789 );
44 attributes.setServicePort( 1000 );
45
46
47 UDPDiscoveryService service = new UDPDiscoveryService( attributes );
48 service.startup();
49 service.addParticipatingCacheName( "testCache1" );
50
51 MockDiscoveryListener discoveryListener = new MockDiscoveryListener();
52 service.addDiscoveryListener( discoveryListener );
53
54
55 UDPDiscoveryReceiver receiver = new UDPDiscoveryReceiver( service, attributes.getUdpDiscoveryAddr(), attributes
56 .getUdpDiscoveryPort() );
57 Thread t = new Thread( receiver );
58 t.start();
59
60
61 UDPDiscoverySender sender = new UDPDiscoverySender( attributes.getUdpDiscoveryAddr(), attributes
62 .getUdpDiscoveryPort() );
63
64
65
66 ArrayList<String> cacheNames = new ArrayList<String>();
67 int numJunk = 10;
68 for ( int i = 0; i < numJunk; i++ )
69 {
70 cacheNames.add( "junkCacheName" + i );
71 }
72 cacheNames.add( "testCache1" );
73
74
75 int max = 10;
76 int cnt = 0;
77 for ( ; cnt < max; cnt++ )
78 {
79 sender.passiveBroadcast( "localhost", 1111, cacheNames, 1 );
80 SleepUtil.sleepAtLeast( 20 );
81 }
82
83 SleepUtil.sleepAtLeast( 200 );
84
85
86
87
88
89 assertTrue( "Receiver count [" + receiver.getCnt() + "] should be the at least the number sent [" + cnt + "].",
90 cnt <= receiver.getCnt() );
91 }
92 }