Skip to content

Commit

Permalink
+测试代码
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed Aug 19, 2023
1 parent f8ef643 commit 162cbcd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
32 changes: 32 additions & 0 deletions common_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,38 @@ func Test_CommonOption(t *testing.T) {
con.StartReadLoop()
})

t.Run("13-15.client: WithServerBufioMultipleTimesPayloadSize-Compress-Close", func(t *testing.T) {
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) {
}))
if err != nil {
t.Error(err)
}
c.StartReadLoop()
}))

defer ts.Close()

url := strings.ReplaceAll(ts.URL, "http", "ws")
con, err := Dial(url,
WithClientEnableUTF8Check(),
WithClientOnMessageFunc(func(c *Conn, op Opcode, payload []byte) {
}))
if err != nil {
t.Error(err)
}
defer con.Close()

con.Close()
err = con.WriteMessageDelay(Text, []byte{128, 129, 130, 131})
if err != ErrClosed {
t.Error("not Close:method fail")
}
con.StartReadLoop()
})

t.Run("16.1.WithServerReadTimeout:local-Upgrade", func(t *testing.T) {
var tsort testServerOptionReadTimeout

Expand Down
15 changes: 9 additions & 6 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,10 +542,10 @@ func (c *Conn) writerDelayBufInner() (err error) {
return
}

// 延迟写消息, 对流量密集型的场景有用
// 延迟写消息, 对流量密集型的场景有用 或者开启tcp delay, WithClientTCPDelay WithServerTCPDelay
// 1. 如果缓存的消息超过了多少条数
// 2. 如果缓存的消费超过了多久的时间
// 3. TODO 最大缓存多少字节
// 3. TODO: 最大缓存多少字节
func (c *Conn) WriteMessageDelay(op Opcode, writeBuf []byte) (err error) {
if atomic.LoadInt32(&c.closed) == 1 {
return ErrClosed
Expand Down Expand Up @@ -592,18 +592,21 @@ func (c *Conn) WriteMessageDelay(op Opcode, writeBuf []byte) (err error) {
// 缓存的消息超过最大值, 则直接写入
c.delayMu.Lock()
if c.delayNum+1 == c.maxDelayWriteNum {
frame.WriteFrameToBytes(c.delayBuf, writeBuf, true, rsv1, c.client, op, maskValue)
err = frame.WriteFrameToBytes(c.delayBuf, writeBuf, true, rsv1, c.client, op, maskValue)
if err != nil {
c.delayMu.Unlock()
return err
}
err = c.writerDelayBufInner()
c.delayMu.Unlock()
return err
}

// 为了平衡生产者,消费者的速度,这里不使用协程

if c.delayBuf != nil {
frame.WriteFrameToBytes(c.delayBuf, writeBuf, true, rsv1, c.client, op, maskValue)
err = frame.WriteFrameToBytes(c.delayBuf, writeBuf, true, rsv1, c.client, op, maskValue)
}
c.delayNum++ // 对记数计+1
c.delayMu.Unlock()
return nil
return err
}
2 changes: 0 additions & 2 deletions opcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,3 @@ const (
Ping = opcode.Ping
Pong = opcode.Pong
)

var ErrClose = "websocket"

0 comments on commit 162cbcd

Please sign in to comment.