### Question:
MasterReplicationCoordinator implements ReplicationCoordinator.Iface { protected TServerInstance getRandomTServer(Set<TServerInstance> tservers, int offset) { checkArgument(tservers.size() > offset, "Must provide an offset less than the size of the set"); Iterator<TServerInstance> iter = tservers.iterator(); while (offset > 0) { iter.next(); offset--; } return iter.next(); } MasterReplicationCoordinator(Master master); protected  MasterReplicationCoordinator(Master master, ZooReader reader); @Override String getServicerAddress(String remoteTableId, TCredentials creds);  }### Answer:
@Test public void randomServer() { Master master = EasyMock.createMock(Master.class); ZooReader reader = EasyMock.createMock(ZooReader.class); ServerContext context = EasyMock.createMock(ServerContext.class); EasyMock.expect(context.getConfiguration()).andReturn(config).anyTimes(); EasyMock.expect(master.getContext()).andReturn(context); EasyMock.expect(master.getInstanceID()).andReturn("1234"); EasyMock.replay(master, reader); MasterReplicationCoordinator coordinator = new MasterReplicationCoordinator(master, reader); TServerInstance inst1 = new TServerInstance(HostAndPort.fromParts("host1", 1234), "session"); assertEquals(inst1, coordinator.getRandomTServer(Collections.singleton(inst1), 0)); }
@Test(expected = IllegalArgumentException.class) public void invalidOffset() { Master master = EasyMock.createMock(Master.class); ServerContext context = EasyMock.createMock(ServerContext.class); EasyMock.expect(context.getConfiguration()).andReturn(config).anyTimes(); ZooReader reader = EasyMock.createMock(ZooReader.class); EasyMock.expect(master.getContext()).andReturn(context); EasyMock.expect(master.getInstanceID()).andReturn("1234"); EasyMock.replay(master, reader); MasterReplicationCoordinator coordinator = new MasterReplicationCoordinator(master, reader); TServerInstance inst1 = new TServerInstance(HostAndPort.fromParts("host1", 1234), "session"); assertEquals(inst1, coordinator.getRandomTServer(Collections.singleton(inst1), 1)); }
@Test public void randomServerFromMany() { Master master = EasyMock.createMock(Master.class); ZooReader reader = EasyMock.createMock(ZooReader.class); ServerContext context = EasyMock.createMock(ServerContext.class); EasyMock.expect(context.getConfiguration()).andReturn(config).anyTimes(); EasyMock.expect(context.getInstanceID()).andReturn("1234").anyTimes(); EasyMock.expect(context.getZooReaderWriter()).andReturn(null).anyTimes(); EasyMock.expect(master.getInstanceID()).andReturn("1234").anyTimes(); EasyMock.expect(master.getContext()).andReturn(context).anyTimes(); EasyMock.replay(master, context, reader); MasterReplicationCoordinator coordinator = new MasterReplicationCoordinator(master, reader); EasyMock.verify(master, reader); TreeSet<TServerInstance> instances = new TreeSet<>(); TServerInstance inst1 = new TServerInstance(HostAndPort.fromParts("host1", 1234), "session"); instances.add(inst1); TServerInstance inst2 = new TServerInstance(HostAndPort.fromParts("host2", 1234), "session"); instances.add(inst2); assertEquals(inst1, coordinator.getRandomTServer(instances, 0)); assertEquals(inst2, coordinator.getRandomTServer(instances, 1)); }