-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance_test.go
144 lines (134 loc) · 3.58 KB
/
instance_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package juggler
import (
"github.com/dcoxall/juggler/utils/jugglertest"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
)
func TestInstanceInterface(t *testing.T) {
var itf interface{} = new(Instance)
if _, ok := itf.(Instancer); !ok {
t.Fatalf("Instance doesn't fulfill Instancer interface")
}
}
func TestStartingStoppingInstance(t *testing.T) {
instance := NewInstance(&jugglertest.ExampleBootstrap{}, "startstop")
if instance.Started() {
t.Errorf("Instance should not have started")
}
ready, err := instance.Start()
if err != nil {
t.Fatalf("error: %s", err)
}
timeout := time.After(2 * time.Second)
select {
case state := <-ready:
if state != Running {
t.Fatalf("instance returned %d instead of %d", state, Running)
}
case <-timeout:
instance.ForceStop()
t.Fatalf("instance state not updated")
}
if !instance.Started() {
t.Errorf("Instance claims to have not started")
}
if instance.Stopped() {
t.Errorf("Instance should not be stopped")
}
stopped, err := instance.Stop()
if err != nil {
t.Fatalf("error: %s", err)
}
timeout = time.After(2 * time.Second)
select {
case state := <-stopped:
if state != Stopped {
t.Fatalf("instance returned %d instead of %d", state, Stopped)
}
case <-timeout:
instance.ForceStop()
t.Fatalf("instance never stopped before timeout period")
}
if !instance.Stopped() {
t.Errorf("Instance claims to have not stopped")
}
}
func TestInstanceStartErrors(t *testing.T) {
instance := NewInstance(&jugglertest.ExampleBootstrap{}, "starterrors")
ready, err := instance.Start()
if err != nil {
t.Fatalf("error: %s", err)
}
timeout := time.After(2 * time.Second)
if _, err := instance.Start(); err == nil {
t.Errorf("Expected an error when starting an already started instance")
}
select {
case <-ready:
case <-timeout:
instance.ForceStop()
t.Fatalf("instance never started before timeout period")
}
stopped, _ := instance.Stop()
timeout = time.After(2 * time.Second)
select {
case <-stopped:
case <-timeout:
instance.ForceStop()
t.Fatalf("instance never stopped within timeout period")
}
}
func TestInstanceStopErrors(t *testing.T) {
instance := NewInstance(&jugglertest.ExampleBootstrap{}, "stoperrors")
if _, err := instance.Stop(); err == nil {
t.Errorf("Expected an error when stopping an already stopped instance")
}
}
func TestInstanceProxying(t *testing.T) {
var wg sync.WaitGroup
instances := map[string]*Instance{
"foo": NewInstance(&jugglertest.ExampleBootstrap{}, "foo"),
"bar": NewInstance(&jugglertest.ExampleBootstrap{}, "bar"),
}
for ref, i := range instances {
wg.Add(1)
go func(instance *Instance) {
ready, _ := instance.Start()
timeout := time.After(2 * time.Second)
select {
case <-ready:
wg.Done()
case <-timeout:
wg.Done()
t.Fatalf("State change failed to occur within timeout for %s", ref)
}
}(i)
}
wg.Wait() // wait until our 2 instances have started
// make a request to both instances and assert different responses
for ref, i := range instances {
proxy, _ := i.ReverseProxy()
req, _ := http.NewRequest("GET", "http://example.com/", nil)
w := httptest.NewRecorder()
proxy.ServeHTTP(w, req)
if body := w.Body.String(); body != ref {
t.Errorf("Expected response body to be %v but got %v", ref, body)
}
wg.Add(1)
go func(instance *Instance) {
stopped, _ := instance.Stop()
timeout := time.After(2 * time.Second)
select {
case <-stopped:
wg.Done()
case <-timeout:
wg.Done()
t.Fatalf("State change failed to occur within timeout for %s", ref)
}
}(i)
}
wg.Wait() // wait until instances have been removed
}