2018-07-10 06:38:00 +02:00
|
|
|
package semerrgroup
|
2018-06-25 22:00:00 +02:00
|
|
|
|
|
|
|
import (
|
2018-07-10 07:04:25 +02:00
|
|
|
"sync"
|
2018-06-25 22:00:00 +02:00
|
|
|
"testing"
|
2018-07-10 07:04:25 +02:00
|
|
|
"time"
|
2018-06-25 22:00:00 +02:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSemaphore(t *testing.T) {
|
2018-07-10 07:04:25 +02:00
|
|
|
var g = New(4)
|
|
|
|
var lock sync.Mutex
|
|
|
|
var counter int
|
2018-06-25 22:00:00 +02:00
|
|
|
for i := 0; i < 10; i++ {
|
2018-07-04 10:37:32 +02:00
|
|
|
g.Go(func() error {
|
2018-07-10 07:04:25 +02:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
lock.Lock()
|
2018-06-25 22:00:00 +02:00
|
|
|
counter++
|
2018-07-10 07:04:25 +02:00
|
|
|
lock.Unlock()
|
2018-07-04 10:37:32 +02:00
|
|
|
return nil
|
|
|
|
})
|
2018-06-25 22:00:00 +02:00
|
|
|
}
|
2018-07-04 10:37:32 +02:00
|
|
|
require.NoError(t, g.Wait())
|
|
|
|
require.Equal(t, counter, 10)
|
2018-06-25 22:00:00 +02:00
|
|
|
}
|