Skip to content

Commit b5483b2

Browse files
authored
chore: add t.Parallel() to more tests (#1357)
* chore: add t.Parallel() to more tests Signed-off-by: egibs <20933572+egibs@users.noreply.github.com> * fix goroutine synchronization Signed-off-by: egibs <20933572+egibs@users.noreply.github.com> --------- Signed-off-by: egibs <20933572+egibs@users.noreply.github.com>
1 parent 5f1df17 commit b5483b2

13 files changed

Lines changed: 143 additions & 15 deletions

File tree

pkg/action/diff_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
// TestRelPath tests the relPath function which computes relative paths for diff operations.
1515
func TestRelPath(t *testing.T) {
16+
t.Parallel()
1617
tmpDir := t.TempDir()
1718

1819
// Create test file structure
@@ -105,6 +106,7 @@ func TestRelPath(t *testing.T) {
105106

106107
for _, tt := range tests {
107108
t.Run(tt.name, func(t *testing.T) {
109+
t.Parallel()
108110
base, rel, err := relPath(tt.from, tt.fr, tt.isArchive, tt.isImage)
109111
if (err != nil) != tt.wantErr {
110112
t.Errorf("relPath() error = %v, wantErr %v", err, tt.wantErr)
@@ -119,6 +121,7 @@ func TestRelPath(t *testing.T) {
119121
}
120122

121123
func TestIsUPXBackup(t *testing.T) {
124+
t.Parallel()
122125
tests := []struct {
123126
name string
124127
path string
@@ -159,6 +162,7 @@ func TestIsUPXBackup(t *testing.T) {
159162

160163
for _, tt := range tests {
161164
t.Run(tt.name, func(t *testing.T) {
165+
t.Parallel()
162166
got := isUPXBackup(tt.path, tt.files)
163167
if got != tt.want {
164168
t.Errorf("isUPXBackup(%q, ...) = %v, want %v", tt.path, got, tt.want)
@@ -168,6 +172,7 @@ func TestIsUPXBackup(t *testing.T) {
168172
}
169173

170174
func TestSelectPrimaryFile(t *testing.T) {
175+
t.Parallel()
171176
tests := []struct {
172177
name string
173178
files map[string]*malcontent.FileReport
@@ -197,6 +202,7 @@ func TestSelectPrimaryFile(t *testing.T) {
197202

198203
for _, tt := range tests {
199204
t.Run(tt.name, func(t *testing.T) {
205+
t.Parallel()
200206
got := selectPrimaryFile(tt.files)
201207
if got == nil && tt.want == "" {
202208
return // both nil, OK
@@ -213,6 +219,7 @@ func TestSelectPrimaryFile(t *testing.T) {
213219
}
214220

215221
func TestFormatKey(t *testing.T) {
222+
t.Parallel()
216223
tests := []struct {
217224
name string
218225
res ScanResult
@@ -241,6 +248,7 @@ func TestFormatKey(t *testing.T) {
241248

242249
for _, tt := range tests {
243250
t.Run(tt.name, func(t *testing.T) {
251+
t.Parallel()
244252
got := formatKey(tt.res, tt.file)
245253
if got != tt.want {
246254
t.Errorf("formatKey() = %q, want %q", got, tt.want)

pkg/action/path_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
)
1414

1515
func TestFindFilesRecursively(t *testing.T) {
16+
t.Parallel()
1617
// Create temporary test directory structure
1718
tmpDir := t.TempDir()
1819

@@ -80,6 +81,7 @@ func TestFindFilesRecursively(t *testing.T) {
8081

8182
for _, tt := range tests {
8283
t.Run(tt.name, func(t *testing.T) {
84+
t.Parallel()
8385
ctx := context.Background()
8486
got, err := findFilesRecursively(ctx, tt.rootPath)
8587

@@ -104,6 +106,7 @@ func TestFindFilesRecursively(t *testing.T) {
104106
}
105107

106108
func TestFindFilesRecursivelySymlinks(t *testing.T) {
109+
t.Parallel()
107110
tmpDir := t.TempDir()
108111

109112
// Create a file
@@ -131,6 +134,7 @@ func TestFindFilesRecursivelySymlinks(t *testing.T) {
131134
}
132135

133136
func TestFindFilesRecursivelySymlinkRoot(t *testing.T) {
137+
t.Parallel()
134138
tmpDir := t.TempDir()
135139

136140
// Create a directory with a file
@@ -163,6 +167,7 @@ func TestFindFilesRecursivelySymlinkRoot(t *testing.T) {
163167
}
164168

165169
func TestFindFilesRecursivelyCanceledContext(t *testing.T) {
170+
t.Parallel()
166171
tmpDir := t.TempDir()
167172

168173
// Create a file
@@ -181,6 +186,7 @@ func TestFindFilesRecursivelyCanceledContext(t *testing.T) {
181186
}
182187

183188
func TestFindFilesRecursivelyPermissionDenied(t *testing.T) {
189+
t.Parallel()
184190
if os.Getuid() == 0 {
185191
t.Skip("Skipping permission test when running as root")
186192
}
@@ -232,6 +238,7 @@ func TestFindFilesRecursivelyPermissionDenied(t *testing.T) {
232238
}
233239

234240
func TestFindFilesRecursivelyEmptyDirectory(t *testing.T) {
241+
t.Parallel()
235242
tmpDir := t.TempDir()
236243

237244
ctx := context.Background()
@@ -246,6 +253,7 @@ func TestFindFilesRecursivelyEmptyDirectory(t *testing.T) {
246253
}
247254

248255
func TestFindFilesRecursivelyDeepNesting(t *testing.T) {
256+
t.Parallel()
249257
tmpDir := t.TempDir()
250258

251259
// Create deeply nested structure
@@ -275,6 +283,7 @@ func TestFindFilesRecursivelyDeepNesting(t *testing.T) {
275283
}
276284

277285
func TestCleanPath(t *testing.T) {
286+
t.Parallel()
278287
tests := []struct {
279288
name string
280289
path string
@@ -321,6 +330,7 @@ func TestCleanPath(t *testing.T) {
321330

322331
for _, tt := range tests {
323332
t.Run(tt.name, func(t *testing.T) {
333+
t.Parallel()
324334
got := CleanPath(tt.path, tt.prefix)
325335
if got != tt.want {
326336
t.Errorf("CleanPath(%q, %q) = %q, want %q", tt.path, tt.prefix, got, tt.want)
@@ -330,6 +340,7 @@ func TestCleanPath(t *testing.T) {
330340
}
331341

332342
func TestFormatPath(t *testing.T) {
343+
t.Parallel()
333344
tests := []struct {
334345
name string
335346
path string
@@ -364,6 +375,7 @@ func TestFormatPath(t *testing.T) {
364375

365376
for _, tt := range tests {
366377
t.Run(tt.name, func(t *testing.T) {
378+
t.Parallel()
367379
got := formatPath(tt.path)
368380
if got != tt.want {
369381
t.Errorf("formatPath(%q) = %q, want %q", tt.path, got, tt.want)

pkg/action/scan.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -414,18 +414,23 @@ func processPaths(ctx context.Context, paths []string, scanInfo scanPathInfo, c
414414
maxConcurrency := getMaxConcurrency(min(c.Concurrency, numPaths))
415415

416416
scanCtx, cancel := context.WithCancel(ctx)
417-
defer cancel()
418417

419-
go func() {
418+
var wg sync.WaitGroup
419+
wg.Go(func() {
420420
<-scanCtx.Done()
421421
logger.Debug("parent context canceled, stopping scan")
422422
cancel()
423+
})
424+
defer func() {
425+
cancel()
426+
wg.Wait()
423427
}()
424428

425429
g, gCtx := errgroup.WithContext(scanCtx)
426430
g.SetLimit(maxConcurrency)
427431

428-
setupMatchHandler(gCtx, matchChan, c, cancel, logger)
432+
waitMatchHandler := setupMatchHandler(gCtx, matchChan, c, cancel, logger)
433+
defer waitMatchHandler()
429434

430435
pc := make(chan string, numPaths)
431436
go func() {
@@ -476,12 +481,13 @@ func getMaxConcurrency(configured int) int {
476481
return max(1, configured)
477482
}
478483

479-
func setupMatchHandler(ctx context.Context, matchChan chan matchResult, c malcontent.Config, cancel context.CancelFunc, logger *clog.Logger) {
484+
func setupMatchHandler(ctx context.Context, matchChan chan matchResult, c malcontent.Config, cancel context.CancelFunc, logger *clog.Logger) func() {
480485
if ctx.Err() != nil {
481-
return
486+
return func() {}
482487
}
483488

484-
go func() {
489+
var wg sync.WaitGroup
490+
wg.Go(func() {
485491
select {
486492
case <-ctx.Done():
487493
return
@@ -493,7 +499,8 @@ func setupMatchHandler(ctx context.Context, matchChan chan matchResult, c malcon
493499
}
494500
cancel()
495501
}
496-
}()
502+
})
503+
return wg.Wait
497504
}
498505

499506
func processPath(ctx context.Context, path string, scanInfo scanPathInfo, c malcontent.Config, r *malcontent.Report, matchChan chan matchResult, matchOnce *sync.Once, logger *clog.Logger) error {

pkg/archive/symlink_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
)
1212

1313
func TestSymlinkExtraction(t *testing.T) {
14+
t.Parallel()
1415
tests := []struct {
1516
name string
1617
tarFile string
@@ -31,6 +32,7 @@ func TestSymlinkExtraction(t *testing.T) {
3132

3233
for _, tt := range tests {
3334
t.Run(tt.name, func(t *testing.T) {
35+
t.Parallel()
3436
tmpDir, err := os.MkdirTemp("", "symlink-test-*")
3537
if err != nil {
3638
t.Fatalf("failed to create temp dir: %v", err)
@@ -54,6 +56,7 @@ func TestSymlinkExtraction(t *testing.T) {
5456
}
5557

5658
func TestHandleSymlink(t *testing.T) {
59+
t.Parallel()
5760
tmpDir, err := os.MkdirTemp("", "symlink-test-*")
5861
if err != nil {
5962
t.Fatalf("failed to create temp dir: %v", err)

pkg/compile/compile_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func clearRulesCacheB(b *testing.B, fss []fs.FS) {
6565
}
6666

6767
func TestRecursive(t *testing.T) {
68+
t.Parallel()
6869
ctx := context.Background()
6970

7071
rules, err := Recursive(ctx, getAllRuleFS())
@@ -78,6 +79,7 @@ func TestRecursive(t *testing.T) {
7879
}
7980

8081
func TestGetRulesHash(t *testing.T) {
82+
t.Parallel()
8183
ctx := context.Background()
8284

8385
fss := getAllRuleFS()
@@ -103,6 +105,7 @@ func TestGetRulesHash(t *testing.T) {
103105
}
104106

105107
func TestCacheOperations(t *testing.T) {
108+
t.Parallel()
106109
ctx := context.Background()
107110

108111
tempDir := t.TempDir()
@@ -140,6 +143,7 @@ func TestCacheOperations(t *testing.T) {
140143
}
141144

142145
func TestRecursiveCached(t *testing.T) {
146+
t.Parallel()
143147
ctx := context.Background()
144148

145149
fss := getAllRuleFS()
@@ -187,6 +191,7 @@ func TestRecursiveCached(t *testing.T) {
187191
}
188192

189193
func TestRecursiveCachedFallback(t *testing.T) {
194+
t.Parallel()
190195
ctx := context.Background()
191196

192197
rules, err := RecursiveCached(ctx, getAllRuleFS())
@@ -200,6 +205,7 @@ func TestRecursiveCachedFallback(t *testing.T) {
200205
}
201206

202207
func TestGetCacheDir(t *testing.T) {
208+
t.Parallel()
203209
cacheDir, err := getCacheDir()
204210
if err != nil {
205211
t.Fatalf("getCacheDir failed: %v", err)
@@ -228,6 +234,7 @@ func TestGetCacheDir(t *testing.T) {
228234
}
229235

230236
func TestCacheFileSize(t *testing.T) {
237+
t.Parallel()
231238
ctx := context.Background()
232239

233240
tempDir := t.TempDir()

pkg/file/file_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
)
1212

1313
func TestGetContents(t *testing.T) {
14+
t.Parallel()
1415
tests := []struct {
1516
name string
1617
content []byte
@@ -103,6 +104,7 @@ func TestGetContents(t *testing.T) {
103104

104105
for _, tt := range tests {
105106
t.Run(tt.name, func(t *testing.T) {
107+
t.Parallel()
106108
tmpDir := t.TempDir()
107109
tmpFile := filepath.Join(tmpDir, "testfile")
108110

@@ -136,6 +138,7 @@ func TestGetContents(t *testing.T) {
136138
}
137139

138140
func TestGetContentsClosedFile(t *testing.T) {
141+
t.Parallel()
139142
tmpDir := t.TempDir()
140143
tmpFile := filepath.Join(tmpDir, "testfile")
141144

@@ -158,6 +161,7 @@ func TestGetContentsClosedFile(t *testing.T) {
158161
}
159162

160163
func TestGetContentsMaxBytesLimit(t *testing.T) {
164+
t.Parallel()
161165
tmpDir := t.TempDir()
162166
tmpFile := filepath.Join(tmpDir, "largefile")
163167

@@ -210,6 +214,7 @@ func TestGetContentsMaxBytesLimit(t *testing.T) {
210214
}
211215

212216
func TestGetContentsNilBuffer(t *testing.T) {
217+
t.Parallel()
213218
tmpDir := t.TempDir()
214219
tmpFile := filepath.Join(tmpDir, "testfile")
215220

@@ -235,6 +240,7 @@ func TestGetContentsNilBuffer(t *testing.T) {
235240
}
236241

237242
func TestConstants(t *testing.T) {
243+
t.Parallel()
238244
tests := []struct {
239245
name string
240246
got int64
@@ -250,6 +256,7 @@ func TestConstants(t *testing.T) {
250256

251257
for _, tt := range tests {
252258
t.Run(tt.name, func(t *testing.T) {
259+
t.Parallel()
253260
if tt.got != tt.want {
254261
t.Errorf("%s = %d, want %d", tt.name, tt.got, tt.want)
255262
}

0 commit comments

Comments
 (0)