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  /** Unit tests for the service. */
27  public class UDPDiscoveryServiceUnitTest
28      extends TestCase
29  {
30      /** Verify that the list is updated. */
31      public void testAddOrUpdateService_NotInList()
32      {
33          // SETUP
34          String host = "228.5.6.7";
35          int port = 6789;
36          UDPDiscoveryAttributes attributes = new UDPDiscoveryAttributes();
37          attributes.setUdpDiscoveryAddr( host );
38          attributes.setUdpDiscoveryPort( port );
39          attributes.setServicePort( 1000 );
40  
41          // create the service
42          UDPDiscoveryService service = new UDPDiscoveryService( attributes );
43          service.startup();
44          service.addParticipatingCacheName( "testCache1" );
45  
46          MockDiscoveryListener discoveryListener = new MockDiscoveryListener();
47          service.addDiscoveryListener( discoveryListener );
48  
49          DiscoveredService discoveredService = new DiscoveredService();
50          discoveredService.setServiceAddress( host );
51          discoveredService.setCacheNames( new ArrayList<String>() );
52          discoveredService.setServicePort( 1000 );
53          discoveredService.setLastHearFromTime( 100 );
54  
55          // DO WORK
56          service.addOrUpdateService( discoveredService );
57  
58          // VERIFY
59          assertTrue( "Service should be in the service list.", service.getDiscoveredServices()
60              .contains( discoveredService ) );
61          assertTrue( "Service should be in the listener list.", discoveryListener.discoveredServices
62              .contains( discoveredService ) );
63      }
64  
65      /** Verify that the list is updated. */
66      public void testAddOrUpdateService_InList_NamesDoNotChange()
67      {
68          // SETUP
69          String host = "228.5.6.7";
70          int port = 6789;
71          UDPDiscoveryAttributes attributes = new UDPDiscoveryAttributes();
72          attributes.setUdpDiscoveryAddr( host );
73          attributes.setUdpDiscoveryPort( port );
74          attributes.setServicePort( 1000 );
75  
76          // create the service
77          UDPDiscoveryService service = new UDPDiscoveryService( attributes );
78          service.startup();
79          service.addParticipatingCacheName( "testCache1" );
80  
81          MockDiscoveryListener discoveryListener = new MockDiscoveryListener();
82          service.addDiscoveryListener( discoveryListener );
83  
84          ArrayList<String> sametCacheNames = new ArrayList<String>();
85          sametCacheNames.add( "name1" );
86  
87          DiscoveredService discoveredService = new DiscoveredService();
88          discoveredService.setServiceAddress( host );
89          discoveredService.setCacheNames( sametCacheNames );
90          discoveredService.setServicePort( 1000 );
91          discoveredService.setLastHearFromTime( 100 );
92  
93  
94          DiscoveredService discoveredService2 = new DiscoveredService();
95          discoveredService2.setServiceAddress( host );
96          discoveredService2.setCacheNames( sametCacheNames );
97          discoveredService2.setServicePort( 1000 );
98          discoveredService2.setLastHearFromTime( 500 );
99  
100         // DO WORK
101         service.addOrUpdateService( discoveredService );
102         // again
103         service.addOrUpdateService( discoveredService2 );
104 
105         // VERIFY
106         assertEquals( "Should only be one in the set.", 1, service.getDiscoveredServices().size() );
107         assertTrue( "Service should be in the service list.", service.getDiscoveredServices()
108             .contains( discoveredService ) );
109         assertTrue( "Service should be in the listener list.", discoveryListener.discoveredServices
110             .contains( discoveredService ) );
111 
112         // need to update the time this sucks. add has no effect convert to a map
113         for (DiscoveredService service1 : service.getDiscoveredServices())
114         {
115             if ( discoveredService.equals( service1 ) )
116             {
117                 assertEquals( "The match should have the new last heard from time.", service1.getLastHearFromTime(),
118                               discoveredService2.getLastHearFromTime() );
119             }
120         }
121         // the mock has a list from all add calls.
122         // it should have been called when the list changed.
123         //assertEquals( "Mock should have been called once.", 1, discoveryListener.discoveredServices.size() );
124         // logic changed.  it's called every time.
125         assertEquals( "Mock should have been called twice.", 2, discoveryListener.discoveredServices.size() );
126     }
127 
128     /** Verify that the list is updated. */
129     public void testAddOrUpdateService_InList_NamesChange()
130     {
131         // SETUP
132         String host = "228.5.6.7";
133         int port = 6789;
134         UDPDiscoveryAttributes attributes = new UDPDiscoveryAttributes();
135         attributes.setUdpDiscoveryAddr( host );
136         attributes.setUdpDiscoveryPort( port );
137         attributes.setServicePort( 1000 );
138 
139         // create the service
140         UDPDiscoveryService service = new UDPDiscoveryService( attributes );
141         service.startup();
142         service.addParticipatingCacheName( "testCache1" );
143 
144         MockDiscoveryListener discoveryListener = new MockDiscoveryListener();
145         service.addDiscoveryListener( discoveryListener );
146 
147         DiscoveredService discoveredService = new DiscoveredService();
148         discoveredService.setServiceAddress( host );
149         discoveredService.setCacheNames( new ArrayList<String>() );
150         discoveredService.setServicePort( 1000 );
151         discoveredService.setLastHearFromTime( 100 );
152 
153         ArrayList<String> differentCacheNames = new ArrayList<String>();
154         differentCacheNames.add( "name1" );
155         DiscoveredService discoveredService2 = new DiscoveredService();
156         discoveredService2.setServiceAddress( host );
157         discoveredService2.setCacheNames( differentCacheNames );
158         discoveredService2.setServicePort( 1000 );
159         discoveredService2.setLastHearFromTime( 500 );
160 
161         // DO WORK
162         service.addOrUpdateService( discoveredService );
163         // again
164         service.addOrUpdateService( discoveredService2 );
165 
166         // VERIFY
167         assertEquals( "Should only be one in the set.", 1, service.getDiscoveredServices().size() );
168         assertTrue( "Service should be in the service list.", service.getDiscoveredServices()
169             .contains( discoveredService ) );
170         assertTrue( "Service should be in the listener list.", discoveryListener.discoveredServices
171             .contains( discoveredService ) );
172 
173         // need to update the time this sucks. add has no effect convert to a map
174         for (DiscoveredService service1 : service.getDiscoveredServices())
175         {
176             if ( discoveredService.equals( service1 ) )
177             {
178                 assertEquals( "The match should have the new last heard from time.", service1.getLastHearFromTime(),
179                               discoveredService2.getLastHearFromTime() );
180                 assertEquals( "The names should be updated.", service1.getCacheNames() + "", differentCacheNames + "" );
181             }
182         }
183         // the mock has a list from all add calls.
184         // it should have been called when the list changed.
185         assertEquals( "Mock should have been called twice.", 2, discoveryListener.discoveredServices.size() );
186         assertEquals( "The second mock listener add should be discoveredService2", discoveredService2,
187                       discoveryListener.discoveredServices.get( 1 ) );
188     }
189 
190     /** Verify that the list is updated. */
191     public void testRemoveDiscoveredService()
192     {
193         // SETUP
194         String host = "228.5.6.7";
195         int port = 6789;
196         UDPDiscoveryAttributes attributes = new UDPDiscoveryAttributes();
197         attributes.setUdpDiscoveryAddr( host );
198         attributes.setUdpDiscoveryPort( port );
199         attributes.setServicePort( 1000 );
200 
201         // create the service
202         UDPDiscoveryService service = new UDPDiscoveryService( attributes );
203         service.startup();
204         service.addParticipatingCacheName( "testCache1" );
205 
206         MockDiscoveryListener discoveryListener = new MockDiscoveryListener();
207         service.addDiscoveryListener( discoveryListener );
208 
209         DiscoveredService discoveredService = new DiscoveredService();
210         discoveredService.setServiceAddress( host );
211         discoveredService.setCacheNames( new ArrayList<String>() );
212         discoveredService.setServicePort( 1000 );
213         discoveredService.setLastHearFromTime( 100 );
214 
215         service.addOrUpdateService( discoveredService );
216 
217         // DO WORK
218         service.removeDiscoveredService( discoveredService );
219 
220         // VERIFY
221         assertFalse( "Service should not be in the service list.", service.getDiscoveredServices()
222             .contains( discoveredService ) );
223         assertFalse( "Service should not be in the listener list.", discoveryListener.discoveredServices
224             .contains( discoveredService ) );
225     }
226 }