Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,17 @@ endif
.PHONY: test-ci
test-ci:
ifdef cover
$(GO) test -run "[^FLAKY]$$" -coverprofile=cover.out ./...
$(GO) test -run "[^FLAKY]$$" -coverprofile=cover.out ./pkg/storer/internal/events
else
$(GO) test -run "[^FLAKY]$$" ./...
$(GO) test -run "[^FLAKY]$$" ./pkg/storer/internal/events
endif

.PHONY: test-ci-race
test-ci-race:
ifdef cover
$(GO) test -race -run "[^FLAKY]$$" -coverprofile=cover.out ./...
$(GO) test -race -run "[^FLAKY]$$" -coverprofile=cover.out ./pkg/storer/internal/events
else
$(GO) test -race -run "[^FLAKY]$$" ./...
$(GO) test -race -run "[^FLAKY]$$" ./pkg/storer/internal/events
endif

.PHONY: test-ci-flaky
Expand Down
89 changes: 45 additions & 44 deletions pkg/storer/internal/events/subscribe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,55 @@ package events_test

import (
"testing"
"testing/synctest"
"time"

"github.com/ethersphere/bee/v2/pkg/storer/internal/events"
)

func TestSubscriber(t *testing.T) {
t.Parallel()

s := events.NewSubscriber()

bin0_1, unsub0_1 := s.Subscribe("0")
bin0_2, unsub0_2 := s.Subscribe("0")
t.Cleanup(func() { unsub0_1(); unsub0_2() })
go s.Trigger("0")

gotSignals := make(chan struct{})

go func() {
defer close(gotSignals)
<-bin0_1
<-bin0_2
}()

select {
case <-gotSignals:
case <-time.After(time.Second):
t.Fatal("signals did not fire in time")
}

select {
case <-bin0_1:
t.Fatalf("trigger should not have fired again")
case <-bin0_2:
t.Fatalf("trigger should not have fired again")
default:
}

bin1, unsub1 := s.Subscribe("1")
go s.Trigger("1")
go s.Trigger("1")
<-bin1
<-bin1

unsub1()

select {
case <-bin1:
t.Fatalf("trigger should not have fired again")
default:
}
synctest.Test(t, func(t *testing.T) {
s := events.NewSubscriber()

bin0_1, unsub0_1 := s.Subscribe("0")
bin0_2, unsub0_2 := s.Subscribe("0")
t.Cleanup(func() { unsub0_1(); unsub0_2() })
go s.Trigger("0")

gotSignals := make(chan struct{})

go func() {
defer close(gotSignals)
<-bin0_1
<-bin0_2
}()

select {
case <-gotSignals:
case <-time.After(time.Second):
t.Fatal("signals did not fire in time")
}

select {
case <-bin0_1:
t.Fatalf("trigger should not have fired again")
case <-bin0_2:
t.Fatalf("trigger should not have fired again")
default:
}

bin1, unsub1 := s.Subscribe("1")
go s.Trigger("1")
go s.Trigger("1")
<-bin1
<-bin1

unsub1()

select {
case <-bin1:
t.Fatalf("trigger should not have fired again")
default:
}
})
}
Loading