File size: 2,376 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package lifecycle

import (
	"context"
	"testing"
	"time"

	"github.com/pkg/errors"
)

func buildPause() (func(error), PauseResumer) {
	ch := make(chan error)

	return func(err error) {
			ch <- err
		},
		PauseResumerFunc(
			func(ctx context.Context) error {
				select {
				case <-ctx.Done():
					return ctx.Err()
				case err := <-ch:
					return err
				}
			},
			func(ctx context.Context) error {
				return nil
			},
		)
}

func TestMultiPauseResume(t *testing.T) {
	t.Run("simple success", func(t *testing.T) {
		to := time.NewTimer(time.Second)
		defer to.Stop()
		done1, pr1 := buildPause()
		done2, pr2 := buildPause()
		ctx := context.Background()
		errCh := make(chan error)
		go func() { errCh <- MultiPauseResume(pr1, pr2).Pause(ctx) }()

		done1(nil)
		done2(nil)

		select {
		case err := <-errCh:
			if err != nil {
				t.Errorf("got %v; want nil", err)
			}
		case <-to.C:
			t.Fatal("never returned")
		}

	})
	t.Run("external cancellation", func(t *testing.T) {
		to := time.NewTimer(time.Second)
		defer to.Stop()

		_, pr1 := buildPause()
		_, pr2 := buildPause()
		ctx, cancel := context.WithCancel(context.Background())
		errCh := make(chan error)
		go func() { errCh <- MultiPauseResume(pr1, pr2).Pause(ctx) }()

		cancel()

		select {
		case err := <-errCh:
			if err == nil {
				t.Error("got nil; want err")
			}
		case <-to.C:
			t.Fatal("never returned")
		}
	})
	t.Run("external cancellation", func(t *testing.T) {
		to := time.NewTimer(time.Second)
		defer to.Stop()

		done1, pr1 := buildPause()
		_, pr2 := buildPause()
		ctx, cancel := context.WithCancel(context.Background())
		errCh := make(chan error)
		go func() { errCh <- MultiPauseResume(pr1, pr2).Pause(ctx) }()

		done1(nil)
		cancel()

		select {
		case err := <-errCh:
			if err == nil {
				t.Error("got nil; want err")
			}
		case <-to.C:
			t.Fatal("never returned")
		}
	})
	t.Run("external cancellation", func(t *testing.T) {
		to := time.NewTimer(time.Second)
		defer to.Stop()

		done1, pr1 := buildPause()
		_, pr2 := buildPause()
		ctx, cancel := context.WithCancel(context.Background())
		errCh := make(chan error)
		go func() { errCh <- MultiPauseResume(pr1, pr2).Pause(ctx) }()

		done1(errors.New("okay"))
		cancel()

		select {
		case err := <-errCh:
			if err == nil {
				t.Error("got nil; want err")
			}
		case <-to.C:
			t.Fatal("never returned")
		}
	})
}