1
0
mirror of https://github.com/redis/go-redis.git synced 2025-07-29 17:41:15 +03:00

fix(tests): simulate start/stop in mock credentials provider

This commit is contained in:
Nedyalko Dyakov
2025-05-13 23:26:29 +03:00
parent c5054e2461
commit 8b51596d19

View File

@ -842,11 +842,12 @@ var _ = Describe("Credentials Provider Priority", func() {
It("should handle credential updates from streaming provider", func() { It("should handle credential updates from streaming provider", func() {
initialCreds := auth.NewBasicCredentials("initial_user", "initial_pass") initialCreds := auth.NewBasicCredentials("initial_user", "initial_pass")
updatedCreds := auth.NewBasicCredentials("updated_user", "updated_pass") updatedCreds := auth.NewBasicCredentials("updated_user", "updated_pass")
updatesChan := make(chan auth.Credentials, 1)
opt = &redis.Options{ opt = &redis.Options{
StreamingCredentialsProvider: &mockStreamingProvider{ StreamingCredentialsProvider: &mockStreamingProvider{
credentials: initialCreds, credentials: initialCreds,
updates: make(chan auth.Credentials, 1), updates: updatesChan,
}, },
} }
@ -861,6 +862,7 @@ var _ = Describe("Credentials Provider Priority", func() {
// wrongpass // wrongpass
Expect(client.Ping(context.Background()).Err()).To(HaveOccurred()) Expect(client.Ping(context.Background()).Err()).To(HaveOccurred())
Expect(recorder.Contains("AUTH updated_user")).To(BeTrue()) Expect(recorder.Contains("AUTH updated_user")).To(BeTrue())
close(updatesChan)
}) })
}) })
@ -875,12 +877,10 @@ func (m *mockStreamingProvider) Subscribe(listener auth.CredentialsListener) (au
return nil, nil, m.err return nil, nil, m.err
} }
// Send initial credentials
listener.OnNext(m.credentials)
// Start goroutine to handle updates // Start goroutine to handle updates
go func() { go func() {
for creds := range m.updates { for creds := range m.updates {
m.credentials = creds
listener.OnNext(creds) listener.OnNext(creds)
} }
}() }()
@ -892,7 +892,6 @@ func (m *mockStreamingProvider) Subscribe(listener auth.CredentialsListener) (au
// allow multiple closes from multiple listeners // allow multiple closes from multiple listeners
} }
}() }()
close(m.updates)
return return
}, nil }, nil
} }