Skip to content

Commit

Permalink
TestAllocatorAllocateOnGameServerUpdateError (#3283)
Browse files Browse the repository at this point in the history
This was flaking as the cache is eventually consistent, so sometimes it
wouldn't be populated in time for the allocation request.

Fixed with a better state check on the allocation cache.

```
--- FAIL: TestAllocatorAllocateOnGameServerUpdateError (19.31s)
    allocator_test.go:499:
        	Error Trace:	/go/src/agones.dev/agones/pkg/gameserverallocations/allocator_test.go:499
        	Error:      	An error is expected but got nil.
        	Test:       	TestAllocatorAllocateOnGameServerUpdateError
```
  • Loading branch information
markmandel authored Jul 22, 2023
1 parent 4c07151 commit 94038da
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions pkg/gameserverallocations/allocator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,34 +469,38 @@ func TestAllocatorAllocateOnGameServerUpdateError(t *testing.T) {
defer cancel()

require.NoError(t, a.Run(ctx))
// wait for it to be up and running
err := wait.PollImmediate(time.Second, 10*time.Second, func() (done bool, err error) {
return a.allocationCache.workerqueue.RunCount() == 1, nil
})
assert.NoError(t, err)
// wait for the single gameserver to be in the cache.
require.Eventuallyf(t, func() bool {
return a.allocationCache.cache.Len() >= 1
}, 10*time.Second, time.Second, "should have a single item in the cache")

gsa := allocationv1.GameServerAllocation{ObjectMeta: metav1.ObjectMeta{Name: "gsa-1", Namespace: defaultNs},
Spec: allocationv1.GameServerAllocationSpec{},
}

gsa.ApplyDefaults()
// without converter we don't end up with at least one selector
// without converter, we don't end up with at least one selector
gsa.Converter()
errs := gsa.Validate()
require.Len(t, errs, 0)
require.Len(t, gsa.Spec.Selectors, 1)

// try the private method
_, err = a.allocate(ctx, gsa.DeepCopy())
_, err := a.allocate(ctx, gsa.DeepCopy())
logrus.WithField("test", t.Name()).WithError(err).Info("allocate (private): failed allocation")
require.NotEqual(t, ErrNoGameServer, err)
assert.EqualError(t, err, "error updating allocated gameserver: failed to update")
require.EqualError(t, err, "error updating allocated gameserver: failed to update")

// triple check there is still a gameserver in the cache
require.Eventuallyf(t, func() bool {
return a.allocationCache.cache.Len() >= 1
}, 10*time.Second, time.Second, "should have a single item in the cache (still)")

// try the public method
_, err = a.Allocate(ctx, gsa.DeepCopy())
logrus.WithField("test", t.Name()).WithError(err).Info("Allocate (public): failed allocation")
require.NotEqual(t, ErrNoGameServer, err)
assert.EqualError(t, err, "error updating allocated gameserver: failed to update")
require.EqualError(t, err, "error updating allocated gameserver: failed to update")
}

func TestAllocatorRunLocalAllocations(t *testing.T) {
Expand Down

0 comments on commit 94038da

Please sign in to comment.