Add logging tests
This commit is contained in:
parent
38a897fd3d
commit
cddb26bca2
5 changed files with 81 additions and 13 deletions
|
@ -9,8 +9,10 @@ import (
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.haedhutner.dev/mvv/LastMUD/internal/server"
|
"net/http"
|
||||||
|
_ "net/http/pprof"
|
||||||
|
|
||||||
|
"code.haedhutner.dev/mvv/LastMUD/internal/server"
|
||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -27,6 +29,10 @@ func main() {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
log.Println(http.ListenAndServe("localhost:6060", nil))
|
||||||
|
}()
|
||||||
|
|
||||||
processInput()
|
processInput()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,21 +15,12 @@ import (
|
||||||
|
|
||||||
const TickRate = time.Duration(50 * time.Millisecond)
|
const TickRate = time.Duration(50 * time.Millisecond)
|
||||||
|
|
||||||
const MaxEnqueuedOutputPerTick = 100
|
|
||||||
|
|
||||||
type GameOutput struct {
|
type GameOutput struct {
|
||||||
connId uuid.UUID
|
connId uuid.UUID
|
||||||
contents []byte
|
contents []byte
|
||||||
closeConnection bool
|
closeConnection bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (game *LastMUDGame) CreateOutput(connId uuid.UUID, contents []byte) GameOutput {
|
|
||||||
return GameOutput{
|
|
||||||
connId: connId,
|
|
||||||
contents: contents,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g GameOutput) Id() uuid.UUID {
|
func (g GameOutput) Id() uuid.UUID {
|
||||||
return g.connId
|
return g.connId
|
||||||
}
|
}
|
||||||
|
@ -55,7 +46,7 @@ func CreateGame(ctx context.Context, wg *sync.WaitGroup) (game *LastMUDGame) {
|
||||||
game = &LastMUDGame{
|
game = &LastMUDGame{
|
||||||
wg: wg,
|
wg: wg,
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
output: make(chan GameOutput, MaxEnqueuedOutputPerTick),
|
output: make(chan GameOutput),
|
||||||
world: data.CreateGameWorld(),
|
world: data.CreateGameWorld(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,6 +58,7 @@ func CreateGame(ctx context.Context, wg *sync.WaitGroup) (game *LastMUDGame) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Will block if no output present
|
||||||
func (game *LastMUDGame) ConsumeNextOutput() *GameOutput {
|
func (game *LastMUDGame) ConsumeNextOutput() *GameOutput {
|
||||||
select {
|
select {
|
||||||
case output := <-game.output:
|
case output := <-game.output:
|
||||||
|
|
|
@ -36,6 +36,10 @@ func handleSayCommand(world *ecs.World, delta time.Duration, player ecs.Entity,
|
||||||
return createCommandError("Can't interpret message as string")
|
return createCommandError("Can't interpret message as string")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if message == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
for p := range allPlayersInRoom {
|
for p := range allPlayersInRoom {
|
||||||
connId, _ := ecs.GetComponent[data.ConnectionIdComponent](world, p)
|
connId, _ := ecs.GetComponent[data.ConnectionIdComponent](world, p)
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ type SystemPriorityOffset = int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
EventOffset = 0
|
EventOffset = 0
|
||||||
CommandOffset = 1000
|
CommandOffset = 10000
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateSystems() []*ecs.System {
|
func CreateSystems() []*ecs.System {
|
||||||
|
@ -17,7 +17,6 @@ func CreateSystems() []*ecs.System {
|
||||||
ecs.CreateSystem("PlayerConnectEventHandler", EventOffset+0, CreateEventHandler(data.EventPlayerConnect, handlePlayerConnectEvent)),
|
ecs.CreateSystem("PlayerConnectEventHandler", EventOffset+0, CreateEventHandler(data.EventPlayerConnect, handlePlayerConnectEvent)),
|
||||||
ecs.CreateSystem("PlayerDisconnectEventHandler", EventOffset+1, CreateEventHandler(data.EventPlayerDisconnect, handlePlayerDisconnectEvent)),
|
ecs.CreateSystem("PlayerDisconnectEventHandler", EventOffset+1, CreateEventHandler(data.EventPlayerDisconnect, handlePlayerDisconnectEvent)),
|
||||||
ecs.CreateSystem("PlayerCommandEventHandler", EventOffset+2, CreateEventHandler(data.EventPlayerCommand, handlePlayerCommandEvent)),
|
ecs.CreateSystem("PlayerCommandEventHandler", EventOffset+2, CreateEventHandler(data.EventPlayerCommand, handlePlayerCommandEvent)),
|
||||||
// ecs.CreateSystem("PlayerSayEventHandler", EventOffset+3, CreateEventHandler(data.EventPlayerSpeak, handlePlayerSayEvent)),
|
|
||||||
ecs.CreateSystem("ParseCommandEventHandler", EventOffset+4, CreateEventHandler(data.EventParseCommand, parseCommand)),
|
ecs.CreateSystem("ParseCommandEventHandler", EventOffset+4, CreateEventHandler(data.EventParseCommand, parseCommand)),
|
||||||
ecs.CreateSystem("SayCommandHandler", CommandOffset+0, CreateCommandHandler(data.CommandSay, handleSayCommand)),
|
ecs.CreateSystem("SayCommandHandler", CommandOffset+0, CreateCommandHandler(data.CommandSay, handleSayCommand)),
|
||||||
ecs.CreateSystem("QuitCommandHandler", CommandOffset+1, CreateCommandHandler(data.CommandQuit, handleQuitCommand)),
|
ecs.CreateSystem("QuitCommandHandler", CommandOffset+1, CreateCommandHandler(data.CommandQuit, handleQuitCommand)),
|
||||||
|
|
67
internal/logging/logging_test.go
Normal file
67
internal/logging/logging_test.go
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
package logging
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLogLevelString(t *testing.T) {
|
||||||
|
assert.Equal(t, "DBG", DBG.String())
|
||||||
|
assert.Equal(t, "INF", INF.String())
|
||||||
|
assert.Equal(t, "WRN", WRN.String())
|
||||||
|
assert.Equal(t, "ERR", ERR.String())
|
||||||
|
assert.Equal(t, "99", LogLevel(99).String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateLogger_NoFile(t *testing.T) {
|
||||||
|
logger := CreateLogger(DBG, INF, "", "2006-01-02 15:04:05.000", "2006-01-02_15-04-05")
|
||||||
|
assert.NotNil(t, logger)
|
||||||
|
assert.Nil(t, logger.file)
|
||||||
|
assert.Equal(t, DBG, logger.maxFileLevel)
|
||||||
|
assert.Equal(t, INF, logger.maxDisplayedLevel)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateLogger_WithFile(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
path := filepath.Join(dir, "test.log")
|
||||||
|
logger := CreateLogger(DBG, DBG, path, "2006-01-02 15:04:05.000", "2006-01-02_15-04-05")
|
||||||
|
assert.NotNil(t, logger)
|
||||||
|
assert.NotNil(t, logger.file)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoggerMethods(t *testing.T) {
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
logger := &Logger{
|
||||||
|
file: nil,
|
||||||
|
timestampFormat: DefaultTimeFormat,
|
||||||
|
maxFileLevel: DBG,
|
||||||
|
maxDisplayedLevel: DBG,
|
||||||
|
}
|
||||||
|
oldStdout := os.Stdout
|
||||||
|
r, w, _ := os.Pipe()
|
||||||
|
os.Stdout = w
|
||||||
|
logger.Debug("debug message")
|
||||||
|
logger.Info("info message")
|
||||||
|
logger.Warn("warn message")
|
||||||
|
logger.Error("error message")
|
||||||
|
w.Close()
|
||||||
|
os.Stdout = oldStdout
|
||||||
|
buf.ReadFrom(r)
|
||||||
|
output := buf.String()
|
||||||
|
assert.Contains(t, output, "debug message")
|
||||||
|
assert.Contains(t, output, "info message")
|
||||||
|
assert.Contains(t, output, "warn message")
|
||||||
|
assert.Contains(t, output, "error message")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAnsiStripRegex(t *testing.T) {
|
||||||
|
input := "\033[31mHello\033[0m"
|
||||||
|
re := regexp.MustCompile(`\x1b\[[0-9;]*m`)
|
||||||
|
output := re.ReplaceAllString(input, "")
|
||||||
|
assert.Equal(t, "Hello", output)
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue