View Javadoc
1   package org.apache.commons.jcs.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 junit.framework.TestCase;
25  
26  import org.apache.commons.jcs.utils.timing.SleepUtil;
27  
28  /**
29   * Unit tests for discovery
30   */
31  public class UDPDiscoveryUnitTest
32      extends TestCase
33  {
34      /**
35       * <p>
36       * @throws Exception
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          // create the service
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          // create a receiver with the service
55          UDPDiscoveryReceiver receiver = new UDPDiscoveryReceiver( service, attributes.getUdpDiscoveryAddr(), attributes
56              .getUdpDiscoveryPort() );
57          Thread t = new Thread( receiver );
58          t.start();
59  
60          // create a sender
61          UDPDiscoverySender sender = new UDPDiscoverySender( attributes.getUdpDiscoveryAddr(), attributes
62              .getUdpDiscoveryPort() );
63  
64          // create more names than we have no wait facades for
65          // the only one that gets added should be testCache1
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          // send max messages
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          // check to see that we got 10 messages
86          //System.out.println( "Receiver count = " + receiver.getCnt() );
87  
88          // request braodcasts change things.
89          assertTrue( "Receiver count [" + receiver.getCnt() + "] should be the at least the number sent [" + cnt + "].",
90                      cnt <= receiver.getCnt() );
91      }
92  }