View Javadoc

1   /**
2    *  Copyright 2003-2006 Greg Luck
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.distribution;
18  
19  import junit.framework.TestCase;
20  import net.sf.ehcache.Cache;
21  import net.sf.ehcache.CacheManager;
22  import net.sf.ehcache.StopWatch;
23  import net.sf.ehcache.AbstractCacheTest;
24  
25  import java.util.List;
26  import java.rmi.RemoteException;
27  
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.commons.logging.Log;
30  
31  /**
32   * Multicast tests. These require special machine configuration.
33   * <p/>
34   * Running on a single machine, as these tests do, you need to add a route command so that two multiCast sockets
35   * can be added at the same time.
36   * <ol>
37   * <li>Mac OSX: <code>route add -net 224.0.0.0 -interface lo0</code>
38   * <li>Linux (from JGroups doco, untested): <code>route add -net 224.0.0.0 netmask 224.0.0.0 dev lo</code>
39   * </ol>
40   *
41   * @author Greg Luck
42   * @version $Id: MulticastRMIPeerProviderTest.java 51 2006-04-24 09:21:10Z gregluck $
43   */
44  public class MulticastRMIPeerProviderTest extends TestCase {
45  
46      private static final Log LOG = LogFactory.getLog(MulticastRMIPeerProviderTest.class.getName());
47  
48      /**
49       * Cache Manager 1
50       */
51      protected CacheManager manager1;
52      /**
53       * Cache Manager 2
54       */
55      protected CacheManager manager2;
56      /**
57       * Cache Manager 3
58       */
59      protected CacheManager manager3;
60  
61      /**
62       * {@inheritDoc}
63       */
64      protected void setUp() throws Exception {
65          if (JVMUtil.isSingleRMIRegistryPerVM()) {
66              return;
67          }
68          manager1 = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR + "distribution/ehcache-distributed1.xml");
69          manager2 = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR + "distribution/ehcache-distributed2.xml");
70          manager3 = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR + "distribution/ehcache-distributed3.xml");
71  
72          //wait for cluster to establish
73          Thread.sleep(6000);
74      }
75  
76      /**
77       * {@inheritDoc}
78       */
79      protected void tearDown() throws Exception {
80  
81          if (JVMUtil.isSingleRMIRegistryPerVM()) {
82              return;
83          }
84  
85          manager1.shutdown();
86          manager2.shutdown();
87          if (manager3 != null) {
88              manager3.shutdown();
89          }
90      }
91  
92      /**
93       * test remote cache peers
94       */
95      public void testProviderFromCacheManager() throws InterruptedException {
96  
97          if (JVMUtil.isSingleRMIRegistryPerVM()) {
98              return;
99          }
100 
101         Cache m1sampleCache1 = manager1.getCache("sampleCache1");
102         Thread.sleep(2000);
103 
104         List peerUrls = manager1.getCachePeerProvider().listRemoteCachePeers(m1sampleCache1);
105         assertEquals(expectedPeers(), peerUrls.size());
106 
107         Cache m2sampleCache1 = manager2.getCache("sampleCache1");
108         assertFalse(m1sampleCache1.getGuid().equals(m2sampleCache1.getGuid()));
109 
110         List peerUrls2 = manager2.getCachePeerProvider().listRemoteCachePeers(m2sampleCache1);
111         assertEquals(expectedPeers(), peerUrls2.size());
112     }
113 
114     /**
115      * There are 3 in the cluster, so there will be two others
116      */
117     protected int expectedPeers() {
118         return 2;
119     }
120 
121 
122     /**
123      * Tests the speed of remotely looking up.
124      *
125      * @throws RemoteException
126      * @throws InterruptedException .19ms
127      *                              This seems to imply a maximum of 5000 per second best case. Not bad.
128      */
129     public void testRemoteGetName() throws RemoteException, InterruptedException {
130 
131         if (JVMUtil.isSingleRMIRegistryPerVM()) {
132             return;
133         }
134 
135         Cache m1sampleCache1 = manager1.getCache("sampleCache1");
136         Thread.sleep(2000);
137         List peerUrls = manager1.getCachePeerProvider().listRemoteCachePeers(m1sampleCache1);
138 
139         CachePeer m1SampleCach1Peer = (CachePeer) peerUrls.get(0);
140 
141         for (int i = 0; i < 100; i++) {
142             m1SampleCach1Peer.getName();
143         }
144         Thread.sleep(2000);
145 
146         StopWatch stopWatch = new StopWatch();
147         for (int i = 0; i < 1000; i++) {
148             m1SampleCach1Peer.getName();
149         }
150         long time = stopWatch.getElapsedTime();
151 
152         LOG.info("Remote name lookup time in ms: " + time / 1000f);
153 
154     }
155 
156 }