Skip to content

Commit

Permalink
Only wait for headless authentication watcher initialization in tests. (
Browse files Browse the repository at this point in the history
  • Loading branch information
Joerger committed Jun 4, 2023
1 parent 11bd535 commit fed578a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
3 changes: 3 additions & 0 deletions lib/auth/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,9 @@ func NewTestAuthServer(cfg TestAuthServerConfig) (*TestAuthServer, error) {
if err != nil {
return nil, trace.Wrap(err)
}
if err := headlessAuthenticationWatcher.WaitInit(ctx); err != nil {
return nil, trace.Wrap(err)
}
srv.AuthServer.SetHeadlessAuthenticationWatcher(headlessAuthenticationWatcher)

srv.Authorizer, err = authz.NewAuthorizer(authz.AuthorizerOpts{
Expand Down
10 changes: 6 additions & 4 deletions lib/services/local/headlessauthn_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,16 @@ func NewHeadlessAuthenticationWatcher(ctx context.Context, cfg HeadlessAuthentic

go h.runWatchLoop(ctx)

// Wait for the watch loop to initialize before returning.
return h, nil
}

// WaitInit waits for the watch loop to initialize.
func (h *HeadlessAuthenticationWatcher) WaitInit(ctx context.Context) error {
select {
case <-h.running:
case <-ctx.Done():
return nil, trace.Wrap(ctx.Err())
}

return h, nil
return trace.Wrap(ctx.Err())
}

// Done returns a channel that's closed when the watcher is closed.
Expand Down
4 changes: 4 additions & 0 deletions lib/services/local/headlessauthn_watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func newHeadlessAuthenticationWatcherTestEnv(t *testing.T, clock clockwork.Clock
})
require.NoError(t, err)

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
require.NoError(t, w.WaitInit(ctx))

return &headlessAuthenticationWatcherTestEnv{
watcher: w,
watcherCancel: watcherCancel,
Expand Down

0 comments on commit fed578a

Please sign in to comment.