Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correcting the misuse of the context in the copyContextWithCustomTime… #1941

Merged
merged 2 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/download/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func union(list1, list2 []string) []string {

func copyContextWithCustomTimeout(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc) {
ctxCopy, cancel := context.WithTimeout(context.Background(), timeout)
requestid.SetInContext(ctxCopy, requestid.FromContext(ctx))
log.SetEntryInContext(ctxCopy, log.EntryFromContext(ctx))
ctxCopy = requestid.SetInContext(ctxCopy, requestid.FromContext(ctx))
ctxCopy = log.SetEntryInContext(ctxCopy, log.EntryFromContext(ctx))
kkHAIKE marked this conversation as resolved.
Show resolved Hide resolved
return ctxCopy, cancel
}
36 changes: 36 additions & 0 deletions pkg/download/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/gomods/athens/pkg/download/mode"
"github.com/gomods/athens/pkg/errors"
"github.com/gomods/athens/pkg/index/nop"
"github.com/gomods/athens/pkg/log"
"github.com/gomods/athens/pkg/module"
"github.com/gomods/athens/pkg/stash"
"github.com/gomods/athens/pkg/storage"
Expand Down Expand Up @@ -495,3 +496,38 @@ func (ml *mockLister) List(ctx context.Context, mod string) (*storage.RevInfo, [
ml.called = true
return nil, ml.list, ml.err
}

type testEntry struct {
msg string
}

var _ log.Entry = &testEntry{}

func (e *testEntry) Debugf(format string, args ...any) {
e.msg = format
}
func (*testEntry) Infof(format string, args ...any) {}
func (*testEntry) Warnf(format string, args ...any) {}
func (*testEntry) Errorf(format string, args ...any) {}
func (*testEntry) WithFields(fields map[string]any) log.Entry { return nil }
func (*testEntry) SystemErr(err error) {}

func Test_copyContextWithCustomTimeout(t *testing.T) {
testEntry := &testEntry{}

// create a context with a logger entry
logctx := log.SetEntryInContext(context.Background(), testEntry)

// check the log work as expected
log.EntryFromContext(logctx).Debugf("first test")
require.Equal(t, "first test", testEntry.msg)

// use copyContextWithCustomTimeout to create a new context with a custom timeout,
// and the returned context should have the same logger entry
newCtx, cancel := copyContextWithCustomTimeout(logctx, 10*time.Second)
defer cancel()

// check the log work as expected
log.EntryFromContext(newCtx).Debugf("second test")
require.Equal(t, "second test", testEntry.msg)
}