-
Notifications
You must be signed in to change notification settings - Fork 11
/
spawner_test.go
98 lines (86 loc) · 1.52 KB
/
spawner_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
92
93
94
95
96
97
98
package ik
import (
"errors"
"testing"
)
type Foo struct {
state string
c chan string
}
func (foo *Foo) Run() error {
foo.state = "run"
retval := <-foo.c
foo.state = "stopped"
return errors.New(retval)
}
func (foo *Foo) Shutdown() error {
foo.c <- "ok"
return nil
}
type Bar struct{ c chan interface{} }
func (bar *Bar) Run() error {
panic(<-bar.c)
}
func (bar *Bar) Shutdown() error {
return nil
}
type Baz struct{ message string }
func (baz *Baz) String() string {
return baz.message
}
func TestSpawner_Spawn(t *testing.T) {
spawner := NewSpawner()
f := &Foo{"", make(chan string)}
spawner.Spawn(f)
err := spawner.GetStatus(f)
if err != Continue {
t.Fail()
}
f.c <- "result"
spawner.Poll(f)
err = spawner.GetStatus(f)
if err == Continue {
t.Fail()
}
if err.Error() != "result" {
t.Fail()
}
}
func TestSpawner_Panic1(t *testing.T) {
spawner := NewSpawner()
f := &Bar{make(chan interface{})}
spawner.Spawn(f)
err := spawner.GetStatus(f)
if err != Continue {
t.Fail()
}
f.c <- "PANIC"
spawner.Poll(f)
err = spawner.GetStatus(f)
panicked, ok := err.(*Panicked)
if !ok {
t.Fail()
}
if panicked.Error() != "PANIC" {
t.Fail()
}
}
func TestSpawner_Panic2(t *testing.T) {
spawner := NewSpawner()
f := &Bar{make(chan interface{})}
spawner.Spawn(f)
err := spawner.GetStatus(f)
if err != Continue {
t.Fail()
}
f.c <- &Baz{"BAZ!"}
spawner.Poll(f)
err = spawner.GetStatus(f)
panicked, ok := err.(*Panicked)
if !ok {
t.Fail()
}
if panicked.Error() != "(*Baz) BAZ!" {
t.Fail()
}
}