fix(notify): 修复传输生命周期竞态,完善背压与协议边界
- 完善 stream/bulk DataID 分配、预留和双向命名空间,修复并发打开及 dedicated/shared 回退时的 ID 冲突 - 将收发、回复、恢复任务和 sidecar 绑定原始会话与物理连接,防止重连后的旧消息误操作新连接 - 加强 close/reset 身份校验及实例移除检查,修复 dedicated attach 失败、通道引用和资源回收竞态 - 收紧批量发送器停止准入,确保在途入队完成后统一清理请求、缓冲区和等待者 - 修复 record 满队列死锁、取消时序号消耗及关闭竞态,确保关闭有界并返回真实错误 - 增加协商式 record 逻辑半关闭,保留反向 ACK;通过 reset 传递 RecordFailure,避免背压掩盖原始失败原因 - 补齐帧长度、批次数量、序号溢出和未确认窗口校验,提前拒绝超限数据并按字节预算拆批 - 为入站分发增加全局及单连接的条数、字节预算和阻塞背压,关闭时唤醒等待者,消除正常断连日志噪音 - 完善 bulk 窗口释放失败处理与传输诊断,补充并发、重连、背压、协议边界及真实 TCP 回归覆盖
This commit is contained in:
@@ -2,6 +2,7 @@ package notify
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@@ -101,3 +102,233 @@ func indexOfString(list []string, target string) int {
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func TestInboundDispatcherBoundsQueuedItems(t *testing.T) {
|
||||
dispatcher := newInboundDispatcherWithLimits(1, 1<<20)
|
||||
defer dispatcher.CloseAndWait()
|
||||
|
||||
releaseFirst := make(chan struct{})
|
||||
firstStarted := make(chan struct{})
|
||||
if !dispatcher.DispatchSized("alpha", 1, func() {
|
||||
close(firstStarted)
|
||||
<-releaseFirst
|
||||
}) {
|
||||
t.Fatal("dispatch first item failed")
|
||||
}
|
||||
select {
|
||||
case <-firstStarted:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("timed out waiting for first item")
|
||||
}
|
||||
|
||||
if !dispatcher.DispatchSized("alpha", 1, func() {}) {
|
||||
t.Fatal("dispatch second item failed")
|
||||
}
|
||||
|
||||
thirdDone := make(chan bool, 1)
|
||||
go func() {
|
||||
thirdDone <- dispatcher.DispatchSized("alpha", 1, func() {})
|
||||
}()
|
||||
select {
|
||||
case <-thirdDone:
|
||||
t.Fatal("dispatch exceeded the queue limit without backpressure")
|
||||
case <-time.After(100 * time.Millisecond):
|
||||
}
|
||||
|
||||
close(releaseFirst)
|
||||
select {
|
||||
case ok := <-thirdDone:
|
||||
if !ok {
|
||||
t.Fatal("blocked dispatch failed after room became available")
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("blocked dispatch was not released when the queue drained")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInboundDispatcherBoundsQueuedBytes(t *testing.T) {
|
||||
dispatcher := newInboundDispatcherWithLimits(1000, 10)
|
||||
defer dispatcher.CloseAndWait()
|
||||
|
||||
releaseFirst := make(chan struct{})
|
||||
firstStarted := make(chan struct{})
|
||||
if !dispatcher.DispatchSized("alpha", 1, func() {
|
||||
close(firstStarted)
|
||||
<-releaseFirst
|
||||
}) {
|
||||
t.Fatal("dispatch first item failed")
|
||||
}
|
||||
select {
|
||||
case <-firstStarted:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("timed out waiting for first item")
|
||||
}
|
||||
|
||||
if !dispatcher.DispatchSized("alpha", 6, func() {}) {
|
||||
t.Fatal("dispatch second item failed")
|
||||
}
|
||||
|
||||
thirdDone := make(chan bool, 1)
|
||||
go func() {
|
||||
thirdDone <- dispatcher.DispatchSized("alpha", 6, func() {})
|
||||
}()
|
||||
select {
|
||||
case <-thirdDone:
|
||||
t.Fatal("dispatch exceeded the byte limit without backpressure")
|
||||
case <-time.After(100 * time.Millisecond):
|
||||
}
|
||||
|
||||
close(releaseFirst)
|
||||
select {
|
||||
case ok := <-thirdDone:
|
||||
if !ok {
|
||||
t.Fatal("blocked dispatch failed after room became available")
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("blocked dispatch was not released when the queue drained")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInboundDispatcherCloseUnblocksBlockedDispatch(t *testing.T) {
|
||||
dispatcher := newInboundDispatcherWithLimits(1, 1<<20)
|
||||
|
||||
releaseFirst := make(chan struct{})
|
||||
firstStarted := make(chan struct{})
|
||||
if !dispatcher.DispatchSized("alpha", 1, func() {
|
||||
close(firstStarted)
|
||||
<-releaseFirst
|
||||
}) {
|
||||
t.Fatal("dispatch first item failed")
|
||||
}
|
||||
select {
|
||||
case <-firstStarted:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("timed out waiting for first item")
|
||||
}
|
||||
if !dispatcher.DispatchSized("alpha", 1, func() {}) {
|
||||
t.Fatal("dispatch second item failed")
|
||||
}
|
||||
|
||||
blockedDone := make(chan bool, 1)
|
||||
go func() {
|
||||
blockedDone <- dispatcher.DispatchSized("alpha", 1, func() {})
|
||||
}()
|
||||
select {
|
||||
case <-blockedDone:
|
||||
t.Fatal("dispatch exceeded the queue limit without backpressure")
|
||||
case <-time.After(100 * time.Millisecond):
|
||||
}
|
||||
|
||||
closeWaitDone := make(chan struct{})
|
||||
go func() {
|
||||
dispatcher.CloseAndWait()
|
||||
close(closeWaitDone)
|
||||
}()
|
||||
|
||||
select {
|
||||
case ok := <-blockedDone:
|
||||
if ok {
|
||||
t.Fatal("dispatch accepted work after the dispatcher closed")
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("CloseAndWait did not release the blocked dispatch")
|
||||
}
|
||||
|
||||
close(releaseFirst)
|
||||
select {
|
||||
case <-closeWaitDone:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("CloseAndWait did not return after in-flight work finished")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInboundDispatcherConcurrentDispatchDrainsEveryItem(t *testing.T) {
|
||||
dispatcher := newInboundDispatcherWithLimits(8, 1<<20)
|
||||
|
||||
const producers = 8
|
||||
const perProducer = 64
|
||||
var handled atomic.Int64
|
||||
var wg sync.WaitGroup
|
||||
for p := 0; p < producers; p++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for i := 0; i < perProducer; i++ {
|
||||
if !dispatcher.DispatchSized("shared", 1, func() {
|
||||
handled.Add(1)
|
||||
time.Sleep(50 * time.Microsecond)
|
||||
}) {
|
||||
t.Errorf("dispatch rejected before close")
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
dispatcher.CloseAndWait()
|
||||
|
||||
if got, want := handled.Load(), int64(producers*perProducer); got != want {
|
||||
t.Fatalf("handled=%d, want %d", got, want)
|
||||
}
|
||||
dispatcher.mu.Lock()
|
||||
queued, queuedBytes := dispatcher.queued, dispatcher.queuedBytes
|
||||
dispatcher.mu.Unlock()
|
||||
if queued != 0 || queuedBytes != 0 {
|
||||
t.Fatalf("queue accounting leaked: queued=%d bytes=%d", queued, queuedBytes)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInboundDispatcherPerSourceLimitDoesNotStarveOtherSources(t *testing.T) {
|
||||
dispatcher := newInboundDispatcherWithCaps(64, 1<<20, 1, 1<<20)
|
||||
defer dispatcher.CloseAndWait()
|
||||
|
||||
releaseFirst := make(chan struct{})
|
||||
firstStarted := make(chan struct{})
|
||||
if !dispatcher.DispatchSized("alpha", 1, func() {
|
||||
close(firstStarted)
|
||||
<-releaseFirst
|
||||
}) {
|
||||
t.Fatal("dispatch first alpha item failed")
|
||||
}
|
||||
select {
|
||||
case <-firstStarted:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("timed out waiting for first alpha item")
|
||||
}
|
||||
if !dispatcher.DispatchSized("alpha", 1, func() {}) {
|
||||
t.Fatal("dispatch second alpha item failed")
|
||||
}
|
||||
|
||||
alphaBlocked := make(chan bool, 1)
|
||||
go func() {
|
||||
alphaBlocked <- dispatcher.DispatchSized("alpha", 1, func() {})
|
||||
}()
|
||||
select {
|
||||
case <-alphaBlocked:
|
||||
t.Fatal("alpha exceeded its per-source queue budget")
|
||||
case <-time.After(100 * time.Millisecond):
|
||||
}
|
||||
|
||||
betaDone := make(chan bool, 1)
|
||||
go func() {
|
||||
betaDone <- dispatcher.DispatchSized("beta", 1, func() {})
|
||||
}()
|
||||
select {
|
||||
case ok := <-betaDone:
|
||||
if !ok {
|
||||
t.Fatal("beta dispatch failed")
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("a saturated alpha source starved the beta source")
|
||||
}
|
||||
|
||||
close(releaseFirst)
|
||||
select {
|
||||
case ok := <-alphaBlocked:
|
||||
if !ok {
|
||||
t.Fatal("blocked alpha dispatch failed after room became available")
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("blocked alpha dispatch was not released")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user