Skip to content

Commit

Permalink
removed cachedGameServer field and method, updated unit test
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
alexey-kremsa-globant committed Jun 29, 2020
1 parent 5994711 commit fbaadef
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
17 changes: 2 additions & 15 deletions pkg/sdkserver/sdkserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ type SDKServer struct {
gsReserveDuration *time.Duration
gsPlayerCapacity int64
gsConnectedPlayers []string
cachedGameServer *sdk.GameServer
}

// NewSDKServer creates a SDKServer that sets up an
Expand Down Expand Up @@ -491,17 +490,7 @@ func (s *SDKServer) GetGameServer(context.Context, *sdk.Empty) (*sdk.GameServer,
if err != nil {
return nil, err
}

s.cachedGameServer = convert(gs)
return s.cachedGameServer, nil
}

func (s *SDKServer) getCachedGameServer() (*sdk.GameServer, error) {
if s.cachedGameServer != nil {
return s.cachedGameServer, nil
}

return s.GetGameServer(context.Background(), &sdk.Empty{})
return convert(gs), nil
}

// WatchGameServer sends events through the stream when changes occur to the
Expand All @@ -511,15 +500,13 @@ func (s *SDKServer) WatchGameServer(_ *sdk.Empty, stream sdk.SDK_WatchGameServer
s.streamMutex.Lock()

if runtime.FeatureEnabled(runtime.FeatureSDKWatchSendOnExecute) {
gs, err := s.getCachedGameServer()
gs, err := s.GetGameServer(context.Background(), &sdk.Empty{})
if err != nil {
s.logger.WithError(errors.WithStack(err)).Error("error getting cached game server")
return err
}

err = stream.Send(gs)
if err != nil {
s.logger.WithError(errors.WithStack(err)).Error("error sending cached game server")
return err
}
}
Expand Down
21 changes: 18 additions & 3 deletions pkg/sdkserver/sdkserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,12 +705,27 @@ func TestSDKServerWatchGameServerFeatureSDKWatchSendOnExecute(t *testing.T) {

stream := newGameServerMockStream()
asyncWatchGameServer(t, sc, stream)

assert.Nil(t, waitConnectedStreamCount(sc, 1))
assert.Equal(t, stream, sc.connectedStreams[0])

assert.Equal(t, fixture.ObjectMeta.Name, sc.cachedGameServer.ObjectMeta.Name)
assert.Equal(t, fixture.ObjectMeta.Namespace, sc.cachedGameServer.ObjectMeta.Namespace)
assert.Equal(t, string(fixture.Status.State), sc.cachedGameServer.Status.State)
totalSendCalls := 0
for i := 0; i < 2; i++ {
select {
case _, ok := <-stream.msgs:
if ok {
totalSendCalls++
} else {
assert.Fail(t, "Channel is closed!")
}
default:
t.Log("No gameserver in the stream, moving on.")
}
}

// if SDKWatchSendOnExecute feature is turned on, there are two stream.Send() calls should happen:
// one in sendGameServerUpdate, another one in WatchGameServer.
assert.Equal(t, 2, totalSendCalls)
}

func TestSDKServerSendGameServerUpdate(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion site/content/en/docs/Guides/feature-stages.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ The current set of `alpha` and `beta` feature gates are:
| Example Gate (not in use) | `Example` | Disabled | None | 0.13.0 |
| [Port Allocations to Multiple Containers]({{< ref "/docs/Reference/gameserver.md" >}}) | `ContainerPortAllocation` | Enabled | `Beta` | 1.7.0 |
| [Player Tracking]({{< ref "/docs/Guides/player-tracking.md" >}}) | `PlayerTracking` | Disabled | `Alpha` | 1.6.0 |
| [SDK Send Game Server on Watch]({{< ref "/docs/Guides/Client SDKs/_index.md#watchgameserver-function-gameserver" >}}) | `SDKWatchSendOnExecute` | Disabled | `Alpha` | 1.7.0 |
| [SDK Send GameServer on Watch execution]({{< ref "/docs/Guides/Client SDKs/_index.md#watchgameserver-function-gameserver" >}}) | `SDKWatchSendOnExecute` | Disabled | `Alpha` | 1.7.0 |
<sup>*</sup>Multicluster Allocation was started before this process was in place, and therefore does not have a
feature gate and cannot be disabled.
{{% /feature %}}
Expand Down

0 comments on commit fbaadef

Please sign in to comment.