62 lines
1.6 KiB
Go
62 lines
1.6 KiB
Go
// Package streamlagprobestub provides an in-memory ports.StreamLagProbe
|
|
// implementation for tests that do not need a Redis instance. Production
|
|
// code never wires this stub.
|
|
package streamlagprobestub
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"time"
|
|
|
|
"galaxy/lobby/internal/ports"
|
|
)
|
|
|
|
// Probe is a concurrency-safe in-memory ports.StreamLagProbe. The zero
|
|
// value reports `(0, false, nil)` for every stream until Set is called.
|
|
type Probe struct {
|
|
mu sync.Mutex
|
|
results map[string]Result
|
|
fallback Result
|
|
}
|
|
|
|
// Result stores the value the probe reports for a stream.
|
|
type Result struct {
|
|
Age time.Duration
|
|
Found bool
|
|
Err error
|
|
}
|
|
|
|
// NewProbe constructs one Probe with no preconfigured results.
|
|
func NewProbe() *Probe {
|
|
return &Probe{results: make(map[string]Result)}
|
|
}
|
|
|
|
// Set installs the result the probe will return for stream.
|
|
func (probe *Probe) Set(stream string, result Result) {
|
|
probe.mu.Lock()
|
|
defer probe.mu.Unlock()
|
|
probe.results[stream] = result
|
|
}
|
|
|
|
// SetFallback installs the result returned when no per-stream result is
|
|
// configured.
|
|
func (probe *Probe) SetFallback(result Result) {
|
|
probe.mu.Lock()
|
|
defer probe.mu.Unlock()
|
|
probe.fallback = result
|
|
}
|
|
|
|
// OldestUnprocessedAge satisfies ports.StreamLagProbe.
|
|
func (probe *Probe) OldestUnprocessedAge(_ context.Context, stream, _ string) (time.Duration, bool, error) {
|
|
probe.mu.Lock()
|
|
defer probe.mu.Unlock()
|
|
|
|
if result, ok := probe.results[stream]; ok {
|
|
return result.Age, result.Found, result.Err
|
|
}
|
|
return probe.fallback.Age, probe.fallback.Found, probe.fallback.Err
|
|
}
|
|
|
|
// Compile-time interface assertion.
|
|
var _ ports.StreamLagProbe = (*Probe)(nil)
|