testutil: refactoring && cleanup

added a simple DummyPeer struct to simplify creation, send/recv and cleanup
This commit is contained in:
CPunch 2024-02-01 17:21:56 -06:00
parent bfcbe6d3d6
commit 556878544d
2 changed files with 53 additions and 41 deletions

View File

@ -82,13 +82,12 @@ func TestLoginSuccSequence(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
recv := make(chan *cnet.PacketEvent)
peer := testutil.MakeDummyPeer(ctx, is, loginPort, 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
testutil.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",
@ -109,13 +108,12 @@ func TestLoginFailSequence(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
recv := make(chan *cnet.PacketEvent)
peer := testutil.MakeDummyPeer(ctx, is, loginPort, 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
testutil.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: "",
@ -132,13 +130,12 @@ func TestCharacterSequence(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
recv := make(chan *cnet.PacketEvent)
peer := testutil.MakeDummyPeer(ctx, is, loginPort, 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
testutil.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",
@ -149,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,
@ -162,7 +159,7 @@ func TestCharacterSequence(t *testing.T) {
// send character name check request
var charResp protocol.SP_LS2CL_REP_SAVE_CHAR_NAME_SUCC
testutil.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,
@ -183,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
testutil.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

@ -14,6 +14,46 @@ import (
"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")
@ -49,28 +89,3 @@ func SetupEnvironment(ctx context.Context) (*db.DBHandler, *redis.RedisHandler,
r.Close()
}
}
func MakeDummyPeer(ctx context.Context, is *is.I, port int, recv chan<- *cnet.PacketEvent) *cnet.Peer {
conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", port))
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
}