Skip to content
Open
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: 4 additions & 0 deletions dcontext/hardsoft.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ func HardContext(softCtx context.Context) context.Context {
if parentHardCtx == nil {
return softCtx
}
// If it's already a hard context, just return it as is.
if hardCtx, ok := softCtx.(childHardContext); ok {
return hardCtx
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused, because I'd have told you that the above if parentHardCtx == nil case would cover this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh, I get it, it's the same type of bug as #26

return childHardContext{
hardCtx: parentHardCtx.(context.Context),
softCtx: softCtx,
Expand Down
29 changes: 29 additions & 0 deletions dcontext/hardsoft_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dcontext_test

import (
"context"
"testing"

"github.com/datawire/dlib/dcontext"
)

type valkey struct{}

func TestContextIdentity(t *testing.T) {
ctx := context.Background()
if ctx != dcontext.HardContext(ctx) {
t.Fatalf("background context %+v treated differently than hard context %+v", ctx, dcontext.HardContext(ctx))
}
softCtx := dcontext.WithSoftness(ctx)
if ctx == softCtx {
t.Fatalf("background context %+v treated same as soft context %+v", ctx, softCtx)
}
ctx = context.WithValue(softCtx, valkey{}, 0)
hardCtx := dcontext.HardContext(ctx)
if ctx == hardCtx {
t.Fatalf("soft context %+v treated same as hard context %+v", ctx, hardCtx)
}
if hardCtx != dcontext.HardContext(hardCtx) {
t.Fatalf("hard context %+v treated differently than hard context %+v", hardCtx, dcontext.HardContext(hardCtx))
}
}