storj/internal/sync2/fence_test.go

47 lines
770 B
Go
Raw Normal View History

2019-01-07 19:00:40 +00:00
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information
package sync2_test
import (
"errors"
"sync/atomic"
"testing"
"time"
"golang.org/x/sync/errgroup"
"storj.io/storj/internal/sync2"
)
func TestFence(t *testing.T) {
var group errgroup.Group
var fence sync2.Fence
var done int32
for i := 0; i < 10; i++ {
group.Go(func() error {
fence.Wait()
if atomic.LoadInt32(&done) == 0 {
return errors.New("fence not yet released")
}
return nil
})
}
// wait a bit for all goroutines to hit the fence
time.Sleep(100 * time.Millisecond)
for i := 0; i < 3; i++ {
group.Go(func() error {
atomic.StoreInt32(&done, 1)
fence.Release()
return nil
})
}
if err := group.Wait(); err != nil {
t.Fatal(err)
}
}