forked from cloudfoundry/gorouter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
162 lines (133 loc) · 3.25 KB
/
app.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package test
import (
"github.com/cloudfoundry/gorouter/common"
"github.com/cloudfoundry/gorouter/route"
"github.com/cloudfoundry/yagnats"
. "github.com/onsi/gomega"
"github.com/pivotal-golang/localip"
"encoding/json"
"errors"
"fmt"
"net/http"
"sync"
"time"
)
type TestApp struct {
mutex sync.Mutex
port uint16 // app listening port
rPort uint16 // router listening port
urls []route.Uri // host registered host name
mbusClient yagnats.NATSConn
tags map[string]string
mux *http.ServeMux
stopped bool
}
func NewTestApp(urls []route.Uri, rPort uint16, mbusClient yagnats.NATSConn, tags map[string]string) *TestApp {
app := new(TestApp)
port, _ := localip.LocalPort()
app.port = port
app.rPort = rPort
app.urls = urls
app.mbusClient = mbusClient
app.tags = tags
app.mux = http.NewServeMux()
return app
}
func (a *TestApp) AddHandler(path string, handler func(http.ResponseWriter, *http.Request)) {
a.mux.HandleFunc(path, handler)
}
func (a *TestApp) Urls() []route.Uri {
return a.urls
}
func (a *TestApp) Endpoint() string {
return fmt.Sprintf("http://%s:%d/", a.urls[0], a.rPort)
}
func (a *TestApp) Listen() {
server := &http.Server{
Addr: fmt.Sprintf(":%d", a.port),
Handler: a.mux,
}
a.Register()
go server.ListenAndServe()
}
func (a *TestApp) RegisterRepeatedly(duration time.Duration) {
a.start()
for {
if a.isStopped() {
break
}
a.Register()
time.Sleep(duration)
}
}
func (a *TestApp) Register() {
uuid, _ := common.GenerateUUID()
rm := registerMessage{
Host: "localhost",
Port: a.port,
Uris: a.urls,
Tags: a.tags,
Dea: "dea",
App: "0",
StaleThresholdInSeconds: 1,
PrivateInstanceId: uuid,
}
b, _ := json.Marshal(rm)
a.mbusClient.Publish("router.register", b)
}
func (a *TestApp) Unregister() {
rm := registerMessage{
Host: "localhost",
Port: a.port,
Uris: a.urls,
Tags: nil,
Dea: "dea",
App: "0",
}
b, _ := json.Marshal(rm)
a.mbusClient.Publish("router.unregister", b)
a.stop()
}
func (a *TestApp) VerifyAppStatus(status int) {
Eventually(func() error {
return a.CheckAppStatus(status)
}, 2*time.Second).ShouldNot(HaveOccurred())
}
func (a *TestApp) CheckAppStatus(status int) error {
for _, url := range a.urls {
uri := fmt.Sprintf("http://%s:%d", url, a.rPort)
resp, err := http.Get(uri)
if err != nil {
return err
}
if resp.StatusCode != status {
return errors.New(fmt.Sprintf("expected status code %d, got %d", status, resp.StatusCode))
}
}
return nil
}
func (a *TestApp) start() {
a.mutex.Lock()
a.stopped = false
a.mutex.Unlock()
}
func (a *TestApp) stop() {
a.mutex.Lock()
a.stopped = true
a.mutex.Unlock()
}
func (a *TestApp) isStopped() bool {
a.mutex.Lock()
defer a.mutex.Unlock()
return a.stopped
}
type registerMessage struct {
Host string `json:"host"`
Port uint16 `json:"port"`
Uris []route.Uri `json:"uris"`
Tags map[string]string `json:"tags"`
Dea string `json:"dea"`
App string `json:"app"`
StaleThresholdInSeconds int `json:"stale_threshold_in_seconds"`
PrivateInstanceId string `json:"private_instance_id"`
}