2018-07-10 06:38:00 +02:00
|
|
|
package semerrgroup
|
2018-06-25 22:00:00 +02:00
|
|
|
|
|
|
|
import (
|
2019-08-09 16:05:07 +02:00
|
|
|
"fmt"
|
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
|
|
|
|
2019-10-06 19:58:33 +02:00
|
|
|
"github.com/goreleaser/goreleaser/internal/pipe"
|
2018-06-25 22:00:00 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSemaphore(t *testing.T) {
|
2022-06-11 21:54:55 +02: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 10:37:32 +02:00
|
|
|
})
|
2018-06-25 22:00:00 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-05 13:36:14 +02:00
|
|
|
|
|
|
|
func TestSemaphoreOrder(t *testing.T) {
|
2021-04-25 19:20:49 +02:00
|
|
|
num := 10
|
|
|
|
g := New(1)
|
|
|
|
output := []int{}
|
2019-08-05 13:36:14 +02:00
|
|
|
for i := 0; i < num; i++ {
|
2019-08-09 16:05:07 +02:00
|
|
|
i := i
|
2019-08-05 13:36:14 +02:00
|
|
|
g.Go(func() error {
|
2019-08-09 16:05:07 +02:00
|
|
|
output = append(output, i)
|
2019-08-05 13:36:14 +02:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
require.NoError(t, g.Wait())
|
2019-08-09 16:05:07 +02:00
|
|
|
require.Equal(t, []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, output)
|
|
|
|
}
|
|
|
|
|
2022-06-11 21:54:55 +02: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 16:05:07 +02:00
|
|
|
})
|
|
|
|
}
|
2019-08-05 13:36:14 +02:00
|
|
|
}
|
2019-10-06 19:58:33 +02:00
|
|
|
|
|
|
|
func TestSemaphoreSkipAware(t *testing.T) {
|
2022-06-11 21:54:55 +02: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 19:58:33 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSemaphoreSkipAndRealError(t *testing.T) {
|
2021-04-25 19:20:49 +02:00
|
|
|
g := NewSkipAware(New(10))
|
2019-10-06 19:58:33 +02: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")
|
|
|
|
}
|