Skip to content

Commit

Permalink
bugfix: flaky TestClientRequestObjectsWithContext
Browse files Browse the repository at this point in the history
The patch makes the test more deterministic. It helps to avoid
canceling a request with an already received response.

Closes #244
  • Loading branch information
oleg-jukovec committed Dec 27, 2022
1 parent 091b938 commit 58c4f0b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.

- Decimal package uses a test variable DecimalPrecision instead of a
package-level variable decimalPrecision (#233)
- Flaky tests TestClientRequestObjectsWithContext and
TestClientIdRequestObjectWithContext (#244)

## [1.9.0] - 2022-11-02

Expand Down
46 changes: 44 additions & 2 deletions tarantool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2397,15 +2397,57 @@ func TestClientRequestObjectsWithPassedCanceledContext(t *testing.T) {
}
}

// waitCtxRequest waits for the WaitGroup in Body() call and returns
// the context from Ctx() call. The request helps us to make sure that
// the context's cancel() call is called before a response received.
type waitCtxRequest struct {
ctx context.Context
wg sync.WaitGroup
}

func (req *waitCtxRequest) Code() int32 {
return NewPingRequest().Code()
}

func (req *waitCtxRequest) Body(res SchemaResolver, enc *encoder) error {
req.wg.Wait()
return NewPingRequest().Body(res, enc)
}

func (req *waitCtxRequest) Ctx() context.Context {
return req.ctx
}

func (req *waitCtxRequest) Async() bool {
return NewPingRequest().Async()
}

func TestClientRequestObjectsWithContext(t *testing.T) {
var err error
conn := test_helpers.ConnectWithValidation(t, server, opts)
defer conn.Close()

ctx, cancel := context.WithCancel(context.Background())
req := NewPingRequest().Context(ctx)
fut := conn.Do(req)
req := &waitCtxRequest{ctx: ctx}
req.wg.Add(1)

var futWg sync.WaitGroup
var fut *Future

futWg.Add(1)
go func() {
defer futWg.Done()
fut = conn.Do(req)
}()

cancel()
req.wg.Done()

futWg.Wait()
if fut == nil {
t.Fatalf("fut must be not nil")
}

resp, err := fut.Get()
if resp != nil {
t.Fatalf("response must be nil")
Expand Down

0 comments on commit 58c4f0b

Please sign in to comment.