Skip to content

Commit

Permalink
chore: prep for order events
Browse files Browse the repository at this point in the history
  • Loading branch information
w-h-a committed Jul 12, 2024
1 parent b5d5ccf commit 5b6c66e
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 20 deletions.
18 changes: 16 additions & 2 deletions client/mockclient/mock_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
type mockClient struct {
options client.ClientOptions
responses map[string]Response
streams map[string]client.Stream
client client.Client
mtx sync.RWMutex
}
Expand Down Expand Up @@ -49,8 +50,15 @@ func (c *mockClient) Call(ctx context.Context, req client.Request, rsp interface
}

func (c *mockClient) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) {
// TODO
return nil, nil
c.mtx.RLock()
defer c.mtx.RUnlock()

mock, ok := c.streams[req.Service()+":"+req.Method()]
if !ok {
return nil, errorutils.NotFound("mock.client", "service:method %s:%s not found in streams %+v", req.Service(), req.Method(), c.streams)
}

return mock, nil
}

func (c *mockClient) String() string {
Expand All @@ -65,6 +73,11 @@ func NewClient(opts ...client.ClientOption) client.Client {
responses = map[string]Response{}
}

streams, ok := GetStreamsFromContext(options.Context)
if !ok {
streams = map[string]client.Stream{}
}

c, ok := GetClientFromContext(options.Context)
if !ok {
c = grpcclient.NewClient()
Expand All @@ -73,6 +86,7 @@ func NewClient(opts ...client.ClientOption) client.Client {
m := &mockClient{
options: options,
responses: responses,
streams: streams,
client: c,
mtx: sync.RWMutex{},
}
Expand Down
77 changes: 77 additions & 0 deletions client/mockclient/mock_stream.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package mockclient

import (
"context"
"reflect"
"sync"

"github.com/w-h-a/pkg/client"
)

type mockStream struct {
send Response
recv Response
err error
mtx sync.RWMutex
}

func (s *mockStream) Context() context.Context {
return nil
}

func (s *mockStream) Request() client.Request {
return nil
}

func (s *mockStream) Send(_ interface{}) error {
s.mtx.RLock()
defer s.mtx.RUnlock()

mock := s.send

if mock.Err != nil {
s.setError(mock.Err)
return mock.Err
}

return nil
}

func (s *mockStream) Recv(msg interface{}) error {
s.mtx.RLock()
defer s.mtx.RUnlock()

mock := s.recv

if mock.Err != nil {
s.setError(mock.Err)
return mock.Err
}

val := reflect.ValueOf(msg)
val = reflect.Indirect(val)

response := mock.Response

val.Set(reflect.ValueOf(response))

return nil
}

func (s *mockStream) Error() error {
s.mtx.RLock()
defer s.mtx.RUnlock()

return s.err
}

func (s *mockStream) Close() error {
return nil
}

func (s *mockStream) setError(e error) {
s.mtx.Lock()
defer s.mtx.Unlock()

s.err = e
}
24 changes: 24 additions & 0 deletions client/mockclient/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package mockclient

import (
"context"
"sync"

"github.com/w-h-a/pkg/client"
)

type responsesKey struct{}
type streamsKey struct{}
type clientKey struct{}

func ClientWithResponses(service, method string, response Response) client.ClientOption {
Expand All @@ -27,6 +29,28 @@ func GetResponsesFromContext(ctx context.Context) (map[string]Response, bool) {
return rsp, ok
}

func ClientWithStreams(service, method string, send, recv Response) client.ClientOption {
return func(o *client.ClientOptions) {
streams, ok := GetStreamsFromContext(o.Context)
if !ok {
streams = map[string]client.Stream{}
}

streams[service+":"+method] = &mockStream{
send: send,
recv: recv,
mtx: sync.RWMutex{},
}

o.Context = context.WithValue(o.Context, streamsKey{}, streams)
}
}

func GetStreamsFromContext(ctx context.Context) (map[string]client.Stream, bool) {
str, ok := ctx.Value(streamsKey{}).(map[string]client.Stream)
return str, ok
}

func ClientWithClient(c client.Client) client.ClientOption {
return func(o *client.ClientOptions) {
o.Context = context.WithValue(o.Context, clientKey{}, c)
Expand Down
46 changes: 28 additions & 18 deletions proto/ticket/ticket.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions proto/ticket/ticket.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ message Ticket {
string title = 1;
string price = 2;
string userId = 3;
string orderId = 4;
}

// create request/response
Expand Down

0 comments on commit 5b6c66e

Please sign in to comment.