EventRetrievalScheduledTask extends BaseTask implements Runnable { public List<Device> filterByExponentialBackOff(final List<Device> devicesToFilter) { final Predicate<Device> hasNoConnectionFailure = d -> !d.hasConnectionFailures(); final Predicate<Device> hasLastConnectionFailureBeforeThreshold = d -> this.calculateThresholdForDevice(d); return devicesToFilter.stream().filter(hasNoConnectionFailure.or(hasLastConnectionFailureBeforeThreshold)) .collect(Collectors.<Device> toList()); }  @Override void run(); List<Device> filterByExponentialBackOff(final List<Device> devicesToFilter);  }
@Test public void applyExponentialBackOffToListOfDevicesWithoutCommunicationErrors() { final List<Device> devices = new ArrayList<>(); devices.add(this.createDevice("device-01", DateTime.now(DateTimeZone.UTC).minusDays(15).toDate(), 0)); final List<Device> filteredList = this.task.filterByExponentialBackOff(devices); assertThat(filteredList.size()).isEqualTo(1); }
@Test public void applyExponentialBackOffToListOfDevicesWithCommunicationTimestampOlderThan24HoursAndManyConnectionsFailure() { final List<Device> devices = new ArrayList<>(); devices.add(this.createDevice("device-02", DateTime.now(DateTimeZone.UTC).minusDays(2).toDate(), 100)); final List<Device> filteredList = this.task.filterByExponentialBackOff(devices); assertThat(filteredList.size()).isEqualTo(1); }
@Test public void applyExponentialBackOffToListOfDevicesWithCommunicationTimestampYoungerThan24HoursAnd1ConnectionFailure() { final List<Device> devices = new ArrayList<>(); devices.add(this.createDevice("device-03", DateTime.now(DateTimeZone.UTC).minusHours(23).toDate(), 1)); final List<Device> filteredList = this.task.filterByExponentialBackOff(devices); assertThat(filteredList.size()).isEqualTo(1); }
@Test public void applyExponentialBackOffToListOfDevicesWithCommunicationTimestampExactly1HourAnd1ConnectionFailure() { final List<Device> devices = new ArrayList<>(); devices.add(this.createDevice("device-04", DateTime.now(DateTimeZone.UTC).minusMinutes(61).toDate(), 1)); final List<Device> filteredList = this.task.filterByExponentialBackOff(devices); assertThat(filteredList.size()).isEqualTo(1); }
@Test public void applyExponentialBackOffToListOfDevicesWithCommunicationTimestampYoungerThan1HourAnd1ConnectionFailure() { final List<Device> devices = new ArrayList<>(); devices.add(this.createDevice("device-05", DateTime.now(DateTimeZone.UTC).minusMinutes(59).toDate(), 1)); final List<Device> filteredList = this.task.filterByExponentialBackOff(devices); assertThat(filteredList.size()).isEqualTo(0); }
@Test public void applyExponentialBackOffToListOfDevicesWithCommunicationTimestampYoungerThan1HourAnd2ConnectionFailures() { final List<Device> devices = new ArrayList<>(); devices.add(this.createDevice("device-06", DateTime.now(DateTimeZone.UTC).minusHours(1).toDate(), 2)); final List<Device> filteredList = this.task.filterByExponentialBackOff(devices); assertThat(filteredList.size()).isEqualTo(0); }
@Test public void applyExponentialBackOffToListOfDevicesWithCommunicationTimestampYoungerThan2HoursAnd1ConnectionFailure() { final List<Device> devices = new ArrayList<>(); devices.add(this.createDevice("device-07", DateTime.now(DateTimeZone.UTC).minusHours(2).toDate(), 1)); final List<Device> filteredList = this.task.filterByExponentialBackOff(devices); assertThat(filteredList.size()).isEqualTo(1); }
@Test public void applyExponentialBackOffToListOfDevicesWithCommunicationTimestampYoungerThan2HoursAnd2ConnectionFailures() { final List<Device> devices = new ArrayList<>(); devices.add(this.createDevice("device-08", DateTime.now(DateTimeZone.UTC).minusHours(2).plusMinutes(1).toDate(), 2)); final List<Device> filteredList = this.task.filterByExponentialBackOff(devices); assertThat(filteredList.size()).isEqualTo(0); }
@Test public void applyExponentialBackOffToListOfDevicesWithCommunicationTimestampYoungerThan2HoursAnd3ConnectionFailures() { final List<Device> devices = new ArrayList<>(); devices.add(this.createDevice("device-09", DateTime.now(DateTimeZone.UTC).minusHours(2).toDate(), 3)); final List<Device> filteredList = this.task.filterByExponentialBackOff(devices); assertThat(filteredList.size()).isEqualTo(0); }