-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe2e_test.go
178 lines (147 loc) · 5.25 KB
/
e2e_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//+build e2e
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/satori/go.uuid"
"github.com/gomodule/redigo/redis"
"github.com/nsqio/go-nsq"
)
// e2e test connecting to the different microservices. Using same parameters as docker-compose.yaml file
func TestE2E(t *testing.T) {
assertThat := assert.New(t)
topic := "locations"
tearDown := func() {
nsq.UnRegister(topic, "")
redisAddr := os.Getenv("REDIS")
if redisAddr == "" {
redisAddr = "localhost:6379"
}
conn, err := redis.Dial("tcp", redisAddr)
if err != nil {
println(err)
}
_, err = conn.Do("FLUSHALL")
assertThat.NoError(err)
}
syncChan := make(chan bool, 0)
secondSyncChan := make(chan bool, 0)
numMessages := 0
setup := func() {
config := nsq.NewConfig()
consumer, err := nsq.NewConsumer(topic, "te", config)
assertThat.NoError(err)
handler := nsq.HandlerFunc(func(message *nsq.Message) error {
numMessages++
if numMessages == 2 {
time.Sleep(1 * time.Second)
syncChan <- true
}
if numMessages == 4 {
time.Sleep(1 * time.Second)
secondSyncChan <- true
}
return nil
})
consumer.AddHandler(handler)
consumer.ConnectToNSQD("localhost:4150")
resp, err := http.Get("http://localhost:8080/healthz") // driver-location
assertThat.NoError(err)
assertThat.Equal(http.StatusOK, resp.StatusCode)
resp, err = http.Get("http://localhost:7070/healthz") // zombie-driver
assertThat.NoError(err)
assertThat.Equal(http.StatusOK, resp.StatusCode)
resp, err = http.Get("http://localhost:1138/healthz") // gateway
assertThat.NoError(err)
assertThat.Equal(http.StatusOK, resp.StatusCode)
body := map[string]interface{}{"max_meters": 500, "last_minutes": 5}
bodyJson, _ := json.Marshal(body)
req, err := http.NewRequest(http.MethodPatch, "http://localhost:7070/config", bytes.NewBuffer(bodyJson))
assertThat.NoError(err)
resp, err = http.DefaultClient.Do(req)
assertThat.NoError(err)
assertThat.Equal(http.StatusOK, resp.StatusCode)
}
t.Run("Given all the services running and the default configuration set", func(t *testing.T) {
setup()
defer tearDown()
t.Run("When creating near locations for a driver", func(t *testing.T) {
driverID := uuid.NewV4().String()
// create location
body := map[string]interface{}{"latitude": 48.864193, "longitude": 2.350498}
bodyJson, _ := json.Marshal(body)
req, err := http.NewRequest(http.MethodPatch, "http://localhost:1138/drivers/"+driverID+"/locations", bytes.NewBuffer(bodyJson))
assertThat.NoError(err)
resp, err := http.DefaultClient.Do(req)
assertThat.NoError(err)
assertThat.Equal(http.StatusOK, resp.StatusCode)
// create near location
body = map[string]interface{}{"latitude": 48.863921, "longitude": 2.349211}
bodyJson, _ = json.Marshal(body)
req, err = http.NewRequest(http.MethodPatch, "http://localhost:1138/drivers/"+driverID+"/locations", bytes.NewBuffer(bodyJson))
assertThat.NoError(err)
resp, err = http.DefaultClient.Do(req)
assertThat.NoError(err)
assertThat.Equal(http.StatusOK, resp.StatusCode)
t.Run("Then the driver is a zombie", func(t *testing.T) {
<-syncChan
resp, err = http.Get("http://localhost:1138/drivers/" + driverID)
assertThat.NoError(err)
assertThat.Equal(http.StatusOK, resp.StatusCode)
body, err := ioutil.ReadAll(resp.Body)
assertThat.NoError(err)
var response ZombieResponse
err = json.Unmarshal(body, &response)
assertThat.NoError(err)
assertThat.Equal(true, response.Zombie)
assertThat.Equal(driverID, response.Id)
})
})
})
t.Run("Given all the services running and the default configuration set", func(t *testing.T) {
setup()
defer tearDown()
t.Run("When creating far locations for a driver", func(t *testing.T) {
driverID := uuid.NewV4().String()
// create location
body := map[string]interface{}{"latitude": 48.864193, "longitude": 2.350498}
bodyJson, _ := json.Marshal(body)
req, err := http.NewRequest(http.MethodPatch, "http://localhost:1138/drivers/"+driverID+"/locations", bytes.NewBuffer(bodyJson))
assertThat.NoError(err)
resp, err := http.DefaultClient.Do(req)
assertThat.NoError(err)
assertThat.Equal(http.StatusOK, resp.StatusCode)
//create far location
body = map[string]interface{}{"latitude": 48.858093, "longitude": 2.294694}
bodyJson, _ = json.Marshal(body)
req, err = http.NewRequest(http.MethodPatch, "http://localhost:1138/drivers/"+driverID+"/locations", bytes.NewBuffer(bodyJson))
assertThat.NoError(err)
resp, err = http.DefaultClient.Do(req)
assertThat.NoError(err)
assertThat.Equal(http.StatusOK, resp.StatusCode)
t.Run("Then the driver is not a zombie", func(t *testing.T) {
<-secondSyncChan
resp, err := http.Get("http://localhost:1138/drivers/" + driverID)
assertThat.NoError(err)
assertThat.Equal(http.StatusOK, resp.StatusCode)
body, err := ioutil.ReadAll(resp.Body)
assertThat.NoError(err)
var response ZombieResponse
err = json.Unmarshal(body, &response)
assertThat.NoError(err)
assertThat.Equal(false, response.Zombie)
assertThat.Equal(driverID, response.Id)
})
})
})
}
type ZombieResponse struct {
Id string `json:"id"`
Zombie bool `json:"zombie"`
}