Add a separate test for initializer retries (#20298)

Separate test for initializer retries
This commit is contained in:
Melissa Kam 2024-01-19 16:59:44 -06:00 committed by GitHub
parent 2e08a7e1c7
commit a9dd6f5c02
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 41 additions and 7 deletions

View File

@ -50,8 +50,7 @@ func TestController_API(t *testing.T) {
}) })
rec := newTestReconciler() rec := newTestReconciler()
expectedInitAttempts := 2 // testing retries init := newTestInitializer(1)
init := newTestInitializer(expectedInitAttempts)
client := svctest.NewResourceServiceBuilder(). client := svctest.NewResourceServiceBuilder().
WithRegisterFns(demo.RegisterTypes). WithRegisterFns(demo.RegisterTypes).
Run(t) Run(t)
@ -80,11 +79,8 @@ func TestController_API(t *testing.T) {
mgr.SetRaftLeader(true) mgr.SetRaftLeader(true)
go mgr.Run(testContext(t)) go mgr.Run(testContext(t))
t.Run("initialize", func(t *testing.T) { // Wait for initialization to complete
for i := 0; i < expectedInitAttempts; i++ {
init.wait(t) init.wait(t)
}
})
t.Run("managed resource type", func(t *testing.T) { t.Run("managed resource type", func(t *testing.T) {
res, err := demo.GenerateV2Artist() res, err := demo.GenerateV2Artist()
@ -225,6 +221,44 @@ func TestController_API(t *testing.T) {
}) })
} }
func TestController_API_InitializeRetry(t *testing.T) {
t.Parallel()
// Configure initializer to error initially in order to test retries
expectedInitAttempts := 2
init := newTestInitializer(expectedInitAttempts)
client := svctest.NewResourceServiceBuilder().
WithRegisterFns(demo.RegisterTypes).
Run(t)
rec := newTestReconciler()
ctrl := controller.
NewController("artist", pbdemov2.ArtistType).
WithBackoff(10*time.Millisecond, 100*time.Millisecond).
WithReconciler(rec).
WithInitializer(init)
mgr := controller.NewManager(client, testutil.Logger(t))
mgr.Register(ctrl)
mgr.SetRaftLeader(true)
go mgr.Run(testContext(t))
// Wait for initialization attempts to complete
for i := 0; i < expectedInitAttempts; i++ {
init.wait(t)
}
// Create a resource and expect it to reconcile now that initialization is complete
res, err := demo.GenerateV2Artist()
require.NoError(t, err)
_, err = client.Write(testContext(t), &pbresource.WriteRequest{Resource: res})
require.NoError(t, err)
rec.wait(t)
}
func waitForAtomicBoolValue(t testutil.TestingTB, actual *atomic.Bool, expected bool) { func waitForAtomicBoolValue(t testutil.TestingTB, actual *atomic.Bool, expected bool) {
t.Helper() t.Helper()
retry.Run(t, func(r *retry.R) { retry.Run(t, func(r *retry.R) {