View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
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  
20  import net.sf.ehcache.AbstractCacheTest;
21  import net.sf.ehcache.CacheManager;
22  import net.sf.ehcache.Ehcache;
23  import net.sf.ehcache.Element;
24  import org.junit.After;
25  import static org.junit.Assert.assertEquals;
26  import static org.junit.Assert.assertNotNull;
27  import org.junit.Before;
28  import org.junit.Test;
29  
30  import java.rmi.Naming;
31  import java.util.Date;
32  
33  /***
34   * Note these tests need a live network interface running in multicast mode to work
35   *
36   * @author Greg Luck
37   * @version $Id: RMIDistributedCacheTest.java 2154 2010-04-06 02:45:52Z cdennis $
38   */
39  public class RMIDistributedCacheTest {
40  
41  
42      /***
43       * manager
44       */
45      protected CacheManager manager;
46      /***
47       * the cache name we wish to test
48       */
49      private String cacheName1 = "sampleCache1";
50      private String cacheName2 = "sampleCache2";
51      /***
52       * the cache we wish to test
53       */
54      private Ehcache sampleCache1;
55  
56  
57      private String hostName = "localhost";
58  
59      private Integer port = Integer.valueOf(40000);
60      private Integer remoteObjectPort = Integer.valueOf(0);
61      private Element element;
62      private CachePeer cache1Peer;
63      private CachePeer cache2Peer;
64  
65      /***
66       * {@inheritDoc}
67       *
68       * @throws Exception
69       */
70      @Before
71      public void setUp() throws Exception {
72  
73          manager = CacheManager.create(AbstractCacheTest.TEST_CONFIG_DIR + "distribution/ehcache-distributed1.xml");
74          MulticastKeepaliveHeartbeatSender.setHeartBeatInterval(1000);
75          sampleCache1 = manager.getCache(cacheName1);
76          sampleCache1.removeAll();
77          element = new Element("key", new Date());
78          sampleCache1.put(element);
79          CacheManagerPeerListener cacheManagerPeerListener =
80                  new RMICacheManagerPeerListener(hostName, port, remoteObjectPort, manager, Integer.valueOf(2000));
81          cacheManagerPeerListener.init();
82          cache1Peer = (CachePeer) Naming.lookup(createNamingUrl() + cacheName1);
83          cache2Peer = (CachePeer) Naming.lookup(createNamingUrl() + cacheName2);
84  
85      }
86  
87      /***
88       * Shutdown the cache
89       */
90      @After
91      public void tearDown() throws InterruptedException {
92  
93          Thread.sleep(10);
94          manager.shutdown();
95          int i = 0;
96      }
97  
98      /***
99       * Getting an RMI Server going is a big deal
100      */
101     @Test
102     public void testCreation() throws Exception {
103         assertNotNull(cache1Peer);
104         assertNotNull(cache2Peer);
105     }
106 
107     /***
108      * The use of one-time registry creation and Naming.rebind should mean we can create as many listeneres as we like.
109      * They will simply replace the ones that were there.
110      */
111     @Test
112     public void testMultipleCreationOfRMIServers() throws Exception {
113         RMICacheManagerPeerListener[] listeners = new RMICacheManagerPeerListener[100];
114         for (int i = 0; i < 100; i++) {
115             listeners[i] = new RMICacheManagerPeerListener(hostName, port, remoteObjectPort, manager, Integer.valueOf(2000));
116         }
117         cache1Peer = (CachePeer) Naming.lookup(createNamingUrl() + cacheName1);
118         assertNotNull(cache1Peer);
119 
120         for (int i = 0; i < 100; i++) {
121             listeners[i].dispose();
122         }
123     }
124 
125 
126     /***
127      * Same as the above with remoteObjectPort the same.
128      */
129     @Test
130     public void testMultipleCreationOfRMIServersWithSpecificRemoteObjectPort() throws Exception {
131         RMICacheManagerPeerListener[] listeners = new RMICacheManagerPeerListener[100];
132         for (int i = 0; i < 100; i++) {
133             listeners[i] = new RMICacheManagerPeerListener(hostName, port, Integer.valueOf(45000), manager, Integer.valueOf(2000));
134         }
135         cache1Peer = (CachePeer) Naming.lookup(createNamingUrl() + cacheName1);
136         assertNotNull(cache1Peer);
137         cache1Peer.put(new Element(1, 4));
138 
139         for (int i = 0; i < 100; i++) {
140             listeners[i].dispose();
141         }
142     }
143 
144 
145     private String createNamingUrl() {
146         return "//" + hostName + ":" + port + "/";
147     }
148 
149     /***
150      * Attempts to get the cache name
151      *
152      * @throws java.net.MalformedURLException
153      * @throws java.rmi.NotBoundException
154      * @throws java.rmi.RemoteException
155      */
156     @Test
157     public void testGetName() throws Exception {
158         String lookupCacheName = cache1Peer.getName();
159         assertEquals(cacheName1, lookupCacheName);
160         lookupCacheName = cache2Peer.getName();
161         assertEquals(cacheName2, lookupCacheName);
162     }
163 
164 
165 }