github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/util/waitgroup/waitgroup_test.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // Package waitgroup test cases reference golang sync.WaitGroup https://golang.org/src/sync/waitgroup_test.go. 18 package waitgroup 19 20 import ( 21 "testing" 22 ) 23 24 func TestWaitGroup(t *testing.T) { 25 wg1 := &SafeWaitGroup{} 26 wg2 := &SafeWaitGroup{} 27 n := 16 28 wg1.Add(n) 29 wg2.Add(n) 30 exited := make(chan bool, n) 31 for i := 0; i != n; i++ { 32 go func(i int) { 33 wg1.Done() 34 wg2.Wait() 35 exited <- true 36 }(i) 37 } 38 wg1.Wait() 39 for i := 0; i != n; i++ { 40 select { 41 case <-exited: 42 t.Fatal("SafeWaitGroup released group too soon") 43 default: 44 } 45 wg2.Done() 46 } 47 for i := 0; i != n; i++ { 48 <-exited // Will block if barrier fails to unlock someone. 49 } 50 } 51 52 func TestWaitGroupAddFail(t *testing.T) { 53 wg := &SafeWaitGroup{} 54 wg.Add(1) 55 wg.Done() 56 wg.Wait() 57 if err := wg.Add(1); err == nil { 58 t.Errorf("Should return error when add positive after Wait") 59 } 60 }