agones.dev/agones@v1.53.0/pkg/gameserverallocations/cache_test.go (about) 1 // Copyright 2019 Google LLC All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package gameserverallocations 16 17 import ( 18 "testing" 19 20 agonesv1 "agones.dev/agones/pkg/apis/agones/v1" 21 "github.com/stretchr/testify/assert" 22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 ) 24 25 func TestGameServerCacheEntry(t *testing.T) { 26 gs1 := &agonesv1.GameServer{ObjectMeta: metav1.ObjectMeta{Name: "gs1"}} 27 gs2 := &agonesv1.GameServer{ObjectMeta: metav1.ObjectMeta{Name: "gs2"}} 28 gs3 := &agonesv1.GameServer{ObjectMeta: metav1.ObjectMeta{Name: "gs3"}} 29 30 cache := gameServerCache{} 31 32 gs, ok := cache.Load("gs1") 33 assert.Nil(t, gs) 34 assert.False(t, ok) 35 36 cache.Store("gs1", gs1) 37 gs, ok = cache.Load("gs1") 38 39 assert.Equal(t, gs, gs1) 40 assert.True(t, ok) 41 42 cache.Store("gs2", gs2) 43 cache.Store("gs3", gs3) 44 45 count := 0 46 cache.Range(func(_ string, _ *agonesv1.GameServer) bool { 47 count++ 48 return count != 2 49 }) 50 51 assert.Equal(t, 2, count, "Should only process one item") 52 53 cache.Delete("gs1") 54 gs, ok = cache.Load("gs1") 55 assert.Nil(t, gs) 56 assert.False(t, ok) 57 } 58 59 func TestGameServerCacheEntryGeneration(t *testing.T) { 60 // Test case 1: If there's no existing value for a key, the incoming GameServer is stored regardless of its Generation. 61 cache := gameServerCache{} 62 gs1 := &agonesv1.GameServer{ObjectMeta: metav1.ObjectMeta{Name: "gs1", Generation: 1}} 63 64 cache.Store("gs1", gs1) 65 gs, ok := cache.Load("gs1") 66 assert.True(t, ok) 67 assert.Equal(t, gs1, gs) 68 69 // Test case 2: If there's an existing value for a key and the incoming GameServer's Generation is less than the existing one, the existing value is not replaced. 70 gs1Lower := &agonesv1.GameServer{ObjectMeta: metav1.ObjectMeta{Name: "gs1", Generation: 0}} 71 cache.Store("gs1", gs1Lower) 72 gs, ok = cache.Load("gs1") 73 assert.True(t, ok) 74 assert.Equal(t, int64(1), gs.Generation, "Should not replace with lower Generation") 75 76 // Test case 3: If there's an existing value for a key and the incoming GameServer's Generation is equal to the existing one and ResourceVersion is the same, the existing value is not replaced. 77 gs1Equal := &agonesv1.GameServer{ObjectMeta: metav1.ObjectMeta{Name: "gs1", Generation: 1, ResourceVersion: ""}} 78 cache.Store("gs1", gs1Equal) 79 gs, ok = cache.Load("gs1") 80 assert.True(t, ok) 81 assert.Equal(t, int64(1), gs.Generation, "Should not replace with equal Generation and same ResourceVersion") 82 83 // Test case 4: If there's an existing value for a key and the incoming GameServer's Generation is greater than the existing one, the existing value is replaced. 84 gs1Higher := &agonesv1.GameServer{ObjectMeta: metav1.ObjectMeta{Name: "gs1", Generation: 2, ResourceVersion: "3"}} 85 cache.Store("gs1", gs1Higher) 86 gs, ok = cache.Load("gs1") 87 assert.True(t, ok) 88 assert.Equal(t, int64(2), gs.Generation, "Should replace with higher Generation") 89 assert.Equal(t, "3", gs.ResourceVersion, "Should replace with higher Generation") 90 91 // Test case 5: If there's an existing value for a key and the incoming GameServer's Generation is equal to the existing one but ResourceVersion is different, the existing value is replaced. 92 gs1SameGenDiffRV := &agonesv1.GameServer{ObjectMeta: metav1.ObjectMeta{Name: "gs1", Generation: 2, ResourceVersion: "4"}} 93 cache.Store("gs1", gs1SameGenDiffRV) 94 gs, ok = cache.Load("gs1") 95 assert.True(t, ok) 96 assert.Equal(t, int64(2), gs.Generation, "Generation should remain the same") 97 assert.Equal(t, "4", gs.ResourceVersion, "Should replace with different ResourceVersion") 98 }