diff --git a/sdks/go/alpha.go b/sdks/go/alpha.go index eb67d083a1..b7c6a05258 100644 --- a/sdks/go/alpha.go +++ b/sdks/go/alpha.go @@ -84,21 +84,14 @@ func (a *Alpha) GetConnectedPlayers() ([]string, error) { return list.GetList(), errors.Wrap(err, "could not list connected players") } -// GetCounter returns a copy of a Counter, given the Counter's key (name). -// Will error if the key was not predefined in the GameServer resource on creation. -func (a *Alpha) GetCounter(key string) (alpha.Counter, error) { - counter, err := a.client.GetCounter(context.Background(), &alpha.GetCounterRequest{Name: key}) - if err == nil { - return alpha.Counter{Name: counter.Name, Count: counter.Count, Capacity: counter.Capacity}, nil - } - return alpha.Counter{}, errors.Wrapf(err, "could not get Counter %s", key) -} - // GetCounterCount returns the Count for a Counter, given the Counter's key (name). // Will error if the key was not predefined in the GameServer resource on creation. func (a *Alpha) GetCounterCount(key string) (int64, error) { counter, err := a.client.GetCounter(context.Background(), &alpha.GetCounterRequest{Name: key}) - return counter.Count, errors.Wrapf(err, "could not get Counter %s count", key) + if err != nil { + return -1, errors.Wrapf(err, "could not get Counter %s count", key) + } + return counter.Count, nil } // IncrementCounter increases a counter by the given nonnegative integer amount. @@ -161,7 +154,10 @@ func (a *Alpha) SetCounterCount(key string, amount int64) (bool, error) { // Will error if the key was not predefined in the GameServer resource on creation. func (a *Alpha) GetCounterCapacity(key string) (int64, error) { counter, err := a.client.GetCounter(context.Background(), &alpha.GetCounterRequest{Name: key}) - return counter.Capacity, errors.Wrapf(err, "could not get Counter %s capacity", key) + if err != nil { + return -1, errors.Wrapf(err, "could not get Counter %s capacity", key) + } + return counter.Capacity, nil } // SetCounterCapacity sets the capacity for a given count. A capacity of 0 is no capacity. diff --git a/sdks/go/alpha_test.go b/sdks/go/alpha_test.go index 79c5896f8a..5b70020e71 100644 --- a/sdks/go/alpha_test.go +++ b/sdks/go/alpha_test.go @@ -113,16 +113,20 @@ func TestAlphaGetAndUpdateCounter(t *testing.T) { t.Parallel() t.Run("Set Counter and Set Capacity", func(t *testing.T) { - counter, err := a.GetCounter("sessions") + count, err := a.GetCounterCount("sessions") + assert.NoError(t, err) + assert.Equal(t, sessions.Count, count) + + capacity, err := a.GetCounterCapacity("sessions") assert.NoError(t, err) - assert.Equal(t, &sessions, &counter) + assert.Equal(t, sessions.Capacity, capacity) wantCapacity := int64(25) ok, err := a.SetCounterCapacity("sessions", wantCapacity) assert.NoError(t, err) assert.True(t, ok) - capacity, err := a.GetCounterCapacity("sessions") + capacity, err = a.GetCounterCapacity("sessions") assert.NoError(t, err) assert.Equal(t, wantCapacity, capacity) @@ -131,15 +135,17 @@ func TestAlphaGetAndUpdateCounter(t *testing.T) { assert.NoError(t, err) assert.True(t, ok) - count, err := a.GetCounterCount("sessions") + count, err = a.GetCounterCount("sessions") assert.NoError(t, err) assert.Equal(t, wantCount, count) }) - t.Run("Set Counter and Set Capacity Non Defined Counter", func(t *testing.T) { - counter, err := a.GetCounter("secessions") + t.Run("Get and Set Non-Defined Counter", func(t *testing.T) { + _, err := a.GetCounterCount("secessions") + assert.Error(t, err) + + _, err = a.GetCounterCapacity("secessions") assert.Error(t, err) - assert.Empty(t, &counter) ok, err := a.SetCounterCapacity("secessions", int64(100)) assert.Error(t, err) @@ -152,15 +158,15 @@ func TestAlphaGetAndUpdateCounter(t *testing.T) { // nolint:dupl // testing DecrementCounter and IncrementCounter are not duplicates. t.Run("Decrement Counter Fails then Success", func(t *testing.T) { - counter, err := a.GetCounter("games") + count, err := a.GetCounterCount("games") assert.NoError(t, err) - assert.Equal(t, &games, &counter) + assert.Equal(t, games.Count, count) ok, err := a.DecrementCounter("games", 21) assert.Error(t, err) assert.False(t, ok) - count, err := a.GetCounterCount("games") + count, err = a.GetCounterCount("games") assert.NoError(t, err) assert.Equal(t, games.Count, count) @@ -183,15 +189,15 @@ func TestAlphaGetAndUpdateCounter(t *testing.T) { // nolint:dupl // testing DecrementCounter and IncrementCounter are not duplicates. t.Run("Increment Counter Fails then Success", func(t *testing.T) { - counter, err := a.GetCounter("gamers") + count, err := a.GetCounterCount("gamers") assert.NoError(t, err) - assert.Equal(t, &gamers, &counter) + assert.Equal(t, gamers.Count, count) ok, err := a.IncrementCounter("gamers", 250) assert.Error(t, err) assert.False(t, ok) - count, err := a.GetCounterCount("gamers") + count, err = a.GetCounterCount("gamers") assert.NoError(t, err) assert.Equal(t, gamers.Count, count)