bugfix: do not reset index if no payment was handled

This commit is contained in:
Carsten Otto
2022-10-22 12:39:33 +02:00
parent 9ceb31c47e
commit 88653ef210
2 changed files with 14 additions and 5 deletions

View File

@@ -7,6 +7,7 @@ import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Optional;
import java.util.OptionalLong;
import java.util.concurrent.TimeUnit;
@Component
@@ -38,19 +39,18 @@ public class Payments {
return;
}
paymentOptionals = grpcPayments.getAllPaymentsAfter(offsetSettledPayments).orElse(List.of());
long maxIndex = getMaxIndexAllSettled(paymentOptionals);
OptionalLong maxIndex = getMaxIndexAllSettled(paymentOptionals);
dao.save(paymentOptionals.stream().flatMap(Optional::stream).toList());
dao.setAllSettledIndexOffset(maxIndex);
maxIndex.ifPresent(dao::setAllSettledIndexOffset);
} while (paymentOptionals.size() == grpcPayments.getLimit());
}
private static long getMaxIndexAllSettled(List<Optional<Payment>> paymentOptionals) {
private static OptionalLong getMaxIndexAllSettled(List<Optional<Payment>> paymentOptionals) {
return paymentOptionals.stream()
.takeWhile(Optional::isPresent)
.flatMap(Optional::stream)
.mapToLong(Payment::index)
.max()
.orElse(0L);
.max();
}
}

View File

@@ -120,6 +120,15 @@ class PaymentsTest {
inOrder.verify(dao).setAllSettledIndexOffset(PAYMENT.index());
}
@Test
void keeps_index_after_saving_if_no_payments_are_settled() {
when(grpcPayments.getAllPaymentsAfter(ALL_SETTLED_INDEX_OFFSET)).thenReturn(
Optional.of(List.of(Optional.empty(), Optional.empty(), Optional.empty()))
).thenReturn(Optional.of(List.of()));
payments.loadOldSettledPayments();
verify(dao, never()).setAllSettledIndexOffset(anyLong());
}
@Test
void ignores_non_settled_payments() {
when(grpcPayments.getAllPaymentsAfter(ALL_SETTLED_INDEX_OFFSET)).thenReturn(