### Question:
Janus { public ServerStats getServer() { updateServerList(); Collection<ServerStats> serverStats = servers.values(); List<ServerStats> availableServerStats = new ArrayList<>(serverStats.size()); for (ServerStats s : serverStats) { if (s.getServerInstance().isAvailable()) { availableServerStats.add(s); } } if (availableServerStats.isEmpty()) { return null; } return loadBalancer.choose(availableServerStats); } Janus(String serviceName, ServerList serverList, LoadBalancer loadBalancer, StatsFactory statsFactory);  Janus(String serviceName, ServerList serverList, LoadBalancer loadBalancer, StatsFactory statsFactory, long refreshInterval); void setRefreshInterval(long refreshInterval); long getRefreshInterval(); String getServiceName(); ServerStats getServer(); static Builder builder(String serviceName); static final String REFRESH_INTERVAL_IN_MILLIS; static final long DEFAULT_REFRESH_INTERVAL_IN_MILLIS; }### Answer:
@Test public void GetServerTest() { Janus janus = new Janus( VIP_TEST, new ConstServerList(VIP_TEST,"http: new RandomLoadBalancer(), new ServerStatsFactory(ServerStats.class,new MetricRegistry())); ServerStats firstResult = janus.getServer(); boolean randomSuccess = false; for (int i = 0; i < 10; i++) { ServerStats nextResult = janus.getServer(); if (firstResult != nextResult) { randomSuccess = true; break; } } Assert.assertTrue(randomSuccess); }
@Test public void ShortCircuitTest() throws InterruptedException { Janus janus = new Janus( VIP_TEST, new ConstServerList(VIP_TEST,"http: new RandomLoadBalancer(), new ServerStatsFactory(ServerStats.class,new MetricRegistry()) ); ServerStats stats = janus.getServer(); Assert.assertEquals( stats.getServerInstance().isShortCircuited(), false ); Assert.assertTrue( stats.getServerInstance().getCircuitBreakerRemainingTime() <= 0.0); stats.incrementErrors(); Assert.assertEquals(stats.getServerInstance().isShortCircuited(), false); stats.incrementErrors(); Assert.assertEquals(stats.getServerInstance().isShortCircuited(), false); stats.incrementErrors(); Assert.assertEquals(stats.getServerInstance().isShortCircuited(), true); ServerStats stats2 = janus.getServer(); Assert.assertNull(stats2); Thread.sleep(1005); Assert.assertEquals(stats.getServerInstance().isShortCircuited(), false); stats = janus.getServer(); Assert.assertNotNull(stats); stats.incrementErrors(); Assert.assertEquals(stats.getServerInstance().isShortCircuited(), false); stats.incrementErrors(); Assert.assertEquals(stats.getServerInstance().isShortCircuited(), false); stats.incrementErrors(); Assert.assertEquals(stats.getServerInstance().isShortCircuited(), true); Assert.assertTrue(stats.getServerInstance().getCircuitBreakerRemainingTime() > 1.0); }
@Test public void noServersTest() { Janus janus = new Janus( VIP_TEST, new ConstServerList(VIP_TEST), new RandomLoadBalancer(), new ServerStatsFactory(ServerStats.class,new MetricRegistry()) ); Assert.assertNull(janus.getServer()); }