Skip to content

Commit

Permalink
Add BorrowContext
Browse files Browse the repository at this point in the history
  • Loading branch information
workanator committed Sep 21, 2017
1 parent 87a59b9 commit a0e39bf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
18 changes: 15 additions & 3 deletions context_impl.go
Expand Up @@ -10,13 +10,25 @@ type flowContext struct {
mu sync.RWMutex
}

// BorrowContext constructs new instance from the context given.
// The function panics if the context given is nil.
func BorrowContext(ctx context.Context) Context {
if ctx == nil {
panic("context is nil")
}

return &flowContext{
ctx: ctx,
mu: sync.RWMutex{},
}
}

// NewContext constructs new instance of TODO context.
func NewContext() Context {
ctx := &flowContext{
return &flowContext{
ctx: context.TODO(),
mu: sync.RWMutex{},
}

return ctx
}

// Release releases resources.
Expand Down
15 changes: 15 additions & 0 deletions context_impl_test.go
Expand Up @@ -6,6 +6,21 @@ import (
"testing"
)

func TestBorrowContext(t *testing.T) {
ctx := BorrowContext(context.Background())
ctx.Release()
}

func TestBorrowContext_Panic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatalf("%s expects panic", t.Name())
}
}()

BorrowContext(nil)
}

func TestNewContext(t *testing.T) {
ctx := NewContext()
ctx.Release()
Expand Down

0 comments on commit a0e39bf

Please sign in to comment.