Skip to content

Commit 58c4f0b

Browse files
committed
bugfix: flaky TestClientRequestObjectsWithContext
The patch makes the test more deterministic. It helps to avoid canceling a request with an already received response. Closes #244
1 parent 091b938 commit 58c4f0b

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
2222

2323
- Decimal package uses a test variable DecimalPrecision instead of a
2424
package-level variable decimalPrecision (#233)
25+
- Flaky tests TestClientRequestObjectsWithContext and
26+
TestClientIdRequestObjectWithContext (#244)
2527

2628
## [1.9.0] - 2022-11-02
2729

tarantool_test.go

+44-2
Original file line numberDiff line numberDiff line change
@@ -2397,15 +2397,57 @@ func TestClientRequestObjectsWithPassedCanceledContext(t *testing.T) {
23972397
}
23982398
}
23992399

2400+
// waitCtxRequest waits for the WaitGroup in Body() call and returns
2401+
// the context from Ctx() call. The request helps us to make sure that
2402+
// the context's cancel() call is called before a response received.
2403+
type waitCtxRequest struct {
2404+
ctx context.Context
2405+
wg sync.WaitGroup
2406+
}
2407+
2408+
func (req *waitCtxRequest) Code() int32 {
2409+
return NewPingRequest().Code()
2410+
}
2411+
2412+
func (req *waitCtxRequest) Body(res SchemaResolver, enc *encoder) error {
2413+
req.wg.Wait()
2414+
return NewPingRequest().Body(res, enc)
2415+
}
2416+
2417+
func (req *waitCtxRequest) Ctx() context.Context {
2418+
return req.ctx
2419+
}
2420+
2421+
func (req *waitCtxRequest) Async() bool {
2422+
return NewPingRequest().Async()
2423+
}
2424+
24002425
func TestClientRequestObjectsWithContext(t *testing.T) {
24012426
var err error
24022427
conn := test_helpers.ConnectWithValidation(t, server, opts)
24032428
defer conn.Close()
24042429

24052430
ctx, cancel := context.WithCancel(context.Background())
2406-
req := NewPingRequest().Context(ctx)
2407-
fut := conn.Do(req)
2431+
req := &waitCtxRequest{ctx: ctx}
2432+
req.wg.Add(1)
2433+
2434+
var futWg sync.WaitGroup
2435+
var fut *Future
2436+
2437+
futWg.Add(1)
2438+
go func() {
2439+
defer futWg.Done()
2440+
fut = conn.Do(req)
2441+
}()
2442+
24082443
cancel()
2444+
req.wg.Done()
2445+
2446+
futWg.Wait()
2447+
if fut == nil {
2448+
t.Fatalf("fut must be not nil")
2449+
}
2450+
24092451
resp, err := fut.Get()
24102452
if resp != nil {
24112453
t.Fatalf("response must be nil")

0 commit comments

Comments
 (0)