Skip to content

Commit

Permalink
Merge pull request #77 from lxzan/testing
Browse files Browse the repository at this point in the history
upgrade compress package
  • Loading branch information
lxzan authored Jan 27, 2024
2 parents 4f35870 + 26098e0 commit 608b2ed
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 19 deletions.
2 changes: 1 addition & 1 deletion compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func TestPermessageNegotiation(t *testing.T) {
assert.Equal(t, string(client.cpsWindow.dict), "he")
_ = client.WriteString("llo")
assert.Equal(t, string(client.cpsWindow.dict), "hello")
_ = client.WriteV(OpcodeText, []byte(", "), []byte("world!"))
_ = client.Writev(OpcodeText, []byte(", "), []byte("world!"))
assert.Equal(t, string(client.cpsWindow.dict), "hello, world!")
})

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/dolthub/maphash v0.1.0
github.com/klauspost/compress v1.17.5-0.20240119100516-32312d57f3c7
github.com/klauspost/compress v1.17.5
github.com/stretchr/testify v1.8.4
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dolthub/maphash v0.1.0 h1:bsQ7JsF4FkkWyrP3oCnFJgrCUAFbFf3kOl4L/QxPDyQ=
github.com/dolthub/maphash v0.1.0/go.mod h1:gkg4Ch4CdCDu5h6PMriVLawB7koZ+5ijb9puGMV50a4=
github.com/klauspost/compress v1.17.5-0.20240119100516-32312d57f3c7 h1:cBVUjnVhEblq1CXwboJtC3F1lQYutr/4sVsnhLUAgiE=
github.com/klauspost/compress v1.17.5-0.20240119100516-32312d57f3c7/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
github.com/klauspost/compress v1.17.5 h1:d4vBd+7CHydUqpFBgUEKkSdtSugf9YFmSkvUYPquI5E=
github.com/klauspost/compress v1.17.5/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
Expand Down
13 changes: 10 additions & 3 deletions option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,23 @@ func TestCompressClientOption(t *testing.T) {
t.Run("", func(t *testing.T) {
var option = &ClientOption{
PermessageDeflate: PermessageDeflate{
Enabled: true,
Level: flate.BestCompression,
Threshold: 1024,
Enabled: true,
ServerContextTakeover: true,
ClientContextTakeover: true,
Level: flate.BestCompression,
Threshold: 1024,
},
}
initClientOption(option)

as.Equal(true, option.PermessageDeflate.Enabled)
as.Equal(flate.BestCompression, option.PermessageDeflate.Level)
as.Equal(1024, option.PermessageDeflate.Threshold)
validateClientOption(as, option)

var cfg = option.getConfig()
as.Nil(cfg.cswPool)
as.Nil(cfg.dswPool)
})
}

Expand Down
26 changes: 18 additions & 8 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,6 @@ func (c *Conn) WriteMessage(opcode Opcode, payload []byte) error {
return err
}

// WriteV 批量写入文本/二进制消息, 文本消息应该使用UTF8编码
// writes batch text/binary messages, text messages should be encoded in UTF8.
func (c *Conn) WriteV(opcode Opcode, payloads ...[]byte) error {
var err = c.doWrite(opcode, internal.Buffers(payloads))
c.emitError(err)
return err
}

// WriteAsync 异步写
// 异步非阻塞地将消息写入到任务队列, 收到回调后才允许回收payload内存
// Asynchronously and non-blockingly write the message to the task queue, allowing the payload memory to be reclaimed only after a callback is received.
Expand All @@ -68,6 +60,24 @@ func (c *Conn) WriteAsync(opcode Opcode, payload []byte, callback func(error)) {
})
}

// Writev 类似WriteMessage, 区别是可以一次写入多个切片
// Similar to WriteMessage, except that you can write multiple slices at once.
func (c *Conn) Writev(opcode Opcode, payloads ...[]byte) error {
var err = c.doWrite(opcode, internal.Buffers(payloads))
c.emitError(err)
return err
}

// WritevAsync 类似WriteAsync, 区别是可以一次写入多个切片
// Similar to WriteAsync, except that you can write multiple slices at once.
func (c *Conn) WritevAsync(opcode Opcode, payloads [][]byte, callback func(error)) {
c.writeQueue.Push(func() {
if err := c.Writev(opcode, payloads...); callback != nil {
callback(err)
}
})
}

// 执行写入逻辑, 注意妥善维护压缩字典
func (c *Conn) doWrite(opcode Opcode, payload internal.Payload) error {
c.mu.Lock()
Expand Down
36 changes: 32 additions & 4 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func TestRecovery(t *testing.T) {
time.Sleep(100 * time.Millisecond)
}

func TestConn_WriteV(t *testing.T) {
func TestConn_Writev(t *testing.T) {
t.Run("", func(t *testing.T) {
var serverHandler = new(webSocketMocker)
var clientHandler = new(webSocketMocker)
Expand All @@ -348,7 +348,7 @@ func TestConn_WriteV(t *testing.T) {
go server.ReadLoop()
go client.ReadLoop()

var err = client.WriteV(OpcodeText, [][]byte{
var err = client.Writev(OpcodeText, [][]byte{
[]byte("he"),
[]byte("llo"),
[]byte(", world!"),
Expand All @@ -357,6 +357,34 @@ func TestConn_WriteV(t *testing.T) {
wg.Wait()
})

t.Run("", func(t *testing.T) {
var serverHandler = new(webSocketMocker)
var clientHandler = new(webSocketMocker)
var serverOption = &ServerOption{}
var clientOption = &ClientOption{}
var wg = &sync.WaitGroup{}
wg.Add(1)

serverHandler.onMessage = func(socket *Conn, message *Message) {
if bytes.Equal(message.Bytes(), []byte("hello, world!")) {
wg.Done()
}
}

server, client := newPeer(serverHandler, serverOption, clientHandler, clientOption)
go server.ReadLoop()
go client.ReadLoop()

client.WritevAsync(OpcodeText, [][]byte{
[]byte("he"),
[]byte("llo"),
[]byte(", world!"),
}, func(err error) {
assert.NoError(t, err)
})
wg.Wait()
})

t.Run("", func(t *testing.T) {
var serverHandler = new(webSocketMocker)
var clientHandler = new(webSocketMocker)
Expand Down Expand Up @@ -389,7 +417,7 @@ func TestConn_WriteV(t *testing.T) {
go server.ReadLoop()
go client.ReadLoop()

var err = client.WriteV(OpcodeText, [][]byte{
var err = client.Writev(OpcodeText, [][]byte{
[]byte("he"),
[]byte("llo"),
[]byte(", world!"),
Expand Down Expand Up @@ -423,7 +451,7 @@ func TestConn_WriteV(t *testing.T) {
go server.ReadLoop()
go client.ReadLoop()

var err = client.WriteV(OpcodeText, [][]byte{
var err = client.Writev(OpcodeText, [][]byte{
[]byte("山高月小"),
[]byte("水落石出")[2:],
}...)
Expand Down

0 comments on commit 608b2ed

Please sign in to comment.