Skip to content

Commit

Permalink
test: de-flake status command test (#629)
Browse files Browse the repository at this point in the history
Confirmed that the fix works because
```
$ go test -tags sqlite -run TestStatusCmd/server_type=read/case=block -count 1000 ./cmd/status
```
passed.
  • Loading branch information
zepatrik authored Jun 24, 2021
1 parent fe19f18 commit 3bcd0e3
Show file tree
Hide file tree
Showing 6 changed files with 1,053 additions and 638 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: 2.1

orbs:
sdk: ory/[email protected].41
sdk: ory/[email protected].43
changelog: ory/[email protected]
goreleaser: ory/[email protected]
slack: circleci/[email protected]
Expand Down
37 changes: 28 additions & 9 deletions cmd/status/root_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package status

import (
"bytes"
"context"
"net"
"strings"
Expand Down Expand Up @@ -51,16 +52,34 @@ func TestStatusCmd(t *testing.T) {
return s.Serve(l)
})

// dispatch start after one context timeout
go func() {
<-time.After(11 * time.Millisecond)
close(startServe)
}()
ctx := context.WithValue(context.Background(), client.ContextKeyTimeout, time.Millisecond)

ctx := context.WithValue(context.Background(), client.ContextKeyTimeout, 10*time.Millisecond)
stdOut := cmdx.ExecNoErrCtx(ctx, t, newStatusCmd(), "--"+FlagEndpoint, string(serverType), "--"+ts.FlagRemote, l.Addr().String(), "--"+FlagBlock)
assert.True(t, strings.HasPrefix(stdOut, "Context deadline exceeded, going to retry."))
assert.True(t, strings.HasSuffix(stdOut, "\n"+grpcHealthV1.HealthCheckResponse_SERVING.String()+"\n"))
stdIn, stdErr := &bytes.Buffer{}, &bytes.Buffer{}
stdOut := &cmdx.CallbackWriter{
Callbacks: map[string]func([]byte) error{
// once we get the first retry message, we want to start serving
"Context deadline exceeded, going to retry.": func([]byte) error {
// select ensures we only call this if the chan is not already closed
select {
case <-startServe:
default:
close(startServe)
}
return nil
},
},
}

require.NoError(t,
cmdx.ExecBackgroundCtx(ctx, newStatusCmd(), stdIn, stdOut, stdErr,
"--"+FlagEndpoint, string(serverType),
"--"+ts.FlagRemote, l.Addr().String(),
"--"+FlagBlock,
).Wait(),
)

fullStdOut := stdOut.String()
assert.True(t, strings.HasSuffix(fullStdOut, grpcHealthV1.HealthCheckResponse_SERVING.String()+"\n"), fullStdOut)

s.GracefulStop()
require.NoError(t, serveErr.Wait())
Expand Down
Loading

0 comments on commit 3bcd0e3

Please sign in to comment.