1   package org.apache.jcs.auxiliary.lateral.socket.tcp.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.jcs.JCS;
27  import org.apache.jcs.auxiliary.lateral.LateralCache;
28  import org.apache.jcs.auxiliary.lateral.LateralCacheAttributes;
29  import org.apache.jcs.auxiliary.lateral.LateralCacheNoWait;
30  import org.apache.jcs.auxiliary.lateral.LateralCacheNoWaitFacade;
31  import org.apache.jcs.auxiliary.lateral.behavior.ILateralCacheAttributes;
32  import org.apache.jcs.auxiliary.lateral.socket.tcp.TCPLateralCacheAttributes;
33  import org.apache.jcs.engine.behavior.ICompositeCacheManager;
34  import org.apache.jcs.engine.control.CompositeCacheManager;
35  
36  /***
37   *
38   * @author Aaron Smuts
39   *
40   */
41  public class UDPDiscoveryUnitTest
42      extends TestCase
43  {
44  
45      /***
46       * Test setup
47       */
48      public void setUp()
49      {
50          JCS.setConfigFilename( "/TestUDPDiscovery.ccf" );
51      }
52  
53      /***
54       * 1. create the attributes for the service
55       * <p>
56       * 2. create the service
57       * <p>
58       * 3. create a no wait facade for the service
59       * <p>
60       * 4. add the facade to the service under the name testCache1
61       * <p>
62       * 5. create a receiver with the service
63       * <p>
64       * 6. create a sender
65       * <p>
66       * 7.create more names than we have no wait facades for the only one that
67       * gets added should be testCache1
68       * <p>
69       * 8. send 10 messages
70       * <p>
71       * 9. check to see that we got 10 messages
72       * <p>
73       * 10. check to see if the testCache1 facade got a nowait.
74       *
75       * @throws Exception
76       */
77      public void testSimpleUDPDiscovery()
78          throws Exception
79      {
80          // create the attributes for the service
81          TCPLateralCacheAttributes lac = new TCPLateralCacheAttributes();
82          lac.setTransmissionType( LateralCacheAttributes.TCP );
83          lac.setTcpServer( "localhost" + ":" + 1111 );
84  
85          ICompositeCacheManager cacheMgr = CompositeCacheManager.getInstance();
86  
87          // create the service
88          UDPDiscoveryService service = new UDPDiscoveryService( lac.getUdpDiscoveryAddr(), lac.getUdpDiscoveryPort(), lac.getTcpListenerPort(), cacheMgr );
89          service.setTcpLateralCacheAttributes( lac );
90  
91          // create a no wait facade for the service
92          ArrayList noWaits = new ArrayList();
93          ILateralCacheAttributes attr = new LateralCacheAttributes();
94          attr.setCacheName( "testCache1" );
95  
96          LateralCacheNoWaitFacade lcnwf = new LateralCacheNoWaitFacade( (LateralCacheNoWait[]) noWaits
97              .toArray( new LateralCacheNoWait[0] ), attr );
98  
99          // add the facade to the service under the name testCache1
100         service.addNoWaitFacade( lcnwf, "testCache1" );
101 
102         // create a receiver with the service
103         UDPDiscoveryReceiver receiver = new UDPDiscoveryReceiver( service, "228.5.6.7", 6789, cacheMgr );
104         Thread t = new Thread( receiver );
105         t.start();
106 
107         // create a sender
108         UDPDiscoverySender sender = new UDPDiscoverySender( "228.5.6.7", 6789 );
109 
110         // create more names than we have no wait facades for
111         // the only one that gets added should be testCache1
112         ArrayList cacheNames = new ArrayList();
113         int numJunk = 10;
114         for ( int i = 0; i < numJunk; i++ )
115         {
116             cacheNames.add( "junkCacheName" + i );
117         }
118         cacheNames.add( "testCache1" );
119 
120         // send max messages
121         int max = 10;
122         int cnt = 0;
123         for ( ; cnt < max; cnt++ )
124         {
125             sender.passiveBroadcast( "localhost", 1111, cacheNames, 1 );
126             Thread.sleep( 3 );
127         }
128 
129         // check to see that we got 10 messages
130         System.out.println( "Receiver count = " + receiver.getCnt() );
131         //assertEquals( "Receiver count should be the same as the number
132         // sent.", cnt, receiver.getCnt() );
133 
134         // request braodcasts change things.
135         assertTrue( "Receiver count should be the at least the number sent.", cnt <= receiver.getCnt() );
136 
137         Thread.sleep( 2000 );
138 
139         // check to see if the testCache1 facade got a nowait.
140         assertEquals( "Should have 1", 1, lcnwf.noWaits.length );
141 
142         //ArrayList cacheNames2 = new ArrayList();
143         //cacheNames2.add( "testCache1" );
144         // add another
145         //sender.passiveBroadcast( "localhost", 11112, cacheNames2, 1 );
146         //Thread.sleep( 30 );
147         //assertEquals( "Should have 2", 2, lcnwf.noWaits.length );
148 
149     }
150 
151     /***
152      * Verify that the config does not throw any errors.
153      *
154      * @throws Exception
155      */
156     public void testUDPDiscoveryConfig()
157         throws Exception
158     {
159         JCS jcs = JCS.getInstance( "testCache1" );
160 
161         System.out.println( jcs.getStats() );
162 
163         JCS jcs2 = JCS.getInstance( "testCache2" );
164 
165         System.out.println( jcs2.getStats() );
166 
167     }
168 
169     /***
170      * Make sure the no wait facade doesn't add dupes.
171      * <p>
172      * @throws Exception
173      */
174     public void testNoWaitFacadeAdd()
175         throws Exception
176     {
177         ArrayList noWaits = new ArrayList();
178         ILateralCacheAttributes attr = new LateralCacheAttributes();
179         attr.setCacheName( "testCache1" );
180         LateralCacheNoWaitFacade lcnwf = new LateralCacheNoWaitFacade( (LateralCacheNoWait[]) noWaits
181             .toArray( new LateralCacheNoWait[0] ), attr );
182 
183         TCPLateralCacheAttributes lac = new TCPLateralCacheAttributes();
184         lac.setTransmissionType( LateralCacheAttributes.TCP );
185         lac.setTcpServer( "localhost" + ":" + 1111 );
186 
187         LateralCache cache = new MockLateralCache( lac );
188 
189         // add one
190         LateralCacheNoWait noWait = new LateralCacheNoWait( cache );
191         lcnwf.addNoWait( noWait );
192         assertEquals( "Facade should have 1 no wait", 1, lcnwf.noWaits.length );
193 
194         // add another
195         LateralCacheNoWait noWait2 = new LateralCacheNoWait( cache );
196         lcnwf.addNoWait( noWait2 );
197         assertEquals( "Facade should have 2 no waits", 2, lcnwf.noWaits.length );
198 
199         // try adding the same one again
200         lcnwf.addNoWait( noWait2 );
201         assertEquals( "Facade should still have 2 no waits", 2, lcnwf.noWaits.length );
202 
203     }
204 }