-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwait.go
77 lines (60 loc) · 1.7 KB
/
wait.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
package redis
import (
"context"
"fmt"
"time"
redis_db "github.com/redis/go-redis/v9"
"github.com/testcontainers/testcontainers-go/wait"
)
const (
defaultStartupTimeout = 30 * time.Second
defaultPollInterval = 100 * time.Millisecond
)
type redisWaitStrategy struct{}
var _ wait.Strategy = (*redisWaitStrategy)(nil)
func newRedisWaitStrategy() redisWaitStrategy {
return redisWaitStrategy{}
}
func (s redisWaitStrategy) WaitUntilReady(ctx context.Context, target wait.StrategyTarget) (err error) {
ctx, cancel := context.WithTimeout(ctx, defaultStartupTimeout)
defer cancel()
if err := wait.NewHostPortStrategy(redisServicePort).WaitUntilReady(ctx, target); err != nil {
return fmt.Errorf("error waiting for port to open: %w", err)
}
host, err := target.Host(ctx)
if err != nil {
return fmt.Errorf("failed to fetch host: %w", err)
}
port, err := target.MappedPort(ctx, redisServicePort)
if err != nil {
return fmt.Errorf("failed to fetch port: %w", err)
}
return s.pollUntilReady(ctx, host, port.Int())
}
func (s redisWaitStrategy) pollUntilReady(ctx context.Context, host string, port int) error {
for {
select {
case <-ctx.Done():
return fmt.Errorf("timed out while waiting for Redis to start: %w", ctx.Err())
case <-time.After(defaultPollInterval):
isReady, err := s.isReady(ctx, host, port)
if err != nil {
return err
}
if isReady {
return nil
}
}
}
}
func (s redisWaitStrategy) isReady(ctx context.Context, host string, port int) (bool, error) {
client := redis_db.NewClient(&redis_db.Options{
Addr: fmt.Sprintf("%s:%d", host, port),
})
defer client.Close()
_, err := client.Ping(ctx).Result()
if err != nil {
return false, nil
}
return true, nil
}