Skip to content

Commit

Permalink
test: make the unit tests for client work
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Nov 27, 2021
1 parent 072ff35 commit 802fa35
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 30 deletions.
6 changes: 6 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"golang.org/x/sys/unix"

gerrors "github.com/panjf2000/gnet/errors"
"github.com/panjf2000/gnet/internal"
"github.com/panjf2000/gnet/internal/netpoll"
"github.com/panjf2000/gnet/internal/socket"
"github.com/panjf2000/gnet/logging"
Expand Down Expand Up @@ -76,6 +77,11 @@ func NewClient(eventHandler EventHandler, opts ...Option) (cli *Client, err erro
el.ln = svr.ln
el.svr = svr
el.poller = p
if rbc := options.ReadBufferCap; rbc <= 0 {
options.ReadBufferCap = 0x10000
} else {
options.ReadBufferCap = internal.CeilToPowerOfTwo(rbc)
}
el.buffer = make([]byte, options.ReadBufferCap)
el.connections = make(map[int]*conn)
el.eventHandler = eventHandler
Expand Down
54 changes: 32 additions & 22 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package gnet

import (
"encoding/binary"
"log"
"math/rand"
"strings"
"sync"
Expand Down Expand Up @@ -49,7 +50,7 @@ func TestCodecServeWithGnetClient(t *testing.T) {
testCodecServeWithGnetClient(t, "tcp", ":9992", false, false, 10, false, NewDelimiterBasedFrameCodec('|'))
})
t.Run("1-loop-FixedLengthFrameCodec", func(t *testing.T) {
testCodecServeWithGnetClient(t, "tcp", ":9993", false, false, 10, false, NewFixedLengthFrameCodec(64))
testCodecServeWithGnetClient(t, "tcp", ":9993", false, false, 10, false, NewFixedLengthFrameCodec(packetLen))
})
t.Run("1-loop-LengthFieldBasedFrameCodec", func(t *testing.T) {
testCodecServeWithGnetClient(t, "tcp", ":9994", false, false, 10, false, nil)
Expand All @@ -61,7 +62,7 @@ func TestCodecServeWithGnetClient(t *testing.T) {
testCodecServeWithGnetClient(t, "tcp", ":9996", true, false, 10, false, NewDelimiterBasedFrameCodec('|'))
})
t.Run("N-loop-FixedLengthFrameCodec", func(t *testing.T) {
testCodecServeWithGnetClient(t, "tcp", ":9997", true, false, 10, false, NewFixedLengthFrameCodec(64))
testCodecServeWithGnetClient(t, "tcp", ":9997", true, false, 10, false, NewFixedLengthFrameCodec(packetLen))
})
t.Run("N-loop-LengthFieldBasedFrameCodec", func(t *testing.T) {
testCodecServeWithGnetClient(t, "tcp", ":9998", true, false, 10, false, nil)
Expand All @@ -75,7 +76,7 @@ func TestCodecServeWithGnetClient(t *testing.T) {
testCodecServeWithGnetClient(t, "tcp", ":9992", false, true, 10, false, NewDelimiterBasedFrameCodec('|'))
})
t.Run("1-loop-FixedLengthFrameCodec", func(t *testing.T) {
testCodecServeWithGnetClient(t, "tcp", ":9993", false, true, 10, false, NewFixedLengthFrameCodec(64))
testCodecServeWithGnetClient(t, "tcp", ":9993", false, true, 10, false, NewFixedLengthFrameCodec(packetLen))
})
t.Run("1-loop-LengthFieldBasedFrameCodec", func(t *testing.T) {
testCodecServeWithGnetClient(t, "tcp", ":9994", false, true, 10, false, nil)
Expand All @@ -87,7 +88,7 @@ func TestCodecServeWithGnetClient(t *testing.T) {
testCodecServeWithGnetClient(t, "tcp", ":9996", true, true, 10, false, NewDelimiterBasedFrameCodec('|'))
})
t.Run("N-loop-FixedLengthFrameCodec", func(t *testing.T) {
testCodecServeWithGnetClient(t, "tcp", ":9997", true, true, 10, false, NewFixedLengthFrameCodec(64))
testCodecServeWithGnetClient(t, "tcp", ":9997", true, true, 10, false, NewFixedLengthFrameCodec(packetLen))
})
t.Run("N-loop-LengthFieldBasedFrameCodec", func(t *testing.T) {
testCodecServeWithGnetClient(t, "tcp", ":9998", true, true, 10, false, nil)
Expand All @@ -103,7 +104,7 @@ func TestCodecServeWithGnetClient(t *testing.T) {
testCodecServeWithGnetClient(t, "tcp", ":9992", false, false, 10, true, NewDelimiterBasedFrameCodec('|'))
})
t.Run("1-loop-FixedLengthFrameCodec", func(t *testing.T) {
testCodecServeWithGnetClient(t, "tcp", ":9993", false, false, 10, true, NewFixedLengthFrameCodec(64))
testCodecServeWithGnetClient(t, "tcp", ":9993", false, false, 10, true, NewFixedLengthFrameCodec(packetLen))
})
t.Run("1-loop-LengthFieldBasedFrameCodec", func(t *testing.T) {
testCodecServeWithGnetClient(t, "tcp", ":9994", false, false, 10, true, nil)
Expand All @@ -115,7 +116,7 @@ func TestCodecServeWithGnetClient(t *testing.T) {
testCodecServeWithGnetClient(t, "tcp", ":9996", true, false, 10, true, NewDelimiterBasedFrameCodec('|'))
})
t.Run("N-loop-FixedLengthFrameCodec", func(t *testing.T) {
testCodecServeWithGnetClient(t, "tcp", ":9997", true, false, 10, true, NewFixedLengthFrameCodec(64))
testCodecServeWithGnetClient(t, "tcp", ":9997", true, false, 10, true, NewFixedLengthFrameCodec(packetLen))
})
t.Run("N-loop-LengthFieldBasedFrameCodec", func(t *testing.T) {
testCodecServeWithGnetClient(t, "tcp", ":9998", true, false, 10, true, nil)
Expand All @@ -129,7 +130,7 @@ func TestCodecServeWithGnetClient(t *testing.T) {
testCodecServeWithGnetClient(t, "tcp", ":9992", false, true, 10, true, NewDelimiterBasedFrameCodec('|'))
})
t.Run("1-loop-FixedLengthFrameCodec", func(t *testing.T) {
testCodecServeWithGnetClient(t, "tcp", ":9993", false, true, 10, true, NewFixedLengthFrameCodec(64))
testCodecServeWithGnetClient(t, "tcp", ":9993", false, true, 10, true, NewFixedLengthFrameCodec(packetLen))
})
t.Run("1-loop-LengthFieldBasedFrameCodec", func(t *testing.T) {
testCodecServeWithGnetClient(t, "tcp", ":9994", false, true, 10, true, nil)
Expand All @@ -141,7 +142,7 @@ func TestCodecServeWithGnetClient(t *testing.T) {
testCodecServeWithGnetClient(t, "tcp", ":9996", true, true, 10, true, NewDelimiterBasedFrameCodec('|'))
})
t.Run("N-loop-FixedLengthFrameCodec", func(t *testing.T) {
testCodecServeWithGnetClient(t, "tcp", ":9997", true, true, 10, true, NewFixedLengthFrameCodec(64))
testCodecServeWithGnetClient(t, "tcp", ":9997", true, true, 10, true, NewFixedLengthFrameCodec(packetLen))
})
t.Run("N-loop-LengthFieldBasedFrameCodec", func(t *testing.T) {
testCodecServeWithGnetClient(t, "tcp", ":9998", true, true, 10, true, nil)
Expand Down Expand Up @@ -175,6 +176,7 @@ func (s *testCodecClientServer) OnOpened(c Conn) (out []byte, action Action) {
}

func (s *testCodecClientServer) OnClosed(c Conn, err error) (action Action) {
log.Println("close:", c.RemoteAddr(), err)
require.Equal(s.tester, c.Context(), c, "invalid context")
atomic.AddInt32(&s.disconnected, 1)
if atomic.LoadInt32(&s.connected) == atomic.LoadInt32(&s.disconnected) &&
Expand Down Expand Up @@ -225,10 +227,10 @@ func (cli *clientEvents) React(packet []byte, c Conn) (out []byte, action Action
return
}

var (
m = 0
allFieldLengths = []int{1, 2, 3, 4, 8}
)
func (cli *clientEvents) Tick() (delay time.Duration, action Action) {
delay = 200 * time.Millisecond
return
}

func testCodecServeWithGnetClient(
t *testing.T,
Expand All @@ -240,32 +242,38 @@ func testCodecServeWithGnetClient(
) {
var err error
if codec == nil {
fieldLength := allFieldLengths[m]
encoderConfig := EncoderConfig{
ByteOrder: binary.BigEndian,
LengthFieldLength: fieldLength,
LengthFieldLength: 2,
LengthAdjustment: 0,
LengthIncludesLengthFieldLength: false,
}
decoderConfig := DecoderConfig{
ByteOrder: binary.BigEndian,
LengthFieldOffset: 0,
LengthFieldLength: fieldLength,
LengthFieldLength: 2,
LengthAdjustment: 0,
InitialBytesToStrip: fieldLength,
InitialBytesToStrip: 2,
}
codec = NewLengthFieldBasedFrameCodec(encoderConfig, decoderConfig)
}
m++
if m > 4 {
m = 0
}
ts := &testCodecClientServer{
tester: t, network: network, addr: addr, multicore: multicore, async: async,
codec: codec, workerPool: goroutine.Default(),
}
ts.clientEV = &clientEvents{}
ts.client, err = NewClient(ts.clientEV, WithLogLevel(logging.DebugLevel), WithCodec(codec))
ts.client, err = NewClient(
ts.clientEV,
WithLogLevel(logging.DebugLevel),
WithCodec(codec),
WithReadBufferCap(8*1024),
WithLockOSThread(true),
WithTCPNoDelay(TCPNoDelay),
WithTCPKeepAlive(time.Minute),
WithSocketRecvBuffer(8*1024),
WithSocketSendBuffer(8*1024),
WithTicker(true),
)
assert.NoError(t, err)
err = ts.client.Start()
assert.NoError(t, err)
Expand Down Expand Up @@ -303,7 +311,9 @@ func startCodecGnetClient(t *testing.T, cli *Client, ev *clientEvents, network,
duration := time.Duration((rand.Float64()*2+1)*float64(time.Second)) / 8
start := time.Now()
for time.Since(start) < duration {
reqData := []byte(strings.Repeat("abcd1234", 8)) // 64 bytes
// reqData := make([]byte, 1024)
// rand.Read(reqData)
reqData := []byte(strings.Repeat("x", packetLen))
err = c.AsyncWrite(reqData)
require.NoError(t, err)
respData := <-rspCh
Expand Down
18 changes: 10 additions & 8 deletions gnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import (
"github.com/panjf2000/gnet/pool/goroutine"
)

var packetLen = 1024

func TestCodecServe(t *testing.T) {
// start a server
// connect 10 clients
Expand All @@ -54,7 +56,7 @@ func TestCodecServe(t *testing.T) {
testCodecServe(t, "tcp", ":9992", false, false, 10, false, NewDelimiterBasedFrameCodec('|'))
})
t.Run("1-loop-FixedLengthFrameCodec", func(t *testing.T) {
testCodecServe(t, "tcp", ":9993", false, false, 10, false, NewFixedLengthFrameCodec(64))
testCodecServe(t, "tcp", ":9993", false, false, 10, false, NewFixedLengthFrameCodec(packetLen))
})
t.Run("1-loop-LengthFieldBasedFrameCodec", func(t *testing.T) {
testCodecServe(t, "tcp", ":9994", false, false, 10, false, nil)
Expand All @@ -66,7 +68,7 @@ func TestCodecServe(t *testing.T) {
testCodecServe(t, "tcp", ":9996", true, false, 10, false, NewDelimiterBasedFrameCodec('|'))
})
t.Run("N-loop-FixedLengthFrameCodec", func(t *testing.T) {
testCodecServe(t, "tcp", ":9997", true, false, 10, false, NewFixedLengthFrameCodec(64))
testCodecServe(t, "tcp", ":9997", true, false, 10, false, NewFixedLengthFrameCodec(packetLen))
})
t.Run("N-loop-LengthFieldBasedFrameCodec", func(t *testing.T) {
testCodecServe(t, "tcp", ":9998", true, false, 10, false, nil)
Expand All @@ -80,7 +82,7 @@ func TestCodecServe(t *testing.T) {
testCodecServe(t, "tcp", ":9992", false, true, 10, false, NewDelimiterBasedFrameCodec('|'))
})
t.Run("1-loop-FixedLengthFrameCodec", func(t *testing.T) {
testCodecServe(t, "tcp", ":9993", false, true, 10, false, NewFixedLengthFrameCodec(64))
testCodecServe(t, "tcp", ":9993", false, true, 10, false, NewFixedLengthFrameCodec(packetLen))
})
t.Run("1-loop-LengthFieldBasedFrameCodec", func(t *testing.T) {
testCodecServe(t, "tcp", ":9994", false, true, 10, false, nil)
Expand All @@ -92,7 +94,7 @@ func TestCodecServe(t *testing.T) {
testCodecServe(t, "tcp", ":9996", true, true, 10, false, NewDelimiterBasedFrameCodec('|'))
})
t.Run("N-loop-FixedLengthFrameCodec", func(t *testing.T) {
testCodecServe(t, "tcp", ":9997", true, true, 10, false, NewFixedLengthFrameCodec(64))
testCodecServe(t, "tcp", ":9997", true, true, 10, false, NewFixedLengthFrameCodec(packetLen))
})
t.Run("N-loop-LengthFieldBasedFrameCodec", func(t *testing.T) {
testCodecServe(t, "tcp", ":9998", true, true, 10, false, nil)
Expand All @@ -108,7 +110,7 @@ func TestCodecServe(t *testing.T) {
testCodecServe(t, "tcp", ":9992", false, false, 10, true, NewDelimiterBasedFrameCodec('|'))
})
t.Run("1-loop-FixedLengthFrameCodec", func(t *testing.T) {
testCodecServe(t, "tcp", ":9993", false, false, 10, true, NewFixedLengthFrameCodec(64))
testCodecServe(t, "tcp", ":9993", false, false, 10, true, NewFixedLengthFrameCodec(packetLen))
})
t.Run("1-loop-LengthFieldBasedFrameCodec", func(t *testing.T) {
testCodecServe(t, "tcp", ":9994", false, false, 10, true, nil)
Expand All @@ -120,7 +122,7 @@ func TestCodecServe(t *testing.T) {
testCodecServe(t, "tcp", ":9996", true, false, 10, true, NewDelimiterBasedFrameCodec('|'))
})
t.Run("N-loop-FixedLengthFrameCodec", func(t *testing.T) {
testCodecServe(t, "tcp", ":9997", true, false, 10, true, NewFixedLengthFrameCodec(64))
testCodecServe(t, "tcp", ":9997", true, false, 10, true, NewFixedLengthFrameCodec(packetLen))
})
t.Run("N-loop-LengthFieldBasedFrameCodec", func(t *testing.T) {
testCodecServe(t, "tcp", ":9998", true, false, 10, true, nil)
Expand All @@ -134,7 +136,7 @@ func TestCodecServe(t *testing.T) {
testCodecServe(t, "tcp", ":9992", false, true, 10, true, NewDelimiterBasedFrameCodec('|'))
})
t.Run("1-loop-FixedLengthFrameCodec", func(t *testing.T) {
testCodecServe(t, "tcp", ":9993", false, true, 10, true, NewFixedLengthFrameCodec(64))
testCodecServe(t, "tcp", ":9993", false, true, 10, true, NewFixedLengthFrameCodec(packetLen))
})
t.Run("1-loop-LengthFieldBasedFrameCodec", func(t *testing.T) {
testCodecServe(t, "tcp", ":9994", false, true, 10, true, nil)
Expand All @@ -146,7 +148,7 @@ func TestCodecServe(t *testing.T) {
testCodecServe(t, "tcp", ":9996", true, true, 10, true, NewDelimiterBasedFrameCodec('|'))
})
t.Run("N-loop-FixedLengthFrameCodec", func(t *testing.T) {
testCodecServe(t, "tcp", ":9997", true, true, 10, true, NewFixedLengthFrameCodec(64))
testCodecServe(t, "tcp", ":9997", true, true, 10, true, NewFixedLengthFrameCodec(packetLen))
})
t.Run("N-loop-LengthFieldBasedFrameCodec", func(t *testing.T) {
testCodecServe(t, "tcp", ":9998", true, true, 10, true, nil)
Expand Down

0 comments on commit 802fa35

Please sign in to comment.