github.com/loov/combiner@v0.1.0/testsuite/tests.go (about)

     1  package testsuite
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  )
     7  
     8  func RunTests(t *testing.T, setup *Setup) {
     9  	t.Helper()
    10  	setup.Test(t, "Sum", testSum)
    11  	setup.Test(t, "SumSequence", testSum)
    12  }
    13  
    14  func testSum(t *testing.T, setup *Setup) {
    15  	const N = 100
    16  
    17  	worker, combiner := setup.Make()
    18  	defer StartClose(combiner)()
    19  
    20  	var wg sync.WaitGroup
    21  
    22  	wg.Add(setup.Procs)
    23  	for proc := 0; proc < setup.Procs; proc++ {
    24  		go func() {
    25  			for i := int64(0); i < N; i++ {
    26  				combiner.Do(int64(1))
    27  			}
    28  			wg.Done()
    29  		}()
    30  	}
    31  
    32  	wg.Wait()
    33  	if worker.Total != N*int64(setup.Procs) {
    34  		t.Fatalf("got %v expected %v", worker.Total, N*setup.Procs)
    35  	}
    36  }
    37  
    38  func testSumSequence(t *testing.T, setup *Setup) {
    39  	const N = 100
    40  
    41  	worker, combiner := setup.Make()
    42  	defer StartClose(combiner)()
    43  
    44  	var wg sync.WaitGroup
    45  
    46  	wg.Add(setup.Procs)
    47  
    48  	for proc := 0; proc < setup.Procs; proc++ {
    49  		go func() {
    50  			for i := int64(0); i < N; i++ {
    51  				combiner.Do(i)
    52  			}
    53  			wg.Done()
    54  		}()
    55  	}
    56  
    57  	wg.Wait()
    58  	if worker.Total != int64(setup.Procs)*N*(N-1)/2 {
    59  		t.Fatalf("got %v expected %v", worker.Total, N*setup.Procs)
    60  	}
    61  }