package rollout import ( "context" "testing" "time" "github.com/carlosframework/platform/internal/appconfig" ) func TestConfigBackoffRunsFromOneMinuteToThirty(t *testing.T) { want := []time.Duration{time.Minute, 2 * time.Minute, 4 * time.Minute, 8 * time.Minute, 16 * time.Minute, 30 * time.Minute, 30 * time.Minute} for i, w := range want { if got := configBackoff(i + 1); got != w { t.Errorf("configBackoff(%d) = %v, want %v", i+1, got, w) } } } func TestAnAckWaitsForItsRevisionToBeActive(t *testing.T) { var wrote []string write := func(_ context.Context, a appconfig.ConfigAck) { wrote = append(wrote, a.Host) } tr := &configTrack{delivered: "c2", active: "c1"} if now := tr.deferAck(appconfig.ConfigAck{Host: "a"}, write); now != nil { t.Fatal("acknowledged a delivered revision that is not active") } if run, _ := tr.activate("c1"); run != nil { t.Fatal("an older revision becoming active released the newer ack") } run, ok := tr.activate("c2") if !ok { t.Fatal("the ack for c2 did not come back when c2 became active") } run(context.Background()) if len(wrote) != 1 { t.Fatalf("wrote %v", wrote) } // With nothing delivered through the scheduler (a shape it does not // restart), the ack is written straight away, as today. free := &configTrack{} if now := free.deferAck(appconfig.ConfigAck{Host: "b"}, write); now == nil { t.Fatal("an untracked route's ack must not wait") } } // Spec §8.1: a sleeping instance needs no attempt, because its next wake // starts with the delivered file. Its ack goes out at delivery, as today, // marked Hibernating like unit config sync's ack for a sleeping route. func TestASleepersAckGoesOutAtDeliveryAsHibernating(t *testing.T) { var got []appconfig.ConfigAck tr := &configTrack{delivered: "c2", active: "c2", sleeping: true} run := tr.deferAck(appconfig.ConfigAck{Host: "a", Restarted: true}, func(_ context.Context, a appconfig.ConfigAck) { got = append(got, a) }) if run == nil { t.Fatal("a sleeper's ack waited for a wake") } run(context.Background()) if len(got) != 1 || !got[0].Hibernating || got[0].Restarted { t.Fatalf("ack = %+v, want Hibernating and not Restarted", got) } } // Spec §8.1: a revision is acknowledged only once active. A held ack that a // newer delivery bypassed must never be released afterwards, or it would be // written after the newer ack with a revision that is no longer delivered. func TestANewerAckSupersedesTheHeldOne(t *testing.T) { t.Run("revert to the active revision", func(t *testing.T) { var wrote []string write := func(_ context.Context, a appconfig.ConfigAck) { wrote = append(wrote, a.Host) } tr := &configTrack{delivered: "c2", active: "c1"} if now := tr.deferAck(appconfig.ConfigAck{Host: "held-c2"}, write); now != nil { t.Fatal("c2's ack was not held while c1 is active") } // The config reverts to c1, which is the active revision. tr.delivered = "c1" now := tr.deferAck(appconfig.ConfigAck{Host: "c1"}, write) if now == nil { t.Fatal("c1's ack waited though c1 is active") } now(context.Background()) // The in-flight generation finishes on c2. if run, ok := tr.activate("c2"); run != nil || ok { t.Fatal("the stale c2 ack was released after a newer ack went out") } if len(wrote) != 1 || wrote[0] != "c1" { t.Fatalf("wrote %v, want only the c1 ack", wrote) } }) t.Run("sleep then hibernating ack", func(t *testing.T) { var wrote []string write := func(_ context.Context, a appconfig.ConfigAck) { wrote = append(wrote, a.Host) } tr := &configTrack{delivered: "c2", active: "c1"} if now := tr.deferAck(appconfig.ConfigAck{Host: "held-c2"}, write); now != nil { t.Fatal("c2's ack was not held while c1 is active") } // The instance sleeps and c3 is delivered to it. tr.delivered, tr.sleeping = "c3", true now := tr.deferAck(appconfig.ConfigAck{Host: "c3"}, write) if now == nil { t.Fatal("a sleeper's ack waited") } now(context.Background()) if run, ok := tr.activate("c2"); run != nil || ok { t.Fatal("the stale c2 ack was released after the Hibernating ack went out") } if len(wrote) != 1 || wrote[0] != "c3" { t.Fatalf("wrote %v, want only the c3 ack", wrote) } }) } // A held ack is released only for the revision that is delivered now, even // when delivered moved on without another ack arriving. func TestAHeldAckIsNotReleasedForARevisionNoLongerDelivered(t *testing.T) { tr := &configTrack{delivered: "c2", active: "c1"} tr.deferAck(appconfig.ConfigAck{Host: "a"}, func(context.Context, appconfig.ConfigAck) { t.Fatal("wrote a stale ack") }) tr.delivered = "c3" if run, ok := tr.activate("c2"); run != nil || ok { t.Fatal("released an ack for c2 while c3 is delivered") } }