mirror of
https://github.com/CPunch/gopenfusion.git
synced 2024-11-21 15:00:07 +00:00
testutil: refactoring && cleanup
added a simple DummyPeer struct to simplify creation, send/recv and cleanup
This commit is contained in:
parent
bfcbe6d3d6
commit
556878544d
@ -82,13 +82,12 @@ func TestLoginSuccSequence(t *testing.T) {
|
|||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
recv := make(chan *cnet.PacketEvent)
|
dummy := testutil.MakeDummyPeer(ctx, is, loginPort)
|
||||||
peer := testutil.MakeDummyPeer(ctx, is, loginPort, recv)
|
defer dummy.Kill()
|
||||||
defer peer.Kill()
|
|
||||||
|
|
||||||
// send login request (this should create an account)
|
// send login request (this should create an account)
|
||||||
var resp protocol.SP_LS2CL_REP_LOGIN_SUCC
|
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{
|
protocol.SP_CL2LS_REQ_LOGIN{
|
||||||
SzID: "testLoginSequence",
|
SzID: "testLoginSequence",
|
||||||
SzPassword: "test",
|
SzPassword: "test",
|
||||||
@ -109,13 +108,12 @@ func TestLoginFailSequence(t *testing.T) {
|
|||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
recv := make(chan *cnet.PacketEvent)
|
dummy := testutil.MakeDummyPeer(ctx, is, loginPort)
|
||||||
peer := testutil.MakeDummyPeer(ctx, is, loginPort, recv)
|
defer dummy.Kill()
|
||||||
defer peer.Kill()
|
|
||||||
|
|
||||||
// send login request (this should not create an account)
|
// send login request (this should not create an account)
|
||||||
var resp protocol.SP_LS2CL_REP_LOGIN_FAIL
|
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{
|
protocol.SP_CL2LS_REQ_LOGIN{
|
||||||
SzID: "",
|
SzID: "",
|
||||||
SzPassword: "",
|
SzPassword: "",
|
||||||
@ -132,13 +130,12 @@ func TestCharacterSequence(t *testing.T) {
|
|||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
recv := make(chan *cnet.PacketEvent)
|
dummy := testutil.MakeDummyPeer(ctx, is, loginPort)
|
||||||
peer := testutil.MakeDummyPeer(ctx, is, loginPort, recv)
|
defer dummy.Kill()
|
||||||
defer peer.Kill()
|
|
||||||
|
|
||||||
// send login request (this should create an account)
|
// send login request (this should create an account)
|
||||||
var resp protocol.SP_LS2CL_REP_LOGIN_SUCC
|
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{
|
protocol.SP_CL2LS_REQ_LOGIN{
|
||||||
SzID: "testCharacterSequence",
|
SzID: "testCharacterSequence",
|
||||||
SzPassword: "test",
|
SzPassword: "test",
|
||||||
@ -149,12 +146,12 @@ func TestCharacterSequence(t *testing.T) {
|
|||||||
is.Equal(resp.ICharCount, int8(0)) // should have 0 characters
|
is.Equal(resp.ICharCount, int8(0)) // should have 0 characters
|
||||||
|
|
||||||
// perform key swap
|
// perform key swap
|
||||||
peer.E_key = protocol.CreateNewKey(
|
dummy.Peer.E_key = protocol.CreateNewKey(
|
||||||
resp.UiSvrTime,
|
resp.UiSvrTime,
|
||||||
uint64(resp.ICharCount+1),
|
uint64(resp.ICharCount+1),
|
||||||
uint64(resp.ISlotNum+1),
|
uint64(resp.ISlotNum+1),
|
||||||
)
|
)
|
||||||
peer.FE_key = protocol.CreateNewKey(
|
dummy.Peer.FE_key = protocol.CreateNewKey(
|
||||||
binary.LittleEndian.Uint64([]byte(protocol.DEFAULT_KEY)),
|
binary.LittleEndian.Uint64([]byte(protocol.DEFAULT_KEY)),
|
||||||
0,
|
0,
|
||||||
1,
|
1,
|
||||||
@ -162,7 +159,7 @@ func TestCharacterSequence(t *testing.T) {
|
|||||||
|
|
||||||
// send character name check request
|
// send character name check request
|
||||||
var charResp protocol.SP_LS2CL_REP_SAVE_CHAR_NAME_SUCC
|
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{
|
protocol.SP_CL2LS_REQ_SAVE_CHAR_NAME{
|
||||||
ISlotNum: 1,
|
ISlotNum: 1,
|
||||||
IGender: 1,
|
IGender: 1,
|
||||||
@ -183,7 +180,7 @@ func TestCharacterSequence(t *testing.T) {
|
|||||||
charCreate := testCharCreate
|
charCreate := testCharCreate
|
||||||
charCreate.PCStyle.IPC_UID = charResp.IPC_UID
|
charCreate.PCStyle.IPC_UID = charResp.IPC_UID
|
||||||
var charCreateResp protocol.SP_LS2CL_REP_CHAR_CREATE_SUCC
|
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)
|
charCreate, &charCreateResp)
|
||||||
|
|
||||||
// verify response
|
// verify response
|
||||||
|
@ -14,6 +14,46 @@ import (
|
|||||||
"github.com/matryer/is"
|
"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()) {
|
func SetupEnvironment(ctx context.Context) (*db.DBHandler, *redis.RedisHandler, func()) {
|
||||||
// spawn postgres container
|
// spawn postgres container
|
||||||
psql, err := sqltestutil.StartPostgresContainer(ctx, "15")
|
psql, err := sqltestutil.StartPostgresContainer(ctx, "15")
|
||||||
@ -49,28 +89,3 @@ func SetupEnvironment(ctx context.Context) (*db.DBHandler, *redis.RedisHandler,
|
|||||||
r.Close()
|
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
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user