1 package org.apache.commons.jcs3.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 org.apache.commons.jcs3.utils.serialization.StandardSerializer;
25 import org.apache.commons.jcs3.utils.timing.SleepUtil;
26
27 import junit.framework.TestCase;
28
29
30
31
32 public class UDPDiscoveryUnitTest
33 extends TestCase
34 {
35
36
37
38
39 public void testSimpleUDPDiscoveryIPv4()
40 throws Exception
41 {
42 simpleUDPDiscovery("228.5.6.7");
43 }
44
45
46
47
48
49 public void testSimpleUDPDiscoveryIPv6()
50 throws Exception
51 {
52 simpleUDPDiscovery("FF02::5678");
53 }
54
55
56
57
58
59 private void simpleUDPDiscovery(String discoveryAddress)
60 throws Exception
61 {
62 final UDPDiscoveryAttributes attributes = new UDPDiscoveryAttributes();
63 attributes.setUdpDiscoveryAddr(discoveryAddress);
64 attributes.setUdpDiscoveryPort(6789);
65 attributes.setServicePort(1000);
66 attributes.setUdpTTL(4);
67
68
69 final UDPDiscoveryService service = new UDPDiscoveryService(attributes, new StandardSerializer());
70 service.startup();
71 service.addParticipatingCacheName( "testCache1" );
72
73 final MockDiscoveryListener discoveryListener = new MockDiscoveryListener();
74 service.addDiscoveryListener( discoveryListener );
75
76
77 final UDPDiscoveryReceiver receiver = new UDPDiscoveryReceiver( service,
78 null,
79 attributes.getUdpDiscoveryAddr(),
80 attributes.getUdpDiscoveryPort() );
81 final Thread t = new Thread( receiver );
82 t.start();
83
84
85 try (final UDPDiscoverySender sender = new UDPDiscoverySender(
86 attributes, service.getSerializer()))
87 {
88
89
90 final ArrayList<String> cacheNames = new ArrayList<>();
91 final int numJunk = 10;
92 for ( int i = 0; i < numJunk; i++ )
93 {
94 cacheNames.add( "junkCacheName" + i );
95 }
96 cacheNames.add( "testCache1" );
97
98
99 final int max = 10;
100 int cnt = 0;
101 for ( ; cnt < max; cnt++ )
102 {
103 sender.passiveBroadcast( "localhost", 1111, cacheNames, 1 );
104 SleepUtil.sleepAtLeast( 20 );
105 }
106
107 SleepUtil.sleepAtLeast( 200 );
108
109
110
111
112
113 assertTrue( "Receiver count [" + receiver.getCnt() + "] should be the at least the number sent [" + cnt + "].",
114 cnt <= receiver.getCnt() );
115 }
116 }
117 }