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  import net.sf.ehcache.AbstractCacheTest;
20  import net.sf.ehcache.CacheManager;
21  import net.sf.ehcache.Ehcache;
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertFalse;
25  
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  import java.util.Collections;
30  import java.util.List;
31  import java.util.concurrent.TimeUnit;
32  
33  /***
34   * @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a>
35   * @version $Id: ManualRMIPeerProviderTest.html 13146 2011-08-01 17:12:39Z oletizi $
36   */
37  public class ManualRMIPeerProviderTest extends MulticastRMIPeerProviderTest {
38  
39  
40      /***
41       * {@inheritDoc}
42       */
43      @Override
44      @Before
45      public void setUp() throws Exception {
46          manager1 = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR + "distribution/ehcache-manual-distributed1.xml");
47          manager2 = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR + "distribution/ehcache-manual-distributed2.xml");
48          manager3 = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR + "distribution/ehcache-manual-distributed3.xml");
49  
50          /* manager3 has an empty manual configuration, which is topped up by adding manual entries.
51           * The sampleCache1 from manager3 is added to the rmiUrls list for manager1 and manager2
52           */
53          CacheManagerPeerProvider peerProvider = manager3.getCacheManagerPeerProvider("RMI");
54          peerProvider.registerPeer("//localhost:40001/sampleCache1");
55          peerProvider.registerPeer("//localhost:40002/sampleCache1");
56  
57          //Allow cluster setup
58          waitForClusterMembership(10, TimeUnit.SECONDS, Collections.singleton("sampleCache1"), manager1, manager2, manager3);
59      }
60  
61  
62      /***
63       * test remote cache peers
64       */
65      @Override
66      @Test
67      public void testProviderFromCacheManager() throws InterruptedException {
68          Ehcache m1sampleCache1 = manager1.getCache("sampleCache1");
69          Thread.sleep(2000);
70  
71          List peerUrls = manager1.getCacheManagerPeerProvider("RMI").listRemoteCachePeers(m1sampleCache1);
72          assertEquals(expectedPeers(), peerUrls.size());
73  
74          Ehcache m2sampleCache1 = manager2.getCache("sampleCache1");
75          assertFalse(m1sampleCache1.getGuid().equals(m2sampleCache1.getGuid()));
76  
77          List peerUrls2 = manager2.getCacheManagerPeerProvider("RMI").listRemoteCachePeers(m2sampleCache1);
78          assertEquals(expectedPeers(), peerUrls2.size());
79  
80          Ehcache m3sampleCache1 = manager3.getCache("sampleCache1");
81          assertFalse(m1sampleCache1.getGuid().equals(m3sampleCache1.getGuid()));
82  
83          List peerUrls3 = manager3.getCacheManagerPeerProvider("RMI").listRemoteCachePeers(m3sampleCache1);
84          assertEquals(expectedPeers(), peerUrls3.size());
85  
86          //Now remove a node, wait for the cluster to self-heal and then test
87          manager1.shutdown();
88          Thread.sleep(1000);
89          peerUrls3 = manager3.getCacheManagerPeerProvider("RMI").listRemoteCachePeers(m3sampleCache1);
90          //The manual provider removes the cache peer that was not reachable
91          assertEquals(1, peerUrls3.size());
92  
93      }
94  
95  
96  }