Skip to content

Commit

Permalink
+测试代码
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed Aug 13, 2023
1 parent 4bf31ad commit 42ecb92
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
2 changes: 1 addition & 1 deletion common_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1203,7 +1203,7 @@ func Test_CommonOption(t *testing.T) {
data := make(chan string, 1)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c, err := Upgrade(w, r, WithServerBufioParseMode(), WithServerOnMessageFunc(func(c *Conn, op Opcode, payload []byte) {
// c.WriteMessage(op, payload)
c.WriteMessage(op, payload)
atomic.AddInt32(&run, int32(1))
data <- string(payload)
}))
Expand Down
6 changes: 4 additions & 2 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,12 +441,14 @@ func (c *Conn) WriteCloseTimeout(sc StatusCode, t time.Duration) (err error) {
return c.WriteTimeout(opcode.Close, buf, t)
}

// data 不能超过125字节
func (c *Conn) WritePing(data []byte) (err error) {
return c.WriteControl(Ping, data)
return c.WriteControl(Ping, data[:])
}

// data 不能超过125字节
func (c *Conn) WritePong(data []byte) (err error) {
return c.WriteControl(Pong, data)
return c.WriteControl(Pong, data[:])
}

func (c *Conn) WriteControl(op Opcode, data []byte) (err error) {
Expand Down
54 changes: 54 additions & 0 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,57 @@ func TestFragmentFrame(t *testing.T) {
}
})
}

type testPingPongCloseHandler struct {
DefCallback
run int32
data chan string
}

func (t *testPingPongCloseHandler) OnClose(c *Conn, err error) {
atomic.AddInt32(&t.run, 1)
t.data <- "eof"
}

// 测试ping pong close信息
func TestPingPongClose(t *testing.T) {
// 写一个超过maxControlFrameSize的消息
t.Run("1.>maxControlFrameSize.fail", func(t *testing.T) {
run := int32(0)
var shandler testPingPongCloseHandler
shandler.data = make(chan string, 1)
upgrade := NewUpgrade(WithServerBufioParseMode(), WithServerCallback(&shandler))
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, WithClientOnMessageFunc(func(c *Conn, mt Opcode, payload []byte) {
atomic.AddInt32(&run, int32(1))
}))
if err != nil {
t.Error(err)
}
defer con.Close()

con.WriteMessage(Close, bytes.Repeat([]byte("a"), maxControlFrameSize+3))
con.writeFragment(Binary, []byte("hello"), 1)
con.StartReadLoop()
select {
case d := <-shandler.data:
if d != "eof" {
t.Errorf("write message or read message fail:got:%s, need:hello\n", d)
}
case <-time.After(1000 * time.Millisecond):
}
if atomic.LoadInt32(&shandler.run) != 1 {
t.Error("not run server:method fail")
}
})
}

0 comments on commit 42ecb92

Please sign in to comment.