From 0a28dbcc3e2d220f9d350e01652b70fdc4d47351 Mon Sep 17 00:00:00 2001 From: CPunch Date: Thu, 1 Feb 2024 18:25:49 -0600 Subject: [PATCH] removed util - WaitWithTimeout && SelectWithTimeout have been moved to internal/testutil - GetTime has been moved to cnet/protocol --- cnet/protocol/time.go | 9 ++++++++ cnet/service_test.go | 10 ++++----- internal/testutil/helpers.go | 21 ++++++++++++++++++ internal/testutil/testutil_test.go | 35 ++++++++++++++++++++++++++++++ login/login.go | 3 +-- shard/join.go | 3 +-- shard/movement.go | 7 +++--- util/util.go | 29 ------------------------- util/util_test.go | 35 ------------------------------ 9 files changed, 75 insertions(+), 77 deletions(-) create mode 100644 cnet/protocol/time.go create mode 100644 internal/testutil/testutil_test.go delete mode 100644 util/util.go delete mode 100644 util/util_test.go diff --git a/cnet/protocol/time.go b/cnet/protocol/time.go new file mode 100644 index 0000000..6ddff77 --- /dev/null +++ b/cnet/protocol/time.go @@ -0,0 +1,9 @@ +package protocol + +import ( + "time" +) + +func GetTime() uint64 { + return uint64(time.Now().UnixMilli()) +} diff --git a/cnet/service_test.go b/cnet/service_test.go index 8c73dcc..568f2c5 100644 --- a/cnet/service_test.go +++ b/cnet/service_test.go @@ -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 } diff --git a/internal/testutil/helpers.go b/internal/testutil/helpers.go index 46d56dc..3aaaee2 100644 --- a/internal/testutil/helpers.go +++ b/internal/testutil/helpers.go @@ -4,6 +4,8 @@ import ( "context" "fmt" "net" + "sync" + "time" "github.com/CPunch/gopenfusion/cnet" "github.com/CPunch/gopenfusion/cnet/protocol" @@ -93,3 +95,22 @@ func SetupEnvironment(ctx context.Context) (*db.DBHandler, *redis.RedisHandler, 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) +} diff --git a/internal/testutil/testutil_test.go b/internal/testutil/testutil_test.go new file mode 100644 index 0000000..58f5b33 --- /dev/null +++ b/internal/testutil/testutil_test.go @@ -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 +} diff --git a/login/login.go b/login/login.go index 6a89364..d96f277 100644 --- a/login/login.go +++ b/login/login.go @@ -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 { diff --git a/shard/join.go b/shard/join.go index 1a9a1a8..730731a 100644 --- a/shard/join.go +++ b/shard/join.go @@ -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 diff --git a/shard/movement.go b/shard/movement.go index dd00ff1..1102427 100644 --- a/shard/movement.go +++ b/shard/movement.go @@ -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(), }) } diff --git a/util/util.go b/util/util.go deleted file mode 100644 index 60bb296..0000000 --- a/util/util.go +++ /dev/null @@ -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) -} diff --git a/util/util_test.go b/util/util_test.go deleted file mode 100644 index ea0f021..0000000 --- a/util/util_test.go +++ /dev/null @@ -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 -}