Skip to content
Permalink
Browse files
Add: ut
  • Loading branch information
AlexStocks committed Mar 2, 2020
1 parent a733de8 commit 2d033a8ed903125df0e3dd6b2f80a5160a2a4211
Show file tree
Hide file tree
Showing 2 changed files with 500 additions and 0 deletions.
@@ -0,0 +1,392 @@
package getty

import (
"bytes"
"net"
"net/http"
"os"
"strconv"
"sync"
"testing"
"time"
)

import (
perrors "github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)

type PackageHandler struct{}

func (h *PackageHandler) Read(ss Session, data []byte) (interface{}, int, error) {
return nil, 0, nil
}

func (h *PackageHandler) Write(ss Session, pkg interface{}) ([]byte, error) {
return nil, nil
}

type MessageHandler struct {
lock sync.Mutex
array []Session
}

func newMessageHandler() *MessageHandler {
return &MessageHandler{}
}

func (h *MessageHandler) SessionNumber() int {
h.lock.Lock()
connNum := len(h.array)
h.lock.Unlock()

return connNum
}

func (h *MessageHandler) OnOpen(session Session) error {
h.lock.Lock()
defer h.lock.Unlock()
h.array = append(h.array, session)

return nil
}
func (h *MessageHandler) OnError(session Session, err error) {}
func (h *MessageHandler) OnClose(session Session) {}
func (h *MessageHandler) OnMessage(session Session, pkg interface{}) {}
func (h *MessageHandler) OnCron(session Session) {}

type Package struct{}

func (p Package) String() string {
return ""
}
func (p Package) Marshal() (*bytes.Buffer, error) { return nil, nil }
func (p *Package) Unmarshal(buf *bytes.Buffer) (int, error) { return 0, nil }

var (
pkg Package
pkgHandler PackageHandler
)

func newSessionCallback(session Session, handler *MessageHandler) error {
session.SetName("hello-client-session")
session.SetMaxMsgLen(1024)
session.SetPkgHandler(&pkgHandler)
session.SetEventListener(handler)
session.SetRQLen(4)
session.SetWQLen(32)
session.SetReadTimeout(3e9)
session.SetWriteTimeout(3e9)
session.SetCronPeriod((int)(30e9 / 1e6))
session.SetWaitTime(3e9)
session.SetTaskPool(nil)

return nil
}

func TestTCPClient(t *testing.T) {
assert.NotNil(t, GetTimeWheel())
listenLocalServer := func() (net.Listener, error) {
listener, err := net.Listen("tcp", ":0")
if err != nil {
return nil, err
}

go http.Serve(listener, nil)
return listener, nil
}

listener, err := listenLocalServer()
assert.Nil(t, err)
assert.NotNil(t, listener)

addr := listener.Addr().(*net.TCPAddr)
t.Logf("server addr: %v", addr)
clt := NewTCPClient(
WithServerAddress(addr.String()),
WithReconnectInterval(5e8),
WithConnectionNumber(1),
)
assert.NotNil(t, clt)
assert.True(t, clt.ID() > 0)
//assert.Equal(t, clt.endPointType, TCP_CLIENT)

var (
msgHandler MessageHandler
)
cb := func(session Session) error {
return newSessionCallback(session, &msgHandler)
}

clt.RunEventLoop(cb)
time.Sleep(1e9)

assert.Equal(t, 1, msgHandler.SessionNumber())
ss := msgHandler.array[0]
ss.SetCompressType(CompressNone)
conn := ss.(*session).Connection.(*gettyTCPConn)
assert.True(t, conn.compress == CompressNone)
err = ss.WriteBytes([]byte("hello"))
assert.Nil(t, err)
ss.SetCompressType(CompressSnappy)
assert.True(t, conn.compress == CompressSnappy)

clt.Close()
assert.True(t, clt.IsClosed())
}

func TestUDPClient(t *testing.T) {
var (
err error
conn *net.UDPConn
)
func() {
srcAddr := &net.UDPAddr{IP: net.IPv4zero, Port: 0}
conn, err = net.ListenUDP("udp", srcAddr)
assert.Nil(t, err)
assert.NotNil(t, conn)
}()
defer conn.Close()

addr := conn.LocalAddr()
clt := NewUDPClient(
WithServerAddress(addr.String()),
WithReconnectInterval(5e8),
WithConnectionNumber(1),
)
assert.NotNil(t, clt)
assert.True(t, clt.ID() > 0)
//assert.Equal(t, clt.endPointType, UDP_CLIENT)

var (
msgHandler MessageHandler
)
cb := func(session Session) error {
return newSessionCallback(session, &msgHandler)
}

clt.RunEventLoop(cb)
time.Sleep(1e9)

assert.Equal(t, 1, msgHandler.SessionNumber())
ss := msgHandler.array[0]
err = ss.WritePkg(nil, 0)
assert.NotNil(t, err)
err = ss.WritePkg([]byte("hello"), 0)
assert.NotNil(t, perrors.Cause(err))
err = ss.WriteBytes([]byte("hello"))
assert.NotNil(t, err)
err = ss.WriteBytesArray([]byte("hello"))
assert.NotNil(t, err)
err = ss.WriteBytesArray([]byte("hello"), []byte("world"))
assert.NotNil(t, err)
ss.SetCompressType(CompressNone)
host, port, _ := net.SplitHostPort(addr.String())
if len(host) < 8 {
host = "127.0.0.1"
}
remotePort, _ := strconv.Atoi(port)
serverAddr := net.UDPAddr{IP: net.ParseIP(host), Port: remotePort}
udpCtx := UDPContext{
Pkg: "hello",
PeerAddr: &serverAddr,
}
t.Logf("udp context:%s", udpCtx)
udpConn := ss.(*session).Connection.(*gettyUDPConn)
_, err = udpConn.send(udpCtx)
assert.NotNil(t, err)
udpCtx.Pkg = []byte("hello")
_, err = udpConn.send(udpCtx)
assert.Nil(t, err)

clt.Close()
assert.True(t, clt.IsClosed())
msgHandler.array[0].Reset()
assert.Nil(t, msgHandler.array[0].Conn())
//ss.WritePkg([]byte("hello"), 0)
}

func TestNewWSClient(t *testing.T) {
var (
server Server
serverMsgHandler MessageHandler
)
addr := "127.0.0.1:65000"
path := "/hello"
func() {
server = NewWSServer(
WithLocalAddress(addr),
WithWebsocketServerPath(path),
)
newServerSession := func(session Session) error {
return newSessionCallback(session, &serverMsgHandler)
}
go server.RunEventLoop(newServerSession)
}()
time.Sleep(1e9)

client := NewWSClient(
WithServerAddress("ws://"+addr+path),
WithConnectionNumber(1),
)

var (
msgHandler MessageHandler
)
cb := func(session Session) error {
return newSessionCallback(session, &msgHandler)
}

client.RunEventLoop(cb)
time.Sleep(1e9)

assert.Equal(t, 1, msgHandler.SessionNumber())
ss := msgHandler.array[0]
ss.SetCompressType(CompressNone)
conn := ss.(*session).Connection.(*gettyWSConn)
assert.True(t, conn.compress == CompressNone)
err := conn.handlePing("hello")
assert.Nil(t, err)
_, err = conn.send("hello")
assert.NotNil(t, err)
_, err = conn.send([]byte("hello"))
assert.Nil(t, err)
err = conn.writePing()
assert.Nil(t, err)

ss.SetReader(nil)
assert.Nil(t, ss.(*session).reader)
ss.SetWriter(nil)
assert.Nil(t, ss.(*session).writer)
assert.Nil(t, ss.(*session).GetAttribute("hello"))

client.Close()
assert.True(t, client.IsClosed())
server.Close()
assert.True(t, server.IsClosed())
}

var (
WssServerCRT = []byte(`-----BEGIN CERTIFICATE-----
MIICHjCCAYegAwIBAgIQKpKqamBqmZ0hfp8sYb4uNDANBgkqhkiG9w0BAQsFADAS
MRAwDgYDVQQKEwdBY21lIENvMCAXDTcwMDEwMTAwMDAwMFoYDzIwODQwMTI5MTYw
MDAwWjASMRAwDgYDVQQKEwdBY21lIENvMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB
iQKBgQC5Nxsk6WjeaYazRYiGxHZ5G3FXSlSjV7lZeebItdEPzO8kVPIGCSTy/M5X
Nnpp3uVDFXQub0/O5t9Y6wcuqpUGMOV+XL7MZqSZlodXm0XhNYzCAjZ+URNjTHGP
NXIqdDEG5Ba8SXMOfY6H97+QxugZoAMFZ+N83ggr12IYNO/FbQIDAQABo3MwcTAO
BgNVHQ8BAf8EBAMCAqQwEwYDVR0lBAwwCgYIKwYBBQUHAwEwDwYDVR0TAQH/BAUw
AwEB/zA5BgNVHREEMjAwgglsb2NhbGhvc3SCC2V4YW1wbGUuY29thwR/AAABhxAA
AAAAAAAAAAAAAAAAAAABMA0GCSqGSIb3DQEBCwUAA4GBAE5dr9q7ORmKZ7yZqeSL
305armc13A7UxffUajeJFujpl2jOqnb5PuKJ7fn5HQKGB0qSq3IHsFua2WONXcTW
Vn4gS0k50IaDpW+yl+ArIo0QwbjPIAcFysX10p9dVO7A1uEpHbRDzefem6r9uVGk
i7dOLEoC8hkfk6nJsNEIEqu6
-----END CERTIFICATE-----`)
WssServerCRTFile = "/tmp/server.crt"
WssServerKEY = []byte(`-----BEGIN RSA PRIVATE KEY-----
MIICXgIBAAKBgQC5Nxsk6WjeaYazRYiGxHZ5G3FXSlSjV7lZeebItdEPzO8kVPIG
CSTy/M5XNnpp3uVDFXQub0/O5t9Y6wcuqpUGMOV+XL7MZqSZlodXm0XhNYzCAjZ+
URNjTHG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MZqSZlodXm0XhNYzCAjZ+URNjTHGP
NXIqdDEG5Ba8SXMOfY6H97+QxugZoAMFZ+N83ggr12IYNO/FbQIDAQABo3MwcTAO
BgNVHQ8BAf8EBAMCAqQwEwYDVR0lBAwwCgYIKwYBBQUHAwEwDwYDVR0TAQH/BAUw
AwEB/zA5BgNVHREEMjAwgglsb2NhbGhvc3SCC2V4YW1wbGUuY29thwR/AAABhxAA
AAAAAAAAAAAAAAAAAAABMA0GCSqGSIb3DQEBCwUAA4GBAE5dr9q7ORmKZ7yZqeSL
305armc13A7UxffUajeJFujpl2jOqnb5PuKJ7fn5HQKGB0qSq3IHsFua2WONXcTW
Vn4gS0k50IaDpW+yl+ArIo0QwbjPIAcFysX10p9dVO7A1uEpHbRDzefem6r9uVGk
i7dOLEoC8hkfk6nJsNEIEqu6
-----END CERTIFICATE-----`)
WssClientCRTFile = "/tmp/client.crt"
)

func DownloadFile(filepath string, content []byte) error {
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()

// Write the body to file
_, err = out.Write(content)
return err
}

func TestNewWSSClient(t *testing.T) {
var (
err error
server Server
serverMsgHandler MessageHandler
)

os.Remove(WssServerCRTFile)
err = DownloadFile(WssServerCRTFile, WssServerCRT)
assert.Nil(t, err)
defer os.Remove(WssServerCRTFile)

os.Remove(WssServerKEYFile)
err = DownloadFile(WssServerKEYFile, WssServerKEY)
assert.Nil(t, err)
defer os.Remove(WssServerKEYFile)

os.Remove(WssClientCRTFile)
err = DownloadFile(WssClientCRTFile, WssClientCRT)
assert.Nil(t, err)
defer os.Remove(WssClientCRTFile)

addr := "127.0.0.1:63450"
path := "/hello"
func() {
server = NewWSSServer(
WithLocalAddress(addr),
WithWebsocketServerPath(path),
WithWebsocketServerCert(WssServerCRTFile),
WithWebsocketServerPrivateKey(WssServerKEYFile),
)
newServerSession := func(session Session) error {
return newSessionCallback(session, &serverMsgHandler)
}
go server.RunEventLoop(newServerSession)
}()
time.Sleep(1e9)

client := NewWSSClient(
WithServerAddress("wss://"+addr+path),
WithConnectionNumber(1),
WithRootCertificateFile(WssClientCRTFile),
)

var (
msgHandler MessageHandler
)
cb := func(session Session) error {
return newSessionCallback(session, &msgHandler)
}

client.RunEventLoop(cb)
time.Sleep(1e9)

assert.Equal(t, 1, msgHandler.SessionNumber())
client.Close()
assert.True(t, client.IsClosed())
assert.False(t, server.IsClosed())
//time.Sleep(1000e9)
//server.Close()
//assert.True(t, server.IsClosed())
}

0 comments on commit 2d033a8

Please sign in to comment.