@@ -50,7 +50,7 @@ type Member struct {
5050}
5151
5252var contextDoneErrRegexp = regexp .MustCompile (
53- fmt . Sprintf ( `^context is done \(request ID [0-9]+\) \(%s\)$` , " context canceled" ) )
53+ `^context is done \(request ID [0-9]+\): context canceled$` )
5454
5555func (m * Member ) EncodeMsgpack (e * msgpack.Encoder ) error {
5656 if err := e .EncodeArrayLen (2 ); err != nil {
@@ -2739,13 +2739,40 @@ func TestClientRequestObjectsWithPassedCanceledContext(t *testing.T) {
27392739 if ! contextDoneErrRegexp .Match ([]byte (err .Error ())) {
27402740 t .Fatalf ("Failed to catch an error from done context" )
27412741 }
2742- // Checking that we could use errors.Is to get known about error.
2743- require .True (t , errors .Is (err , context .Canceled ))
27442742 if resp != nil {
27452743 t .Fatalf ("Response is not nil after the occurred error" )
27462744 }
27472745}
27482746
2747+ func TestComparableCanceledContextErrors (t * testing.T ) {
2748+ conn := test_helpers .ConnectWithValidation (t , dialer , opts )
2749+ defer conn .Close ()
2750+
2751+ // Checking comparable with simple context.WithCancel.
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+ // Checking comparable with simple context.WithTimeout.
2759+ timeout := time .Nanosecond
2760+ ctx , cancel = context .WithTimeout (context .Background (), timeout )
2761+ req = NewPingRequest ().Context (ctx )
2762+ defer cancel ()
2763+ _ , err = conn .Do (req ).Get ()
2764+ require .True (t , errors .Is (err , context .DeadlineExceeded ), err .Error ())
2765+
2766+ // Checking comparable with context.WithCancelCause.
2767+ // Shows ability to compare with custom errors (also with ClientError).
2768+ ctxCause , cancelCause := context .WithCancelCause (context .Background ())
2769+ req = NewPingRequest ().Context (ctxCause )
2770+ cancelCause (ClientError {ErrConnectionClosed , "something went wrong" })
2771+ _ , err = conn .Do (req ).Get ()
2772+ var tmpErr ClientError
2773+ require .True (t , errors .As (err , & tmpErr ), tmpErr .Error ())
2774+ }
2775+
27492776// waitCtxRequest waits for the WaitGroup in Body() call and returns
27502777// the context from Ctx() call. The request helps us to make sure that
27512778// the context's cancel() call is called before a response received.
@@ -3302,7 +3329,6 @@ func TestClientIdRequestObjectWithPassedCanceledContext(t *testing.T) {
33023329 resp , err := conn .Do (req ).Get ()
33033330 require .Nilf (t , resp , "Response is empty" )
33043331 require .NotNilf (t , err , "Error is not empty" )
3305- require .True (t , errors .Is (err , context .Canceled ))
33063332 require .Regexp (t , contextDoneErrRegexp , err .Error ())
33073333}
33083334
0 commit comments