generated from VLADIMIR/template
up
This commit is contained in:
@@ -377,7 +377,7 @@ func verifyExportedGameReads(t *testing.T, f serviceFixture, worker *game_export
|
||||
games := game_service.NewGameService(f.games, f.teams, f.scenarios, nil, "", actions_repo.NewActionsRepo(f.pool), applications_repo.NewApplicationsRepo(f.pool))
|
||||
mapRepo := game_map_repo.NewGameMapRepo(f.pool)
|
||||
server := app.NewServer(nil, nil, nil, nil, games, nil, nil, nil, nil, nil, nil,
|
||||
screen_service.NewScreenService(screen_sessions_repo.NewScreenSessionsRepo(f.pool)), nil, nil, map_service.NewMapService(mapRepo), nil, worker, nil, nil, nil, nil)
|
||||
screen_service.NewScreenService(screen_sessions_repo.NewScreenSessionsRepo(f.pool)), nil, nil, map_service.NewMapService(mapRepo), nil, worker, nil, nil, nil, nil, nil)
|
||||
ctx := processor_jwt.ContextWithClaims(context.Background(), &processor_jwt.JWTClaims{UserID: f.userID, Roles: []string{"organizer"}})
|
||||
game, err := server.GetGame(ctx, &proto.GetGameReq{ID: int32(f.gameID)})
|
||||
check(t, err)
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
// Package wifi_service reads the collector's latest snapshot without LAN access.
|
||||
package wifi_service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"evening_detective_server/internal/modules/cudy"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
path string
|
||||
now func() time.Time
|
||||
}
|
||||
|
||||
func New(path string) *Service { return &Service{path: path, now: time.Now} }
|
||||
|
||||
func (s *Service) Get(ctx context.Context) (cudy.Snapshot, bool) {
|
||||
if s == nil || s.path == "" {
|
||||
return cudy.Snapshot{}, false
|
||||
}
|
||||
snapshot := cudy.Snapshot{Enabled: true, HasError: true}
|
||||
if ctx.Err() != nil {
|
||||
return snapshot, true
|
||||
}
|
||||
file, err := os.Open(s.path)
|
||||
if err != nil {
|
||||
return snapshot, true
|
||||
}
|
||||
defer file.Close()
|
||||
data, err := io.ReadAll(io.LimitReader(file, 2<<20+1))
|
||||
if err != nil || len(data) > 2<<20 || json.Unmarshal(data, &snapshot) != nil {
|
||||
return cudy.Snapshot{Enabled: true, HasError: true}, true
|
||||
}
|
||||
if !snapshot.Enabled {
|
||||
return snapshot, false
|
||||
}
|
||||
stale := snapshot.HasError || snapshot.CollectedAt.IsZero() || s.now().Sub(snapshot.CollectedAt) >= 30*time.Second || snapshot.CollectedAt.After(s.now())
|
||||
return snapshot, stale
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package wifi_service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"evening_detective_server/internal/modules/cudy"
|
||||
)
|
||||
|
||||
func TestSnapshotFreshnessAndFailures(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "state.json")
|
||||
now := time.Unix(1234, 0)
|
||||
service := New(path)
|
||||
service.now = func() time.Time { return now }
|
||||
snapshot := cudy.Snapshot{Enabled: true, CollectedAt: now, Clients: []cudy.Client{{MAC: "AA:BB:CC:DD:EE:01"}}}
|
||||
write := func() {
|
||||
t.Helper()
|
||||
data, _ := json.Marshal(snapshot)
|
||||
if err := os.WriteFile(path, data, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
write()
|
||||
result, stale := service.Get(context.Background())
|
||||
if stale || len(result.Clients) != 1 {
|
||||
t.Fatal("fresh snapshot rejected")
|
||||
}
|
||||
now = now.Add(30 * time.Second)
|
||||
if _, stale = service.Get(context.Background()); !stale {
|
||||
t.Fatal("stale snapshot accepted")
|
||||
}
|
||||
snapshot.HasError = true
|
||||
write()
|
||||
result, stale = service.Get(context.Background())
|
||||
if !stale || !result.HasError || len(result.Clients) != 1 {
|
||||
t.Fatal("failed refresh lost snapshot")
|
||||
}
|
||||
if err := os.WriteFile(path, []byte("bad JSON"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, stale = service.Get(context.Background())
|
||||
if !stale || !result.HasError {
|
||||
t.Fatal("corrupt snapshot accepted")
|
||||
}
|
||||
snapshot = cudy.Snapshot{}
|
||||
write()
|
||||
result, stale = service.Get(context.Background())
|
||||
if result.Enabled || stale {
|
||||
t.Fatal("disabled source marked stale")
|
||||
}
|
||||
if result, stale = New("").Get(context.Background()); result.Enabled || stale {
|
||||
t.Fatal("unconfigured source enabled")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user