v8.run/go/exp@v0.0.26-0.20230226010534-afcdbd3f782d/broadcast/evbus/evbus_test.go (about)

     1  package evbus_test
     2  
     3  import (
     4  	"sync/atomic"
     5  	"testing"
     6  
     7  	"v8.run/go/exp/broadcast/evbus"
     8  )
     9  
    10  func TestEvbus(t *testing.T) {
    11  	var ctr atomic.Uint64
    12  	for i := 0; i < 8; i++ {
    13  		ss := evbus.Subscribe("TestEvbus", func(struct{}) (Unsubscribe bool) {
    14  			ctr.Add(1)
    15  			return false
    16  		})
    17  		defer ss.Unsubscribe()
    18  	}
    19  	for i := 0; i < 100; i++ {
    20  		evbus.Publish("TestEvbus", struct{}{})
    21  	}
    22  
    23  	if ctr.Load() != 800 {
    24  		t.Errorf("ctr=%d, ctr != 800", ctr.Load())
    25  	}
    26  }
    27  
    28  func BenchmarkEvbus(b *testing.B) {
    29  	for i := 0; i < 8; i++ {
    30  		ss := evbus.Subscribe("BenchmarkEvbus", func(v *testing.B) (Unsubscribe bool) {
    31  			return false
    32  		})
    33  		defer ss.Unsubscribe()
    34  	}
    35  
    36  	b.RunParallel(
    37  		func(p *testing.PB) {
    38  			for p.Next() {
    39  				evbus.Publish("BenchmarkEvbus", b)
    40  			}
    41  		},
    42  	)
    43  }