-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgo_test.go
50 lines (40 loc) · 817 Bytes
/
go_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package fungi
import (
"sync"
"testing"
"github.com/stretchr/testify/require"
)
func TestGo(t *testing.T) {
var wg sync.WaitGroup
var mutex sync.Mutex
total := 0
slice := []int{1, 2, 3, 4, 5}
stream := SliceStream(slice)
wg.Add(5)
count := Go(func(i int) {
defer wg.Done()
mutex.Lock()
defer mutex.Unlock()
total += i
})
consume := Loop(Nop[int])
require.NoError(t, consume(count(stream)))
wg.Wait()
require.Equal(t, 15, total)
}
func TestGoWait(t *testing.T) {
var wg sync.WaitGroup
var mutex sync.Mutex
total := 0
slice := []int{1, 2, 3, 4, 5}
stream := SliceStream(slice)
count := GoWait(func(i int) {
mutex.Lock()
defer mutex.Unlock()
total += i
}, &wg)
consume := Loop(Nop[int])
require.NoError(t, consume(count(stream)))
wg.Wait()
require.Equal(t, 15, total)
}