View Javadoc
1   package org.apache.commons.jcs3.utils.discovery;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Unit tests for discovery
31   */
32  public class UDPDiscoveryUnitTest
33      extends TestCase
34  {
35      /**
36       * <p>
37       * @throws Exception
38       */
39      public void testSimpleUDPDiscoveryIPv4()
40          throws Exception
41      {
42          simpleUDPDiscovery("228.5.6.7");
43      }
44  
45      /**
46       * <p>
47       * @throws Exception
48       */
49      public void testSimpleUDPDiscoveryIPv6()
50          throws Exception
51      {
52          simpleUDPDiscovery("FF02::5678");
53      }
54  
55      /**
56       * <p>
57       * @throws Exception
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); /* datagram TTL */
67  
68          // create the service
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          // create a receiver with the service
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          // create a sender
85          try (final UDPDiscoverySender sender = new UDPDiscoverySender(
86                  attributes, service.getSerializer()))
87          {
88              // create more names than we have no wait facades for
89              // the only one that gets added should be testCache1
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              // send max messages
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             // check to see that we got 10 messages
110             //System.out.println( "Receiver count = " + receiver.getCnt() );
111 
112             // request braodcasts change things.
113             assertTrue( "Receiver count [" + receiver.getCnt() + "] should be the at least the number sent [" + cnt + "].",
114                         cnt <= receiver.getCnt() );
115         }
116     }
117 }