-
Notifications
You must be signed in to change notification settings - Fork 1
/
async_test.go
91 lines (72 loc) · 1.59 KB
/
async_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package rollback
import (
"errors"
"testing"
)
func fakeCreateAcctFunc(email, pass string) error {
return nil
}
func fakeCreateWorkerErrorFunc(email string) error {
return errors.New("worker error")
}
func fakeCreateEmpFunc(email string) error {
return nil
}
type fakeCreateAcct struct {
email, pass string
err error
chErr chan error
rollback bool
}
func (h *fakeCreateAcct) Do() error {
return fakeCreateAcctFunc(h.email, h.pass)
}
func (h *fakeCreateAcct) Rollback() {
h.rollback = true
}
type fakeCreateWorker struct {
email string
err error
chErr chan error
rollback bool
}
func (h *fakeCreateWorker) Do() error {
return fakeCreateWorkerErrorFunc(h.email)
}
func (h *fakeCreateWorker) Rollback() {
h.rollback = true
}
type fakeCreateEmp struct {
email string
err error
chErr chan error
rollback bool
}
func (h *fakeCreateEmp) Do() error {
return fakeCreateEmpFunc(h.email)
}
func (h *fakeCreateEmp) Rollback() {
h.rollback = true
}
func TestAsyncHandler(t *testing.T) {
chErr := make(chan error)
chDone := make(chan struct{})
chFinish := make(chan struct{})
chRollback := make(chan struct{})
go AsyncHandler(chErr, chDone, chFinish, chRollback, &fakeCreateAcct{})
err := <-chErr
if err != nil {
t.Error(err)
}
}
func TestAsyncHandlerError(t *testing.T) {
chErr := make(chan error)
chDone := make(chan struct{})
chFinish := make(chan struct{})
chRollback := make(chan struct{})
go AsyncHandler(chErr, chDone, chFinish, chRollback, &fakeCreateWorker{})
err := <-chErr
if err == nil {
t.Error("it should error but not")
}
}