@Test public void testGetService() throws Exception { Set<Object> expectedResult = new HashSet<>(asList("1", "2", "3")); ThreadLocalRandom rnd = ThreadLocalRandom.current(); new Expectations(Context.class, ThreadLocalRandom.class) {{ context.lookupMicroservice(META_DATA); times = 4; result = expectedResult; rnd.nextInt(anyInt); times = 4; result = 1; }}; List<Object> result = new ArrayList<>(); for (int i = 0; i < 4; i++) { result.add(strategy.getService()); } assertThat(result).containsExactly("2", "2", "2", "2"); }
@Override public Object getService() { Set<Object> services = context.lookupMicroservice(metaData); if (services.isEmpty()) { throw new RuntimeException("No service found for: " + metaData); } return services.toArray()[ThreadLocalRandom.current().nextInt(services.size())]; }
RandomRobinLookupStrategy extends AbstractLookupStrategy { @Override public Object getService() { Set<Object> services = context.lookupMicroservice(metaData); if (services.isEmpty()) { throw new RuntimeException("No service found for: " + metaData); } return services.toArray()[ThreadLocalRandom.current().nextInt(services.size())]; } }
RandomRobinLookupStrategy extends AbstractLookupStrategy { @Override public Object getService() { Set<Object> services = context.lookupMicroservice(metaData); if (services.isEmpty()) { throw new RuntimeException("No service found for: " + metaData); } return services.toArray()[ThreadLocalRandom.current().nextInt(services.size())]; }  }
RandomRobinLookupStrategy extends AbstractLookupStrategy { @Override public Object getService() { Set<Object> services = context.lookupMicroservice(metaData); if (services.isEmpty()) { throw new RuntimeException("No service found for: " + metaData); } return services.toArray()[ThreadLocalRandom.current().nextInt(services.size())]; }  @Override Object getService(); }
RandomRobinLookupStrategy extends AbstractLookupStrategy { @Override public Object getService() { Set<Object> services = context.lookupMicroservice(metaData); if (services.isEmpty()) { throw new RuntimeException("No service found for: " + metaData); } return services.toArray()[ThreadLocalRandom.current().nextInt(services.size())]; }  @Override Object getService();  }