removed util

- WaitWithTimeout && SelectWithTimeout have been moved to internal/testutil
- GetTime has been moved to cnet/protocol
This commit is contained in:
CPunch 2024-02-01 18:25:49 -06:00
parent 1a6de671e5
commit 0a28dbcc3e
9 changed files with 75 additions and 77 deletions

9
cnet/protocol/time.go Normal file
View File

@ -0,0 +1,9 @@
package protocol
import (
"time"
)
func GetTime() uint64 {
return uint64(time.Now().UnixMilli())
}

View File

@ -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"
) )
@ -44,7 +44,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
@ -68,7 +68,7 @@ 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 +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
} }

View File

@ -4,6 +4,8 @@ import (
"context" "context"
"fmt" "fmt"
"net" "net"
"sync"
"time"
"github.com/CPunch/gopenfusion/cnet" "github.com/CPunch/gopenfusion/cnet"
"github.com/CPunch/gopenfusion/cnet/protocol" "github.com/CPunch/gopenfusion/cnet/protocol"
@ -93,3 +95,22 @@ func SetupEnvironment(ctx context.Context) (*db.DBHandler, *redis.RedisHandler,
r.Close() 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)
}

View 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
}

View File

@ -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 {

View File

@ -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

View File

@ -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(),
}) })
} }

View File

@ -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)
}

View File

@ -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
}