github.com/ethersphere/bee/v2@v2.2.0/pkg/storer/internal/events/subscribe_test.go (about) 1 // Copyright 2023 The Swarm Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package events_test 6 7 import ( 8 "testing" 9 "time" 10 11 "github.com/ethersphere/bee/v2/pkg/storer/internal/events" 12 ) 13 14 func TestSubscriber(t *testing.T) { 15 t.Parallel() 16 17 s := events.NewSubscriber() 18 19 bin0_1, unsub0_1 := s.Subscribe("0") 20 bin0_2, unsub0_2 := s.Subscribe("0") 21 t.Cleanup(func() { unsub0_1(); unsub0_2() }) 22 go s.Trigger("0") 23 24 gotSignals := make(chan struct{}) 25 26 go func() { 27 defer close(gotSignals) 28 <-bin0_1 29 <-bin0_2 30 }() 31 32 select { 33 case <-gotSignals: 34 case <-time.After(time.Second): 35 t.Fatal("signals did not fire in time") 36 } 37 38 select { 39 case <-bin0_1: 40 t.Fatalf("trigger should not have fired again") 41 case <-bin0_2: 42 t.Fatalf("trigger should not have fired again") 43 default: 44 } 45 46 bin1, unsub1 := s.Subscribe("1") 47 go s.Trigger("1") 48 go s.Trigger("1") 49 <-bin1 50 <-bin1 51 52 unsub1() 53 54 select { 55 case <-bin1: 56 t.Fatalf("trigger should not have fired again") 57 default: 58 } 59 }