Skip to content

Commit

Permalink
+测试代码
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed Aug 22, 2023
1 parent c60b58b commit a0afe04
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
1 change: 0 additions & 1 deletion common_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1550,7 +1550,6 @@ func Test_CommonOption(t *testing.T) {
WithClientWindowsParseMode(),
WithClientDelayWriteInitBufferSize(4096),
WithClientOnMessageFunc(func(c *Conn, op Opcode, payload []byte) {
// c.WriteMessageDelay(op, []byte("hello"))
}))
if err != nil {
t.Error(err)
Expand Down
43 changes: 42 additions & 1 deletion conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ func Test_WriteControl(t *testing.T) {
})
}

// 测试ping pong close信息
// 测试ping pong close control信息
func TestPingPongClose(t *testing.T) {
// 写一个超过maxControlFrameSize的消息
t.Run("1.>maxControlFrameSize.fail", func(t *testing.T) {
Expand Down Expand Up @@ -783,4 +783,45 @@ func TestPingPongClose(t *testing.T) {
}
}
})

t.Run("8.fail-control", func(t *testing.T) {
run := int32(0)
data := make(chan string, 1)
upgrade := NewUpgrade(WithServerOnCloseFunc(func(c *Conn, err error) {
atomic.AddInt32(&run, 1)
data <- err.Error()
}))
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c, err := upgrade.Upgrade(w, r)
if err != nil {
t.Error(err)
}
c.StartReadLoop()
}))

defer ts.Close()

url := strings.ReplaceAll(ts.URL, "http", "ws")
con, err := Dial(url, WithClientDisableBufioClearHack(),
WithClientEnableUTF8Check(), WithClientOnCloseFunc(func(c *Conn, err error) {
}))
if err != nil {
t.Error(err)
}
defer con.Close()
// 这里必须要报错
err = con.WriteMessage(4 /*这是rfc保留的frame*/, []byte("hello"))
if err != nil {
t.Error("not error")
}
con.StartReadLoop()
select {
case _ = <-data:
case <-time.After(500 * time.Millisecond):
}

if atomic.LoadInt32(&run) != 1 {
t.Error("not run server:method fail")
}
})
}

0 comments on commit a0afe04

Please sign in to comment.