mirror of
https://github.com/CPunch/gopenfusion.git
synced 2024-12-04 14:36:02 +00:00
Compare commits
3 Commits
de3e067b48
...
cafca9093c
Author | SHA1 | Date | |
---|---|---|---|
cafca9093c | |||
d84fcd2c93 | |||
1f63f9856e |
68
internal/testutil/account.go
Normal file
68
internal/testutil/account.go
Normal file
@ -0,0 +1,68 @@
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"github.com/CPunch/gopenfusion/cnet/protocol"
|
||||
"github.com/CPunch/gopenfusion/internal/db"
|
||||
"github.com/CPunch/gopenfusion/internal/redis"
|
||||
)
|
||||
|
||||
var (
|
||||
TestCharCreate = protocol.SP_CL2LS_REQ_CHAR_CREATE{
|
||||
PCStyle: protocol.SPCStyle{
|
||||
INameCheck: 1, SzFirstName: "Hector",
|
||||
SzLastName: "Bannonvenom", IGender: 1, IFaceStyle: 1,
|
||||
IHairStyle: 17, IHairColor: 11, ISkinColor: 10, IEyeColor: 2,
|
||||
IHeight: 1, IBody: 0, IClass: 0,
|
||||
},
|
||||
SOn_Item: protocol.SOnItem{
|
||||
IEquipHandID: 0, IEquipUBID: 53, IEquipLBID: 17, IEquipFootID: 58,
|
||||
IEquipHeadID: 0, IEquipFaceID: 0, IEquipBackID: 0,
|
||||
},
|
||||
SOn_Item_Index: protocol.SOnItem_Index{
|
||||
IEquipUBID_index: 15, IEquipLBID_index: 12, IEquipFootID_index: 17,
|
||||
IFaceStyle: 2, IHairStyle: 18,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
// creates a new account and player in the database
|
||||
func MakeTestPlayer(db *db.DBHandler, id string, password string) (acc *db.Account, plr *db.Player, err error) {
|
||||
acc, err = db.NewAccount(id, password)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var plrID int
|
||||
plrID, err = db.NewPlayer(acc.AccountID, TestCharCreate.PCStyle.SzFirstName, TestCharCreate.PCStyle.SzLastName, 1)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
charCreate := TestCharCreate
|
||||
charCreate.PCStyle.IPC_UID = int64(plrID)
|
||||
err = db.FinishPlayer(&charCreate, acc.AccountID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = db.FinishTutorial(plrID, acc.AccountID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
plr, err = db.GetPlayer(plrID)
|
||||
return
|
||||
}
|
||||
|
||||
func QueueLogin(redisHndlr *redis.RedisHandler, FEKey []byte, plrID, accID int) (int64, error) {
|
||||
key, err := protocol.GenSerialKey()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return key, redisHndlr.QueueLogin(key, redis.LoginMetadata{
|
||||
FEKey: FEKey,
|
||||
PlayerID: int32(plrID),
|
||||
AccountID: accID,
|
||||
})
|
||||
}
|
50
internal/testutil/dummy.go
Normal file
50
internal/testutil/dummy.go
Normal file
@ -0,0 +1,50 @@
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/CPunch/gopenfusion/cnet"
|
||||
"github.com/CPunch/gopenfusion/cnet/protocol"
|
||||
"github.com/matryer/is"
|
||||
)
|
||||
|
||||
type DummyPeer struct {
|
||||
Recv chan *cnet.PacketEvent
|
||||
Peer *cnet.Peer
|
||||
is *is.I
|
||||
}
|
||||
|
||||
// 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, is: is}
|
||||
}
|
||||
|
||||
// SendAndRecv sends a packet (sID & out), waits for the expected response (rID) and decodes it into in
|
||||
func (dp *DummyPeer) SendAndRecv(sID, rID uint32, out, in interface{}) {
|
||||
// send out packet
|
||||
err := dp.Peer.Send(sID, out)
|
||||
dp.is.NoErr(err) // peer.Send() should not return an error
|
||||
|
||||
// receive response
|
||||
evnt := <-dp.Recv
|
||||
defer protocol.PutBuffer(evnt.Pkt)
|
||||
|
||||
dp.is.Equal(evnt.PktID, rID) // should receive expected type
|
||||
dp.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()
|
||||
}
|
52
internal/testutil/env.go
Normal file
52
internal/testutil/env.go
Normal file
@ -0,0 +1,52 @@
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/CPunch/gopenfusion/internal/db"
|
||||
"github.com/CPunch/gopenfusion/internal/redis"
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
"github.com/bitcomplete/sqltestutil"
|
||||
)
|
||||
|
||||
// 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()
|
||||
}
|
||||
}
|
@ -1,102 +1,10 @@
|
||||
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
|
||||
is *is.I
|
||||
}
|
||||
|
||||
// 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, is: is}
|
||||
}
|
||||
|
||||
// SendAndRecv sends a packet (sID & out), waits for the expected response (rID) and decodes it into in
|
||||
func (dp *DummyPeer) SendAndRecv(sID, rID uint32, out, in interface{}) {
|
||||
// send out packet
|
||||
err := dp.Peer.Send(sID, out)
|
||||
dp.is.NoErr(err) // peer.Send() should not return an error
|
||||
|
||||
// receive response
|
||||
evnt := <-dp.Recv
|
||||
defer protocol.PutBuffer(evnt.Pkt)
|
||||
|
||||
dp.is.Equal(evnt.PktID, rID) // should receive expected type
|
||||
dp.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:
|
||||
|
@ -27,25 +27,6 @@ test data was scraped by dumping packets, just adding a println to the LoginServ
|
||||
to print the packet data
|
||||
*/
|
||||
|
||||
var (
|
||||
testCharCreate = protocol.SP_CL2LS_REQ_CHAR_CREATE{
|
||||
PCStyle: protocol.SPCStyle{
|
||||
INameCheck: 1, SzFirstName: "Hector",
|
||||
SzLastName: "Bannonvenom", IGender: 1, IFaceStyle: 1,
|
||||
IHairStyle: 17, IHairColor: 11, ISkinColor: 10, IEyeColor: 2,
|
||||
IHeight: 1, IBody: 0, IClass: 0,
|
||||
},
|
||||
SOn_Item: protocol.SOnItem{
|
||||
IEquipHandID: 0, IEquipUBID: 53, IEquipLBID: 17, IEquipFootID: 58,
|
||||
IEquipHeadID: 0, IEquipFaceID: 0, IEquipBackID: 0,
|
||||
},
|
||||
SOn_Item_Index: protocol.SOnItem_Index{
|
||||
IEquipUBID_index: 15, IEquipLBID_index: 12, IEquipFootID_index: 17,
|
||||
IFaceStyle: 2, IHairStyle: 18,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
ret := 1
|
||||
defer func() {
|
||||
@ -175,18 +156,18 @@ func TestCharacterSequence(t *testing.T) {
|
||||
IFNCode: 260,
|
||||
ILNCode: 551,
|
||||
IMNCode: 33,
|
||||
SzFirstName: testCharCreate.PCStyle.SzFirstName,
|
||||
SzLastName: testCharCreate.PCStyle.SzLastName,
|
||||
SzFirstName: testutil.TestCharCreate.PCStyle.SzFirstName,
|
||||
SzLastName: testutil.TestCharCreate.PCStyle.SzLastName,
|
||||
}, &charResp)
|
||||
|
||||
// verify response
|
||||
is.Equal(charResp.ISlotNum, int8(1)) // should have the same slot number
|
||||
is.Equal(charResp.IGender, int8(1)) // should have the same gender
|
||||
is.Equal(charResp.SzFirstName, testCharCreate.PCStyle.SzFirstName) // should have the same first name
|
||||
is.Equal(charResp.SzLastName, testCharCreate.PCStyle.SzLastName) // should have the same last name
|
||||
is.Equal(charResp.SzFirstName, testutil.TestCharCreate.PCStyle.SzFirstName) // should have the same first name
|
||||
is.Equal(charResp.SzLastName, testutil.TestCharCreate.PCStyle.SzLastName) // should have the same last name
|
||||
|
||||
// send character create request
|
||||
charCreate := testCharCreate
|
||||
charCreate := testutil.TestCharCreate
|
||||
charCreate.PCStyle.IPC_UID = charResp.IPC_UID
|
||||
var charCreateResp protocol.SP_LS2CL_REP_CHAR_CREATE_SUCC
|
||||
dummy.SendAndRecv(protocol.P_CL2LS_REQ_CHAR_CREATE, protocol.P_LS2CL_REP_CHAR_CREATE_SUCC,
|
||||
|
Loading…
Reference in New Issue
Block a user