|
| 1 | +// Copyright 2026 The kpt and Nephio Authors |
| 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 repomap |
| 16 | + |
| 17 | +import ( |
| 18 | + "errors" |
| 19 | + "sync" |
| 20 | + "sync/atomic" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/nephio-project/porch/pkg/repository" |
| 24 | + mockrepository "github.com/nephio-project/porch/test/mockery/mocks/porch/pkg/repository" |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | +) |
| 27 | + |
| 28 | +func TestLoadOrCreate_Success(t *testing.T) { |
| 29 | + m := &SafeRepoMap{} |
| 30 | + key := repository.RepositoryKey{Name: "test-repo"} |
| 31 | + |
| 32 | + repo, err := m.LoadOrCreate(key, func() (repository.Repository, error) { |
| 33 | + mock := &mockrepository.MockRepository{} |
| 34 | + mock.On("KubeObjectName").Return("created") |
| 35 | + return mock, nil |
| 36 | + }) |
| 37 | + |
| 38 | + assert.NoError(t, err) |
| 39 | + assert.NotNil(t, repo) |
| 40 | +} |
| 41 | + |
| 42 | +func TestLoadOrCreate_Reuse(t *testing.T) { |
| 43 | + m := &SafeRepoMap{} |
| 44 | + key := repository.RepositoryKey{Name: "test-repo"} |
| 45 | + |
| 46 | + var callCount int32 |
| 47 | + create := func() (repository.Repository, error) { |
| 48 | + atomic.AddInt32(&callCount, 1) |
| 49 | + return &mockrepository.MockRepository{}, nil |
| 50 | + } |
| 51 | + |
| 52 | + repo1, err1 := m.LoadOrCreate(key, create) |
| 53 | + repo2, err2 := m.LoadOrCreate(key, create) |
| 54 | + |
| 55 | + assert.NoError(t, err1) |
| 56 | + assert.NoError(t, err2) |
| 57 | + assert.Same(t, repo1, repo2) |
| 58 | + assert.Equal(t, int32(1), atomic.LoadInt32(&callCount)) |
| 59 | +} |
| 60 | + |
| 61 | +func TestLoadOrCreate_ErrorRetry(t *testing.T) { |
| 62 | + m := &SafeRepoMap{} |
| 63 | + key := repository.RepositoryKey{Name: "test-repo"} |
| 64 | + |
| 65 | + var callCount int32 |
| 66 | + create := func() (repository.Repository, error) { |
| 67 | + count := atomic.AddInt32(&callCount, 1) |
| 68 | + if count == 1 { |
| 69 | + return nil, errors.New("first attempt failed") |
| 70 | + } |
| 71 | + return &mockrepository.MockRepository{}, nil |
| 72 | + } |
| 73 | + |
| 74 | + // First call should fail |
| 75 | + repo1, err1 := m.LoadOrCreate(key, create) |
| 76 | + assert.Error(t, err1) |
| 77 | + assert.Nil(t, repo1) |
| 78 | + |
| 79 | + // Second call should succeed (retry) |
| 80 | + repo2, err2 := m.LoadOrCreate(key, create) |
| 81 | + assert.NoError(t, err2) |
| 82 | + assert.NotNil(t, repo2) |
| 83 | + assert.Equal(t, int32(2), atomic.LoadInt32(&callCount)) |
| 84 | +} |
| 85 | + |
| 86 | +func TestLoadOrCreate_Concurrent(t *testing.T) { |
| 87 | + m := &SafeRepoMap{} |
| 88 | + key := repository.RepositoryKey{Name: "test-repo"} |
| 89 | + |
| 90 | + var callCount int32 |
| 91 | + create := func() (repository.Repository, error) { |
| 92 | + atomic.AddInt32(&callCount, 1) |
| 93 | + return &mockrepository.MockRepository{}, nil |
| 94 | + } |
| 95 | + |
| 96 | + const goroutines = 10 |
| 97 | + var wg sync.WaitGroup |
| 98 | + results := make([]repository.Repository, goroutines) |
| 99 | + |
| 100 | + for i := 0; i < goroutines; i++ { |
| 101 | + wg.Add(1) |
| 102 | + go func(idx int) { |
| 103 | + defer wg.Done() |
| 104 | + repo, err := m.LoadOrCreate(key, create) |
| 105 | + assert.NoError(t, err) |
| 106 | + results[idx] = repo |
| 107 | + }(i) |
| 108 | + } |
| 109 | + |
| 110 | + wg.Wait() |
| 111 | + |
| 112 | + // All goroutines should get the same instance |
| 113 | + for i := 1; i < goroutines; i++ { |
| 114 | + assert.Same(t, results[0], results[i]) |
| 115 | + } |
| 116 | + |
| 117 | + // Create should only be called once |
| 118 | + assert.Equal(t, int32(1), atomic.LoadInt32(&callCount)) |
| 119 | +} |
| 120 | + |
| 121 | +func TestLoadOrCreate_ConcurrentError(t *testing.T) { |
| 122 | + m := &SafeRepoMap{} |
| 123 | + key := repository.RepositoryKey{Name: "test-repo"} |
| 124 | + |
| 125 | + var callCount int32 |
| 126 | + create := func() (repository.Repository, error) { |
| 127 | + atomic.AddInt32(&callCount, 1) |
| 128 | + return nil, errors.New("always fails") |
| 129 | + } |
| 130 | + |
| 131 | + const goroutines = 5 |
| 132 | + var wg sync.WaitGroup |
| 133 | + |
| 134 | + for i := 0; i < goroutines; i++ { |
| 135 | + wg.Add(1) |
| 136 | + go func() { |
| 137 | + defer wg.Done() |
| 138 | + _, err := m.LoadOrCreate(key, create) |
| 139 | + assert.Error(t, err) |
| 140 | + }() |
| 141 | + } |
| 142 | + |
| 143 | + wg.Wait() |
| 144 | + |
| 145 | + // With concurrent errors and deletion, multiple goroutines may call create |
| 146 | + // This is expected behavior - failed entries are deleted and retried |
| 147 | + count := atomic.LoadInt32(&callCount) |
| 148 | + assert.GreaterOrEqual(t, count, int32(1)) |
| 149 | + assert.LessOrEqual(t, count, int32(goroutines)) |
| 150 | +} |
| 151 | + |
| 152 | +func TestLoad(t *testing.T) { |
| 153 | + m := &SafeRepoMap{} |
| 154 | + key := repository.RepositoryKey{Name: "test-repo"} |
| 155 | + |
| 156 | + // Load non-existent key |
| 157 | + repo, ok := m.Load(key) |
| 158 | + assert.False(t, ok) |
| 159 | + assert.Nil(t, repo) |
| 160 | + |
| 161 | + // Create a repo |
| 162 | + m.LoadOrCreate(key, func() (repository.Repository, error) { |
| 163 | + return &mockrepository.MockRepository{}, nil |
| 164 | + }) |
| 165 | + |
| 166 | + // Load existing key |
| 167 | + repo, ok = m.Load(key) |
| 168 | + assert.True(t, ok) |
| 169 | + assert.NotNil(t, repo) |
| 170 | +} |
| 171 | + |
| 172 | +func TestLoadAndDelete(t *testing.T) { |
| 173 | + m := &SafeRepoMap{} |
| 174 | + key := repository.RepositoryKey{Name: "test-repo"} |
| 175 | + |
| 176 | + // LoadAndDelete non-existent key |
| 177 | + repo, ok := m.LoadAndDelete(key) |
| 178 | + assert.False(t, ok) |
| 179 | + assert.Nil(t, repo) |
| 180 | + |
| 181 | + // Create a repo |
| 182 | + m.LoadOrCreate(key, func() (repository.Repository, error) { |
| 183 | + return &mockrepository.MockRepository{}, nil |
| 184 | + }) |
| 185 | + |
| 186 | + // LoadAndDelete existing key |
| 187 | + repo, ok = m.LoadAndDelete(key) |
| 188 | + assert.True(t, ok) |
| 189 | + assert.NotNil(t, repo) |
| 190 | + |
| 191 | + // Verify it's deleted |
| 192 | + repo, ok = m.Load(key) |
| 193 | + assert.False(t, ok) |
| 194 | +} |
| 195 | + |
| 196 | +func TestRange(t *testing.T) { |
| 197 | + m := &SafeRepoMap{} |
| 198 | + |
| 199 | + // Create multiple repos |
| 200 | + keys := []repository.RepositoryKey{ |
| 201 | + {Name: "repo1"}, |
| 202 | + {Name: "repo2"}, |
| 203 | + {Name: "repo3"}, |
| 204 | + } |
| 205 | + |
| 206 | + for _, key := range keys { |
| 207 | + m.LoadOrCreate(key, func() (repository.Repository, error) { |
| 208 | + return &mockrepository.MockRepository{}, nil |
| 209 | + }) |
| 210 | + } |
| 211 | + |
| 212 | + // Range over all entries |
| 213 | + var count int |
| 214 | + m.Range(func(key, value any) bool { |
| 215 | + count++ |
| 216 | + return true |
| 217 | + }) |
| 218 | + |
| 219 | + assert.Equal(t, len(keys), count) |
| 220 | + |
| 221 | + // Test early termination |
| 222 | + count = 0 |
| 223 | + m.Range(func(key, value any) bool { |
| 224 | + count++ |
| 225 | + return count < 2 // Stop after 2 iterations |
| 226 | + }) |
| 227 | + |
| 228 | + assert.Equal(t, 2, count) |
| 229 | +} |
0 commit comments