Skip to content

Commit 26fd8e2

Browse files
committed
sdk/testing: simplify WaitForReady
Signed-off-by: Dr. Stefan Schimanski <stefan.schimanski@gmail.com>
1 parent 2cdb296 commit 26fd8e2

File tree

3 files changed

+51
-30
lines changed

3 files changed

+51
-30
lines changed

sdk/testing/kcp.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,12 @@ func SharedKcpServer(t *testing.T) kcptestingserver.RunningServer {
8484
ctx, cancel := context.WithCancel(context.Background())
8585
t.Cleanup(cancel)
8686

87-
err = kcptestingserver.WaitForReady(ctx, t, s.RootShardSystemMasterBaseConfig(t), true)
88-
require.NoError(t, err, "error waiting for readiness")
87+
rootCfg := s.RootShardSystemMasterBaseConfig(t)
88+
t.Logf("Waiting for readiness for server at %s", rootCfg.Host)
89+
err = kcptestingserver.WaitForReady(ctx, rootCfg)
90+
require.NoError(t, err, "external server is not ready")
91+
92+
kcptestingserver.MonitorEndpoints(t, rootCfg, "/livez", "/readyz")
8993

9094
return s
9195
}

sdk/testing/server/fixture.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,18 @@ func NewFixture(t *testing.T, cfgs ...Config) Fixture {
112112
return err
113113
}
114114

115-
return WaitForReady(srv.ctx, t, srv.RootShardSystemMasterBaseConfig(t), !cfgs[i].RunInProcess)
115+
rootCfg := srv.RootShardSystemMasterBaseConfig(t)
116+
t.Logf("Waiting for readiness for server at %s", rootCfg.Host)
117+
if err := WaitForReady(srv.ctx, rootCfg); err != nil {
118+
return err
119+
}
120+
121+
if !cfgs[i].RunInProcess {
122+
rootCfg := srv.RootShardSystemMasterBaseConfig(t)
123+
MonitorEndpoints(t, rootCfg, "/livez", "/readyz")
124+
}
125+
126+
return nil
116127
})
117128
}
118129
err := g.Wait()

sdk/testing/server/ready.go

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"errors"
2222
"fmt"
2323
"strings"
24-
"sync"
2524
"testing"
2625
"time"
2726

@@ -31,9 +30,8 @@ import (
3130
"k8s.io/client-go/rest"
3231
)
3332

34-
func WaitForReady(ctx context.Context, t *testing.T, cfg *rest.Config, keepMonitoring bool) error {
35-
t.Logf("waiting for readiness for server at %s", cfg.Host)
36-
33+
// WaitForReady waits for /livez and then /readyz to return success.
34+
func WaitForReady(ctx context.Context, cfg *rest.Config) error {
3735
cfg = rest.CopyConfig(cfg)
3836
if cfg.NegotiatedSerializer == nil {
3937
cfg.NegotiatedSerializer = kubernetesscheme.Codecs.WithoutConversion()
@@ -44,45 +42,53 @@ func WaitForReady(ctx context.Context, t *testing.T, cfg *rest.Config, keepMonit
4442
return fmt.Errorf("failed to create unversioned client: %w", err)
4543
}
4644

47-
wg := sync.WaitGroup{}
48-
wg.Add(2)
49-
for _, endpoint := range []string{"/livez", "/readyz"} {
50-
go func(endpoint string) {
51-
defer wg.Done()
52-
waitForEndpoint(ctx, t, client, endpoint)
53-
}(endpoint)
45+
if err := waitForEndpoint(ctx, client, "/livez"); err != nil {
46+
return fmt.Errorf("server at %s didn't become ready: %w", cfg.Host, err)
5447
}
55-
wg.Wait()
56-
t.Logf("server at %s is ready", cfg.Host)
57-
58-
if keepMonitoring {
59-
for _, endpoint := range []string{"/livez", "/readyz"} {
60-
go func(endpoint string) {
61-
monitorEndpoint(ctx, t, client, endpoint)
62-
}(endpoint)
63-
}
48+
if err := waitForEndpoint(ctx, client, "/readyz"); err != nil {
49+
return fmt.Errorf("server at %s didn't become ready: %w", cfg.Host, err)
6450
}
51+
6552
return nil
6653
}
6754

68-
func waitForEndpoint(ctx context.Context, t *testing.T, client *rest.RESTClient, endpoint string) {
55+
func waitForEndpoint(ctx context.Context, client *rest.RESTClient, endpoint string) error {
6956
var lastError error
7057
if err := wait.PollUntilContextTimeout(ctx, 100*time.Millisecond, time.Minute, true, func(ctx context.Context) (bool, error) {
7158
req := rest.NewRequest(client).RequestURI(endpoint)
72-
_, err := req.Do(ctx).Raw()
73-
if err != nil {
59+
if _, err := req.Do(ctx).Raw(); err != nil {
7460
lastError = fmt.Errorf("error contacting %s: failed components: %v", req.URL(), unreadyComponentsFromError(err))
7561
return false, nil
7662
}
77-
78-
t.Logf("success contacting %s", req.URL())
7963
return true, nil
8064
}); err != nil && lastError != nil {
81-
t.Error(lastError)
65+
return lastError
66+
}
67+
return nil
68+
}
69+
70+
// MonitorEndpoints keeps watching the given endpoints and fails t on error.
71+
func MonitorEndpoints(t *testing.T, client *rest.Config, endpoints ...string) {
72+
ctx, cancel := context.WithCancel(context.Background())
73+
t.Cleanup(cancel)
74+
for _, endpoint := range endpoints {
75+
go func(endpoint string) {
76+
monitorEndpoint(ctx, t, client, endpoint)
77+
}(endpoint)
8278
}
8379
}
8480

85-
func monitorEndpoint(ctx context.Context, t *testing.T, client *rest.RESTClient, endpoint string) {
81+
func monitorEndpoint(ctx context.Context, t *testing.T, cfg *rest.Config, endpoint string) {
82+
cfg = rest.CopyConfig(cfg)
83+
if cfg.NegotiatedSerializer == nil {
84+
cfg.NegotiatedSerializer = kubernetesscheme.Codecs.WithoutConversion()
85+
}
86+
client, err := rest.UnversionedRESTClientFor(cfg)
87+
if err != nil {
88+
t.Errorf("failed to create unversioned client: %v", err)
89+
return
90+
}
91+
8692
// we need a shorter deadline than the server, or else:
8793
// timeout.go:135] post-timeout activity - time-elapsed: 23.784917ms, GET "/livez" result: Header called after Handler finished
8894
if deadline, ok := t.Deadline(); ok {

0 commit comments

Comments
 (0)