From d29bcdde7aa217a479569e5c7ebe92392a3c8bc3 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Sun, 24 Nov 2024 17:37:51 +0900 Subject: [PATCH 1/2] Add Prepend() for prepending mathcher --- grpcstub.go | 23 ++++++++++++++++++++--- grpcstub_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/grpcstub.go b/grpcstub.go index 92af08b..bd772b5 100644 --- a/grpcstub.go +++ b/grpcstub.go @@ -134,6 +134,7 @@ type Server struct { healthCheck bool disableReflection bool status serverStatus + prependOnce bool t TB mu sync.RWMutex } @@ -326,7 +327,7 @@ func (s *Server) Match(fn func(req *Request) bool) *matcher { } s.mu.Lock() defer s.mu.Unlock() - s.matchers = append(s.matchers, m) + s.addMatcher(m) return m } @@ -347,7 +348,7 @@ func (s *Server) Service(service string) *matcher { matchFuncs: []matchFunc{fn}, t: s.t, } - s.matchers = append(s.matchers, m) + s.addMatcher(m) return m } @@ -379,7 +380,7 @@ func (s *Server) Method(method string) *matcher { matchFuncs: []matchFunc{fn}, t: s.t, } - s.matchers = append(s.matchers, m) + s.addMatcher(m) return m } @@ -517,6 +518,13 @@ func (s *Server) ClearMatchers() { s.matchers = nil } +func (s *Server) Prepend() *Server { + s.mu.Lock() + defer s.mu.Unlock() + s.prependOnce = true + return s +} + // ClearRequests clear requests. func (s *Server) ClearRequests() { s.requests = nil @@ -530,6 +538,15 @@ func (m *matcher) Requests() []*Request { return m.requests } +func (s *Server) addMatcher(m *matcher) { + if s.prependOnce { + s.matchers = append([]*matcher{m}, s.matchers...) + s.prependOnce = false + return + } + s.matchers = append(s.matchers, m) +} + func (s *Server) registerServer() { for _, fd := range s.fds { for i := 0; i < fd.Services().Len(); i++ { diff --git a/grpcstub_test.go b/grpcstub_test.go index d47ebff..eb6de77 100644 --- a/grpcstub_test.go +++ b/grpcstub_test.go @@ -804,3 +804,51 @@ func TestUnmarshalProtoMessage(t *testing.T) { t.Errorf("got %v\nwant %v", got, want) } } + +func TestPrepend(t *testing.T) { + t.Run("Default", func(t *testing.T) { + ctx := context.Background() + ts := NewServer(t, "testdata/route_guide.proto") + t.Cleanup(func() { + ts.Close() + }) + ts.Service("routeguide.RouteGuide").Response(map[string]any{"name": "hello"}) + ts.Service("routeguide.RouteGuide").Response(map[string]any{"name": "world"}) + + client := routeguide.NewRouteGuideClient(ts.Conn()) + res, err := client.GetFeature(ctx, &routeguide.Point{ + Latitude: 10, + Longitude: 13, + }) + if err != nil { + t.Fatal(err) + } + got := res.Name + if want := "hello"; got != want { + t.Errorf("got %v\nwant %v", got, want) + } + }) + + t.Run("Prepend", func(t *testing.T) { + ctx := context.Background() + ts := NewServer(t, "testdata/route_guide.proto") + t.Cleanup(func() { + ts.Close() + }) + ts.Service("routeguide.RouteGuide").Response(map[string]any{"name": "hello"}) + ts.Prepend().Service("routeguide.RouteGuide").Response(map[string]any{"name": "world"}) + + client := routeguide.NewRouteGuideClient(ts.Conn()) + res, err := client.GetFeature(ctx, &routeguide.Point{ + Latitude: 10, + Longitude: 13, + }) + if err != nil { + t.Fatal(err) + } + got := res.Name + if want := "world"; got != want { + t.Errorf("got %v\nwant %v", got, want) + } + }) +} From 274432527f4c834a463ce750c84e022252485d08 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Sun, 24 Nov 2024 17:39:58 +0900 Subject: [PATCH 2/2] bonsai --- grpcstub.go | 1 + grpcstub_test.go | 2 ++ 2 files changed, 3 insertions(+) diff --git a/grpcstub.go b/grpcstub.go index bd772b5..dc584ed 100644 --- a/grpcstub.go +++ b/grpcstub.go @@ -518,6 +518,7 @@ func (s *Server) ClearMatchers() { s.matchers = nil } +// Prepend prepend matcher. func (s *Server) Prepend() *Server { s.mu.Lock() defer s.mu.Unlock() diff --git a/grpcstub_test.go b/grpcstub_test.go index eb6de77..dfbc337 100644 --- a/grpcstub_test.go +++ b/grpcstub_test.go @@ -814,6 +814,7 @@ func TestPrepend(t *testing.T) { }) ts.Service("routeguide.RouteGuide").Response(map[string]any{"name": "hello"}) ts.Service("routeguide.RouteGuide").Response(map[string]any{"name": "world"}) + ts.Service("routeguide.RouteGuide").Response(map[string]any{"name": "!!!"}) client := routeguide.NewRouteGuideClient(ts.Conn()) res, err := client.GetFeature(ctx, &routeguide.Point{ @@ -837,6 +838,7 @@ func TestPrepend(t *testing.T) { }) ts.Service("routeguide.RouteGuide").Response(map[string]any{"name": "hello"}) ts.Prepend().Service("routeguide.RouteGuide").Response(map[string]any{"name": "world"}) + ts.Service("routeguide.RouteGuide").Response(map[string]any{"name": "!!!"}) client := routeguide.NewRouteGuideClient(ts.Conn()) res, err := client.GetFeature(ctx, &routeguide.Point{