Skip to content

Commit

Permalink
Feature/implement update room status handler (#405)
Browse files Browse the repository at this point in the history
* Implement UpdateRoomStatus handler logic and update unitary tests.
* Create e2e test cases for room status event feature
  • Loading branch information
guilhermocc authored Apr 18, 2022
1 parent 34ce520 commit 89b19c1
Show file tree
Hide file tree
Showing 6 changed files with 338 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"service": "GRPCForwarder",
"method": "SendRoomStatus",
"input": {
"contains": {
"room": {
"metadata":{
"eventMetadata1": "value1",
"eventMetadata2": "true",
"forwarderMetadata1": "value1",
"forwarderMetadata2": "245",
"roomType": "green",
"mockIdentifier": "24db7925-fdc9-45ef-8b87-f1fff6a9eaaa"
}
},
"statusType": 3
}
},
"output": {
"data": {
"code": 500,
"message": "Internal server error from matchmaker"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"service": "GRPCForwarder",
"method": "SendRoomStatus",
"input": {
"contains": {
"room": {
"metadata":{
"eventMetadata1": "value1",
"eventMetadata2": "true",
"forwarderMetadata1": "value1",
"forwarderMetadata2": "245",
"roomType": "green",
"mockIdentifier": "9da046b8-3ef9-4f28-966d-d5c0cd1fe627"
}
},
"statusType": 3
}
},
"output": {
"data": {
"code": 200,
"message": "Event received with success"
}
}
}
161 changes: 157 additions & 4 deletions e2e/suites/management/events_forwarding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func TestEventsForwarding(t *testing.T) {
require.Equal(t, true, roomEventResponse.Success)
})

t.Run("[Room event failures] Forward room event return success false when some error occurs in GRPC call", func(t *testing.T) {
t.Run("[Room event failure] Forward room event return success false when some error occurs in GRPC call", func(t *testing.T) {
t.Parallel()

// This configuration make the grpc service return with failure
Expand Down Expand Up @@ -296,7 +296,7 @@ func TestEventsForwarding(t *testing.T) {
require.Equal(t, "failed to forward event room at \"matchmaker-grpc\"", roomEventResponse.Message)
})

t.Run("[Room event failure] Forward player event return success false when forwarding event for an inexistent room", func(t *testing.T) {
t.Run("[Room event failure] Forward room event return success false when forwarding event for an inexistent room", func(t *testing.T) {
t.Parallel()
inexistentRoom := "inexistent-room"

Expand All @@ -322,7 +322,7 @@ func TestEventsForwarding(t *testing.T) {
require.Equal(t, false, roomEventResponse.Success)
})

t.Run("[Room event failure] Forward player event return success false when the forwarder connection can't be established", func(t *testing.T) {
t.Run("[Room event failure] Forward room event return success false when the forwarder connection can't be established", func(t *testing.T) {
t.Parallel()

roomEventRequest := &maestroApiV1.ForwardPlayerEventRequest{
Expand Down Expand Up @@ -406,7 +406,7 @@ func TestEventsForwarding(t *testing.T) {
require.Equal(t, true, pingResponse.Success)
})

t.Run("[Ping event failures] Ping event return success when some error occurs in GRPC call", func(t *testing.T) {
t.Run("[Ping event failure] Ping event return success when some error occurs in GRPC call", func(t *testing.T) {
t.Parallel()

// This configuration make the grpc service return with failure
Expand Down Expand Up @@ -465,6 +465,159 @@ func TestEventsForwarding(t *testing.T) {
require.NoError(t, err)
require.Equal(t, true, pingResponse.Success)
})

t.Run("[Room status event success] Forward room status event return success true when no error occurs while forwarding events call", func(t *testing.T) {
t.Parallel()

// This configuration make the grpc service return with success
err := addStubRequestToMockedGrpcServer("events-forwarder-grpc-send-room-status-event-success")
require.NoError(t, err)

roomStatusEventRequest := &maestroApiV1.UpdateRoomStatusRequest{
Status: "ready",
Metadata: &_struct.Struct{
Fields: map[string]*structpb.Value{
"mockIdentifier": {
Kind: &structpb.Value_StringValue{
StringValue: "9da046b8-3ef9-4f28-966d-d5c0cd1fe627",
},
},
"eventMetadata1": {
Kind: &structpb.Value_StringValue{
StringValue: "value1",
},
},
"eventMetadata2": {
Kind: &structpb.Value_BoolValue{
BoolValue: true,
},
},
},
},
Timestamp: time.Now().Unix(),
}
roomStatusEventResponse := &maestroApiV1.UpdateRoomStatusResponse{}
err = roomsApiClient.Do("PUT", fmt.Sprintf("/scheduler/%s/rooms/%s/status", schedulerWithForwarderAndRooms.Name, roomsNames[0]), roomStatusEventRequest, roomStatusEventResponse)
require.NoError(t, err)
require.Equal(t, true, roomStatusEventResponse.Success)
})

t.Run("[Room status event success] Forward room status event return success true when no forwarder is configured for the scheduler", func(t *testing.T) {
t.Parallel()

roomStatusEventRequest := &maestroApiV1.UpdateRoomStatusRequest{
Status: "ready",
Timestamp: time.Now().Unix(),
}
roomStatusEventResponse := &maestroApiV1.UpdateRoomStatusResponse{}
err := roomsApiClient.Do("PUT", fmt.Sprintf("/scheduler/%s/rooms/%s/status", schedulerWithRooms.Name, roomNameNoForwarder), roomStatusEventRequest, roomStatusEventResponse)
require.NoError(t, err)
require.Equal(t, true, roomStatusEventResponse.Success)
})

t.Run("[Room status event failure] Forward room status event return success false when some error occurs in GRPC call", func(t *testing.T) {
t.Parallel()

// This configuration make the grpc service return with failure
err := addStubRequestToMockedGrpcServer("events-forwarder-grpc-send-room-event-failure")
require.NoError(t, err)

roomStatusEventRequest := &maestroApiV1.UpdateRoomStatusRequest{
Status: "ready",
Metadata: &_struct.Struct{
Fields: map[string]*structpb.Value{
"mockIdentifier": {
Kind: &structpb.Value_StringValue{
StringValue: "24db7925-fdc9-45ef-8b87-f1fff6a9eaaa",
},
},
"eventMetadata1": {
Kind: &structpb.Value_StringValue{
StringValue: "value1",
},
},
"eventMetadata2": {
Kind: &structpb.Value_BoolValue{
BoolValue: true,
},
},
},
},
Timestamp: time.Now().Unix(),
}
roomStatusEventResponse := &maestroApiV1.UpdateRoomStatusResponse{}
err = roomsApiClient.Do("PUT", fmt.Sprintf("/scheduler/%s/rooms/%s/status", schedulerWithForwarderAndRooms.Name, roomsNames[0]), roomStatusEventRequest, roomStatusEventResponse)
require.NoError(t, err)
require.Equal(t, false, roomStatusEventResponse.Success)
})

t.Run("[Room status event failure] Forward room status event return success false when forwarding event for an inexistent room", func(t *testing.T) {
t.Parallel()
inexistentRoom := "inexistent-room"

err := addStubRequestToMockedGrpcServer("events-forwarder-grpc-send-player-event-failure")

roomStatusEventRequest := &maestroApiV1.UpdateRoomStatusRequest{
Status: "ready",
Metadata: &_struct.Struct{
Fields: map[string]*structpb.Value{
"mockIdentifier": {
Kind: &structpb.Value_StringValue{
StringValue: "9da046b8-3ef9-4f28-966d-d5c0cd1fe627",
},
},
"eventMetadata1": {
Kind: &structpb.Value_StringValue{
StringValue: "value1",
},
},
"eventMetadata2": {
Kind: &structpb.Value_BoolValue{
BoolValue: true,
},
},
},
},
Timestamp: time.Now().Unix(),
}
roomStatusEventResponse := &maestroApiV1.UpdateRoomStatusResponse{}
err = roomsApiClient.Do("PUT", fmt.Sprintf("/scheduler/%s/rooms/%s/status", schedulerWithForwarderAndRooms.Name, inexistentRoom), roomStatusEventRequest, roomStatusEventResponse)
require.NoError(t, err)
require.Equal(t, false, roomStatusEventResponse.Success)
})

t.Run("[Room status event failure] Forward room status event return success false when the forwarder connection can't be established", func(t *testing.T) {
t.Parallel()

roomStatusEventRequest := &maestroApiV1.UpdateRoomStatusRequest{
Status: "ready",
Metadata: &_struct.Struct{
Fields: map[string]*structpb.Value{
"mockIdentifier": {
Kind: &structpb.Value_StringValue{
StringValue: "9da046b8-3ef9-4f28-966d-d5c0cd1fe627",
},
},
"eventMetadata1": {
Kind: &structpb.Value_StringValue{
StringValue: "value1",
},
},
"eventMetadata2": {
Kind: &structpb.Value_BoolValue{
BoolValue: true,
},
},
},
},
Timestamp: time.Now().Unix(),
}
roomStatusEventResponse := &maestroApiV1.UpdateRoomStatusResponse{}
err := roomsApiClient.Do("PUT", fmt.Sprintf("/scheduler/%s/rooms/%s/status", schedulerWithInvalidGrpc.Name, invalidGrpcRooms[0]), roomStatusEventRequest, roomStatusEventResponse)
require.NoError(t, err)
require.Equal(t, false, roomStatusEventResponse.Success)
})

})

}
Expand Down
10 changes: 10 additions & 0 deletions internal/api/handlers/rooms_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ func (h *RoomsHandler) UpdateRoomWithPing(ctx context.Context, message *api.Upda
// UpdateRoomStatus was only implemented to keep compatibility with previous maestro version (v9), it has no inner execution since the
// ping event is already forwarding the incoming rooms status to matchmaker
func (h *RoomsHandler) UpdateRoomStatus(ctx context.Context, message *api.UpdateRoomStatusRequest) (*api.UpdateRoomStatusResponse, error) {
handlerLogger := h.logger.With(zap.String(logs.LogFieldSchedulerName, message.SchedulerName), zap.String(logs.LogFieldRoomID, message.RoomName))
eventMetadata := message.Metadata.AsMap()
eventMetadata["eventType"] = events.FromRoomEventTypeToString(events.Status)
eventMetadata["pingType"] = message.Status

err := h.eventsService.ProduceEvent(ctx, events.NewRoomEvent(message.SchedulerName, message.RoomName, eventMetadata))
if err != nil {
handlerLogger.Error("error forwarding room status event", zap.Any("event_message", message), zap.Error(err))
return &api.UpdateRoomStatusResponse{Success: false}, nil
}
return &api.UpdateRoomStatusResponse{Success: true}, nil
}

Expand Down
Loading

0 comments on commit 89b19c1

Please sign in to comment.