mirror of
https://github.com/CPunch/gopenfusion.git
synced 2025-10-26 10:50:09 +00:00
Compare commits
13 Commits
23170093ee
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| cafca9093c | |||
| d84fcd2c93 | |||
| 1f63f9856e | |||
| de3e067b48 | |||
| 02afe67ac3 | |||
| 79f68187bf | |||
| cd93a058ce | |||
| 0a28dbcc3e | |||
| 1a6de671e5 | |||
| 261ea6505f | |||
| 556878544d | |||
| bfcbe6d3d6 | |||
| e5a9ed1481 |
9
cnet/protocol/time.go
Normal file
9
cnet/protocol/time.go
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package protocol
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetTime() uint64 {
|
||||||
|
return uint64(time.Now().UnixMilli())
|
||||||
|
}
|
||||||
@@ -43,7 +43,7 @@ type Service struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func RandomPort() (int, error) {
|
func RandomPort() (int, error) {
|
||||||
l, err := net.Listen("tcp", "127.0.0.1:0")
|
l, err := net.Listen("tcp", ":0")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
|
|
||||||
"github.com/CPunch/gopenfusion/cnet"
|
"github.com/CPunch/gopenfusion/cnet"
|
||||||
"github.com/CPunch/gopenfusion/cnet/protocol"
|
"github.com/CPunch/gopenfusion/cnet/protocol"
|
||||||
"github.com/CPunch/gopenfusion/util"
|
"github.com/CPunch/gopenfusion/internal/testutil"
|
||||||
"github.com/matryer/is"
|
"github.com/matryer/is"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -32,6 +32,7 @@ func TestMain(m *testing.M) {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// this is fine since we don't defer anything
|
||||||
os.Exit(m.Run())
|
os.Exit(m.Run())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,7 +45,7 @@ func TestService(t *testing.T) {
|
|||||||
// shutdown service when test is done
|
// shutdown service when test is done
|
||||||
defer func() {
|
defer func() {
|
||||||
cancel()
|
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
|
// our dummy packet handler
|
||||||
@@ -67,8 +68,8 @@ func TestService(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// run service
|
// run service
|
||||||
go func() { is.NoErr(srvc.Start()) }() // srvc.Start error
|
go func() { is.NoErr(srvc.Start()) }() // srvc.Start error
|
||||||
is.True(util.SelectWithTimeout(srvc.Started(), timeout)) // wait for service to start with timeout
|
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
|
wg.Add(maxDummyPeers * 2) // 2 wg.Done() per peer for receiving packets
|
||||||
for i := 0; i < maxDummyPeers; i++ {
|
for i := 0; i < maxDummyPeers; i++ {
|
||||||
@@ -93,5 +94,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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,11 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
|
ret := 1
|
||||||
|
defer func() {
|
||||||
|
os.Exit(ret)
|
||||||
|
}()
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
psql, err := sqltestutil.StartPostgresContainer(ctx, "15")
|
psql, err := sqltestutil.StartPostgresContainer(ctx, "15")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -35,7 +40,7 @@ func TestMain(m *testing.M) {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Exit(m.Run())
|
ret = m.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDBAccount(t *testing.T) {
|
func TestDBAccount(t *testing.T) {
|
||||||
|
|||||||
@@ -14,6 +14,11 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
|
ret := 1
|
||||||
|
defer func() {
|
||||||
|
os.Exit(ret)
|
||||||
|
}()
|
||||||
|
|
||||||
r, err := miniredis.Run()
|
r, err := miniredis.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@@ -26,7 +31,7 @@ func TestMain(m *testing.M) {
|
|||||||
}
|
}
|
||||||
defer rh.Close()
|
defer rh.Close()
|
||||||
|
|
||||||
os.Exit(m.Run())
|
ret = m.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRedisLogin(t *testing.T) {
|
func TestRedisLogin(t *testing.T) {
|
||||||
|
|||||||
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,14 +1,10 @@
|
|||||||
package util
|
package testutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetTime() uint64 {
|
|
||||||
return uint64(time.Now().UnixMilli())
|
|
||||||
}
|
|
||||||
|
|
||||||
func SelectWithTimeout(ch <-chan struct{}, timeout time.Duration) bool {
|
func SelectWithTimeout(ch <-chan struct{}, timeout time.Duration) bool {
|
||||||
select {
|
select {
|
||||||
case <-ch:
|
case <-ch:
|
||||||
35
internal/testutil/testutil_test.go
Normal file
35
internal/testutil/testutil_test.go
Normal 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
|
||||||
|
}
|
||||||
@@ -12,7 +12,6 @@ import (
|
|||||||
"github.com/CPunch/gopenfusion/internal/config"
|
"github.com/CPunch/gopenfusion/internal/config"
|
||||||
"github.com/CPunch/gopenfusion/internal/db"
|
"github.com/CPunch/gopenfusion/internal/db"
|
||||||
"github.com/CPunch/gopenfusion/internal/redis"
|
"github.com/CPunch/gopenfusion/internal/redis"
|
||||||
"github.com/CPunch/gopenfusion/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -34,7 +33,7 @@ func (server *LoginServer) AcceptLogin(peer *cnet.Peer, SzID string, IClientVerC
|
|||||||
ISlotNum: ISlotNum,
|
ISlotNum: ISlotNum,
|
||||||
IPaymentFlag: 1,
|
IPaymentFlag: 1,
|
||||||
IOpenBetaFlag: 0,
|
IOpenBetaFlag: 0,
|
||||||
UiSvrTime: util.GetTime(),
|
UiSvrTime: protocol.GetTime(),
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := peer.Send(protocol.P_LS2CL_REP_LOGIN_SUCC, resp); err != nil {
|
if err := peer.Send(protocol.P_LS2CL_REP_LOGIN_SUCC, resp); err != nil {
|
||||||
|
|||||||
@@ -3,8 +3,6 @@ package login_test
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
|
||||||
"net"
|
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -12,9 +10,8 @@ import (
|
|||||||
"github.com/CPunch/gopenfusion/cnet/protocol"
|
"github.com/CPunch/gopenfusion/cnet/protocol"
|
||||||
"github.com/CPunch/gopenfusion/internal/db"
|
"github.com/CPunch/gopenfusion/internal/db"
|
||||||
"github.com/CPunch/gopenfusion/internal/redis"
|
"github.com/CPunch/gopenfusion/internal/redis"
|
||||||
|
"github.com/CPunch/gopenfusion/internal/testutil"
|
||||||
"github.com/CPunch/gopenfusion/login"
|
"github.com/CPunch/gopenfusion/login"
|
||||||
"github.com/alicebob/miniredis/v2"
|
|
||||||
"github.com/bitcomplete/sqltestutil"
|
|
||||||
"github.com/matryer/is"
|
"github.com/matryer/is"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -25,84 +22,26 @@ var (
|
|||||||
rh *redis.RedisHandler
|
rh *redis.RedisHandler
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
/*
|
||||||
testCharCreate = protocol.SP_CL2LS_REQ_CHAR_CREATE{
|
test data was scraped by dumping packets, just adding a println to the LoginService
|
||||||
PCStyle: protocol.SPCStyle{
|
to print the packet data
|
||||||
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 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) {
|
func TestMain(m *testing.M) {
|
||||||
|
ret := 1
|
||||||
|
defer func() {
|
||||||
|
os.Exit(ret)
|
||||||
|
}()
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
// spawn postgres container
|
// setup environment
|
||||||
psql, err := sqltestutil.StartPostgresContainer(ctx, "15")
|
var closer func()
|
||||||
if err != nil {
|
testDB, rh, closer = testutil.SetupEnvironment(ctx)
|
||||||
panic(err)
|
defer closer()
|
||||||
}
|
|
||||||
|
|
||||||
// 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()
|
|
||||||
|
|
||||||
|
var err error
|
||||||
loginPort, err = cnet.RandomPort()
|
loginPort, err = cnet.RandomPort()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@@ -122,10 +61,9 @@ func TestMain(m *testing.M) {
|
|||||||
|
|
||||||
// wait for login server to start, then start tests
|
// wait for login server to start, then start tests
|
||||||
<-loginSrv.Service().Started()
|
<-loginSrv.Service().Started()
|
||||||
ret := m.Run()
|
ret = m.Run()
|
||||||
cancel()
|
cancel()
|
||||||
<-loginSrv.Service().Stopped()
|
<-loginSrv.Service().Stopped()
|
||||||
os.Exit(ret)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This test tries a typical login sequence.
|
// This test tries a typical login sequence.
|
||||||
@@ -134,13 +72,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 := makeDummyPeer(ctx, is, 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
|
||||||
sendAndRecv(peer, recv, is, protocol.P_CL2LS_REQ_LOGIN, protocol.P_LS2CL_REP_LOGIN_SUCC,
|
dummy.SendAndRecv(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",
|
||||||
@@ -161,13 +98,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 := makeDummyPeer(ctx, is, 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
|
||||||
sendAndRecv(peer, recv, is, protocol.P_CL2LS_REQ_LOGIN, protocol.P_LS2CL_REP_LOGIN_FAIL,
|
dummy.SendAndRecv(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: "",
|
||||||
@@ -184,13 +120,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 := makeDummyPeer(ctx, is, 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
|
||||||
sendAndRecv(peer, recv, is, protocol.P_CL2LS_REQ_LOGIN, protocol.P_LS2CL_REP_LOGIN_SUCC,
|
dummy.SendAndRecv(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",
|
||||||
@@ -201,12 +136,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,
|
||||||
@@ -214,28 +149,28 @@ 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
|
||||||
sendAndRecv(peer, recv, is, protocol.P_CL2LS_REQ_SAVE_CHAR_NAME, protocol.P_LS2CL_REP_SAVE_CHAR_NAME_SUCC,
|
dummy.SendAndRecv(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,
|
||||||
IFNCode: 260,
|
IFNCode: 260,
|
||||||
ILNCode: 551,
|
ILNCode: 551,
|
||||||
IMNCode: 33,
|
IMNCode: 33,
|
||||||
SzFirstName: testCharCreate.PCStyle.SzFirstName,
|
SzFirstName: testutil.TestCharCreate.PCStyle.SzFirstName,
|
||||||
SzLastName: testCharCreate.PCStyle.SzLastName,
|
SzLastName: testutil.TestCharCreate.PCStyle.SzLastName,
|
||||||
}, &charResp)
|
}, &charResp)
|
||||||
|
|
||||||
// verify response
|
// verify response
|
||||||
is.Equal(charResp.ISlotNum, int8(1)) // should have the same slot number
|
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.IGender, int8(1)) // should have the same gender
|
||||||
is.Equal(charResp.SzFirstName, testCharCreate.PCStyle.SzFirstName) // should have the same first name
|
is.Equal(charResp.SzFirstName, testutil.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.SzLastName, testutil.TestCharCreate.PCStyle.SzLastName) // should have the same last name
|
||||||
|
|
||||||
// send character create request
|
// send character create request
|
||||||
charCreate := testCharCreate
|
charCreate := testutil.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
|
||||||
sendAndRecv(peer, recv, is, protocol.P_CL2LS_REQ_CHAR_CREATE, protocol.P_LS2CL_REP_CHAR_CREATE_SUCC,
|
dummy.SendAndRecv(protocol.P_CL2LS_REQ_CHAR_CREATE, protocol.P_LS2CL_REP_CHAR_CREATE_SUCC,
|
||||||
charCreate, &charCreateResp)
|
charCreate, &charCreateResp)
|
||||||
|
|
||||||
// verify response
|
// verify response
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import (
|
|||||||
"github.com/CPunch/gopenfusion/cnet/protocol"
|
"github.com/CPunch/gopenfusion/cnet/protocol"
|
||||||
"github.com/CPunch/gopenfusion/internal/redis"
|
"github.com/CPunch/gopenfusion/internal/redis"
|
||||||
"github.com/CPunch/gopenfusion/shard/entity"
|
"github.com/CPunch/gopenfusion/shard/entity"
|
||||||
"github.com/CPunch/gopenfusion/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (server *ShardServer) attachPlayer(peer *cnet.Peer, meta redis.LoginMetadata) (*entity.Player, error) {
|
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{
|
resp := &protocol.SP_FE2CL_REP_PC_ENTER_SUCC{
|
||||||
IID: int32(plr.PlayerID),
|
IID: int32(plr.PlayerID),
|
||||||
PCLoadData2CL: plr.ToPCLoadData2CL(),
|
PCLoadData2CL: plr.ToPCLoadData2CL(),
|
||||||
UiSvrTime: util.GetTime(),
|
UiSvrTime: protocol.GetTime(),
|
||||||
}
|
}
|
||||||
|
|
||||||
// setup peer
|
// setup peer
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"github.com/CPunch/gopenfusion/cnet"
|
"github.com/CPunch/gopenfusion/cnet"
|
||||||
"github.com/CPunch/gopenfusion/cnet/protocol"
|
"github.com/CPunch/gopenfusion/cnet/protocol"
|
||||||
"github.com/CPunch/gopenfusion/shard/entity"
|
"github.com/CPunch/gopenfusion/shard/entity"
|
||||||
"github.com/CPunch/gopenfusion/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (server *ShardServer) updatePlayerPosition(plr *entity.Player, X, Y, Z, Angle int) {
|
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,
|
CKeyValue: move.CKeyValue,
|
||||||
ISpeed: move.ISpeed,
|
ISpeed: move.ISpeed,
|
||||||
IID: int32(plr.PlayerID),
|
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,
|
IY: stop.IY,
|
||||||
IZ: stop.IZ,
|
IZ: stop.IZ,
|
||||||
IID: int32(plr.PlayerID),
|
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,
|
CKeyValue: jump.CKeyValue,
|
||||||
ISpeed: jump.ISpeed,
|
ISpeed: jump.ISpeed,
|
||||||
IID: int32(plr.PlayerID),
|
IID: int32(plr.PlayerID),
|
||||||
ISvrTime: util.GetTime(),
|
ISvrTime: protocol.GetTime(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,8 @@ func (server *ShardServer) LoadNPCs() {
|
|||||||
|
|
||||||
data, err := os.ReadFile(config.GetTDataPath() + "/NPCs.json")
|
data, err := os.ReadFile(config.GetTDataPath() + "/NPCs.json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
log.Printf("Warning: failed to load NPCs: %v", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// yes, we have to do it this way so our NPCs IDs will be incremented and unique
|
// yes, we have to do it this way so our NPCs IDs will be incremented and unique
|
||||||
|
|||||||
@@ -50,9 +50,9 @@ func NewShardServer(ctx context.Context, dbHndlr *db.DBHandler, redisHndlr *redi
|
|||||||
return server, nil
|
return server, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server *ShardServer) Start() {
|
func (server *ShardServer) Start() error {
|
||||||
server.LoadNPCs()
|
server.LoadNPCs()
|
||||||
server.service.Start()
|
return server.service.Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server *ShardServer) onDisconnect(peer *cnet.Peer) {
|
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) onConnect(peer *cnet.Peer) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (server *ShardServer) Service() *cnet.Service {
|
||||||
|
return server.service
|
||||||
|
}
|
||||||
|
|||||||
@@ -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
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user