41 lines
1.2 KiB
Go
41 lines
1.2 KiB
Go
package worker
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"proxy-pool/internal/controlplane/snapshotwire"
|
|
)
|
|
|
|
func TestInitialSnapshotSourceIssuesNextFullSnapshot(t *testing.T) {
|
|
now := time.Date(2026, 7, 31, 12, 0, 0, 0, time.UTC)
|
|
source, err := NewInitialSnapshotSource(epochReaderStub{epoch: 9}, time.Minute, func() time.Time { return now })
|
|
if err != nil {
|
|
t.Fatalf("NewInitialSnapshotSource(): %v", err)
|
|
}
|
|
updates, err := source.Watch(context.Background(), SnapshotWatchRequest{
|
|
WorkerID: "worker-a", SessionID: "session-a", LastAppliedVersion: 4,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("Watch(): %v", err)
|
|
}
|
|
full := <-updates
|
|
if full.GetVersion() != 5 || full.GetOwnershipEpoch() != 9 || !full.GetValidUntil().AsTime().Equal(now.Add(time.Minute)) {
|
|
t.Fatalf("snapshot = %+v", full)
|
|
}
|
|
checksum, err := snapshotwire.Checksum(full)
|
|
if err != nil || string(checksum[:]) != string(full.GetChecksum()) {
|
|
t.Fatalf("snapshot checksum = %x, %v; want %x", full.GetChecksum(), err, checksum)
|
|
}
|
|
}
|
|
|
|
type epochReaderStub struct {
|
|
epoch uint64
|
|
err error
|
|
}
|
|
|
|
func (reader epochReaderStub) CurrentOwnershipEpoch(context.Context) (uint64, error) {
|
|
return reader.epoch, reader.err
|
|
}
|