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