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