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

     1  package broadcast
     2  
     3  import (
     4  	"sync/atomic"
     5  	"testing"
     6  )
     7  
     8  func TestTopicBroadcast(t *testing.T) {
     9  	tt := NewTopic[uint32]()
    10  	var ctr uint32
    11  	s := tt.Subscribe(func(v uint32) {
    12  		atomic.AddUint32(&ctr, v)
    13  	})
    14  	for i := 0; i < 100; i++ {
    15  		s.t.Broadcast(100)
    16  	}
    17  	s.Unsubscribe()
    18  	for i := 0; i < 100; i++ {
    19  		s.t.Broadcast(100)
    20  	}
    21  
    22  	if ctr != 10000 {
    23  		t.Fatalf("expected ctr == 10000, got %d", ctr)
    24  	}
    25  }
    26  
    27  func BenchmarkBroadcast_8(b *testing.B) {
    28  	tt := NewTopic[uint32]()
    29  	var ctr uint32
    30  	for i := 0; i < 8; i++ {
    31  		tt.Subscribe(func(v uint32) {
    32  			atomic.AddUint32(&ctr, v)
    33  		})
    34  	}
    35  
    36  	b.RunParallel(func(p *testing.PB) {
    37  		for p.Next() {
    38  			tt.Broadcast(1)
    39  		}
    40  	})
    41  }
    42  
    43  func BenchmarkBroadcast_256(b *testing.B) {
    44  	tt := NewTopic[uint32]()
    45  	var ctr uint32
    46  	for i := 0; i < 256; i++ {
    47  		tt.Subscribe(func(v uint32) {
    48  			atomic.AddUint32(&ctr, v)
    49  		})
    50  	}
    51  
    52  	b.RunParallel(func(p *testing.PB) {
    53  		for p.Next() {
    54  			tt.Broadcast(1)
    55  		}
    56  	})
    57  }
    58  
    59  func BenchmarkBroadcast_1024(b *testing.B) {
    60  	tt := NewTopic[uint32]()
    61  	var ctr uint32
    62  	for i := 0; i < 1024; i++ {
    63  		tt.Subscribe(func(v uint32) {
    64  			atomic.AddUint32(&ctr, v)
    65  		})
    66  	}
    67  
    68  	b.RunParallel(func(p *testing.PB) {
    69  		for p.Next() {
    70  			tt.Broadcast(1)
    71  		}
    72  	})
    73  }
    74  
    75  func BenchmarkBroadcast_2048(b *testing.B) {
    76  	tt := NewTopic[uint32]()
    77  	var ctr uint32
    78  	for i := 0; i < 2048; i++ {
    79  		tt.Subscribe(func(v uint32) {
    80  			atomic.AddUint32(&ctr, v)
    81  		})
    82  	}
    83  
    84  	b.RunParallel(func(p *testing.PB) {
    85  		for p.Next() {
    86  			tt.Broadcast(1)
    87  		}
    88  	})
    89  }