-
Notifications
You must be signed in to change notification settings - Fork 36
/
wait_strategy.go
178 lines (158 loc) · 3.97 KB
/
wait_strategy.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
package test_env
import (
"bytes"
"context"
"fmt"
"net"
"net/http"
"net/url"
"strconv"
"time"
"github.com/docker/go-connections/nat"
"github.com/ethereum/go-ethereum/rpc"
"github.com/rs/zerolog"
tc "github.com/testcontainers/testcontainers-go"
tcwait "github.com/testcontainers/testcontainers-go/wait"
)
type HTTPStrategy struct {
Path string
Port nat.Port
RetryDelay time.Duration
ExpectedStatusCode int
timeout time.Duration
}
func NewHTTPStrategy(path string, port nat.Port) *HTTPStrategy {
return &HTTPStrategy{
Path: path,
Port: port,
RetryDelay: 10 * time.Second,
ExpectedStatusCode: 200,
timeout: 2 * time.Minute,
}
}
func (w *HTTPStrategy) WithTimeout(timeout time.Duration) *HTTPStrategy {
w.timeout = timeout
return w
}
func (w *HTTPStrategy) WithStatusCode(statusCode int) *HTTPStrategy {
w.ExpectedStatusCode = statusCode
return w
}
// WaitUntilReady implements Strategy.WaitUntilReady
func (w *HTTPStrategy) WaitUntilReady(ctx context.Context, target tcwait.StrategyTarget) (err error) {
ctx, cancel := context.WithTimeout(ctx, w.timeout)
defer cancel()
host, err := GetHost(ctx, target.(tc.Container))
if err != nil {
return
}
var mappedPort nat.Port
mappedPort, err = target.MappedPort(ctx, w.Port)
if err != nil {
return err
}
tripper := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
client := http.Client{Transport: tripper, Timeout: time.Second}
address := net.JoinHostPort(host, strconv.Itoa(mappedPort.Int()))
endpoint := url.URL{
Scheme: "http",
Host: address,
Path: w.Path,
}
var body []byte
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(100 * time.Millisecond):
state, err := target.State(ctx)
if err != nil {
return err
}
if !state.Running {
return fmt.Errorf("container is not running %s", state.Status)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), bytes.NewReader(body))
if err != nil {
return err
}
resp, err := client.Do(req)
if err != nil {
continue
}
if resp.StatusCode != w.ExpectedStatusCode {
_ = resp.Body.Close()
continue
}
if err := resp.Body.Close(); err != nil {
continue
}
return nil
}
}
}
type WebSocketStrategy struct {
Port nat.Port
RetryDelay time.Duration
timeout time.Duration
l zerolog.Logger
}
func NewWebSocketStrategy(port nat.Port, l zerolog.Logger) *WebSocketStrategy {
return &WebSocketStrategy{
Port: port,
RetryDelay: 10 * time.Second,
timeout: 2 * time.Minute,
}
}
func (w *WebSocketStrategy) WithTimeout(timeout time.Duration) *WebSocketStrategy {
w.timeout = timeout
return w
}
func (w *WebSocketStrategy) WaitUntilReady(ctx context.Context, target tcwait.StrategyTarget) (err error) {
var client *rpc.Client
var host string
ctx, cancel := context.WithTimeout(ctx, w.timeout)
defer cancel()
i := 0
for {
host, err = GetHost(ctx, target.(tc.Container))
if err != nil {
w.l.Error().Msg("Failed to get the target host")
return err
}
wsPort, err := target.MappedPort(ctx, w.Port)
if err != nil {
return err
}
url := fmt.Sprintf("ws://%s:%s", host, wsPort.Port())
w.l.Info().Msgf("Attempting to dial %s", url)
client, err = rpc.DialContext(ctx, url)
if err == nil {
client.Close()
w.l.Info().Msg("WebSocket rpc port is ready")
return nil
}
if client != nil {
client.Close() // Close client if DialContext failed
client = nil
}
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(w.RetryDelay):
i++
w.l.Info().Msgf("WebSocket attempt %d failed: %s. Retrying...", i, err)
}
}
}