This commit is contained in:
2026-08-05 16:26:10 +08:00
parent 06f7f9d2f2
commit 5ba9716e12
155 changed files with 2883 additions and 551 deletions
@@ -25,7 +25,6 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -65,7 +64,7 @@ class EventInboxServiceTest {
@Test
void identicalEventAndDigestDoesNotPersistAgain() {
EventEnvelope envelope = withHash(envelope("sha256:placeholder"));
BridgeInbox existing = existing(envelope.payloadHash());
BridgeInbox existing = existing(envelope, envelope.payloadHash());
when(store.findByEventId(envelope.eventId())).thenReturn(existing);
assertThat(service.receive(envelope).result()).isEqualTo("重复");
@@ -74,17 +73,43 @@ class EventInboxServiceTest {
@Test
void sameEventWithDifferentDigestRejectsOverwrite() {
EventEnvelope envelope = withHash(envelope("sha256:placeholder"));
when(store.findByEventId(envelope.eventId())).thenReturn(existing("sha256:" + "0".repeat(64)));
when(store.findByEventId(envelope.eventId()))
.thenReturn(existing(envelope, "sha256:" + "0".repeat(64)));
assertThatThrownBy(() -> service.receive(envelope))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("拒绝覆盖");
}
@Test
void sameEventAndPayloadWithDifferentTenantRejectsOverwrite() {
EventEnvelope original = withHash(envelope("sha256:placeholder"));
EventEnvelope otherTenant = withTenant(original, "20002");
when(store.findByEventId(otherTenant.eventId()))
.thenReturn(existing(original, original.payloadHash()));
assertThatThrownBy(() -> service.receive(otherTenant))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("信封身份不一致");
}
@Test
void sameEventAndPayloadWithDifferentAggregateVersionRejectsOverwrite() {
EventEnvelope original = withHash(envelope("sha256:placeholder"));
EventEnvelope otherEnvelope = withAggregateVersion(original, 2L);
when(store.findByEventId(otherEnvelope.eventId()))
.thenReturn(existing(original, original.payloadHash()));
assertThatThrownBy(() -> service.receive(otherEnvelope))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("信封身份不一致");
}
@Test
void duplicateInsertAfterRaceIsTreatedAsDuplicate() {
EventEnvelope envelope = withHash(envelope("sha256:placeholder"));
when(store.findByEventId(envelope.eventId())).thenReturn(null, existing(envelope.payloadHash()));
when(store.findByEventId(envelope.eventId()))
.thenReturn(null, existing(envelope, envelope.payloadHash()));
doThrow(new DuplicateKeyException("并发重复")).when(store).insert(any());
assertThat(service.receive(envelope).result()).isEqualTo("重复");
@@ -115,9 +140,32 @@ class EventInboxServiceTest {
source.correlationId(), source.causationId(), source.payload(), hash);
}
private BridgeInbox existing(String hash) {
private EventEnvelope withTenant(EventEnvelope source, String tenantId) {
return new EventEnvelope(source.eventId(), source.eventType(), source.eventVersion(), source.occurredAt(),
source.sourceRegion(), source.targetRegion(), tenantId, source.aggregateType(),
source.aggregateId(), source.aggregateVersion(), source.handoverVersion(), source.traceId(),
source.correlationId(), source.causationId(), source.payload(), source.payloadHash());
}
private EventEnvelope withAggregateVersion(EventEnvelope source, Long aggregateVersion) {
return new EventEnvelope(source.eventId(), source.eventType(), source.eventVersion(), source.occurredAt(),
source.sourceRegion(), source.targetRegion(), source.tenantId(), source.aggregateType(),
source.aggregateId(), aggregateVersion, source.handoverVersion(), source.traceId(),
source.correlationId(), source.causationId(), source.payload(), source.payloadHash());
}
private BridgeInbox existing(EventEnvelope envelope, String hash) {
BridgeInbox inbox = new BridgeInbox();
inbox.setEventId("550e8400-e29b-41d4-a716-446655440000");
inbox.setEventId(envelope.eventId());
inbox.setTenantId(envelope.tenantId());
inbox.setEventType(envelope.eventType());
inbox.setEventVersion(envelope.eventVersion());
inbox.setSourceRegion(envelope.sourceRegion().name());
inbox.setTargetRegion(envelope.targetRegion().name());
inbox.setAggregateType(envelope.aggregateType());
inbox.setAggregateId(envelope.aggregateId());
inbox.setAggregateVersion(envelope.aggregateVersion());
inbox.setHandoverVersion(envelope.handoverVersion());
inbox.setPayloadHash(hash);
inbox.setReceivedAt(LocalDateTime.of(2026, 7, 29, 18, 0));
return inbox;