github.com/GoWebProd/gip@v0.0.0-20230623090727-b60d41d5d320/cond/cond_test.go (about) 1 package cond 2 3 import ( 4 "sync" 5 "testing" 6 "time" 7 ) 8 9 func TestSingle(t *testing.T) { 10 var ( 11 c Single 12 v int 13 ) 14 15 go func() { 16 time.Sleep(10 * time.Millisecond) 17 18 v = 1 19 20 c.Done() 21 }() 22 23 c.Wait() 24 25 if v != 1 { 26 t.Fatalf("bad value: %d", v) 27 } 28 29 c.Wait() 30 } 31 32 func TestMulti(t *testing.T) { 33 var ( 34 c Multi 35 v int 36 wg sync.WaitGroup 37 err bool 38 ) 39 40 for i := 0; i < 100; i++ { 41 wg.Add(1) 42 43 go func() { 44 defer wg.Done() 45 46 c.Wait() 47 48 if v != 1 { 49 err = true 50 } 51 }() 52 } 53 54 time.Sleep(10 * time.Millisecond) 55 56 v = 1 57 58 c.Done() 59 wg.Wait() 60 61 if err { 62 t.Fatal("one of goroutines failed") 63 } 64 65 c.Wait() 66 }