-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_concurrent_test.go
More file actions
443 lines (368 loc) · 10.2 KB
/
config_concurrent_test.go
File metadata and controls
443 lines (368 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
package gitconfig
import (
"os"
"path/filepath"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestConcurrentReads tests that multiple goroutines can safely read from the same config.
func TestConcurrentReads(t *testing.T) {
t.Parallel()
td := t.TempDir()
configPath := filepath.Join(td, "config")
// Create a config with multiple keys
content := `[user]
name = John Doe
email = john@example.com
[core]
editor = vim
autocrlf = true
filemode = false
[remote "origin"]
url = https://github.com/test/repo.git
fetch = +refs/heads/*:refs/remotes/origin/*
`
err := os.WriteFile(configPath, []byte(content), 0o644)
require.NoError(t, err)
cfg, err := LoadConfig(configPath)
require.NoError(t, err)
require.NotNil(t, cfg)
// Launch multiple goroutines reading different keys
var wg sync.WaitGroup
iterations := 100
goroutines := 10
for g := range goroutines {
wg.Add(1)
go func(id int) {
defer wg.Done()
for range iterations {
// Each goroutine reads different keys based on its ID
switch id % 3 {
case 0:
name, ok := cfg.Get("user.name")
assert.True(t, ok)
assert.Equal(t, "John Doe", name)
case 1:
editor, ok := cfg.Get("core.editor")
assert.True(t, ok)
assert.Equal(t, "vim", editor)
case 2:
url, ok := cfg.Get("remote.origin.url")
assert.True(t, ok)
assert.Equal(t, "https://github.com/test/repo.git", url)
}
}
}(g)
}
wg.Wait()
}
// TestConcurrentLoad tests that loading multiple configs concurrently is safe.
func TestConcurrentLoad(t *testing.T) {
t.Parallel()
td := t.TempDir()
// Create multiple config files
configs := make([]string, 5)
for i := range configs {
configPath := filepath.Join(td, "config"+string(rune('0'+i)))
content := "[user]\n\tname = User" + string(rune('0'+i))
err := os.WriteFile(configPath, []byte(content), 0o644)
require.NoError(t, err)
configs[i] = configPath
}
// Load all configs concurrently
var wg sync.WaitGroup
results := make([]*Config, len(configs))
errors := make([]error, len(configs))
for i := range configs {
wg.Add(1)
go func(index int) {
defer wg.Done()
cfg, err := LoadConfig(configs[index])
results[index] = cfg
errors[index] = err
}(i)
}
wg.Wait()
// Verify all loads succeeded
for i := range configs {
require.NoError(t, errors[i], "config %d should load without error", i)
require.NotNil(t, results[i], "config %d should not be nil", i)
name, ok := results[i].Get("user.name")
assert.True(t, ok)
assert.Equal(t, "User"+string(rune('0'+i)), name)
}
}
// TestConcurrentReadsSameKey tests race conditions when reading the same key.
func TestConcurrentReadsSameKey(t *testing.T) {
t.Parallel()
td := t.TempDir()
configPath := filepath.Join(td, "config")
content := "[user]\n\tname = Concurrent Test"
err := os.WriteFile(configPath, []byte(content), 0o644)
require.NoError(t, err)
cfg, err := LoadConfig(configPath)
require.NoError(t, err)
require.NotNil(t, cfg)
// Many goroutines reading the same key
var wg sync.WaitGroup
iterations := 50
goroutines := 20
for range goroutines {
wg.Add(1)
go func() {
defer wg.Done()
for range iterations {
name, ok := cfg.Get("user.name")
assert.True(t, ok)
assert.Equal(t, "Concurrent Test", name)
}
}()
}
wg.Wait()
}
// TestConcurrentGetAll tests concurrent access to multi-valued keys.
func TestConcurrentGetAll(t *testing.T) {
t.Parallel()
td := t.TempDir()
configPath := filepath.Join(td, "config")
content := `[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
fetch = +refs/tags/*:refs/tags/*
fetch = +refs/pull/*/head:refs/remotes/origin/pr/*
`
err := os.WriteFile(configPath, []byte(content), 0o644)
require.NoError(t, err)
cfg, err := LoadConfig(configPath)
require.NoError(t, err)
require.NotNil(t, cfg)
var wg sync.WaitGroup
iterations := 50
goroutines := 10
for range goroutines {
wg.Add(1)
go func() {
defer wg.Done()
for range iterations {
values, ok := cfg.GetAll("remote.origin.fetch")
assert.True(t, ok)
assert.Len(t, values, 3)
}
}()
}
wg.Wait()
}
// TestSerialWrites tests that writes are properly serialized (no concurrent write support expected).
func TestSerialWrites(t *testing.T) {
t.Parallel()
td := t.TempDir()
configPath := filepath.Join(td, "config")
content := "[user]\n\tname = Initial"
err := os.WriteFile(configPath, []byte(content), 0o644)
require.NoError(t, err)
// Load separate config instances for each write
configs := make([]*Config, 5)
for i := range configs {
cfg, err := LoadConfig(configPath)
require.NoError(t, err)
configs[i] = cfg
}
// Write sequentially (not concurrently, as that would cause data loss)
// Set automatically writes to disk
for i, cfg := range configs {
err := cfg.Set("user.id", string(rune('0'+i)))
require.NoError(t, err)
}
// Load final state
finalCfg, err := LoadConfig(configPath)
require.NoError(t, err)
// Last write should win
id, ok := finalCfg.Get("user.id")
assert.True(t, ok)
assert.Equal(t, "4", id)
}
// TestConcurrentMultiScopeReads tests concurrent reads across multiple scopes.
func TestConcurrentMultiScopeReads(t *testing.T) {
// Note: not using t.Parallel() because we need t.Setenv()
td := t.TempDir()
t.Setenv("GOPASS_HOMEDIR", td)
gitDir := filepath.Join(td, ".git")
require.NoError(t, os.MkdirAll(gitDir, 0o755))
// Create local config
localPath := filepath.Join(gitDir, "config")
localContent := "[user]\n\tname = Local User\n\temail = local@example.com"
err := os.WriteFile(localPath, []byte(localContent), 0o644)
require.NoError(t, err)
// Create global config
globalPath := filepath.Join(td, "global-config")
globalContent := "[user]\n\tname = Global User\n[core]\n\teditor = vim"
err = os.WriteFile(globalPath, []byte(globalContent), 0o644)
require.NoError(t, err)
// Load configs
cs := New()
cs.GlobalConfig = "global-config"
cs.LocalConfig = ".git/config"
cs.NoWrites = true
cs.LoadAll(td)
// Concurrent reads from different scopes
var wg sync.WaitGroup
iterations := 50
goroutines := 10
for g := range goroutines {
wg.Add(1)
go func(id int) {
defer wg.Done()
for range iterations {
switch id % 3 {
case 0:
// Read value that exists in local scope
name := cs.GetLocal("user.name")
assert.Equal(t, "Local User", name)
case 1:
// Read value from global scope
editor := cs.GetGlobal("core.editor")
assert.Equal(t, "vim", editor)
case 2:
// Read with precedence (local wins)
name := cs.Get("user.name")
assert.Equal(t, "Local User", name)
}
}
}(g)
}
wg.Wait()
}
// TestConcurrentConfigCreation tests creating multiple config instances concurrently.
func TestConcurrentConfigCreation(t *testing.T) {
t.Parallel()
var wg sync.WaitGroup
goroutines := 10
results := make([]*Configs, goroutines)
for i := range goroutines {
wg.Add(1)
go func(index int) {
defer wg.Done()
results[index] = New()
}(i)
}
wg.Wait()
// Verify all instances were created successfully
for i := range goroutines {
assert.NotNil(t, results[i], "config instance %d should not be nil", i)
assert.NotEmpty(t, results[i].LocalConfig)
assert.NotEmpty(t, results[i].WorktreeConfig)
}
}
// TestConcurrentEnvConfigLoad tests loading environment configs concurrently.
func TestConcurrentEnvConfigLoad(t *testing.T) {
// Set up test environment variables
testPrefix := "GITCONFIG_CONCURRENT"
t.Setenv(testPrefix+"_COUNT", "2")
t.Setenv(testPrefix+"_KEY_0", "user.name")
t.Setenv(testPrefix+"_VALUE_0", "Env User")
t.Setenv(testPrefix+"_KEY_1", "user.email")
t.Setenv(testPrefix+"_VALUE_1", "env@example.com")
var wg sync.WaitGroup
goroutines := 10
results := make([]*Config, goroutines)
for i := range goroutines {
wg.Add(1)
go func(index int) {
defer wg.Done()
results[index] = LoadConfigFromEnv(testPrefix)
}(i)
}
wg.Wait()
// Verify all loads succeeded
for i := range goroutines {
require.NotNil(t, results[i], "env config %d should not be nil", i)
name, ok := results[i].Get("user.name")
assert.True(t, ok)
assert.Equal(t, "Env User", name)
email, ok := results[i].Get("user.email")
assert.True(t, ok)
assert.Equal(t, "env@example.com", email)
}
}
// TestConcurrentReadDuringLoad tests reading while other configs are being loaded.
func TestConcurrentReadDuringLoad(t *testing.T) {
t.Parallel()
td := t.TempDir()
configPath := filepath.Join(td, "config")
content := "[user]\n\tname = Load Test User"
err := os.WriteFile(configPath, []byte(content), 0o644)
require.NoError(t, err)
// Load initial config
cfg, err := LoadConfig(configPath)
require.NoError(t, err)
require.NotNil(t, cfg)
var wg sync.WaitGroup
readGoroutines := 5
loadGoroutines := 5
duration := 100 * time.Millisecond
// Goroutines continuously reading from existing config
for range readGoroutines {
wg.Add(1)
go func() {
defer wg.Done()
end := time.Now().Add(duration)
for time.Now().Before(end) {
name, ok := cfg.Get("user.name")
assert.True(t, ok)
assert.Equal(t, "Load Test User", name)
}
}()
}
// Goroutines loading new config instances
for range loadGoroutines {
wg.Add(1)
go func() {
defer wg.Done()
end := time.Now().Add(duration)
for time.Now().Before(end) {
newCfg, err := LoadConfig(configPath)
assert.NoError(t, err)
if newCfg != nil {
name, ok := newCfg.Get("user.name")
assert.True(t, ok)
assert.Equal(t, "Load Test User", name)
}
}
}()
}
wg.Wait()
}
// TestNoDataRacesInGet tests that Get operations don't cause data races.
func TestNoDataRacesInGet(t *testing.T) {
t.Parallel()
td := t.TempDir()
configPath := filepath.Join(td, "config")
content := `[user]
name = Race Test
[core]
editor = vim
[remote "origin"]
url = https://github.com/test/repo.git
`
err := os.WriteFile(configPath, []byte(content), 0o644)
require.NoError(t, err)
cfg, err := LoadConfig(configPath)
require.NoError(t, err)
require.NotNil(t, cfg)
// Run with race detector enabled: go test -race
var wg sync.WaitGroup
for range 50 {
wg.Add(1)
go func() {
defer wg.Done()
for range 100 {
_, _ = cfg.Get("user.name")
_, _ = cfg.Get("core.editor")
_, _ = cfg.Get("remote.origin.url")
}
}()
}
wg.Wait()
}