@@ -3,6 +3,7 @@ package tarantool_test
33import (
44 "context"
55 "encoding/binary"
6+ "errors"
67 "fmt"
78 "io"
89 "log"
@@ -48,7 +49,8 @@ type Member struct {
4849 Val uint
4950}
5051
51- var contextDoneErrRegexp = regexp .MustCompile (`^context is done \(request ID [0-9]+\)$` )
52+ var contextDoneErrRegexp = regexp .MustCompile (
53+ `^context is done \(request ID [0-9]+\): context canceled$` )
5254
5355func (m * Member ) EncodeMsgpack (e * msgpack.Encoder ) error {
5456 if err := e .EncodeArrayLen (2 ); err != nil {
@@ -2742,6 +2744,45 @@ func TestClientRequestObjectsWithPassedCanceledContext(t *testing.T) {
27422744 }
27432745}
27442746
2747+ // Checking comparable with simple context.WithCancel.
2748+ func TestComparableErrorsCanceledContext (t * testing.T ) {
2749+ conn := test_helpers .ConnectWithValidation (t , dialer , opts )
2750+ defer conn .Close ()
2751+
2752+ ctx , cancel := context .WithCancel (context .Background ())
2753+ req := NewPingRequest ().Context (ctx )
2754+ cancel ()
2755+ _ , err := conn .Do (req ).Get ()
2756+ require .True (t , errors .Is (err , context .Canceled ), err .Error ())
2757+ }
2758+
2759+ // Checking comparable with simple context.WithTimeout.
2760+ func TestComparableErrorsTimeoutContext (t * testing.T ) {
2761+ conn := test_helpers .ConnectWithValidation (t , dialer , opts )
2762+ defer conn .Close ()
2763+
2764+ timeout := time .Nanosecond
2765+ ctx , cancel := context .WithTimeout (context .Background (), timeout )
2766+ req := NewPingRequest ().Context (ctx )
2767+ defer cancel ()
2768+ _ , err := conn .Do (req ).Get ()
2769+ require .True (t , errors .Is (err , context .DeadlineExceeded ), err .Error ())
2770+ }
2771+
2772+ // Checking comparable with context.WithCancelCause.
2773+ // Shows ability to compare with custom errors (also with ClientError).
2774+ func TestComparableErrorsCancelCauseContext (t * testing.T ) {
2775+ conn := test_helpers .ConnectWithValidation (t , dialer , opts )
2776+ defer conn .Close ()
2777+
2778+ ctxCause , cancelCause := context .WithCancelCause (context .Background ())
2779+ req := NewPingRequest ().Context (ctxCause )
2780+ cancelCause (ClientError {ErrConnectionClosed , "something went wrong" })
2781+ _ , err := conn .Do (req ).Get ()
2782+ var tmpErr ClientError
2783+ require .True (t , errors .As (err , & tmpErr ), tmpErr .Error ())
2784+ }
2785+
27452786// waitCtxRequest waits for the WaitGroup in Body() call and returns
27462787// the context from Ctx() call. The request helps us to make sure that
27472788// the context's cancel() call is called before a response received.
0 commit comments