|
8 | 8 | "path/filepath"
|
9 | 9 | "sync"
|
10 | 10 | "testing"
|
| 11 | + "time" |
11 | 12 | )
|
12 | 13 |
|
13 | 14 | func Test_mirror_detect_race(t *testing.T) {
|
@@ -106,3 +107,176 @@ func Test_mirror_detect_race(t *testing.T) {
|
106 | 107 | })
|
107 | 108 |
|
108 | 109 | }
|
| 110 | + |
| 111 | +func Test_mirror_detect_race_repo_pool(t *testing.T) { |
| 112 | + testTmpDir := mustTmpDir(t) |
| 113 | + defer os.RemoveAll(testTmpDir) |
| 114 | + |
| 115 | + tempClone := mustTmpDir(t) |
| 116 | + defer os.RemoveAll(tempClone) |
| 117 | + |
| 118 | + upstream1 := filepath.Join(testTmpDir, testUpstreamRepo) |
| 119 | + remote1 := "file://" + upstream1 |
| 120 | + upstream2 := filepath.Join(testTmpDir, "upstream2") |
| 121 | + remote2 := "file://" + upstream2 |
| 122 | + root := filepath.Join(testTmpDir, testRoot) |
| 123 | + |
| 124 | + fileU1SHA1 := mustInitRepo(t, upstream1, "file", t.Name()+"-u1-main-1") |
| 125 | + fileU2SHA1 := mustInitRepo(t, upstream2, "file", t.Name()+"-u2-main-1") |
| 126 | + |
| 127 | + rpc := RepoPoolConfig{ |
| 128 | + Defaults: DefaultConfig{ |
| 129 | + Root: root, Interval: testInterval, MirrorTimeout: testTimeout, GitGC: "always", |
| 130 | + }, |
| 131 | + } |
| 132 | + |
| 133 | + rp, err := NewRepoPool(t.Context(), rpc, testLog, testENVs) |
| 134 | + if err != nil { |
| 135 | + t.Fatalf("unexpected error: %v", err) |
| 136 | + } |
| 137 | + |
| 138 | + t.Run("add-remove-repo-test", func(t *testing.T) { |
| 139 | + wg := &sync.WaitGroup{} |
| 140 | + wg.Add(1) |
| 141 | + |
| 142 | + // add/remove 2 repositories |
| 143 | + go func() { |
| 144 | + defer wg.Done() |
| 145 | + for i := 0; i < 10; i++ { |
| 146 | + t.Log("adding remote1", "count", i) |
| 147 | + readStopped := make(chan bool) |
| 148 | + ctx, cancel := context.WithCancel(t.Context()) |
| 149 | + |
| 150 | + newConfig := RepoPoolConfig{ |
| 151 | + Defaults: DefaultConfig{ |
| 152 | + Root: root, Interval: testInterval, MirrorTimeout: testTimeout, GitGC: "always", |
| 153 | + }, |
| 154 | + Repositories: []RepositoryConfig{{ |
| 155 | + Remote: remote1, |
| 156 | + Worktrees: []WorktreeConfig{{Link: "link1"}}}, |
| 157 | + }, |
| 158 | + } |
| 159 | + if err := newConfig.ValidateAndApplyDefaults(); err != nil { |
| 160 | + t.Error("failed to validate new config", "err", err) |
| 161 | + return |
| 162 | + } |
| 163 | + |
| 164 | + if err := rp.AddRepository(newConfig.Repositories[0]); err != nil { |
| 165 | + t.Error("unexpected err", "err", err) |
| 166 | + return |
| 167 | + } |
| 168 | + |
| 169 | + rp.StartLoop() |
| 170 | + |
| 171 | + go func() { |
| 172 | + for { |
| 173 | + time.Sleep(1 * time.Second) |
| 174 | + select { |
| 175 | + case <-ctx.Done(): |
| 176 | + close(readStopped) |
| 177 | + return |
| 178 | + default: |
| 179 | + if got, err := rp.Hash(txtCtx, remote1, "HEAD", ""); err != nil { |
| 180 | + t.Error("unexpected err", "count", i, "err", err) |
| 181 | + } else if got != fileU1SHA1 { |
| 182 | + t.Errorf("remote1 hash mismatch got:%s want:%s", got, fileU1SHA1) |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + } |
| 187 | + }() |
| 188 | + |
| 189 | + if err := rp.AddWorktreeLink(remote1, WorktreeConfig{"link2", "", []string{}}); err != nil { |
| 190 | + t.Error("unexpected err", "err", err) |
| 191 | + return |
| 192 | + } |
| 193 | + |
| 194 | + time.Sleep(2 * time.Second) |
| 195 | + |
| 196 | + if err := rp.RemoveWorktreeLink(remote1, "link1"); err != nil { |
| 197 | + t.Error("unexpected err", "err", err) |
| 198 | + return |
| 199 | + } |
| 200 | + |
| 201 | + cancel() |
| 202 | + <-readStopped |
| 203 | + |
| 204 | + if err := rp.RemoveRepository(remote1); err != nil { |
| 205 | + t.Error("unexpected err", "err", err) |
| 206 | + return |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + }() |
| 211 | + |
| 212 | + go func() { |
| 213 | + defer wg.Done() |
| 214 | + for i := 0; i < 10; i++ { |
| 215 | + t.Log("adding remote2", "count", i) |
| 216 | + readStopped := make(chan bool) |
| 217 | + ctx, cancel := context.WithCancel(t.Context()) |
| 218 | + |
| 219 | + newConfig := RepoPoolConfig{ |
| 220 | + Defaults: DefaultConfig{ |
| 221 | + Root: root, Interval: testInterval, MirrorTimeout: testTimeout, GitGC: "always", |
| 222 | + }, |
| 223 | + Repositories: []RepositoryConfig{{Remote: remote2, |
| 224 | + Worktrees: []WorktreeConfig{{Link: "link3"}}}, |
| 225 | + }, |
| 226 | + } |
| 227 | + if err := newConfig.ValidateAndApplyDefaults(); err != nil { |
| 228 | + t.Error("failed to validate new config", "err", err) |
| 229 | + return |
| 230 | + } |
| 231 | + |
| 232 | + if err := rp.AddRepository(newConfig.Repositories[0]); err != nil { |
| 233 | + t.Error("unexpected err", "err", err) |
| 234 | + return |
| 235 | + } |
| 236 | + |
| 237 | + rp.StartLoop() |
| 238 | + |
| 239 | + // start loop to trigger read on repo pool |
| 240 | + go func() { |
| 241 | + for { |
| 242 | + time.Sleep(1 * time.Second) |
| 243 | + select { |
| 244 | + case <-ctx.Done(): |
| 245 | + close(readStopped) |
| 246 | + return |
| 247 | + default: |
| 248 | + if got, err := rp.Hash(txtCtx, remote2, "HEAD", ""); err != nil { |
| 249 | + t.Error("unexpected err", "count", i, "err", err) |
| 250 | + } else if got != fileU2SHA1 { |
| 251 | + t.Errorf("remote2 hash mismatch got:%s want:%s", got, fileU2SHA1) |
| 252 | + } |
| 253 | + } |
| 254 | + } |
| 255 | + }() |
| 256 | + |
| 257 | + if err := rp.AddWorktreeLink(remote2, WorktreeConfig{"link4", "", []string{}}); err != nil { |
| 258 | + t.Error("unexpected err", "err", err) |
| 259 | + return |
| 260 | + } |
| 261 | + |
| 262 | + time.Sleep(2 * time.Second) |
| 263 | + |
| 264 | + if err := rp.RemoveWorktreeLink(remote2, "link3"); err != nil { |
| 265 | + t.Error("unexpected err", "err", err) |
| 266 | + return |
| 267 | + } |
| 268 | + |
| 269 | + cancel() |
| 270 | + |
| 271 | + <-readStopped |
| 272 | + |
| 273 | + if err := rp.RemoveRepository(remote2); err != nil { |
| 274 | + t.Error("unexpected err", "err", err) |
| 275 | + return |
| 276 | + } |
| 277 | + } |
| 278 | + }() |
| 279 | + |
| 280 | + wg.Wait() |
| 281 | + }) |
| 282 | +} |
0 commit comments