148 lines
6.4 KiB
Kotlin
148 lines
6.4 KiB
Kotlin
package com.gzzn.omms.msgexchange.ingress
|
||
|
||
import com.gzzn.omms.msgexchange.config.PipelineProps
|
||
import com.gzzn.omms.msgexchange.domain.ErrorClass
|
||
import com.gzzn.omms.msgexchange.domain.ProcStatus
|
||
import com.gzzn.omms.msgexchange.infra.stub.StubInbox
|
||
import com.gzzn.omms.msgexchange.infra.stub.StubInboxCursor
|
||
import com.gzzn.omms.msgexchange.infra.stub.StubPipelineTx
|
||
import com.gzzn.omms.msgexchange.infra.stub.StubProcState
|
||
import org.junit.jupiter.api.Assertions.assertEquals
|
||
import org.junit.jupiter.api.Assertions.assertFalse
|
||
import org.junit.jupiter.api.Assertions.assertNotNull
|
||
import org.junit.jupiter.api.Assertions.assertNull
|
||
import org.junit.jupiter.api.BeforeEach
|
||
import org.junit.jupiter.api.Test
|
||
import java.time.Instant
|
||
|
||
/**
|
||
* 收报环节最要紧的几条规矩:
|
||
* - 取新消息只看 ID,不看处理标记。处理完却没能回填的行(尤其是永远不回填的死信)
|
||
* 不允许占住批次,也不允许挡住后面的新消息——这是曾经的线上隐患;
|
||
* - 水位只在成功登记后才推进,而且和登记写在同一个事务里,中断后重扫就能补齐;
|
||
* - 遇到 ID 缺口先停下来(可能有更小的消息还没到),缺口等太久则跳过(否则水位永远卡住);
|
||
* - 这一层不碰信箱的处理标记,标记留给回填环节写。
|
||
*/
|
||
class InboxPollerTest {
|
||
|
||
private val t0: Instant = Instant.parse("2026-09-08T03:00:00Z")
|
||
private val props = PipelineProps()
|
||
|
||
private lateinit var inbox: StubInbox
|
||
private lateinit var proc: StubProcState
|
||
private lateinit var cursor: StubInboxCursor
|
||
private lateinit var poller: InboxPoller
|
||
|
||
@BeforeEach
|
||
fun setUp() {
|
||
inbox = StubInbox().apply { clear() }
|
||
proc = StubProcState().apply { clear() }
|
||
cursor = StubInboxCursor().apply { clear() }
|
||
poller = InboxPoller(inbox, proc, cursor, StubPipelineTx(), props)
|
||
}
|
||
|
||
@Test
|
||
fun `external rows are enqueued in id order and advance the watermark without marking the mailbox`() {
|
||
val first = inbox.simulateExternalWrite("<MSG/>")
|
||
val second = inbox.simulateExternalWrite("<MSG/>")
|
||
|
||
assertEquals(2, poller.pollOnce(t0))
|
||
|
||
assertEquals(ProcStatus.PENDING, proc.find(first)!!.state)
|
||
assertEquals(ProcStatus.PENDING, proc.find(second)!!.state)
|
||
assertEquals(second, cursor.cursor.committedUpTo)
|
||
assertNull(cursor.cursor.holeSince)
|
||
// 收报只写自有库,不碰信箱的处理标记
|
||
assertFalse(inbox.isMarked(first))
|
||
assertEquals(0, poller.pollOnce(t0)) // 重复扫描幂等
|
||
}
|
||
|
||
/**
|
||
* 回归用例:处理完却永远不会回填的行(典型是解码失败的死信)曾经占满每一批的名额,
|
||
* 导致收报整体停摆。取新消息这件事必须和"有没有处理标记"彻底分开。
|
||
*/
|
||
@Test
|
||
fun `terminal rows without a mailbox mark do not block discovery of later messages`() {
|
||
props.pipeline.claimBatch = 3
|
||
val dead = (1..3).map { inbox.simulateExternalWrite("<MSG/>") }
|
||
assertEquals(3, poller.pollOnce(t0))
|
||
dead.forEach {
|
||
proc.markTerminal(it, ProcStatus.DEAD, errorClass = ErrorClass.MALFORMED, lastError = "raw-missing")
|
||
}
|
||
|
||
val fresh = inbox.simulateExternalWrite("<MSG/>")
|
||
|
||
assertEquals(1, poller.pollOnce(t0))
|
||
assertEquals(ProcStatus.PENDING, proc.find(fresh)!!.state)
|
||
assertEquals(fresh, cursor.cursor.committedUpTo)
|
||
}
|
||
|
||
@Test
|
||
fun `watermark stops at a hole so later ids cannot overtake a missing smaller id`() {
|
||
val first = inbox.simulateExternalWrite("<MSG/>")
|
||
val hole = inbox.simulateExternalWrite("<MSG/>")
|
||
val afterHole = inbox.simulateExternalWrite("<MSG/>")
|
||
inbox.removeRow(hole)
|
||
|
||
assertEquals(1, poller.pollOnce(t0))
|
||
|
||
assertEquals(first, cursor.cursor.committedUpTo)
|
||
assertNotNull(cursor.cursor.holeSince)
|
||
assertNull(proc.find(afterHole)) // 不得越过空洞入队(FIFO)
|
||
assertEquals(0, poller.pollOnce(t0.plusSeconds(60))) // 宽限期内水位不推进
|
||
assertEquals(first, cursor.cursor.committedUpTo)
|
||
}
|
||
|
||
@Test
|
||
fun `an aged hole is released and later ids resume enqueuing`() {
|
||
val hole = inbox.simulateExternalWrite("<MSG/>")
|
||
val afterHole = inbox.simulateExternalWrite("<MSG/>")
|
||
inbox.removeRow(hole)
|
||
poller.pollOnce(t0) // 记录空洞观测时刻
|
||
|
||
val agedOut = t0.plus(props.pipeline.maxCommitDelay)
|
||
assertEquals(0, poller.pollOnce(agedOut)) // 空洞判永久:推进水位但不越过入队
|
||
|
||
assertNull(cursor.cursor.holeSince)
|
||
assertEquals(afterHole - 1, cursor.cursor.committedUpTo)
|
||
assertEquals(1, poller.pollOnce(agedOut)) // 下一轮恢复发现
|
||
assertEquals(afterHole, cursor.cursor.committedUpTo)
|
||
assertNotNull(proc.find(afterHole))
|
||
}
|
||
|
||
@Test
|
||
fun `compat http path and poller do not double enqueue the same message`() {
|
||
val receipt = InboxService(inbox, proc).accept("<MSG/>")
|
||
|
||
assertEquals(0, poller.pollOnce(t0))
|
||
assertEquals(receipt.msgId, cursor.cursor.committedUpTo) // 已在 PG:读取进度照常推进
|
||
assertNotNull(proc.find(receipt.msgId))
|
||
}
|
||
|
||
@Test
|
||
fun `a newly exposed hole does not inherit the age of the previous hole`() {
|
||
val first = inbox.simulateExternalWrite("<MSG/>")
|
||
val oldHole = inbox.simulateExternalWrite("<MSG/>")
|
||
val third = inbox.simulateExternalWrite("<MSG/>")
|
||
val newHole = inbox.simulateExternalWrite("<MSG/>")
|
||
val fifth = inbox.simulateExternalWrite("<MSG/>")
|
||
inbox.removeRow(oldHole)
|
||
inbox.removeRow(newHole)
|
||
|
||
assertEquals(1, poller.pollOnce(t0))
|
||
val almostAged = t0.plus(props.pipeline.maxCommitDelay).minusSeconds(1)
|
||
inbox.restoreRow(oldHole, "<MSG/>", almostAged)
|
||
|
||
assertEquals(2, poller.pollOnce(almostAged))
|
||
assertEquals(third, cursor.cursor.committedUpTo)
|
||
assertEquals(almostAged, cursor.cursor.holeSince)
|
||
assertNull(proc.find(fifth))
|
||
|
||
// 旧空洞的期限已到,但新空洞必须获得完整等待窗口。
|
||
assertEquals(0, poller.pollOnce(t0.plus(props.pipeline.maxCommitDelay)))
|
||
assertEquals(third, cursor.cursor.committedUpTo)
|
||
assertNull(proc.find(fifth))
|
||
assertEquals(first + 2, third)
|
||
}
|
||
}
|