use of com.zimbra.cs.account.MockServer in project zm-mailbox by Zimbra.
the class ImapLoadBalancingMechanismTest method accountIdHashMechanismHashFromPool.
@Test
public void accountIdHashMechanismHashFromPool() throws Exception {
ImapLoadBalancingMechanism mech = ImapLoadBalancingMechanism.newInstance(ImapLoadBalancingMechanism.ImapLBMech.AccountIdHash.name());
ArrayList<Server> pool = new ArrayList<Server>();
Server s0 = new MockServer("server-0", "0");
Server s1 = new MockServer("server-1", "1");
Server s2 = new MockServer("server-2", "2");
// Add to pool out-of-order to verify that we are sorting correctly
pool.add(s1);
pool.add(s0);
pool.add(s2);
HashMap<String, String> headers = new HashMap<String, String>();
HttpServletRequest req = new MockHttpServletRequest(null, null, null, 123, "127.0.0.1", headers);
String acctId0 = "79e9a595-c34b-469a-a1ee-e2b9d03f9aa7";
/* hash=1536494685, hash % 3 = 0 */
String acctId1 = "615922e5-2318-4b8b-8734-d209a99f8252";
/* hash=454270162, hash % 3 = 1 */
String acctId2 = "f5c68357-61d9-4658-a7fc-e7273929ca0c";
/* hash=1373626454, hash % 3 = 2 */
assertEquals("Should have got 0th entry from sorted pool", s0, mech.getImapServerFromPool(req, acctId0, pool));
assertEquals("Should have got 1st entry from sorted pool", s1, mech.getImapServerFromPool(req, acctId1, pool));
assertEquals("Should have got 2nd entry from sorted pool", s2, mech.getImapServerFromPool(req, acctId2, pool));
}
use of com.zimbra.cs.account.MockServer in project zm-mailbox by Zimbra.
the class ImapLoadBalancingMechanismTest method ClientIpHashMechanismIpHashFromPool.
@Test
public void ClientIpHashMechanismIpHashFromPool() throws Exception {
ImapLoadBalancingMechanism mech = ImapLoadBalancingMechanism.newInstance(ImapLoadBalancingMechanism.ImapLBMech.ClientIpHash.name());
ArrayList<Server> pool = new ArrayList<Server>();
Server s0 = new MockServer("server-0", "0");
Server s1 = new MockServer("server-1", "1");
Server s2 = new MockServer("server-2", "2");
// Add to pool out-of-order to verify that we are sorting correctly
pool.add(s1);
pool.add(s0);
pool.add(s2);
HashMap<String, String> headers = new HashMap<String, String>();
/**
* For IPV4, Java uses the 32bit value of an IPV4 address as the hashCode of an IPV4
* address, so with the current test setup, and a pool size of 3:
* 127.0.0.2 == (0x7f << 24) + 2 == 2130706434
* And 2130706434 % 3 = 0
* So 127.0.0.2 should return server 0 in our 3 node pool,
* 127.0.0.3 should return server 1 in our 3 node pool,
* 127.0.0.4 should return server 2 in our 3 node pool
*/
headers.put(ImapLoadBalancingMechanism.ClientIpHashMechanism.CLIENT_IP, "127.0.0.2");
HttpServletRequest req = new MockHttpServletRequest(null, null, null, 123, "127.0.0.1", headers);
Assert.assertEquals(s0, mech.getImapServerFromPool(req, pool));
headers.put(ImapLoadBalancingMechanism.ClientIpHashMechanism.CLIENT_IP, "127.0.0.3");
Assert.assertEquals(s1, mech.getImapServerFromPool(req, pool));
headers.put(ImapLoadBalancingMechanism.ClientIpHashMechanism.CLIENT_IP, "127.0.0.4");
Assert.assertEquals(s2, mech.getImapServerFromPool(req, pool));
/* Verify we get the same results using IPV6 */
headers.put(ImapLoadBalancingMechanism.ClientIpHashMechanism.CLIENT_IP, "::FFFF:127.0.0.2");
Assert.assertEquals(s0, mech.getImapServerFromPool(req, pool));
headers.put(ImapLoadBalancingMechanism.ClientIpHashMechanism.CLIENT_IP, "::FFFF:127.0.0.3");
Assert.assertEquals(s1, mech.getImapServerFromPool(req, pool));
headers.put(ImapLoadBalancingMechanism.ClientIpHashMechanism.CLIENT_IP, "::FFFF:127.0.0.4");
Assert.assertEquals(s2, mech.getImapServerFromPool(req, pool));
}
Aggregations