test: 补齐短时间可测验收并修复建群下行卡住

This commit is contained in:
Nixevol
2026-09-30 10:20:47 +08:00
parent 4e3c84366f
commit 479a08ee11
12 changed files with 1078 additions and 87 deletions
+20 -4
View File
@@ -33,15 +33,27 @@ type AppResp struct {
Raw map[string]any
}
// MQTTLoginOpts 控制握手参数。
type MQTTLoginOpts struct {
// MaxReceiveBytes 非 nil 时写入 hello.max_receive_bytes。
MaxReceiveBytes *int
}
// MQTTLogin 用密码连上 /mqtt、订阅 down、完成 hello。
func MQTTLogin(t *testing.T, httpBase, endpointID, password string) *MQTTSession {
t.Helper()
mc, err := harness.DialMQTTWebSocket(httpBase, 10*time.Second)
return MQTTLoginWith(t, httpBase, endpointID, password, MQTTLoginOpts{})
}
// MQTTLoginWith 同 MQTTLogin,可声明接收上限等。
func MQTTLoginWith(t *testing.T, httpBase, endpointID, password string, opts MQTTLoginOpts) *MQTTSession {
t.Helper()
mc, err := harness.DialMQTTWebSocket(httpBase, 15*time.Second)
if err != nil {
t.Fatalf("dial mqtt: %v", err)
}
s := &MQTTSession{t: t, mc: mc, EndpointID: endpointID, pktID: 10, done: make(chan struct{})}
s.connectSubscribeHello(password)
s.connectSubscribeHello(password, opts)
go s.readLoop()
return s
}
@@ -70,7 +82,7 @@ func (s *MQTTSession) nextPkt() uint16 {
return s.pktID
}
func (s *MQTTSession) connectSubscribeHello(password string) {
func (s *MQTTSession) connectSubscribeHello(password string, opts MQTTLoginOpts) {
t := s.t
pk := packets.Packet{
FixedHeader: packets.FixedHeader{Type: packets.Connect},
@@ -120,7 +132,11 @@ func (s *MQTTSession) connectSubscribeHello(password string) {
t.Fatal(err)
}
hello, _ := protocol.Marshal(protocol.Hello{V: protocol.Version, Type: protocol.TypeHello, RID: "h0"})
helloFrame := protocol.Hello{V: protocol.Version, Type: protocol.TypeHello, RID: "h0"}
if opts.MaxReceiveBytes != nil {
helloFrame.MaxReceiveBytes = opts.MaxReceiveBytes
}
hello, _ := protocol.Marshal(helloFrame)
s.publishRaw(hello)
deadline := time.Now().Add(10 * time.Second)
for time.Now().Before(deadline) {