Skip to content

Commit

Permalink
Removes unnecessary GetCounter
Browse files Browse the repository at this point in the history
  • Loading branch information
igooch committed Sep 12, 2023
1 parent 2a6cbe2 commit 041ce80
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
20 changes: 8 additions & 12 deletions sdks/go/alpha.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
32 changes: 19 additions & 13 deletions sdks/go/alpha_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -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)

Expand Down

0 comments on commit 041ce80

Please sign in to comment.