Skip to content

Instantly share code, notes, and snippets.

@eric
Forked from ryandotsmith/cond_test.go
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eric/6afa983831e0e1738d07 to your computer and use it in GitHub Desktop.
Save eric/6afa983831e0e1738d07 to your computer and use it in GitHub Desktop.
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