diff --git a/internal/pkg/agent/application/upgrade/details/details.go b/internal/pkg/agent/application/upgrade/details/details.go index e31f570b44f..7a09763b786 100644 --- a/internal/pkg/agent/application/upgrade/details/details.go +++ b/internal/pkg/agent/application/upgrade/details/details.go @@ -62,13 +62,14 @@ func (d *Details) Fail(err error) { } // RegisterObserver allows an interested consumer of Details to register -// themselves as an Observer. When NotifyObservers is called, all registered -// observers are sent the current upgrade details. +// themselves as an Observer. The registered observer is immediately notified +// of the current upgrade details. func (d *Details) RegisterObserver(observer Observer) { d.mu.Lock() defer d.mu.Unlock() d.observers = append(d.observers, observer) + d.notifyObserver(observer) } // NotifyObservers sends the current upgrade details to all registered @@ -81,10 +82,14 @@ func (d *Details) NotifyObservers() { func (d *Details) notifyObservers() { for _, observer := range d.observers { - if d.State == StateCompleted { - observer(nil) - } else { - observer(d) - } + d.notifyObserver(observer) + } +} + +func (d *Details) notifyObserver(observer Observer) { + if d.State == StateCompleted { + observer(nil) + } else { + observer(d) } } diff --git a/internal/pkg/agent/application/upgrade/details/details_test.go b/internal/pkg/agent/application/upgrade/details/details_test.go index 4c45e062070..92fd0a47eaf 100644 --- a/internal/pkg/agent/application/upgrade/details/details_test.go +++ b/internal/pkg/agent/application/upgrade/details/details_test.go @@ -44,9 +44,11 @@ func TestDetailsObserver(t *testing.T) { var observedDetails *Details obs := func(updatedDetails *Details) { observedDetails = updatedDetails } + det.RegisterObserver(obs) - require.Nil(t, observedDetails) require.Len(t, det.observers, 1) + require.NotNil(t, observedDetails) + require.Equal(t, StateRequested, observedDetails.State) det.SetState(StateDownloading) require.Equal(t, StateDownloading, det.State)