Skip to content

Commit

Permalink
Test simplifiedEventBus.bus for a data race
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Litvinov <[email protected]>
  • Loading branch information
Zensey committed Mar 25, 2024
1 parent e5d0518 commit de6316d
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions eventbus/event_bus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
package eventbus

import (
"sync"
"sync/atomic"
"testing"
"time"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -69,3 +72,34 @@ func TestUnsubscribeMethod(t *testing.T) {
assert.Equal(t, 1, h.val)
assert.Equal(t, 7, h2.val)
}

func Test_simplifiedEventBus_Publish_DataRace(t *testing.T) {
eventBus := New()

fn := func(data string) {}

active := new(atomic.Bool)
active.Store(true)

var wg sync.WaitGroup
wg.Add(2)

go func() {
defer wg.Done()

for i := 0; i < 100; i++ {
eventBus.SubscribeWithUID("topic", "1", fn)
eventBus.Publish("topic", "test data")
time.Sleep(time.Millisecond)
}
active.Store(false)
}()
go func() {
defer wg.Done()
for active.Load() == true {
eventBus.UnsubscribeWithUID("topic", "1", fn)
time.Sleep(time.Millisecond)
}
}()
wg.Wait()
}

0 comments on commit de6316d

Please sign in to comment.