-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_client_test.go
223 lines (199 loc) · 4.71 KB
/
api_client_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"
"encoding/json"
"fmt"
"io"
"log"
"slices"
"strings"
"time"
"github.com/Arceliar/phony"
"github.com/go-resty/resty/v2"
)
var readDelay = 10 * time.Millisecond
var eventWaitingDelay = 100 * time.Millisecond
const retriesForStateChange = 10
type ApiClient struct {
phony.Inbox
baseUrl string
callClient *resty.Client
subscriptionClient *resty.Client
currentReader io.ReadCloser
currentBuffer []byte
currentResponse *resty.Response
cancel context.CancelFunc
receivedEvents []ReceivedEvent
}
func NewApiClient(baseUrl string) *ApiClient {
client := &ApiClient{
baseUrl: baseUrl,
callClient: resty.New().SetBaseURL(baseUrl).SetTimeout(1 * time.Second),
subscriptionClient: resty.New().SetBaseURL(baseUrl).SetTimeout(1 * time.Second),
currentBuffer: make([]byte, 0),
cancel: func() {},
}
go client.subscribeToEvents()
return client
}
func (a *ApiClient) WaitForState(expectedState string) error {
var err error
var found bool
log.Printf("Waiting for state '%s' ...", expectedState)
for i := 0; i < retriesForStateChange; i++ {
phony.Block(a, func() {
res, e := a.callClient.
R().
Get("/machine/state")
if e != nil {
err = e
return
}
foundState := strings.TrimSpace(string(res.Body()))
if expectedState == foundState {
found = true
return
}
log.Printf("Expected state '%v' but found '%v', sleeping to retry...", expectedState, foundState)
time.Sleep(eventWaitingDelay)
})
}
if !found {
return fmt.Errorf("could not find state: %v", expectedState)
}
return err
}
func (a *ApiClient) PostCommand(command string) error {
var err error
phony.Block(a, func() {
log.Printf("Requesting start ...")
_, e := a.callClient.
R().
Post("/commands/" + command)
if e != nil {
err = e
return
}
})
return err
}
func (a *ApiClient) WaitForEventSeen(eventName string) error {
var found bool
for i := 0; i < retriesForStateChange; i++ {
log.Printf("Waiting for event '%s' ...", eventName)
phony.Block(a, func() {
pos := slices.IndexFunc(a.receivedEvents, func(c ReceivedEvent) bool {
return c.Name == eventName
})
if pos >= 0 {
found = true
}
})
time.Sleep(eventWaitingDelay)
if found {
return nil
}
}
if !found {
return fmt.Errorf("Gave up waiting for event: %v", eventName)
}
return nil
}
func (a *ApiClient) BaseUrl() string {
return a.baseUrl
}
func (a *ApiClient) subscribeToEvents() {
a.Act(a, func() {
a.tryCloseCurrentReaderSync()
ctx, cancel := context.WithCancel(context.Background())
a.cancel = cancel
var err error
a.currentResponse, err = a.subscriptionClient.
R().
SetContext(ctx).
SetDoNotParseResponse(true).
Get("/events")
if err != nil {
log.Println("Error subscribing to events:", err)
}
a.currentReader = a.currentResponse.RawResponse.Body
a.scheduleNextReadSync()
})
}
func (a *ApiClient) readNextEvent() {
a.Act(a, func() {
if a.currentReader == nil {
log.Println("No open request")
return
}
buf := make([]byte, 1024)
n, err := a.currentReader.Read(buf)
if err == io.EOF {
log.Println("server finished streaming events", err)
a.Close()
return
}
if err != nil {
a.Close()
log.Println("error reading event stream", err)
return
}
a.currentBuffer = append(a.currentBuffer, buf[:n]...)
err = a.tryExtractEventsSync()
if err != nil {
log.Println(err)
return
}
})
}
func (a *ApiClient) scheduleNextReadSync() {
go func() {
time.Sleep(readDelay)
a.readNextEvent()
}()
}
func (a *ApiClient) Close() {
log.Println("disconnecting the client")
a.cancel()
a.Act(a, func() {
a.tryCloseCurrentReaderSync()
})
}
func (a *ApiClient) tryCloseCurrentReaderSync() {
if a.currentReader == nil {
return
}
a.currentReader.Close()
a.currentReader = nil
a.currentBuffer = make([]byte, 0)
a.currentResponse = nil
}
func (a *ApiClient) tryExtractEventsSync() error {
for {
newlinePos := slices.IndexFunc(a.currentBuffer, func(c byte) bool {
return c == '\n'
})
if newlinePos == -1 {
break
}
eventLine := a.currentBuffer[:newlinePos]
a.currentBuffer = a.currentBuffer[newlinePos+1:]
var event ReceivedEvent
err := json.Unmarshal(eventLine, &event)
if err != nil {
// fail fast
return fmt.Errorf("Error unmarshalling event: %v", err)
}
a.receivedEvents = append(a.receivedEvents, event)
log.Println("Received event:", event)
}
a.scheduleNextReadSync()
return nil
}
type ReceivedEvent struct {
Timestamp time.Time `json:"timestamp"`
Name string `json:"name"`
Properties map[string]interface{} `json:"properties"`
}