Skip to content

Commit

Permalink
Remove check for previous event
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerHelmuth committed Dec 9, 2024
1 parent d1902c6 commit e75b063
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 38 deletions.
15 changes: 1 addition & 14 deletions internal/sharedcomponent/sharedcomponent.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,26 +116,13 @@ func (h *hostWrapper) GetExtensions() map[component.ID]component.Component {
return h.host.GetExtensions()
}

func (h *hostWrapper) hasEvent(e *componentstatus.Event) bool {
hasEvent := false
h.previousEvents.Do(func(a any) {
if previousEvent, ok := a.(*componentstatus.Event); ok && previousEvent == e {
hasEvent = true
}
})
return hasEvent
}

func (h *hostWrapper) Report(e *componentstatus.Event) {
// Only remember an event if it will be emitted and it has not been sent already.
h.lock.Lock()
if len(h.sources) > 0 && !h.hasEvent(e) {
if len(h.sources) > 0 {
h.previousEvents.Value = e
h.previousEvents = h.previousEvents.Next()
}
h.lock.Unlock()

h.lock.Lock()
for _, s := range h.sources {
s.Report(e)
}
Expand Down
24 changes: 0 additions & 24 deletions internal/sharedcomponent/sharedcomponent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,27 +210,3 @@ type testHost struct {
func (h *testHost) Report(e *componentstatus.Event) {
h.newStatusFunc(h.InstanceID, e)
}

func TestReportWithExistingEvent(t *testing.T) {
reportedStatuses := make([]componentstatus.Status, 0)
newStatusFunc := func(id *componentstatus.InstanceID, ev *componentstatus.Event) {
reportedStatuses = append(reportedStatuses, ev.Status())
}
base := &baseComponent{}
base.StartFunc = func(_ context.Context, host component.Host) error {
e := componentstatus.NewEvent(componentstatus.StatusOK)
componentstatus.ReportStatus(host, e)
componentstatus.ReportStatus(host, e)
return nil
}
comps := NewMap[component.ID, *baseComponent]()
comp, err := comps.LoadOrStore(
id,
func() (*baseComponent, error) { return base, nil },
)
require.NoError(t, err)
baseHost := componenttest.NewNopHost()
err = comp.Start(context.Background(), &testHost{Host: baseHost, InstanceID: &componentstatus.InstanceID{}, newStatusFunc: newStatusFunc})
require.NoError(t, err)
require.Equal(t, 3, len(reportedStatuses))
}

0 comments on commit e75b063

Please sign in to comment.