-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathjob_test.go
65 lines (52 loc) · 1011 Bytes
/
job_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
package queue
import (
"errors"
"fmt"
"reflect"
"sync"
"testing"
)
func TestJob(t *testing.T) {
var (
result interface{}
wg sync.WaitGroup
)
wg.Add(1)
job := NewJob("foo", func(v interface{}) {
result = fmt.Sprintf("%s_bar", v)
wg.Done()
})
go job.Job()
wg.Wait()
if !reflect.DeepEqual(result, "foo_bar") {
t.Error(result)
}
}
func TestSyncJob(t *testing.T) {
sjob := NewSyncJob("foo", func(v interface{}) (interface{}, error) {
return fmt.Sprintf("%s_bar", v), nil
})
go sjob.Job()
result := <-sjob.Wait()
if err := sjob.Error(); err != nil {
t.Error(err.Error())
return
}
if !reflect.DeepEqual(result, "foo_bar") {
t.Error(result)
}
}
func TestSyncJobError(t *testing.T) {
sjob := NewSyncJob("foo", func(v interface{}) (interface{}, error) {
return nil, errors.New("mock error")
})
go sjob.Job()
result := <-sjob.Wait()
if err := sjob.Error(); err == nil {
t.Error("mock error")
return
}
if result != nil {
t.Error("result is nil")
}
}