make newSendCtx also handle context.DeadlineExceeded (#4603)

* make newSendCtx also handle context.DeadlineExceeded

* add assertion to check that context is valid
This commit is contained in:
Sorawis Nilparuk (Bo) authored and GitHub committed 2024-04-19 09:23:40 -07:00
1 parent d72556fb97
commit 8f457c9c93
2 files changed
+25 -1

No files matched your search

+1 -1
View File
@@ -368,7 +368,7 @@ func newSendCtx(ctx context.Context) (context.Context, context.CancelFunc) {
switch {
case ctx == nil:
return context.Background(), nil
case ctx.Err() == context.Canceled:
case ctx.Err() == context.Canceled || ctx.Err() == context.DeadlineExceeded:
sendCtx, sendCancel = context.WithTimeout(context.Background(), cancelledSendTimeout)
info, ok := RequestInfoFromContext(ctx)
if ok {
+24
View File
@@ -6,6 +6,7 @@ package event
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -44,6 +45,28 @@ func Test_newSendCtx(t *testing.T) {
}(),
wantCancel: true,
},
{
name: "deadline-exceeded-with-info",
ctx: func() context.Context {
var err error
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(-1*time.Millisecond))
defer cancel()
ctx, err = NewRequestInfoContext(ctx, testInfo)
require.NoError(t, err)
return ctx
}(),
wantCancel: true,
wantInfo: true,
},
{
name: "deadline-exceeded-no-info",
ctx: func() context.Context {
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(-1*time.Millisecond))
defer cancel()
return ctx
}(),
wantCancel: true,
},
{
name: "no-info",
ctx: context.Background(),
@@ -65,6 +88,7 @@ func Test_newSendCtx(t *testing.T) {
ctx, cancel := newSendCtx(tt.ctx)
require.NotNil(ctx)
assert.True(ctx.Err() != context.Canceled)
assert.True(ctx.Err() != context.DeadlineExceeded)
if tt.wantCancel {
require.NotNil(cancel)
} else {