From d249ea53efb245ccc4e563b2accdee84a43cef2e Mon Sep 17 00:00:00 2001 From: zxl Date: Tue, 31 Oct 2023 16:56:27 +0800 Subject: [PATCH] refactor: unexpose NewContext() method --- router_context.go | 16 ++++++----- router_context_test.go | 60 +++++++++++++++++++++--------------------- session.go | 2 +- 3 files changed, 40 insertions(+), 38 deletions(-) diff --git a/router_context.go b/router_context.go index 9e455db..5024af5 100644 --- a/router_context.go +++ b/router_context.go @@ -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 { @@ -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 diff --git a/router_context_test.go b/router_context_test.go index 46dd15a..89bd6ed 100644 --- a/router_context_test.go +++ b/router_context_test.go @@ -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)) @@ -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) @@ -56,7 +56,7 @@ 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) @@ -64,7 +64,7 @@ func Test_routeContext_Set(t *testing.T) { } 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") @@ -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") @@ -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) @@ -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) } @@ -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) @@ -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) @@ -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) @@ -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 @@ -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 @@ -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) @@ -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() @@ -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"}) }) @@ -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") }) @@ -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) }) @@ -232,7 +232,7 @@ 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) } @@ -240,7 +240,7 @@ func Test_routeContext_SetSession(t *testing.T) { 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) @@ -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) @@ -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) @@ -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"}) }) @@ -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") }) @@ -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) }) @@ -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) } diff --git a/session.go b/session.go index 435ce6f..a079de5 100644 --- a/session.go +++ b/session.go @@ -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, } }