2018-07-09 21:38:00 -07:00
|
|
|
package semerrgroup
|
2018-06-25 17:00:00 -03:00
|
|
|
|
|
|
|
import (
|
2019-08-09 11:05:07 -03:00
|
|
|
"fmt"
|
2018-07-09 22:04:25 -07:00
|
|
|
"sync"
|
2018-06-25 17:00:00 -03:00
|
|
|
"testing"
|
2018-07-09 22:04:25 -07:00
|
|
|
"time"
|
2018-06-25 17:00:00 -03:00
|
|
|
|
2019-10-06 14:58:33 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/pipe"
|
2018-06-25 17:00:00 -03:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSemaphore(t *testing.T) {
|
2022-06-11 16:54:55 -03:00
|
|
|
for _, i := range []int{1, 4} {
|
|
|
|
t.Run(fmt.Sprintf("limit-%d", i), func(t *testing.T) {
|
|
|
|
g := New(i)
|
|
|
|
var lock sync.Mutex
|
|
|
|
var counter int
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
g.Go(func() error {
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
lock.Lock()
|
|
|
|
counter++
|
|
|
|
lock.Unlock()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
require.NoError(t, g.Wait())
|
|
|
|
require.Equal(t, counter, 10)
|
2018-07-04 01:37:32 -07:00
|
|
|
})
|
2018-06-25 17:00:00 -03:00
|
|
|
}
|
|
|
|
}
|
2019-08-05 04:36:14 -07:00
|
|
|
|
|
|
|
func TestSemaphoreOrder(t *testing.T) {
|
2021-04-25 14:20:49 -03:00
|
|
|
num := 10
|
|
|
|
g := New(1)
|
|
|
|
output := []int{}
|
2019-08-05 04:36:14 -07:00
|
|
|
for i := 0; i < num; i++ {
|
2019-08-09 11:05:07 -03:00
|
|
|
i := i
|
2019-08-05 04:36:14 -07:00
|
|
|
g.Go(func() error {
|
2019-08-09 11:05:07 -03:00
|
|
|
output = append(output, i)
|
2019-08-05 04:36:14 -07:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
require.NoError(t, g.Wait())
|
2019-08-09 11:05:07 -03:00
|
|
|
require.Equal(t, []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, output)
|
|
|
|
}
|
|
|
|
|
2022-06-11 16:54:55 -03:00
|
|
|
func TestSemaphoreError(t *testing.T) {
|
|
|
|
for _, i := range []int{1, 4} {
|
|
|
|
t.Run(fmt.Sprintf("limit-%d", i), func(t *testing.T) {
|
|
|
|
g := New(i)
|
|
|
|
var lock sync.Mutex
|
|
|
|
output := []int{}
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
i := i
|
|
|
|
g.Go(func() error {
|
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
output = append(output, i)
|
|
|
|
return fmt.Errorf("fake err")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
require.EqualError(t, g.Wait(), "fake err")
|
|
|
|
require.Len(t, output, 10)
|
2019-08-09 11:05:07 -03:00
|
|
|
})
|
|
|
|
}
|
2019-08-05 04:36:14 -07:00
|
|
|
}
|
2019-10-06 14:58:33 -03:00
|
|
|
|
|
|
|
func TestSemaphoreSkipAware(t *testing.T) {
|
2022-06-11 16:54:55 -03:00
|
|
|
for _, i := range []int{1, 4} {
|
|
|
|
t.Run(fmt.Sprintf("limit-%d", i), func(t *testing.T) {
|
|
|
|
g := NewSkipAware(New(i))
|
|
|
|
var lock sync.Mutex
|
|
|
|
var counter int
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
g.Go(func() error {
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
lock.Lock()
|
|
|
|
counter++
|
|
|
|
lock.Unlock()
|
|
|
|
return pipe.Skip("fake skip")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
require.EqualError(t, g.Wait(), "fake skip")
|
|
|
|
require.Equal(t, counter, 10)
|
2019-10-06 14:58:33 -03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSemaphoreSkipAndRealError(t *testing.T) {
|
2021-04-25 14:20:49 -03:00
|
|
|
g := NewSkipAware(New(10))
|
2019-10-06 14:58:33 -03:00
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
g.Go(func() error {
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
return pipe.Skip("fake skip")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
g.Go(func() error {
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
return fmt.Errorf("errrrrr")
|
|
|
|
})
|
|
|
|
require.EqualError(t, g.Wait(), "errrrrr")
|
|
|
|
}
|