181 lines
5.3 KiB
Go
181 lines
5.3 KiB
Go
// pool_client_test.go Day 32 集成:Submit 收 Resp / Deliver 解析 / 连接池。
|
||
package cmpp
|
||
|
||
import (
|
||
"context"
|
||
"sync"
|
||
"testing"
|
||
"time"
|
||
)
|
||
|
||
// TestNewPool_SizeCap P1 压测优化(09-27):上限 16→64——conn_count=50
|
||
// 此前被静默截断为 16,华为云实测 32 并发连接稳定;64 为防呆上限,
|
||
// 超配(100)截断到 64,非法(0)夹到 1。
|
||
func TestNewPool_SizeCap(t *testing.T) {
|
||
if got := len(NewPool(Config{}, 100).clients); got != 64 {
|
||
t.Fatalf("size=100 应截断到 64,实得 %d", got)
|
||
}
|
||
if got := len(NewPool(Config{}, 50).clients); got != 50 {
|
||
t.Fatalf("size=50 应原样保留(旧上限 16 会截断),实得 %d", got)
|
||
}
|
||
if got := len(NewPool(Config{}, 0).clients); got != 1 {
|
||
t.Fatalf("size=0 应夹到 1,实得 %d", got)
|
||
}
|
||
}
|
||
|
||
// TestClient_Submit 验收:发送消息能收到 Submit_Resp(网关分配 MsgID,Result=0)。
|
||
func TestClient_Submit(t *testing.T) {
|
||
g := newFakeGateway(t, "s3cret", true, true)
|
||
c := NewClient(fastCfg(g.ln.Addr().String()))
|
||
if err := c.Connect(context.Background()); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
defer func() { _ = c.Close() }()
|
||
|
||
req := &SubmitReq{
|
||
RegisteredDelivery: 1, ServiceID: "SMS", MsgFmt: MsgFmtASCII,
|
||
MsgSrc: "901234", FeeType: "01", SrcID: "1065712345",
|
||
DestTerminalIDs: []string{"13800001111"}, Content: []byte("verify"),
|
||
}
|
||
resp, err := c.Submit(req)
|
||
if err != nil {
|
||
t.Fatalf("submit: %v", err)
|
||
}
|
||
if resp.Result != 0 || resp.MsgID == 0 {
|
||
t.Fatalf("submit_resp: %+v", resp)
|
||
}
|
||
if g.submits.Load() != 1 {
|
||
t.Fatalf("网关 submit 计数 = %d", g.submits.Load())
|
||
}
|
||
// 拒绝场景:网关回 result=1
|
||
g.setReject(true)
|
||
if resp, err := c.Submit(req); err != nil || resp.Result != 1 {
|
||
t.Fatalf("拒绝场景: resp=%+v err=%v", resp, err)
|
||
}
|
||
}
|
||
|
||
// TestClient_Deliver 验收:模拟通道下发 Deliver(状态报告+上行),解析正确并自动应答。
|
||
func TestClient_Deliver(t *testing.T) {
|
||
g := newFakeGateway(t, "s3cret", true, true)
|
||
c := NewClient(fastCfg(g.ln.Addr().String()))
|
||
if err := c.Connect(context.Background()); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
defer func() { _ = c.Close() }()
|
||
|
||
got := make(chan *Deliver, 2)
|
||
c.OnDeliver = func(d *Deliver) { got <- d }
|
||
|
||
// 状态报告
|
||
g.pushDeliver(&Deliver{
|
||
MsgID: 12345, DestID: "10086", MsgFmt: MsgFmtASCII, SrcTerminal: "13800001111",
|
||
IsReport: true,
|
||
Report: &ReportInfo{MsgID: 12345, Stat: "DELIVRD", SubmitTime: "0904113001",
|
||
DoneTime: "0904113005", DestTerminalID: "13800001111", SMSCSequence: 7},
|
||
})
|
||
// 上行短消息
|
||
g.pushDeliver(&Deliver{
|
||
MsgID: 222, DestID: "10086", MsgFmt: MsgFmtASCII,
|
||
SrcTerminal: "13800001111", Content: []byte("TD"),
|
||
})
|
||
timeout := time.After(2 * time.Second)
|
||
var report, uplink *Deliver
|
||
for report == nil || uplink == nil {
|
||
select {
|
||
case d := <-got:
|
||
if d.IsReport {
|
||
report = d
|
||
} else {
|
||
uplink = d
|
||
}
|
||
case <-timeout:
|
||
t.Fatalf("未收到下发: report=%v uplink=%v", report != nil, uplink != nil)
|
||
}
|
||
}
|
||
if report.Report == nil || report.Report.Stat != "DELIVRD" || !report.Report.IsSuccess() {
|
||
t.Fatalf("报告解析: %+v", report.Report)
|
||
}
|
||
if string(uplink.Content) != "TD" || uplink.SrcTerminal != "13800001111" {
|
||
t.Fatalf("上行解析: %+v", uplink)
|
||
}
|
||
// 客户端自动回 Deliver_Resp ×2
|
||
deadline := time.Now().Add(2 * time.Second)
|
||
for time.Now().Before(deadline) && g.deliverAck.Load() < 2 {
|
||
time.Sleep(20 * time.Millisecond)
|
||
}
|
||
if g.deliverAck.Load() < 2 {
|
||
t.Fatalf("deliver_resp 计数 = %d, 期望 2", g.deliverAck.Load())
|
||
}
|
||
}
|
||
|
||
// TestPool_SubmitAndDeliver 验收:连接池 2 条并行提交 + Deliver 透传。
|
||
func TestPool_SubmitAndDeliver(t *testing.T) {
|
||
g := newFakeGateway(t, "s3cret", true, true)
|
||
cfg := fastCfg(g.ln.Addr().String())
|
||
p := NewPool(cfg, 2)
|
||
|
||
delivers := make(chan *Deliver, 8)
|
||
p.OnDeliver(func(d *Deliver) { delivers <- d })
|
||
ctx, cancel := context.WithCancel(context.Background())
|
||
defer cancel()
|
||
p.Start(ctx)
|
||
defer p.Stop()
|
||
|
||
// 等两条连接就绪
|
||
deadline := time.Now().Add(3 * time.Second)
|
||
for time.Now().Before(deadline) && p.Healthy() < 2 {
|
||
time.Sleep(20 * time.Millisecond)
|
||
}
|
||
if p.Healthy() != 2 {
|
||
t.Fatalf("池健康连接 = %d, 期望 2", p.Healthy())
|
||
}
|
||
// 并发 10 次提交全成功
|
||
var wg sync.WaitGroup
|
||
errCh := make(chan error, 10)
|
||
for i := 0; i < 10; i++ {
|
||
wg.Add(1)
|
||
go func() {
|
||
defer wg.Done()
|
||
resp, err := p.Submit(&SubmitReq{
|
||
ServiceID: "SMS", MsgFmt: MsgFmtASCII, MsgSrc: "901234",
|
||
FeeType: "01", SrcID: "1065712345",
|
||
DestTerminalIDs: []string{"13800001111"}, Content: []byte("pool"),
|
||
})
|
||
if err != nil {
|
||
errCh <- err
|
||
return
|
||
}
|
||
if resp.Result != 0 {
|
||
errCh <- errPoolResult
|
||
}
|
||
}()
|
||
}
|
||
wg.Wait()
|
||
select {
|
||
case err := <-errCh:
|
||
t.Fatalf("池化提交失败: %v", err)
|
||
default:
|
||
}
|
||
if g.submits.Load() != 10 {
|
||
t.Fatalf("网关 submit 总数 = %d, 期望 10", g.submits.Load())
|
||
}
|
||
// Deliver 经池透传
|
||
g.pushDeliver(&Deliver{MsgID: 1, DestID: "10086", SrcTerminal: "13800001111",
|
||
Content: []byte("hi"), MsgFmt: MsgFmtASCII})
|
||
select {
|
||
case d := <-delivers:
|
||
if string(d.Content) != "hi" {
|
||
t.Fatalf("透传内容: %q", d.Content)
|
||
}
|
||
case <-time.After(2 * time.Second):
|
||
t.Fatal("池未透传 Deliver")
|
||
}
|
||
}
|
||
|
||
// errPoolResult 测试哨兵错误。
|
||
var errPoolResult = &testError{"pool submit result != 0"}
|
||
|
||
type testError struct{ msg string }
|
||
|
||
func (e *testError) Error() string { return e.msg }
|