Skip to content

Commit

Permalink
refactor: unexpose NewContext() method
Browse files Browse the repository at this point in the history
  • Loading branch information
DarthPestilane committed Oct 31, 2023
1 parent d598798 commit d249ea5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 38 deletions.
16 changes: 9 additions & 7 deletions router_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ import (
"time"
)

// NewContext creates a routeContext pointer.
func NewContext() *routeContext {
return &routeContext{
rawCtx: context.Background(),
}
}

// Context is a generic context in a message routing.
// It allows us to pass variables between handler and middlewares.
type Context interface {
Expand Down Expand Up @@ -77,6 +70,15 @@ type Context interface {
Copy() Context
}

var _ Context = &routeContext{} // implementation check

// newContext creates a routeContext pointer.
func newContext() *routeContext {
return &routeContext{
rawCtx: context.Background(),
}
}

// routeContext implements the Context interface.
type routeContext struct {
rawCtx context.Context
Expand Down
60 changes: 30 additions & 30 deletions router_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,33 @@ import (
"testing"
)

func newContext(sess *session, reqMsg *Message) *routeContext {
ctx := NewContext()
func newTestContext(sess *session, reqMsg *Message) *routeContext {
ctx := newContext()
ctx.session = sess
ctx.reqMsg = reqMsg
return ctx
}

func Test_routeContext_Deadline(t *testing.T) {
c := newContext(nil, nil)
c := newTestContext(nil, nil)
dl, ok := c.Deadline()
assert.False(t, ok)
assert.Zero(t, dl)
}

func Test_routeContext_Done(t *testing.T) {
c := newContext(nil, nil)
c := newTestContext(nil, nil)
done := c.Done()
assert.Nil(t, done)
}

func Test_routeContext_Err(t *testing.T) {
c := newContext(nil, nil)
c := newTestContext(nil, nil)
assert.Nil(t, c.Err())
}

func Test_routeContext_Value(t *testing.T) {
c := newContext(nil, nil)
c := newTestContext(nil, nil)
assert.Nil(t, c.Value("not found"))
c.Set("found", true)
assert.True(t, c.Value("found").(bool))
Expand All @@ -44,7 +44,7 @@ func Test_routeContext_Value(t *testing.T) {
}

func Test_routeContext_Get(t *testing.T) {
c := newContext(nil, nil)
c := newTestContext(nil, nil)
v, ok := c.Get("not found")
assert.False(t, ok)
assert.Nil(t, v)
Expand All @@ -56,15 +56,15 @@ func Test_routeContext_Get(t *testing.T) {
}

func Test_routeContext_Set(t *testing.T) {
c := newContext(nil, nil)
c := newTestContext(nil, nil)
c.Set("found", true)
v, ok := c.storage["found"]
assert.True(t, ok)
assert.True(t, v.(bool))
}

func Test_routeContext_Remove(t *testing.T) {
c := newContext(nil, nil)
c := newTestContext(nil, nil)
c.Set("found", true)
c.Remove("found")
v, ok := c.Get("found")
Expand All @@ -77,7 +77,7 @@ func Test_routeContext_Bind(t *testing.T) {
reqMsg := NewMessage(1, []byte(`{"data":"test"}`))
sess := newSession(nil, &sessionOption{Codec: &JsonCodec{}})

c := newContext(sess, reqMsg)
c := newTestContext(sess, reqMsg)
data := make(map[string]string)
assert.NoError(t, c.Bind(&data))
assert.EqualValues(t, data["data"], "test")
Expand All @@ -90,7 +90,7 @@ func Test_routeContext_Bind(t *testing.T) {
reqMsg := NewMessage(1, []byte("test"))
sess := newSession(nil, &sessionOption{})

c := newContext(sess, reqMsg)
c := newTestContext(sess, reqMsg)
var data string
assert.Error(t, c.Bind(&data))
assert.Empty(t, data)
Expand All @@ -100,7 +100,7 @@ func Test_routeContext_Bind(t *testing.T) {
func Test_routeContext_Session(t *testing.T) {
sess := newSession(nil, &sessionOption{})

c := newContext(sess, nil)
c := newTestContext(sess, nil)
assert.Equal(t, c.Session(), sess)
}

Expand All @@ -109,7 +109,7 @@ func Test_routeContext_SetResponse(t *testing.T) {
reqMsg := NewMessage(1, []byte("test"))
sess := newSession(nil, &sessionOption{})

c := newContext(sess, reqMsg)
c := newTestContext(sess, reqMsg)
err := c.SetResponse(1, []string{"invalid", "data"})
assert.Error(t, err)
assert.Nil(t, c.respMsg)
Expand All @@ -123,7 +123,7 @@ func Test_routeContext_SetResponse(t *testing.T) {
codec.EXPECT().Encode(gomock.Any()).Return(nil, fmt.Errorf("some err"))
sess := newSession(nil, &sessionOption{Codec: codec})

c := newContext(sess, reqMsg)
c := newTestContext(sess, reqMsg)
err := c.SetResponse(1, "test")
assert.Error(t, err)
assert.Nil(t, c.respMsg)
Expand All @@ -136,7 +136,7 @@ func Test_routeContext_SetResponse(t *testing.T) {
codec.EXPECT().Encode(gomock.Any()).Return([]byte("test"), nil)
sess := newSession(nil, &sessionOption{Codec: codec})

c := newContext(sess, reqMsg)
c := newTestContext(sess, reqMsg)
err := c.SetResponse(1, "test")
assert.NoError(t, err)
assert.Equal(t, c.respMsg, reqMsg)
Expand All @@ -146,7 +146,7 @@ func Test_routeContext_SetResponse(t *testing.T) {
func Test_routeContext_Send(t *testing.T) {
t.Run("when success", func(t *testing.T) {
sess := newSession(nil, &sessionOption{})
ctx := newContext(sess, nil)
ctx := newTestContext(sess, nil)
ctx.SetResponseMessage(NewMessage(1, []byte("test")))
go ctx.Send()
ctx2 := <-sess.respQueue
Expand All @@ -158,7 +158,7 @@ func Test_routeContext_SendTo(t *testing.T) {
t.Run("when success", func(t *testing.T) {
sess1 := newSession(nil, &sessionOption{})
sess2 := newSession(nil, &sessionOption{})
ctx := newContext(sess1, nil)
ctx := newTestContext(sess1, nil)
ctx.SetResponseMessage(NewMessage(1, []byte("test")))
go ctx.SendTo(sess2)
ctx2 := <-sess2.respQueue
Expand All @@ -169,7 +169,7 @@ func Test_routeContext_SendTo(t *testing.T) {
func Test_routeContext_reset(t *testing.T) {
sess := newSession(nil, &sessionOption{})
reqMsg := NewMessage(1, []byte("test"))
ctx := newContext(sess, reqMsg)
ctx := newTestContext(sess, reqMsg)
ctx.reset()
assert.Equal(t, ctx.rawCtx, context.Background())
assert.Nil(t, ctx.session)
Expand All @@ -179,7 +179,7 @@ func Test_routeContext_reset(t *testing.T) {
}

func Test_routeContext_Copy(t *testing.T) {
ctx := newContext(nil, nil)
ctx := newTestContext(nil, nil)
ctx.SetResponseMessage(NewMessage(1, []byte("resp origin")))

ctx2 := ctx.Copy()
Expand All @@ -196,7 +196,7 @@ func Test_routeContext_MustSetResponse(t *testing.T) {
reqMsg := NewMessage(1, []byte("test"))
sess := newSession(nil, &sessionOption{})

c := newContext(sess, reqMsg)
c := newTestContext(sess, reqMsg)
assert.Panics(t, func() {
c.MustSetResponse(1, []string{"invalid", "data"})
})
Expand All @@ -210,7 +210,7 @@ func Test_routeContext_MustSetResponse(t *testing.T) {
codec.EXPECT().Encode(gomock.Any()).Return(nil, fmt.Errorf("some err"))
sess := newSession(nil, &sessionOption{Codec: codec})

c := newContext(sess, reqMsg)
c := newTestContext(sess, reqMsg)
assert.Panics(t, func() {
c.MustSetResponse(1, "test")
})
Expand All @@ -223,7 +223,7 @@ func Test_routeContext_MustSetResponse(t *testing.T) {
codec.EXPECT().Encode(gomock.Any()).Return([]byte("test"), nil)
sess := newSession(nil, &sessionOption{Codec: codec})

c := newContext(sess, reqMsg)
c := newTestContext(sess, reqMsg)
assert.NotPanics(t, func() {
assert.Equal(t, c.MustSetResponse(1, "test"), c)
})
Expand All @@ -232,15 +232,15 @@ func Test_routeContext_MustSetResponse(t *testing.T) {

func Test_routeContext_SetSession(t *testing.T) {
sess := newSession(nil, &sessionOption{})
c := newContext(nil, nil)
c := newTestContext(nil, nil)
assert.Equal(t, c.SetSession(sess), c)
assert.Equal(t, c.Session(), sess)
}

func Test_routeContext_SetRequest(t *testing.T) {
t.Run("when session hasn't codec", func(t *testing.T) {
sess := newSession(nil, &sessionOption{})
c := newContext(sess, nil)
c := newTestContext(sess, nil)
err := c.SetRequest(1, []string{"invalid", "data"})
assert.Error(t, err)
assert.Nil(t, c.reqMsg)
Expand All @@ -253,7 +253,7 @@ func Test_routeContext_SetRequest(t *testing.T) {
codec.EXPECT().Encode(gomock.Any()).Return(nil, fmt.Errorf("some err"))
sess := newSession(nil, &sessionOption{Codec: codec})

c := newContext(sess, nil)
c := newTestContext(sess, nil)
err := c.SetRequest(1, "test")
assert.Error(t, err)
assert.Nil(t, c.reqMsg)
Expand All @@ -266,7 +266,7 @@ func Test_routeContext_SetRequest(t *testing.T) {
codec.EXPECT().Encode(gomock.Any()).Return([]byte("test"), nil)
sess := newSession(nil, &sessionOption{Codec: codec})

c := newContext(sess, nil)
c := newTestContext(sess, nil)
err := c.SetRequest(1, "test")
assert.NoError(t, err)
assert.Equal(t, c.reqMsg, reqMsg)
Expand All @@ -277,7 +277,7 @@ func Test_routeContext_MustSetRequest(t *testing.T) {
t.Run("when session hasn't codec", func(t *testing.T) {
sess := newSession(nil, &sessionOption{})

c := newContext(sess, nil)
c := newTestContext(sess, nil)
assert.Panics(t, func() {
c.MustSetRequest(1, []string{"invalid", "data"})
})
Expand All @@ -290,7 +290,7 @@ func Test_routeContext_MustSetRequest(t *testing.T) {
codec.EXPECT().Encode(gomock.Any()).Return(nil, fmt.Errorf("some err"))
sess := newSession(nil, &sessionOption{Codec: codec})

c := newContext(sess, nil)
c := newTestContext(sess, nil)
assert.Panics(t, func() {
c.MustSetRequest(1, "test")
})
Expand All @@ -303,7 +303,7 @@ func Test_routeContext_MustSetRequest(t *testing.T) {
codec.EXPECT().Encode(gomock.Any()).Return([]byte("test"), nil)
sess := newSession(nil, &sessionOption{Codec: codec})

c := newContext(sess, nil)
c := newTestContext(sess, nil)
assert.NotPanics(t, func() {
assert.Equal(t, c.MustSetRequest(1, "test"), c)
})
Expand All @@ -312,7 +312,7 @@ func Test_routeContext_MustSetRequest(t *testing.T) {

func Test_routeContext_SetRequestMessage(t *testing.T) {
reqMsg := NewMessage(1, []byte("test"))
c := NewContext()
c := newContext()
c.SetRequestMessage(reqMsg)
assert.Equal(t, c.reqMsg, reqMsg)
}
2 changes: 1 addition & 1 deletion session.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func newSession(conn net.Conn, opt *sessionOption) *session {
respQueue: make(chan Context, opt.respQueueSize),
packer: opt.Packer,
codec: opt.Codec,
ctxPool: sync.Pool{New: func() interface{} { return NewContext() }},
ctxPool: sync.Pool{New: func() interface{} { return newContext() }},
asyncRouter: opt.asyncRouter,
}
}
Expand Down

0 comments on commit d249ea5

Please sign in to comment.