Skip to content

Commit

Permalink
+测试代码
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed Aug 14, 2023
1 parent c3f3dec commit e133eeb
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions common_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,49 @@ func Test_CommonOption(t *testing.T) {
}
})

t.Run("10.client: WithClientDecompression", func(t *testing.T) {
run := int32(0)
data := make(chan string, 1)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c, err := Upgrade(w, r, WithServerDecompressAndCompress(), WithServerOnMessageFunc(func(c *Conn, op Opcode, payload []byte) {
c.WriteMessage(op, payload)
}))
if err != nil {
t.Error(err)
}
c.StartReadLoop()
}))

defer ts.Close()

url := strings.ReplaceAll(ts.URL, "http", "ws")
con, err := Dial(url, WithClientBufioParseMode(), WithClientCompression(), WithClientDecompression(), WithClientOnMessageFunc(func(c *Conn, mt Opcode, payload []byte) {
atomic.AddInt32(&run, int32(1))
data <- string(payload)
}))
if err != nil {
t.Error(err)
}
defer con.Close()

err = con.WriteMessage(Binary, []byte("hello"))
if err != nil {
t.Error(err)
}

con.StartReadLoop()
select {
case d := <-data:
if d != "hello" {
t.Errorf("write message or read message fail:got:%s, need:hello\n", d)
}
case <-time.After(1000 * time.Millisecond):
}
if atomic.LoadInt32(&run) != 1 {
t.Error("not run server:method fail")
}
})

t.Run("11.server.local: WithServerDisableBufioClearHack", func(t *testing.T) {
run := int32(0)
data := make(chan string, 1)
Expand Down

0 comments on commit e133eeb

Please sign in to comment.