Compare commits

...

6 Commits

Author SHA1 Message Date
0a28dbcc3e removed util
- WaitWithTimeout && SelectWithTimeout have been moved to internal/testutil
- GetTime has been moved to cnet/protocol
2024-02-01 18:25:49 -06:00
1a6de671e5 moved 'testutil' to 'internal/testutil' 2024-02-01 17:28:00 -06:00
261ea6505f testutil: fix possible orphaned container in SetupEnvironment 2024-02-01 17:25:11 -06:00
556878544d testutil: refactoring && cleanup
added a simple DummyPeer struct to simplify creation, send/recv and cleanup
2024-02-01 17:21:56 -06:00
bfcbe6d3d6 started testutil: login_test now uses these helpers
should simplify new tests in the future
2024-02-01 17:11:50 -06:00
e5a9ed1481 shardserver: added Service()
also, Start() now returns an error result
2024-02-01 16:53:27 -06:00
11 changed files with 195 additions and 153 deletions

9
cnet/protocol/time.go Normal file
View File

@ -0,0 +1,9 @@
package protocol
import (
"time"
)
func GetTime() uint64 {
return uint64(time.Now().UnixMilli())
}

View File

@ -12,7 +12,7 @@ import (
"github.com/CPunch/gopenfusion/cnet"
"github.com/CPunch/gopenfusion/cnet/protocol"
"github.com/CPunch/gopenfusion/util"
"github.com/CPunch/gopenfusion/internal/testutil"
"github.com/matryer/is"
)
@ -44,7 +44,7 @@ func TestService(t *testing.T) {
// shutdown service when test is done
defer func() {
cancel()
is.True(util.SelectWithTimeout(srvc.Stopped(), timeout)) // wait for service to stop with timeout
is.True(testutil.SelectWithTimeout(srvc.Stopped(), timeout)) // wait for service to stop with timeout
}()
// our dummy packet handler
@ -67,8 +67,8 @@ func TestService(t *testing.T) {
}
// run service
go func() { is.NoErr(srvc.Start()) }() // srvc.Start error
is.True(util.SelectWithTimeout(srvc.Started(), timeout)) // wait for service to start with timeout
go func() { is.NoErr(srvc.Start()) }() // srvc.Start error
is.True(testutil.SelectWithTimeout(srvc.Started(), timeout)) // wait for service to start with timeout
wg.Add(maxDummyPeers * 2) // 2 wg.Done() per peer for receiving packets
for i := 0; i < maxDummyPeers; i++ {
@ -93,5 +93,5 @@ func TestService(t *testing.T) {
}()
}
is.True(util.WaitWithTimeout(&wg, timeout)) // wait for all dummy peers to be done with timeout
is.True(testutil.WaitWithTimeout(&wg, timeout)) // wait for all dummy peers to be done with timeout
}

View File

@ -0,0 +1,116 @@
package testutil
import (
"context"
"fmt"
"net"
"sync"
"time"
"github.com/CPunch/gopenfusion/cnet"
"github.com/CPunch/gopenfusion/cnet/protocol"
"github.com/CPunch/gopenfusion/internal/db"
"github.com/CPunch/gopenfusion/internal/redis"
"github.com/alicebob/miniredis/v2"
"github.com/bitcomplete/sqltestutil"
"github.com/matryer/is"
)
type DummyPeer struct {
Recv chan *cnet.PacketEvent
Peer *cnet.Peer
}
// MakeDummyPeer creates a new dummy peer and returns it
func MakeDummyPeer(ctx context.Context, is *is.I, port int) *DummyPeer {
conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", port))
is.NoErr(err)
recv := make(chan *cnet.PacketEvent)
peer := cnet.NewPeer(ctx, conn)
go func() {
peer.Handler(recv)
}()
return &DummyPeer{Recv: recv, Peer: peer}
}
// SendAndRecv sends a packet (sID & out), waits for the expected response (rID) and decodes it into in
func (dp *DummyPeer) SendAndRecv(is *is.I, sID, rID uint32, out, in interface{}) {
// send out packet
err := dp.Peer.Send(sID, out)
is.NoErr(err) // peer.Send() should not return an error
// receive response
evnt := <-dp.Recv
defer protocol.PutBuffer(evnt.Pkt)
is.Equal(evnt.PktID, rID) // should receive expected type
is.NoErr(protocol.NewPacket(evnt.Pkt).Decode(in)) // packet.Decode() should not return an error
}
// Kill closes the peer's connection
func (dp *DummyPeer) Kill() {
dp.Peer.Kill()
}
// SetupEnvironment spawns a postgres container and returns a db and redis handler
// along with a cleanup function
func SetupEnvironment(ctx context.Context) (*db.DBHandler, *redis.RedisHandler, func()) {
// spawn postgres container
psql, err := sqltestutil.StartPostgresContainer(ctx, "15")
if err != nil {
panic(err)
}
// open db handler
testDB, err := db.OpenFromConnectionString("postgres", psql.ConnectionString()+"?sslmode=disable")
if err != nil {
psql.Shutdown(ctx)
panic(err)
}
if err = testDB.Setup(); err != nil {
psql.Shutdown(ctx)
panic(err)
}
// start miniredis
r, err := miniredis.Run()
if err != nil {
psql.Shutdown(ctx)
panic(err)
}
// open redis handler
rh, err := redis.OpenRedis(r.Addr())
if err != nil {
psql.Shutdown(ctx)
panic(err)
}
return testDB, rh, func() {
psql.Shutdown(ctx)
rh.Close()
r.Close()
}
}
func SelectWithTimeout(ch <-chan struct{}, timeout time.Duration) bool {
select {
case <-ch:
return true
case <-time.After(timeout):
return false
}
}
func WaitWithTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
done := make(chan struct{})
go func() {
defer close(done)
wg.Wait()
}()
return SelectWithTimeout(done, timeout)
}

