RoutingRuleSelector { Optional<Cell> select(String routeKey, Predicate<Cell> filter) { Map<Cell, Pattern> cellRoutingPatterns = compileRoutingPatterns.apply(routingRulesSupplier.get()); return cellRoutingPatterns.entrySet().stream() .filter(entry -> filter.test(entry.getKey())) .filter(entry -> entry.getValue().matcher(routeKey).matches()) .findFirst() .map(Map.Entry::getKey); } RoutingRuleSelector(CellInfoResolver cellInfoResolver, Supplier<String> routingRulesSupplier);   }
@Test public void testParser() { RoutingRuleSelector selector = new RoutingRuleSelector(cellInfoResolver, () -> "cell1=(app1.*);cell2=(app2.*)"); assertThat(selector.select("app1", cell -> true)).contains(CELL_1); assertThat(selector.select("app2", cell -> true)).contains(CELL_2); }
@Test public void testReload() { AtomicReference<String> rules = new AtomicReference<>("cell1=(app1.*);cell2=(app2.*)"); RoutingRuleSelector selector = new RoutingRuleSelector(cellInfoResolver, rules::get); assertThat(selector.select("app1", cell -> true)).contains(CELL_1); rules.set("cell1=(app2.*);cell2=(app1.*)"); assertThat(selector.select("app1", cell -> true)).contains(CELL_2); }
@Test public void testReloadFailure() { AtomicReference<String> rules = new AtomicReference<>("cell1=(app1.*);cell2=(app2.*)"); RoutingRuleSelector selector = new RoutingRuleSelector(cellInfoResolver, rules::get); assertThat(selector.select("app1", cell -> true)).contains(CELL_1); rules.set(null); assertThat(selector.select("app1", cell -> true)).contains(CELL_1); rules.set("cell1=(app2.*);cell2=(app1.*)"); assertThat(selector.select("app1", cell -> true)).contains(CELL_2); }