-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipego_test.go
68 lines (65 loc) · 1.34 KB
/
pipego_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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package pp_test
import (
"context"
"fmt"
"testing"
"time"
pp "github.com/sonalys/pipego"
"github.com/stretchr/testify/require"
)
func Test_Run(t *testing.T) {
ctx := context.Background()
t.Run("no steps", func(t *testing.T) {
err := pp.Run(ctx)
require.NoError(t, err)
})
t.Run("with steps", func(t *testing.T) {
run := false
err := pp.Run(ctx, func(_ context.Context) (err error) {
run = true
return
})
require.NoError(t, err)
require.True(t, run)
})
t.Run("with duration", func(t *testing.T) {
run := false
delay := 100 * time.Millisecond
err := pp.Run(ctx, func(_ context.Context) (err error) {
run = true
time.Sleep(delay)
return
})
require.NoError(t, err)
require.True(t, run)
})
t.Run("keep step order", func(t *testing.T) {
var i int
err := pp.Run(ctx,
func(_ context.Context) (err error) {
require.Equal(t, 0, i)
i++
return err
},
func(_ context.Context) (err error) {
require.Equal(t, 1, i)
i++
return err
},
)
require.NoError(t, err)
require.Equal(t, 2, i)
})
t.Run("stop on error", func(t *testing.T) {
err := pp.Run(ctx,
func(_ context.Context) (err error) {
return fmt.Errorf("mock")
},
func(_ context.Context) (err error) {
require.Fail(t, "should not run")
return
},
)
require.Equal(t, fmt.Errorf("mock"), err)
})
}