diff options
Diffstat (limited to 'internal/peer_test.go')
| -rw-r--r-- | internal/peer_test.go | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/internal/peer_test.go b/internal/peer_test.go new file mode 100644 index 0000000..2cada60 --- /dev/null +++ b/internal/peer_test.go @@ -0,0 +1,83 @@ +package internal + +import ( + "context" + "errors" + "testing" + "time" +) + +func TestPeerActiveAtStale(t *testing.T) { + now := time.Date(2023, 1, 1, 12, 0, 0, 0, time.UTC) + conf := config{ + PeerURL: "https://peer.example/gogios/index.json", + PeerStaleThresholdS: 600, + PeerPrimaryName: "primary", + PeerSecondaryName: "secondary", + } + + lastUpdated := now.Add(-11 * time.Minute) + active, _ := peerActiveAt(context.Background(), conf, now, "secondary", + func(context.Context, string) (time.Time, error) { + return lastUpdated, nil + }) + + if !active { + t.Fatalf("expected active when peer is stale") + } +} + +func TestPeerActiveAtFreshStandby(t *testing.T) { + now := time.Date(2023, 1, 1, 12, 0, 0, 0, time.UTC) + conf := config{ + PeerURL: "https://peer.example/gogios/index.json", + PeerStaleThresholdS: 600, + PeerPrimaryName: "primary", + PeerSecondaryName: "secondary", + } + + lastUpdated := now.Add(-1 * time.Minute) + active, _ := peerActiveAt(context.Background(), conf, now, "secondary", + func(context.Context, string) (time.Time, error) { + return lastUpdated, nil + }) + + if active { + t.Fatalf("expected passive when peer is healthy and local is standby") + } +} + +func TestPeerActiveAtFetchError(t *testing.T) { + now := time.Date(2023, 1, 1, 12, 0, 0, 0, time.UTC) + conf := config{ + PeerURL: "https://peer.example/gogios/index.json", + PeerStaleThresholdS: 600, + PeerPrimaryName: "primary", + PeerSecondaryName: "secondary", + } + + active, _ := peerActiveAt(context.Background(), conf, now, "secondary", + func(context.Context, string) (time.Time, error) { + return time.Time{}, errors.New("boom") + }) + + if !active { + t.Fatalf("expected active on peer fetch error") + } +} + +func TestScheduledMasterWeekParity(t *testing.T) { + primary := "primary" + secondary := "secondary" + + weekOne := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC) // Sunday + weekTwo := time.Date(2023, 1, 8, 0, 0, 0, 0, time.UTC) // Next Sunday + + if master := scheduledMaster(primary, secondary, weekOne); master != primary { + t.Fatalf("expected primary to be master in week 1, got %s", master) + } + + if master := scheduledMaster(primary, secondary, weekTwo); master != secondary { + t.Fatalf("expected secondary to be master in week 2, got %s", master) + } +} |
