notify/client_session_epoch_test.go
starainrt 09d972c7b7
feat(notify): 重构通信内核并补齐 stream/bulk/record/transfer 能力
- 引入 LogicalConn/TransportConn 分层,ClientConn 保留兼容适配层
  - 新增 Stream、Bulk、RecordStream 三条数据面能力及对应控制路径
  - 完成 transfer/file 传输内核与状态快照、诊断能力
  - 补齐 reconnect、inbound dispatcher、modern psk 等基础模块
  - 增加大规模回归、并发与基准测试覆盖
  - 更新依赖库
2026-04-15 15:24:36 +08:00

82 lines
2.8 KiB
Go

package notify
import (
"context"
"errors"
"testing"
)
func TestClientStopSessionIfCurrentEpoch(t *testing.T) {
client := NewClient().(*ClientCommon)
client.markSessionStarted()
staleEpoch := client.beginClientSessionEpoch()
currentEpoch := client.beginClientSessionEpoch()
if client.stopClientSessionIfCurrent(staleEpoch, "stale", nil) {
t.Fatal("stale epoch should not stop current session")
}
status := client.Status()
if !status.Alive || status.Reason != "" || status.Err != nil {
t.Fatalf("unexpected status after stale stop: %+v", status)
}
if !client.stopClientSessionIfCurrent(currentEpoch, "current", nil) {
t.Fatal("current epoch should stop session")
}
status = client.Status()
if status.Alive || status.Reason != "current" || status.Err != nil {
t.Fatalf("unexpected status after current stop: %+v", status)
}
}
func TestClientReadErrorWithStaleEpochDoesNotStopCurrentSession(t *testing.T) {
client := NewClient().(*ClientCommon)
client.markSessionStarted()
staleEpoch := client.beginClientSessionEpoch()
currentEpoch := client.beginClientSessionEpoch()
readErr := errors.New("read failed")
client.handleTransportReadResultWithSession(context.Background(), nil, nil, 0, nil, readErr, staleEpoch)
status := client.Status()
if !status.Alive || status.Reason != "" || status.Err != nil {
t.Fatalf("unexpected status after stale read error: %+v", status)
}
client.handleTransportReadResultWithSession(context.Background(), nil, nil, 0, nil, readErr, currentEpoch)
status = client.Status()
if status.Alive || status.Reason != "client read error" || !errors.Is(status.Err, readErr) {
t.Fatalf("unexpected status after current read error: %+v", status)
}
}
func TestHeartbeatFailureWithStaleEpochDoesNotStopCurrentSession(t *testing.T) {
client := NewClient().(*ClientCommon)
client.markSessionStarted()
staleEpoch := client.beginClientSessionEpoch()
currentEpoch := client.beginClientSessionEpoch()
heartbeatErr := errors.New("heartbeat failed")
failedCount, stop := client.handleHeartbeatResultWithSession(staleEpoch, heartbeatErr, 2)
if failedCount != 3 || !stop {
t.Fatalf("unexpected stale heartbeat result: failedCount=%d stop=%v", failedCount, stop)
}
status := client.Status()
if !status.Alive || status.Reason != "" || status.Err != nil {
t.Fatalf("unexpected status after stale heartbeat error: %+v", status)
}
failedCount, stop = client.handleHeartbeatResultWithSession(currentEpoch, heartbeatErr, 2)
if failedCount != 3 || !stop {
t.Fatalf("unexpected current heartbeat result: failedCount=%d stop=%v", failedCount, stop)
}
status = client.Status()
if status.Alive || status.Reason != "heartbeat failed more than 3 times" || status.Err == nil || status.Err.Error() != "heartbeat failed more than 3 times" {
t.Fatalf("unexpected status after current heartbeat error: %+v", status)
}
}