View Javadoc
1   package org.apache.commons.jcs3.auxiliary.remote;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import org.apache.commons.jcs3.auxiliary.AuxiliaryCache;
7   import org.apache.commons.jcs3.auxiliary.remote.behavior.IRemoteCacheAttributes;
8   import org.apache.commons.jcs3.engine.CacheStatus;
9   
10  /*
11   * Licensed to the Apache Software Foundation (ASF) under one
12   * or more contributor license agreements.  See the NOTICE file
13   * distributed with this work for additional information
14   * regarding copyright ownership.  The ASF licenses this file
15   * to you under the Apache License, Version 2.0 (the
16   * "License"); you may not use this file except in compliance
17   * with the License.  You may obtain a copy of the License at
18   *
19   *   http://www.apache.org/licenses/LICENSE-2.0
20   *
21   * Unless required by applicable law or agreed to in writing,
22   * software distributed under the License is distributed on an
23   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
24   * KIND, either express or implied.  See the License for the
25   * specific language governing permissions and limitations
26   * under the License.
27   */
28  
29  import junit.framework.TestCase;
30  
31  /**
32   * Tests for RemoteCacheNoWaitFacade.
33   */
34  public class RemoteCacheNoWaitFacadeUnitTest
35      extends TestCase
36  {
37      /**
38       * Verify that we can add an item.
39       */
40      public void testAddNoWait_InList()
41      {
42          // SETUP
43          final List<RemoteCacheNoWait<String, String>> noWaits = new ArrayList<>();
44          final IRemoteCacheAttributes cattr = new RemoteCacheAttributes();
45          cattr.setCacheName( "testCache1" );
46  
47          final RemoteCache<String, String> client = new RemoteCache<>(cattr, null, null, null);
48          final RemoteCacheNoWait<String, String> noWait = new RemoteCacheNoWait<>( client );
49          noWaits.add( noWait );
50  
51          final RemoteCacheNoWaitFacade<String, String> facade = new RemoteCacheNoWaitFacade<>(noWaits, cattr, null, null, null );
52  
53          // VERIFY
54          assertEquals( "Should have one entry.", 1, facade.noWaits.size() );
55          assertTrue( "Should be in the list.", facade.noWaits.contains( noWait ) );
56          assertSame( "Should have same facade.", facade, ((RemoteCache<String, String>)facade.noWaits.get(0).getRemoteCache()).getFacade() );
57      }
58  
59      /**
60       * Verify that failover works
61       */
62      public void testFailover()
63      {
64          // SETUP
65          final IRemoteCacheAttributes cattr = new RemoteCacheAttributes();
66          cattr.setCacheName("testCache1");
67          cattr.setFailoverServers("localhost:1101,localhost:1102");
68          cattr.setReceive(false);
69  
70          final TestRemoteCacheFactory factory = new TestRemoteCacheFactory();
71          factory.initialize();
72  
73          final AuxiliaryCache<String, String> cache = factory.createCache(cattr, null, null, null);
74          final RemoteCacheNoWaitFacade<String, String> facade =
75                  (RemoteCacheNoWaitFacade<String, String>) cache;
76          assertEquals("Should have two failovers.", 2, cattr.getFailovers().size());
77          assertEquals("Should have two managers.", 2, factory.managers.size());
78          assertEquals("Should have primary server.", 0, cattr.getFailoverIndex());
79          RemoteCacheNoWait<String, String> primary = facade.getPrimaryServer();
80          assertEquals("Should be ALIVE", CacheStatus.ALIVE, primary.getStatus());
81  
82          // Make primary unusable
83          facade.getPrimaryServer().getCacheEventQueue().destroy();
84          assertEquals("Should be ERROR", CacheStatus.ERROR, primary.getStatus());
85          facade.attemptRestorePrimary = false;
86          facade.connectAndRestore();
87  
88          // VERIFY
89          assertEquals("Should have two failovers.", 2, cattr.getFailovers().size());
90          assertEquals("Should have two managers.", 2, factory.managers.size());
91          assertEquals("Should have switched to secondary server.", 1, cattr.getFailoverIndex());
92          assertNotSame("Should have diferent primary now", primary, facade.getPrimaryServer());
93          assertEquals("Should be ALIVE", CacheStatus.ALIVE, facade.getPrimaryServer().getStatus());
94      }
95  }