From 838f19b9ff900d3d9ded7b121c64ab182532a698 Mon Sep 17 00:00:00 2001 From: Shaunak Kashyap Date: Wed, 4 Oct 2023 17:48:23 -0700 Subject: [PATCH] Immediately notify observer when registered --- .../application/upgrade/details/details.go | 19 ++++++++++++------- .../upgrade/details/details_test.go | 4 +++- 2 files changed, 15 insertions(+), 8 deletions(-) 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)