Skip to content

Commit

Permalink
Fix DasLongPollCustody.hasCustodyDataColumnSidecar(), add tests (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nashatyrev authored Nov 7, 2024
1 parent 98c8a32 commit 2260ffc
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public SafeFuture<Boolean> hasCustodyDataColumnSidecar(DataColumnSlotAndIdentifi
SafeFuture<Optional<Boolean>> existingFuture =
delegate
.hasCustodyDataColumnSidecar(columnId)
.thenApply(doesExist -> doesExist ? Optional.empty() : Optional.of(true));
.thenApply(doesExist -> doesExist ? Optional.of(true) : Optional.empty());
return anyNonEmpty(pendingFuture, existingFuture)
.thenApply(maybeResult -> maybeResult.orElse(false));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,13 @@ private void advanceTimeGradually(Duration delta) {
void testLongPollingColumnRequest() throws Exception {
SafeFuture<Optional<DataColumnSidecar>> fRet0 =
custody.getCustodyDataColumnSidecar(columnId10_0);
SafeFuture<Boolean> fHas0 = custody.hasCustodyDataColumnSidecar(columnId10_0);
SafeFuture<Optional<DataColumnSidecar>> fRet0_1 =
custody.getCustodyDataColumnSidecar(columnId10_0);
SafeFuture<Boolean> fHas0_1 = custody.hasCustodyDataColumnSidecar(columnId10_0);
SafeFuture<Optional<DataColumnSidecar>> fRet1 =
custody.getCustodyDataColumnSidecar(columnId10_1);
SafeFuture<Boolean> fHas1 = custody.hasCustodyDataColumnSidecar(columnId10_1);

advanceTimeGradually(currentSlotTimeout.minus(dbDelay).minus(ofMillis(1)));

Expand All @@ -115,12 +118,16 @@ void testLongPollingColumnRequest() throws Exception {
advanceTimeGradually(dbDelay);

assertThat(fRet0).isCompletedWithValue(Optional.of(sidecar10_0));
assertThat(fHas0).isCompletedWithValue(true);
assertThat(fRet0_1).isCompletedWithValue(Optional.of(sidecar10_0));
assertThat(fHas0_1).isCompletedWithValue(true);
assertThat(fRet1).isNotDone();
assertThat(fHas1).isNotDone();

advanceTimeGradually(currentSlotTimeout);

assertThat(fRet1).isCompletedWithValue(Optional.empty());
assertThat(fHas1).isCompletedWithValue(false);
}

@Test
Expand All @@ -129,6 +136,7 @@ void testPendingRequestIsExecutedWhenLongReadQuickWrite() throws Exception {
delayedDb.setDelay(ofMillis(10));
SafeFuture<Optional<DataColumnSidecar>> fRet0 =
custody.getCustodyDataColumnSidecar(columnId10_0);
SafeFuture<Boolean> fHas0 = custody.hasCustodyDataColumnSidecar(columnId10_0);
advanceTimeGradually(ofMillis(1));

// quicker DB write
Expand All @@ -137,6 +145,7 @@ void testPendingRequestIsExecutedWhenLongReadQuickWrite() throws Exception {

advanceTimeGradually(ofMillis(10));
assertThat(fRet0).isCompletedWithValue(Optional.ofNullable(sidecar10_0));
assertThat(fHas0).isCompletedWithValue(true);
}

@Test
Expand All @@ -150,9 +159,11 @@ void testPendingRequestIsExecutedWhenLongWriteQuickRead() throws Exception {
delayedDb.setDelay(ofMillis(5));
SafeFuture<Optional<DataColumnSidecar>> fRet0 =
custody.getCustodyDataColumnSidecar(columnId10_0);
SafeFuture<Boolean> fHas0 = custody.hasCustodyDataColumnSidecar(columnId10_0);

advanceTimeGradually(ofMillis(50));
assertThat(fRet0).isCompletedWithValue(Optional.ofNullable(sidecar10_0));
assertThat(fHas0).isCompletedWithValue(true);
}

@Test
Expand All @@ -163,31 +174,38 @@ void testEmptyIsNotRetrunedImmediatelyAtBeginningOfCurrentSlot() {
custody.onSlot(UInt64.valueOf(10));
SafeFuture<Optional<DataColumnSidecar>> fRet0 =
custody.getCustodyDataColumnSidecar(columnId10_0);
SafeFuture<Boolean> fHas0 = custody.hasCustodyDataColumnSidecar(columnId10_0);

advanceTimeGradually(ofMillis(100));
assertThat(fRet0).isNotDone();
assertThat(fHas0).isNotDone();

advanceTimeGradually(currentSlotTimeout);
assertThat(fRet0).isCompletedWithValue(Optional.empty());
assertThat(fHas0).isCompletedWithValue(false);
}

@Test
void testOptionalEmptyIsReturnedOnTimeout() {
SafeFuture<Optional<DataColumnSidecar>> fRet0 =
custody.getCustodyDataColumnSidecar(columnId10_0);
SafeFuture<Boolean> fHas0 = custody.hasCustodyDataColumnSidecar(columnId10_0);

custody.onSlot(UInt64.valueOf(9));
advanceTimeGradually(currentSlotTimeout.plusMillis(100));

assertThat(fRet0).isNotDone();
assertThat(fHas0).isNotDone();

custody.onSlot(UInt64.valueOf(10));

advanceTimeGradually(currentSlotTimeout.minusMillis(10));
assertThat(fRet0).isNotDone();
assertThat(fHas0).isNotDone();

advanceTimeGradually(Duration.ofMillis(110));
assertThat(fRet0).isCompletedWithValue(Optional.empty());
assertThat(fHas0).isCompletedWithValue(false);
}

@Test
Expand All @@ -197,7 +215,35 @@ void testOptionalEmptyIsReturnedImmediatelyForPastSlot() {

SafeFuture<Optional<DataColumnSidecar>> fRet =
custody.getCustodyDataColumnSidecar(columnId10_0);
SafeFuture<Boolean> fHas0 = custody.hasCustodyDataColumnSidecar(columnId10_0);
advanceTimeGradually(Duration.ofMillis(20)); // more than db delay
assertThat(fRet).isCompletedWithValue(Optional.empty());
assertThat(fHas0).isCompletedWithValue(false);
}

@Test
void testTimeoutOccursForAll() {
SafeFuture<Optional<DataColumnSidecar>> fRet0 =
custody.getCustodyDataColumnSidecar(columnId10_0);
SafeFuture<Boolean> fHas0 = custody.hasCustodyDataColumnSidecar(columnId10_0);
SafeFuture<Optional<DataColumnSidecar>> fRet1 =
custody.getCustodyDataColumnSidecar(columnId10_0);
SafeFuture<Boolean> fHas1 = custody.hasCustodyDataColumnSidecar(columnId10_1);

custody.onSlot(UInt64.valueOf(9));
advanceTimeGradually(currentSlotTimeout.plusMillis(100));
custody.onSlot(UInt64.valueOf(10));
advanceTimeGradually(currentSlotTimeout.minusMillis(10));

assertThat(fRet0).isNotDone();
assertThat(fHas0).isNotDone();
assertThat(fRet1).isNotDone();
assertThat(fHas1).isNotDone();

advanceTimeGradually(Duration.ofMillis(110));
assertThat(fRet0).isCompletedWithValue(Optional.empty());
assertThat(fHas0).isCompletedWithValue(false);
assertThat(fRet1).isCompletedWithValue(Optional.empty());
assertThat(fHas1).isCompletedWithValue(false);
}
}

0 comments on commit 2260ffc

Please sign in to comment.