Skip to content

Commit 1abecdb

Browse files
Johnathan WalkerCopilot
andcommitted
Add regression test and fix for missing offsetString in EventContext
Added EventContextTest.updateCheckpointAsyncSetsOffsetString() which verifies that offsetString is populated on the Checkpoint passed to the store. This test fails without the fix (offsetString is null) and passes with it. Fix: added setOffsetString(eventData.getOffsetString()) in EventContext.updateCheckpointAsync(), matching the pattern already used in EventBatchContext. Fixes #46752 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent d55cdf5 commit 1abecdb

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

sdk/eventhubs/azure-messaging-eventhubs/src/main/java/com/azure/messaging/eventhubs/models/EventContext.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public Mono<Void> updateCheckpointAsync() {
9494
.setConsumerGroup(partitionContext.getConsumerGroup())
9595
.setPartitionId(partitionContext.getPartitionId())
9696
.setSequenceNumber(eventData.getSequenceNumber())
97+
.setOffsetString(eventData.getOffsetString())
9798
.setOffset(eventData.getOffset());
9899
return this.checkpointStore.updateCheckpoint(checkpoint);
99100
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.messaging.eventhubs.models;
5+
6+
import com.azure.messaging.eventhubs.CheckpointStore;
7+
import com.azure.messaging.eventhubs.EventData;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
import org.mockito.ArgumentCaptor;
11+
import org.mockito.Mock;
12+
import org.mockito.MockitoAnnotations;
13+
import reactor.core.publisher.Mono;
14+
import reactor.test.StepVerifier;
15+
16+
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
import static org.junit.jupiter.api.Assertions.assertNotNull;
18+
import static org.mockito.ArgumentMatchers.any;
19+
import static org.mockito.Mockito.verify;
20+
import static org.mockito.Mockito.when;
21+
22+
/**
23+
* Tests for {@link EventContext}.
24+
*/
25+
class EventContextTest {
26+
private final PartitionContext partitionContext
27+
= new PartitionContext("TEST_NAMESPACE", "TEST_EVENT_HUB", "TEST_DEFAULT_GROUP", "TEST_PARTITION_ID");
28+
29+
@Mock
30+
private CheckpointStore checkpointStore;
31+
@Mock
32+
private EventData eventData;
33+
34+
@BeforeEach
35+
void beforeEach() {
36+
MockitoAnnotations.initMocks(this);
37+
}
38+
39+
/**
40+
* Verifies that updateCheckpointAsync sets offsetString on the checkpoint.
41+
* Regression test for https://github.com/Azure/azure-sdk-for-java/issues/46752
42+
* where only setOffset(Long) was called, causing BlobCheckpointStore to store
43+
* null offset because it reads getOffsetString().
44+
*/
45+
@Test
46+
void updateCheckpointAsyncSetsOffsetString() {
47+
// Arrange
48+
final Long sequenceNumber = 10L;
49+
final Long offset = 15L;
50+
final String offsetString = "15";
51+
52+
when(eventData.getSequenceNumber()).thenReturn(sequenceNumber);
53+
when(eventData.getOffset()).thenReturn(offset);
54+
when(eventData.getOffsetString()).thenReturn(offsetString);
55+
when(checkpointStore.updateCheckpoint(any(Checkpoint.class))).thenReturn(Mono.empty());
56+
57+
final EventContext context = new EventContext(partitionContext, eventData, checkpointStore, null);
58+
59+
// Act
60+
StepVerifier.create(context.updateCheckpointAsync()).verifyComplete();
61+
62+
// Assert - offsetString must be set on the checkpoint passed to the store
63+
ArgumentCaptor<Checkpoint> captor = ArgumentCaptor.forClass(Checkpoint.class);
64+
verify(checkpointStore).updateCheckpoint(captor.capture());
65+
66+
Checkpoint captured = captor.getValue();
67+
assertEquals(partitionContext.getFullyQualifiedNamespace(), captured.getFullyQualifiedNamespace());
68+
assertEquals(partitionContext.getEventHubName(), captured.getEventHubName());
69+
assertEquals(partitionContext.getConsumerGroup(), captured.getConsumerGroup());
70+
assertEquals(partitionContext.getPartitionId(), captured.getPartitionId());
71+
assertEquals(sequenceNumber, captured.getSequenceNumber());
72+
assertEquals(offset, captured.getOffset());
73+
assertNotNull(captured.getOffsetString(), "offsetString must not be null - BlobCheckpointStore depends on it");
74+
assertEquals(offsetString, captured.getOffsetString());
75+
}
76+
}

0 commit comments

Comments
 (0)