Skip to content

Commit

Permalink
Added testing for context
Browse files Browse the repository at this point in the history
  • Loading branch information
anuptalwalkar committed Nov 18, 2016
1 parent fa47d72 commit 437a91d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
12 changes: 9 additions & 3 deletions service/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,24 @@ package service

import gcontext "context"

// Context embeds Host and go context for use
// Context embedds Host and go context for use
type Context interface {
gcontext.Context
Host

Resource(key string) interface{}
SetResource(key string, value interface{})
}

type context struct {
gcontext.Context
Host

resources map[string]interface{}
}

var _ Context = &context{}

// NewContext always returns service.Context for use in the service
func NewContext(ctx gcontext.Context, host Host) Context {
return &context{
Expand All @@ -45,13 +51,13 @@ func NewContext(ctx gcontext.Context, host Host) Context {

// Resources returns resources associated with the current context
func (c *context) Resource(key string) interface{} {
if res, ok := c.tryResource(key); ok {
if res, ok := c.TryResource(key); ok {
return res
}
return nil
}

func (c *context) tryResource(key string) (interface{}, bool) {
func (c *context) TryResource(key string) (interface{}, bool) {
res, ok := c.resources[key]
return res, ok
}
Expand Down
56 changes: 56 additions & 0 deletions service/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package service

import (
"testing"

"github.com/stretchr/testify/assert"

gcontext "context"
)

type _testStruct struct {
data string
}

func TestContext_Simple(t *testing.T) {
ctx := NewContext(gcontext.Background(), NullHost())
testStruct := _testStruct{
data: "hello",
}
ctx.SetResource("resource", testStruct)

assert.NotNil(t, ctx.Resource("resource"))
assert.Equal(t, "hello", ctx.Resource("resource").(_testStruct).data)
}

func TestContext_NilResource(t *testing.T) {
ctx := NewContext(gcontext.Background(), NullHost())

assert.Nil(t, ctx.Resource("resource"))
}

func TestContext_HostAccess(t *testing.T) {
ctx := NewContext(gcontext.Background(), NullHost())
assert.NotNil(t, ctx.Config())
assert.Equal(t, "dummy", ctx.Name())
}

0 comments on commit 437a91d

Please sign in to comment.