-
-
Save eric/6afa983831e0e1738d07 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"testing" | |
"sync" | |
"sync/atomic" | |
) | |
func TestOneBarrier(t *testing.T) { | |
expected := int32(2) | |
actual := int32(0) | |
var barrier sync.WaitGroup | |
finished := make(chan bool, expected) | |
barrier.Add(expected) | |
for i := int32(0); i < expected; i++ { | |
go func() { | |
barrier.Done() | |
barrier.Wait() | |
atomic.AddInt32(&actual, 1) | |
finished <- true | |
}() | |
} | |
for i := int32(0); i < expected; i++ { | |
<-finished | |
} | |
if actual != expected { | |
t.Errorf("actual=%d expected=%d\n", actual, expected) | |
} | |
} | |
func TestTwoBarriers(t *testing.T) { | |
expected := int32(2) | |
actual := int32(0) | |
var ready sync.WaitGroup | |
var completed sync.WaitGroup | |
barrier.Add(expected) | |
completed.Add(expected) | |
for i := int32(0); i < expected; i++ { | |
go func() { | |
ready.Done() | |
ready.Wait() | |
atomic.AddInt32(&actual, 1) | |
completed.Done() | |
}() | |
} | |
completed.Wait() | |
if actual != expected { | |
t.Errorf("actual=%d expected=%d\n", actual, expected) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment