17
17
package net.sf.ehcache.distribution;
19
import static org.junit.Assert.assertEquals;
20
import static org.junit.Assert.assertFalse;
19
import java.util.ArrayList;
22
20
import java.util.Collections;
23
21
import java.util.List;
24
22
import java.util.concurrent.TimeUnit;
26
24
import net.sf.ehcache.AbstractCacheTest;
27
25
import net.sf.ehcache.CacheManager;
28
import net.sf.ehcache.Ehcache;
26
import net.sf.ehcache.config.Configuration;
30
28
import org.junit.Before;
31
import org.junit.Test;
34
31
* @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a>
35
* @version $Id: ManualRMIPeerProviderTest.java 4741 2011-09-25 10:05:01Z asingh $
32
* @version $Id: ManualRMIPeerProviderTest.java 6496 2012-10-31 14:34:15Z cdennis $
37
34
public class ManualRMIPeerProviderTest extends MulticastRMIPeerProviderTest {
45
41
public void setUp() throws Exception {
46
manager1 = new CacheManager(getConfiguration(AbstractCacheTest.TEST_CONFIG_DIR + "distribution/ehcache-manual-distributed1.xml").name("cm1"));
47
manager2 = new CacheManager(getConfiguration(AbstractCacheTest.TEST_CONFIG_DIR + "distribution/ehcache-manual-distributed2.xml").name("cm2"));
48
manager3 = new CacheManager(getConfiguration(AbstractCacheTest.TEST_CONFIG_DIR + "distribution/ehcache-manual-distributed3.xml").name("cm3"));
42
List<Configuration> configurations = new ArrayList<Configuration>();
43
configurations.add(getConfiguration(AbstractCacheTest.TEST_CONFIG_DIR + "distribution/ehcache-manual-distributed1.xml").name("cm1"));
44
configurations.add(getConfiguration(AbstractCacheTest.TEST_CONFIG_DIR + "distribution/ehcache-manual-distributed2.xml").name("cm2"));
45
configurations.add(getConfiguration(AbstractCacheTest.TEST_CONFIG_DIR + "distribution/ehcache-manual-distributed3.xml").name("cm3"));
47
List<CacheManager> managers = startupManagers(configurations);
48
manager1 = managers.get(0);
49
manager2 = managers.get(1);
50
manager3 = managers.get(2);
50
52
/* manager3 has an empty manual configuration, which is topped up by adding manual entries.
51
53
* The sampleCache1 from manager3 is added to the rmiUrls list for manager1 and manager2
53
55
CacheManagerPeerProvider peerProvider = manager3.getCacheManagerPeerProvider("RMI");
54
peerProvider.registerPeer("//localhost:40001/sampleCache1");
55
peerProvider.registerPeer("//localhost:40002/sampleCache1");
56
peerProvider.registerPeer("//localhost:5011/sampleCache1");
57
peerProvider.registerPeer("//localhost:5012/sampleCache1");
57
59
//Allow cluster setup
58
60
waitForClusterMembership(10, TimeUnit.SECONDS, Collections.singleton("sampleCache1"), manager1, manager2, manager3);
63
* test remote cache peers
67
public void testProviderFromCacheManager() throws InterruptedException {
68
Ehcache m1sampleCache1 = manager1.getCache("sampleCache1");
71
List peerUrls = manager1.getCacheManagerPeerProvider("RMI").listRemoteCachePeers(m1sampleCache1);
72
assertEquals(expectedPeers(), peerUrls.size());
74
Ehcache m2sampleCache1 = manager2.getCache("sampleCache1");
75
assertFalse(m1sampleCache1.getGuid().equals(m2sampleCache1.getGuid()));
77
List peerUrls2 = manager2.getCacheManagerPeerProvider("RMI").listRemoteCachePeers(m2sampleCache1);
78
assertEquals(expectedPeers(), peerUrls2.size());
80
Ehcache m3sampleCache1 = manager3.getCache("sampleCache1");
81
assertFalse(m1sampleCache1.getGuid().equals(m3sampleCache1.getGuid()));
83
List peerUrls3 = manager3.getCacheManagerPeerProvider("RMI").listRemoteCachePeers(m3sampleCache1);
84
assertEquals(expectedPeers(), peerUrls3.size());
86
//Now remove a node, wait for the cluster to self-heal and then test
89
peerUrls3 = manager3.getCacheManagerPeerProvider("RMI").listRemoteCachePeers(m3sampleCache1);
90
//The manual provider removes the cache peer that was not reachable
91
assertEquals(1, peerUrls3.size());