private Subscriptions filterByStep(Subscriptions subs) { List<Subscription> subscriptions = subs .getExpressions() .stream() .filter(s -> s.getFrequency() == stepMillis) .collect(Collectors.toList()); return new Subscriptions().withExpressions(subscriptions); } }
/** Set the available subscriptions. */ public Subscriptions withExpressions(List<Subscription> expressions) { setExpressions(expressions); return this; }
private Subscriptions newSubs(Subscription... subs) { return new Subscriptions().withExpressions(Arrays.asList(subs)); }
/** Refresh the subscriptions from the server. */ void refresh() { // Request latest expressions from the server try { HttpResponse res = client.get(uri) .withConnectTimeout(connectTimeout) .withReadTimeout(readTimeout) .addHeader("If-None-Match", etag) .send() .decompress(); if (res.status() == 304) { LOGGER.debug("no modification to subscriptions"); } else if (res.status() != 200) { LOGGER.warn("failed to update subscriptions, received status {}", res.status()); } else { etag = res.header("ETag"); payload = filterByStep(mapper.readValue(res.entity(), Subscriptions.class)); } } catch (Exception e) { LOGGER.warn("failed to update subscriptions", e); } // Update with the current payload, it will be null if there hasn't been a single // successful request if (payload != null) { long now = clock.wallTime(); payload.update(subscriptions, now, now + configTTL); } }
private byte[] json(Subscription... subs) throws Exception { Subscriptions payload = new Subscriptions().withExpressions(Arrays.asList(subs)); return mapper.writeValueAsBytes(payload); }
@Test public void updateInit() { Map<Subscription, Long> subs = new HashMap<>(); Subscription a = newSub("a", ":true,:sum", 10L); Subscription b = newSub("b", ":true,:sum", 60L); newSubs(a, b).update(subs, 0L, 15L); Assertions.assertEquals(map(15L, a, b), subs); }
@Test public void updatePartial() { Subscription a = newSub("a", ":true,:sum", 10L); Subscription b = newSub("b", ":true,:sum", 60L); Map<Subscription, Long> subs = map(15L, a, b); newSubs(b).update(subs, 10L, 30L); Map<Subscription, Long> expected = map(15L, a, b); expected.put(b, 30L); Assertions.assertEquals(expected, subs); }
@Test public void updateComplete() { Subscription a = newSub("a", ":true,:sum", 10L); Subscription b = newSub("b", ":true,:sum", 60L); Map<Subscription, Long> subs = map(15L, a, b); newSubs(a, b).update(subs, 10L, 30L); Assertions.assertEquals(map(30L, a, b), subs); }
@Test public void updatePartialExpire() { Subscription a = newSub("a", ":true,:sum", 10L); Subscription b = newSub("b", ":true,:sum", 60L); Map<Subscription, Long> subs = map(15L, a, b); newSubs(b).update(subs, 16L, 30L); Map<Subscription, Long> expected = map(30L, b); Assertions.assertEquals(expected, subs); } }