/*
*
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.netflix.loadbalancer;


import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.junit.BeforeClass;
import org.junit.Test;

public class SimpleRoundRobinLBTest {
	static BaseLoadBalancer lb;
    static Map<String, Boolean> isAliveMap = new ConcurrentHashMap<String, Boolean>();
	
	@BeforeClass
	public static void setup(){
		isAliveMap.put("dummyservice0.netflix.com:8080", Boolean.TRUE);
		isAliveMap.put("dummyservice1.netflix.com:8080", Boolean.TRUE);
		isAliveMap.put("dummyservice2.netflix.com:8080", Boolean.TRUE);
		isAliveMap.put("dummyservice3.netflix.com:8080", Boolean.TRUE);
		
		IPing ping = new PingFake();
		IRule rule = new RoundRobinRule();
		lb = new BaseLoadBalancer(ping,rule);
		lb.setPingInterval(5);
		lb.setMaxTotalPingTime(2);
		
		// the setting of servers is done by a call to DiscoveryService
		List<Server> servers = new ArrayList<Server>();
		for (String svc: isAliveMap.keySet()){
			servers.add(new Server(svc));	
		}		
		lb.addServers(servers);
		
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
		}
	}
	
	/**
	 * Simulate a single user who should just round robin among the available servers
	 */
	@Test
	public void testRoundRobin(){
		Set<String> servers = new HashSet<String>();
		System.out.println("Simple Round Robin Test");
		for (int i=0; i < 9; i++){
			Server svc = lb.chooseServer("user1");
			servers.add(svc.getId());
		}
		assertEquals(isAliveMap.keySet(), servers);
	}
	
	/**
	 * Simulate a single user who should just round robin among the available servers
	 */
	@Test
	public void testRoundRobinWithAServerFailure() throws Exception {
		System.out.println("Round Robin Test With Server SERVER DOWN");
		isAliveMap.put("dummyservice2.netflix.com:8080", Boolean.FALSE);
		((BaseLoadBalancer)lb).markServerDown("dummyservice2.netflix.com:8080");
		Thread.sleep(3000);
		for (int i=0; i < 12; i++){
			Server svc = lb.chooseServer("user1");
			assertNotNull(svc);
			System.out.println("server: " + svc.getHost());
			assertFalse(svc.getId().equals("dummyservice2.netflix.com:8080"));
		}
	}
	
	static class PingFake implements IPing {
		public boolean isAlive(Server server) {
			Boolean res = isAliveMap.get(server.getId());
			return ((res != null) && (res.booleanValue()));
		}
	}
	
}

@@ -1469,9 +1469,9 @@
 val(
-5
+1
 );%0A%09
@@ -1727,16 +1727,93 @@
 rs);%0A%09%09%0A
+%09%09// make sure the ping cycle has kicked in and all servers are set to alive%0A
 %09%09try %7B%0A
@@ -1828,17 +1828,17 @@
 d.sleep(
-2
+5
 000);%0A%09%09
@@ -1872,24 +1872,70 @@
 on e) %7B%0A%09%09%7D%0A
+%09%09System.out.println(lb.getServerList(true));%0A
 %09%7D%0A%09%0A%09/**%0A%09 

Make sure ping thread runs and set all servers alive before running junit.