You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

87 lines
1.9 KiB

package testutils
import (
"fmt"
lucifer3 "git.aiterp.net/lucifer3/server"
"log"
"math/rand"
"sync/atomic"
"testing"
)
type syncEvent struct {
id uint64
}
func (s syncEvent) EventDescription() string {
return fmt.Sprintf("syncEvent(%d)", s.id)
}
type TestEventLogger struct {
entries []any
syncN uint64
syncCh chan struct{}
}
func (l *TestEventLogger) Active() bool {
return true
}
func (l *TestEventLogger) Sync(bus *lucifer3.EventBus) {
l.syncCh = make(chan struct{})
v := rand.Int63()
atomic.StoreUint64(&l.syncN, uint64(v))
bus.RunEvent(syncEvent{id: uint64(v)})
<-l.syncCh
}
func (l *TestEventLogger) AssertEvent(t *testing.T, eventStr string) {
for i, entry := range l.entries {
if event, ok := entry.(lucifer3.Event); ok && eventStr == event.EventDescription() {
l.entries = l.entries[i+1:]
return
}
}
t.Errorf("Event not found: %s", eventStr)
}
func (l *TestEventLogger) AssertCommand(t *testing.T, commandStr string) {
for i, entry := range l.entries {
if cmd, ok := entry.(lucifer3.Command); ok {
log.Println(cmd.CommandDescription() == commandStr)
}
if command, ok := entry.(lucifer3.Command); ok && commandStr == command.CommandDescription() {
l.entries = l.entries[i+1:]
return
}
}
t.Errorf("Command not found: %s", commandStr)
}
func (l *TestEventLogger) HandleEvent(_ *lucifer3.EventBus, event lucifer3.Event) {
if ev, ok := event.(syncEvent); ok {
if atomic.CompareAndSwapUint64(&l.syncN, ev.id, 0) {
close(l.syncCh)
}
}
l.entries = append(l.entries, event)
}
func (l *TestEventLogger) HandleCommand(_ *lucifer3.EventBus, command lucifer3.Command) {
l.entries = append(l.entries, command)
}
func evStr(ev any) string {
if ev, ok := ev.(lucifer3.Event); ok {
return ev.EventDescription()
} else if ev, ok := ev.(lucifer3.Command); ok {
return ev.CommandDescription()
}
panic("Unknown type")
}