63 lines
1.6 KiB
Go
63 lines
1.6 KiB
Go
package testkit
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
|
|
"galaxy/authsession/internal/domain/gatewayprojection"
|
|
"galaxy/authsession/internal/ports"
|
|
)
|
|
|
|
// RecordingProjectionPublisher is a deterministic
|
|
// GatewaySessionProjectionPublisher double that records every published
|
|
// snapshot.
|
|
type RecordingProjectionPublisher struct {
|
|
mu sync.Mutex
|
|
|
|
// Err is returned directly from PublishSession when set.
|
|
Err error
|
|
|
|
// Errors is an optional FIFO error script consumed before Err. Nil entries
|
|
// represent successful publish attempts.
|
|
Errors []error
|
|
|
|
published []gatewayprojection.Snapshot
|
|
}
|
|
|
|
// PublishSession records snapshot and returns the configured error, if any.
|
|
func (p *RecordingProjectionPublisher) PublishSession(ctx context.Context, snapshot gatewayprojection.Snapshot) error {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
if err := snapshot.Validate(); err != nil {
|
|
return err
|
|
}
|
|
|
|
p.mu.Lock()
|
|
defer p.mu.Unlock()
|
|
|
|
p.published = append(p.published, cloneProjectionSnapshot(snapshot))
|
|
if len(p.Errors) > 0 {
|
|
err := p.Errors[0]
|
|
p.Errors = append([]error(nil), p.Errors[1:]...)
|
|
return err
|
|
}
|
|
|
|
return p.Err
|
|
}
|
|
|
|
// PublishedSnapshots returns a stable snapshot of every published projection.
|
|
func (p *RecordingProjectionPublisher) PublishedSnapshots() []gatewayprojection.Snapshot {
|
|
p.mu.Lock()
|
|
defer p.mu.Unlock()
|
|
|
|
snapshots := make([]gatewayprojection.Snapshot, 0, len(p.published))
|
|
for _, snapshot := range p.published {
|
|
snapshots = append(snapshots, cloneProjectionSnapshot(snapshot))
|
|
}
|
|
|
|
return snapshots
|
|
}
|
|
|
|
var _ ports.GatewaySessionProjectionPublisher = (*RecordingProjectionPublisher)(nil)
|