-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi_steps_test.go
223 lines (187 loc) · 4.96 KB
/
api_steps_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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//go:build api_test
// +build api_test
package mermaidlive
import (
"context"
"errors"
"log"
"os"
"testing"
"time"
"github.com/cskr/pubsub/v2"
"github.com/cucumber/godog"
"github.com/cucumber/godog/colors"
)
var opts = godog.Options{
Output: colors.Colored(os.Stdout),
Format: "pretty",
}
const testPort = "8081"
// sutBaseUrl is set at the beginning of the test suite run
var sutBaseUrl string
// context keys
type clientKey struct{}
type secondClientKey struct{}
type serverKey struct{}
func aSystemInState(ctx context.Context, state string) (context.Context, error) {
// client
client := NewApiClient(sutBaseUrl)
ctx = context.WithValue(ctx, clientKey{}, client)
if err := client.WaitForState(state); err != nil {
return ctx, err
}
var err error
return ctx, err
}
func someWorkHasProgressed(ctx context.Context) error {
client, err := getClient(ctx)
if err != nil {
return err
}
return client.WaitForEventSeen("Tick")
}
func theRequestIsIgnored(ctx context.Context) error {
client, err := getClient(ctx)
if err != nil {
return err
}
return client.WaitForEventSeen("RequestIgnored")
}
func theSystemIsFoundInState(ctx context.Context, state string) error {
client, err := getClient(ctx)
if err != nil {
return err
}
if err := client.WaitForState(state); err != nil {
return err
}
return nil
}
func theSystemIsRequested(ctx context.Context, command string) error {
client, err := getClient(ctx)
if err != nil {
return err
}
return client.PostCommand(command)
}
func twoClientsHaveObserved(ctx context.Context, eventName string) error {
client1, err := getClient(ctx)
if err != nil {
return err
}
client2, err := getSecondClient(ctx)
if err != nil {
return err
}
err = client1.WaitForEventSeen(eventName)
if err != nil {
return err
}
err = client2.WaitForEventSeen(eventName)
if err != nil {
return err
}
return nil
}
func twoConnectedClients(ctx context.Context) (context.Context, error) {
client := NewApiClient(sutBaseUrl)
ctx = context.WithValue(ctx, secondClientKey{}, client)
return ctx, nil
}
func workIsCanceled(ctx context.Context) error {
client, err := getClient(ctx)
if err != nil {
return err
}
return client.WaitForEventSeen("WorkAborted")
}
func workIsCompleted(ctx context.Context) error {
client, err := getClient(ctx)
if err != nil {
return err
}
return client.WaitForEventSeen("WorkDone")
}
func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.After(func(ctx context.Context, sc *godog.Scenario, err error) (context.Context, error) {
if client1, err := getClient(ctx); err == nil {
log.Println("closing client 1")
client1.Close()
}
if client2, err := getSecondClient(ctx); err == nil {
log.Println("closing client 2")
client2.Close()
}
return ctx, nil
})
ctx.Step(`^a system in state "([^"]*)"$`, aSystemInState)
ctx.Step(`^some work has progressed$`, someWorkHasProgressed)
ctx.Step(`^the request is ignored$`, theRequestIsIgnored)
ctx.Step(`^the system is found in state "([^"]*)"$`, theSystemIsFoundInState)
ctx.Step(`^the system "([^"]*)" is requested$`, theSystemIsRequested)
ctx.Step(`^two clients have observed "([^"]*)"$`, twoClientsHaveObserved)
ctx.Step(`^two connected clients$`, twoConnectedClients)
ctx.Step(`^work is canceled$`, workIsCanceled)
ctx.Step(`^work is completed$`, workIsCompleted)
}
func TestApi(t *testing.T) {
configureSutBaseUrl()
configureTestParameters()
suite := godog.TestSuite{
ScenarioInitializer: InitializeScenario,
Options: &opts,
}
if suite.Run() != 0 {
t.Fatal("non-zero status returned, failed to run feature tests")
}
}
func getClient(ctx context.Context) (*ApiClient, error) {
return getClientByKey(ctx, clientKey{})
}
func getSecondClient(ctx context.Context) (*ApiClient, error) {
return getClientByKey(ctx, secondClientKey{})
}
func getClientByKey(ctx context.Context, key interface{}) (*ApiClient, error) {
client, ok := ctx.Value(key).(*ApiClient)
if !ok {
return nil, errors.New("client not found in test context. Please check test definitions")
}
return client, nil
}
func configureSutBaseUrl() {
if url, ok := os.LookupEnv("SUT_BASE_URL"); ok {
sutBaseUrl = url
} else {
startServer()
}
log.Println("SUT_BASE_URL:", sutBaseUrl)
}
func configureTestParameters() {
readDelay = updateDurationIfInEnv("TEST_READ_DELAY", readDelay)
eventWaitingDelay = updateDurationIfInEnv("TEST_WAIT_DELAY", eventWaitingDelay)
}
func startServer() {
log.Println("Starting a new server")
eventPublisher := pubsub.New[string, Event](1)
server := NewServerWithOptions(
testPort,
eventPublisher,
GetFS(),
50*time.Millisecond,
)
sutBaseUrl = "http://localhost:" + testPort
go server.Run(testPort)
}
func updateDurationIfInEnv(key string, def time.Duration) time.Duration {
v, ok := os.LookupEnv(key)
if !ok {
return def
}
d, err := time.ParseDuration(v)
if err != nil {
log.Println("could not parse duration from " + key + "=" + v)
return def
}
log.Printf("set new duration %s=%v", key, d)
return d
}