Skip to content

Commit

Permalink
Implement ForwarderClient SendRoomStatus method (#402)
Browse files Browse the repository at this point in the history
- Implement `ForwarderClient` `SendRoomStatus` method
  • Loading branch information
guilhermocc authored Apr 18, 2022
1 parent 30a7bcf commit 937db94
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 0 deletions.
14 changes: 14 additions & 0 deletions internal/adapters/events/forwarder_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ func (f *ForwarderClient) SendRoomReSync(ctx context.Context, forwarder forwarde
})
}

// SendRoomStatus fetch or create a grpc client and send a room status event to forwarder.
func (f *ForwarderClient) SendRoomStatus(ctx context.Context, forwarder forwarder.Forwarder, in *pb.RoomStatus) (*pb.Response, error) {
client, err := f.getGrpcClient(Address(forwarder.Address))
if err != nil {
return nil, errors.NewErrUnexpected("failed to connect at %s", forwarder.Address).WithError(err)
}

ctx, cancel := context.WithTimeout(ctx, forwarder.Options.Timeout*time.Millisecond)
defer cancel()
return runReportingLatencyMetrics("SendRoomStatus", func() (*pb.Response, error) {
return client.SendRoomStatus(ctx, in)
})
}

// SendPlayerEvent fetch or create a grpc client and send a player event to forwarder.
func (f *ForwarderClient) SendPlayerEvent(ctx context.Context, forwarder forwarder.Forwarder, in *pb.PlayerEvent) (*pb.Response, error) {
client, err := f.getGrpcClient(Address(forwarder.Address))
Expand Down
89 changes: 89 additions & 0 deletions internal/adapters/events/forwarder_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,18 @@ package events

import (
"context"
"fmt"
"os"
"time"

"testing"

"github.com/patrickmn/go-cache"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/topfreegames/maestro/internal/core/entities/forwarder"
"github.com/topfreegames/maestro/test"

pb "github.com/topfreegames/protos/maestro/grpc/generated"
)

Expand Down Expand Up @@ -127,6 +130,92 @@ func TestSendRoomReSync(t *testing.T) {
})
}

func TestForwarderClient_SendRoomStatus(t *testing.T) {
type fields struct {
c *cache.Cache
}
type args struct {
ctx context.Context
forwarder forwarder.Forwarder
in pb.RoomStatus
}
tests := []struct {
name string
requestStubFile string
fields fields
args args
want *pb.Response
wantErr assert.ErrorAssertionFunc
}{
{
name: "return response with code 200 when request succeeds",
requestStubFile: "../../../test/data/events_mock/events-forwarder-grpc-send-room-status-success.json",
fields: fields{
c: cache.New(time.Minute, time.Minute),
},
args: args{
ctx: context.Background(),
forwarder: newForwarder(grpcMockAddress),
in: newRoomStatusEvent("65e810a0-bb81-4633-93c9-826414a0062d"),
},
want: &pb.Response{
Code: 200,
Message: "Event received with success",
},
wantErr: assert.NoError,
},
{
name: "return response with code 500 when request fails",
requestStubFile: "../../../test/data/events_mock/events-forwarder-grpc-send-room-status-failure.json",
fields: fields{
c: cache.New(time.Minute, time.Minute),
},
args: args{
ctx: context.Background(),
forwarder: newForwarder(grpcMockAddress),
in: newRoomStatusEvent("f7415c97-5c28-418b-b19b-87380e2d0113"),
},
want: &pb.Response{
Code: 500,
Message: "Internal server error from matchmaker",
},
wantErr: assert.NoError,
},
{
name: "return error when connection establishment fails",
requestStubFile: "",
fields: fields{
c: cache.New(time.Minute, time.Minute),
},
args: args{
ctx: context.Background(),
forwarder: newForwarder(""),
in: newRoomStatusEvent(""),
},
wantErr: assert.Error,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.requestStubFile != "" {
err := test.AddStubRequestToMockedGrpcServer(
httpInputMockAddress,
tt.requestStubFile,
)
require.NoError(t, err)
}
f := &ForwarderClient{
c: tt.fields.c,
}
got, err := f.SendRoomStatus(tt.args.ctx, tt.args.forwarder, &tt.args.in)
if !tt.wantErr(t, err, fmt.Sprintf("SendRoomStatus(%v, %v, %v)", tt.args.ctx, tt.args.forwarder, tt.args.in)) {
return
}
assert.Equalf(t, tt.want, got, "SendRoomStatus(%v, %v, %v)", tt.args.ctx, tt.args.forwarder, tt.args.in)
})
}
}

func TestSendPlayerEvent(t *testing.T) {
t.Run("success case should return response", func(t *testing.T) {
err := test.AddStubRequestToMockedGrpcServer(
Expand Down
1 change: 1 addition & 0 deletions internal/core/ports/events_ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type EventsForwarder interface {
type ForwarderClient interface {
SendRoomEvent(ctx context.Context, forwarder forwarder.Forwarder, in *pb.RoomEvent) (*pb.Response, error)
SendRoomReSync(ctx context.Context, forwarder forwarder.Forwarder, in *pb.RoomStatus) (*pb.Response, error)
SendRoomStatus(ctx context.Context, forwarder forwarder.Forwarder, in *pb.RoomStatus) (*pb.Response, error)
SendPlayerEvent(ctx context.Context, forwarder forwarder.Forwarder, in *pb.PlayerEvent) (*pb.Response, error)
CacheFlush()
CacheDelete(forwarderAddress string) error
Expand Down
15 changes: 15 additions & 0 deletions internal/core/ports/mock/events_ports_mock.go

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"service": "GRPCForwarder",
"method": "SendRoomStatus",
"input": {
"contains": {
"room": {
"metadata":{
"mockIdentifier": "f7415c97-5c28-418b-b19b-87380e2d0113"
}
}
}
},
"output": {
"data": {
"code": 500,
"message": "Internal server error from matchmaker"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"service": "GRPCForwarder",
"method": "SendRoomStatus",
"input": {
"contains": {
"room": {
"metadata":{
"mockIdentifier": "65e810a0-bb81-4633-93c9-826414a0062d"
}
}
}
},
"output": {
"data": {
"code": 200,
"message": "Event received with success"
}
}
}

0 comments on commit 937db94

Please sign in to comment.