View File

@ -0,0 +1,35 @@
package testutil_test
import (
"sync"
"testing"
"time"
"github.com/CPunch/gopenfusion/internal/testutil"
"github.com/matryer/is"
)
func TestWaitWithTimeout(t *testing.T) {
is := is.New(t)
wg := &sync.WaitGroup{}
go func() {
time.Sleep(1 * time.Second)
wg.Done()
}()
wg.Add(1)
is.True(!testutil.WaitWithTimeout(wg, 500*time.Millisecond)) // timeout should occur
is.True(testutil.WaitWithTimeout(wg, 750*time.Millisecond)) // timeout shouldn't occur
}
func TestSelectWithTimeout(t *testing.T) {
is := is.New(t)
ch := make(chan struct{})
go func() {
time.Sleep(1 * time.Second)
close(ch)
}()
is.True(!testutil.SelectWithTimeout(ch, 500*time.Millisecond)) // timeout should occur
is.True(testutil.SelectWithTimeout(ch, 750*time.Millisecond)) // timeout shouldn't occur
}

View File

@ -12,7 +12,6 @@ import (
"github.com/CPunch/gopenfusion/internal/config"
"github.com/CPunch/gopenfusion/internal/db"
"github.com/CPunch/gopenfusion/internal/redis"
"github.com/CPunch/gopenfusion/util"
)
const (
@ -34,7 +33,7 @@ func (server *LoginServer) AcceptLogin(peer *cnet.Peer, SzID string, IClientVerC
ISlotNum: ISlotNum,
IPaymentFlag: 1,
IOpenBetaFlag: 0,
UiSvrTime: util.GetTime(),
UiSvrTime: protocol.GetTime(),
}
if err := peer.Send(protocol.P_LS2CL_REP_LOGIN_SUCC, resp); err != nil {

View File

@ -3,8 +3,6 @@ package login_test
import (
"context"
"encoding/binary"
"fmt"
"net"
"os"
"testing"
@ -12,9 +10,8 @@ import (
"github.com/CPunch/gopenfusion/cnet/protocol"
"github.com/CPunch/gopenfusion/internal/db"
"github.com/CPunch/gopenfusion/internal/redis"
"github.com/CPunch/gopenfusion/internal/testutil"
"github.com/CPunch/gopenfusion/login"
"github.com/alicebob/miniredis/v2"
"github.com/bitcomplete/sqltestutil"
"github.com/matryer/is"
)
@ -44,65 +41,16 @@ var (
}
)
func makeDummyPeer(ctx context.Context, is *is.I, recv chan<- *cnet.PacketEvent) *cnet.Peer {
conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", loginPort))
is.NoErr(err)
peer := cnet.NewPeer(ctx, conn)
go func() {
peer.Handler(recv)
}()
return peer
}
func sendAndRecv(peer *cnet.Peer, recv chan *cnet.PacketEvent, is *is.I, sID, rID uint32, out, in interface{}) {
// send out packet
err := peer.Send(sID, out)
is.NoErr(err) // peer.Send() should not return an error
// receive response
evnt := <-recv
defer protocol.PutBuffer(evnt.Pkt)
is.Equal(evnt.PktID, rID) // should receive expected type
is.NoErr(protocol.NewPacket(evnt.Pkt).Decode(in)) // packet.Decode() should not return an error
}
func TestMain(m *testing.M) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// spawn postgres container
psql, err := sqltestutil.StartPostgresContainer(ctx, "15")
if err != nil {
panic(err)
}
// open db handler
testDB, err = db.OpenFromConnectionString("postgres", psql.ConnectionString()+"?sslmode=disable")
if err != nil {
panic(err)
}
if err = testDB.Setup(); err != nil {
panic(err)
}
// start miniredis
r, err := miniredis.Run()
if err != nil {
panic(err)
}
defer r.Close()
// open redis handler
rh, err = redis.OpenRedis(r.Addr())
if err != nil {
panic(err)
}
defer rh.Close()
// setup environment
var closer func()
testDB, rh, closer = testutil.SetupEnvironment(ctx)
defer closer()
var err error
loginPort, err = cnet.RandomPort()
if err != nil {
panic(err)
@ -134,13 +82,12 @@ func TestLoginSuccSequence(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
recv := make(chan *cnet.PacketEvent)
peer := makeDummyPeer(ctx, is, recv)
defer peer.Kill()
dummy := testutil.MakeDummyPeer(ctx, is, loginPort)
defer dummy.Kill()
// send login request (this should create an account)
var resp protocol.SP_LS2CL_REP_LOGIN_SUCC
sendAndRecv(peer, recv, is, protocol.P_CL2LS_REQ_LOGIN, protocol.P_LS2CL_REP_LOGIN_SUCC,
dummy.SendAndRecv(is, protocol.P_CL2LS_REQ_LOGIN, protocol.P_LS2CL_REP_LOGIN_SUCC,
protocol.SP_CL2LS_REQ_LOGIN{
SzID: "testLoginSequence",
SzPassword: "test",
@ -161,13 +108,12 @@ func TestLoginFailSequence(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
recv := make(chan *cnet.PacketEvent)
peer := makeDummyPeer(ctx, is, recv)
defer peer.Kill()
dummy := testutil.MakeDummyPeer(ctx, is, loginPort)
defer dummy.Kill()
// send login request (this should not create an account)
var resp protocol.SP_LS2CL_REP_LOGIN_FAIL
sendAndRecv(peer, recv, is, protocol.P_CL2LS_REQ_LOGIN, protocol.P_LS2CL_REP_LOGIN_FAIL,
dummy.SendAndRecv(is, protocol.P_CL2LS_REQ_LOGIN, protocol.P_LS2CL_REP_LOGIN_FAIL,
protocol.SP_CL2LS_REQ_LOGIN{
SzID: "",
SzPassword: "",
@ -184,13 +130,12 @@ func TestCharacterSequence(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
recv := make(chan *cnet.PacketEvent)
peer := makeDummyPeer(ctx, is, recv)
defer peer.Kill()
dummy := testutil.MakeDummyPeer(ctx, is, loginPort)
defer dummy.Kill()
// send login request (this should create an account)
var resp protocol.SP_LS2CL_REP_LOGIN_SUCC
sendAndRecv(peer, recv, is, protocol.P_CL2LS_REQ_LOGIN, protocol.P_LS2CL_REP_LOGIN_SUCC,
dummy.SendAndRecv(is, protocol.P_CL2LS_REQ_LOGIN, protocol.P_LS2CL_REP_LOGIN_SUCC,
protocol.SP_CL2LS_REQ_LOGIN{
SzID: "testCharacterSequence",
SzPassword: "test",
@ -201,12 +146,12 @@ func TestCharacterSequence(t *testing.T) {
is.Equal(resp.ICharCount, int8(0)) // should have 0 characters
// perform key swap
peer.E_key = protocol.CreateNewKey(
dummy.Peer.E_key = protocol.CreateNewKey(
resp.UiSvrTime,
uint64(resp.ICharCount+1),
uint64(resp.ISlotNum+1),
)
peer.FE_key = protocol.CreateNewKey(
dummy.Peer.FE_key = protocol.CreateNewKey(
binary.LittleEndian.Uint64([]byte(protocol.DEFAULT_KEY)),
0,
1,
@ -214,7 +159,7 @@ func TestCharacterSequence(t *testing.T) {
// send character name check request
var charResp protocol.SP_LS2CL_REP_SAVE_CHAR_NAME_SUCC
sendAndRecv(peer, recv, is, protocol.P_CL2LS_REQ_SAVE_CHAR_NAME, protocol.P_LS2CL_REP_SAVE_CHAR_NAME_SUCC,
dummy.SendAndRecv(is, protocol.P_CL2LS_REQ_SAVE_CHAR_NAME, protocol.P_LS2CL_REP_SAVE_CHAR_NAME_SUCC,
protocol.SP_CL2LS_REQ_SAVE_CHAR_NAME{
ISlotNum: 1,
IGender: 1,
@ -235,7 +180,7 @@ func TestCharacterSequence(t *testing.T) {
charCreate := testCharCreate
charCreate.PCStyle.IPC_UID = charResp.IPC_UID
var charCreateResp protocol.SP_LS2CL_REP_CHAR_CREATE_SUCC
sendAndRecv(peer, recv, is, protocol.P_CL2LS_REQ_CHAR_CREATE, protocol.P_LS2CL_REP_CHAR_CREATE_SUCC,
dummy.SendAndRecv(is, protocol.P_CL2LS_REQ_CHAR_CREATE, protocol.P_LS2CL_REP_CHAR_CREATE_SUCC,
charCreate, &charCreateResp)
// verify response

View File

@ -8,7 +8,6 @@ import (
"github.com/CPunch/gopenfusion/cnet/protocol"
"github.com/CPunch/gopenfusion/internal/redis"
"github.com/CPunch/gopenfusion/shard/entity"
"github.com/CPunch/gopenfusion/util"
)
func (server *ShardServer) attachPlayer(peer *cnet.Peer, meta redis.LoginMetadata) (*entity.Player, error) {
@ -51,7 +50,7 @@ func (server *ShardServer) RequestEnter(peer *cnet.Peer, pkt protocol.Packet) er
resp := &protocol.SP_FE2CL_REP_PC_ENTER_SUCC{
IID: int32(plr.PlayerID),
PCLoadData2CL: plr.ToPCLoadData2CL(),
UiSvrTime: util.GetTime(),
UiSvrTime: protocol.GetTime(),
}
// setup peer

View File

@ -6,7 +6,6 @@ import (
"github.com/CPunch/gopenfusion/cnet"
"github.com/CPunch/gopenfusion/cnet/protocol"
"github.com/CPunch/gopenfusion/shard/entity"
"github.com/CPunch/gopenfusion/util"
)
func (server *ShardServer) updatePlayerPosition(plr *entity.Player, X, Y, Z, Angle int) {
@ -41,7 +40,7 @@ func (server *ShardServer) playerMove(peer *cnet.Peer, pkt protocol.Packet) erro
CKeyValue: move.CKeyValue,
ISpeed: move.ISpeed,
IID: int32(plr.PlayerID),
ISvrTime: util.GetTime(),
ISvrTime: protocol.GetTime(),
})
}
@ -63,7 +62,7 @@ func (server *ShardServer) playerStop(peer *cnet.Peer, pkt protocol.Packet) erro
IY: stop.IY,
IZ: stop.IZ,
IID: int32(plr.PlayerID),
ISvrTime: util.GetTime(),
ISvrTime: protocol.GetTime(),
})
}
@ -91,6 +90,6 @@ func (server *ShardServer) playerJump(peer *cnet.Peer, pkt protocol.Packet) erro
CKeyValue: jump.CKeyValue,
ISpeed: jump.ISpeed,
IID: int32(plr.PlayerID),
ISvrTime: util.GetTime(),
ISvrTime: protocol.GetTime(),
})
}

View File

@ -50,9 +50,9 @@ func NewShardServer(ctx context.Context, dbHndlr *db.DBHandler, redisHndlr *redi
return server, nil
}
func (server *ShardServer) Start() {
func (server *ShardServer) Start() error {
server.LoadNPCs()
server.service.Start()
return server.service.Start()
}
func (server *ShardServer) onDisconnect(peer *cnet.Peer) {
@ -66,3 +66,7 @@ func (server *ShardServer) onDisconnect(peer *cnet.Peer) {
func (server *ShardServer) onConnect(peer *cnet.Peer) {
}
func (server *ShardServer) Service() *cnet.Service {
return server.service
}

View File

@ -1,29 +0,0 @@
package util
import (
"sync"
"time"
)
func GetTime() uint64 {
return uint64(time.Now().UnixMilli())
}
func SelectWithTimeout(ch <-chan struct{}, timeout time.Duration) bool {
select {
case <-ch:
return true
case <-time.After(timeout):
return false
}
}
func WaitWithTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
done := make(chan struct{})
go func() {
defer close(done)
wg.Wait()
}()
return SelectWithTimeout(done, timeout)
}

View File

@ -1,35 +0,0 @@
package util_test
import (
"sync"
"testing"
"time"
"github.com/CPunch/gopenfusion/util"
"github.com/matryer/is"
)
func TestWaitWithTimeout(t *testing.T) {
is := is.New(t)
wg := &sync.WaitGroup{}
go func() {
time.Sleep(1 * time.Second)
wg.Done()
}()
wg.Add(1)
is.True(!util.WaitWithTimeout(wg, 500*time.Millisecond)) // timeout should occur
is.True(util.WaitWithTimeout(wg, 750*time.Millisecond)) // timeout shouldn't occur
}
func TestSelectWithTimeout(t *testing.T) {
is := is.New(t)
ch := make(chan struct{})
go func() {
time.Sleep(1 * time.Second)
close(ch)
}()
is.True(!util.SelectWithTimeout(ch, 500*time.Millisecond)) // timeout should occur
is.True(util.SelectWithTimeout(ch, 750*time.Millisecond)) // timeout shouldn't occur
}