Skip to content

Commit 5eb5b31

Browse files
committed
Testing
1 parent 90b07ef commit 5eb5b31

File tree

2 files changed

+64
-7
lines changed

2 files changed

+64
-7
lines changed

internal/core/concurrency.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ func ParseConcurrency(options *CompilerOptions) Concurrency {
2323
return parseConcurrency(options.Concurrency)
2424
}
2525

26-
func parseConcurrency(v string) Concurrency {
26+
func parseConcurrency(s string) Concurrency {
2727
checkerCount := 4
2828

29-
switch strings.ToLower(v) {
29+
switch strings.ToLower(s) {
3030
case "default", "auto", "true", "yes", "on":
3131
break
3232
case "single", "none", "false", "no", "off":
@@ -38,8 +38,10 @@ func parseConcurrency(v string) Concurrency {
3838
case "checker-per-file":
3939
checkerCount = -1
4040
default:
41-
if v, err := strconv.Atoi(v); err == nil && v > 0 {
42-
checkerCount = v
41+
if s != "" {
42+
if v, err := strconv.Atoi(s); err == nil && v > 0 {
43+
checkerCount = v
44+
}
4345
}
4446
}
4547

@@ -53,9 +55,11 @@ func (c Concurrency) SingleThreaded() bool {
5355
}
5456

5557
func (c Concurrency) CheckerCount(numFiles int) int {
56-
checkerCount := min(c.checkerCount, numFiles)
57-
checkerCount = max(1, checkerCount)
58-
return checkerCount
58+
checkerCount := c.checkerCount
59+
if c.checkerCount == -1 {
60+
return max(1, numFiles)
61+
}
62+
return min(max(1, checkerCount), numFiles)
5963
}
6064

6165
var testProgramConcurrency = sync.OnceValues(func() (concurrency Concurrency, raw string) {

internal/core/concurrency_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package core
2+
3+
import (
4+
"runtime"
5+
"testing"
6+
7+
"gotest.tools/v3/assert"
8+
)
9+
10+
func TestConcurrency(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
opts *CompilerOptions
14+
numFiles int
15+
singleThreaded bool
16+
checkerCount int
17+
}{
18+
{"defaults", &CompilerOptions{}, 100, false, 4},
19+
{"default", &CompilerOptions{Concurrency: "default"}, 100, false, 4},
20+
{"auto", &CompilerOptions{Concurrency: "true"}, 100, false, 4},
21+
{"true", &CompilerOptions{Concurrency: "true"}, 100, false, 4},
22+
{"yes", &CompilerOptions{Concurrency: "yes"}, 100, false, 4},
23+
{"on", &CompilerOptions{Concurrency: "on"}, 100, false, 4},
24+
{"singleThreaded", &CompilerOptions{SingleThreaded: TSTrue}, 100, true, 1},
25+
{"single", &CompilerOptions{Concurrency: "single"}, 100, true, 1},
26+
{"none", &CompilerOptions{Concurrency: "none"}, 100, true, 1},
27+
{"false", &CompilerOptions{Concurrency: "false"}, 100, true, 1},
28+
{"no", &CompilerOptions{Concurrency: "no"}, 100, true, 1},
29+
{"off", &CompilerOptions{Concurrency: "off"}, 100, true, 1},
30+
{"max", &CompilerOptions{Concurrency: "max"}, 1000, false, runtime.GOMAXPROCS(0)},
31+
{"half", &CompilerOptions{Concurrency: "half"}, 1000, false, runtime.GOMAXPROCS(0) / 2},
32+
{"checker-per-file", &CompilerOptions{Concurrency: "checker-per-file"}, 100, false, 100},
33+
{"more than files", &CompilerOptions{Concurrency: "1000"}, 100, false, 100},
34+
{"10", &CompilerOptions{Concurrency: "10"}, 100, false, 10},
35+
{"1", &CompilerOptions{Concurrency: "1"}, 100, true, 1},
36+
{"invalid", &CompilerOptions{Concurrency: "i dunno"}, 100, false, 4},
37+
}
38+
39+
for _, tt := range tests {
40+
t.Run(tt.name, func(t *testing.T) {
41+
c := ParseConcurrency(tt.opts)
42+
singleThreaded := c.SingleThreaded()
43+
checkerCount := c.CheckerCount(tt.numFiles)
44+
assert.Equal(t, singleThreaded, tt.singleThreaded)
45+
assert.Equal(t, checkerCount, tt.checkerCount)
46+
})
47+
}
48+
49+
t.Run("TestProgramConcurrency", func(t *testing.T) {
50+
c, _ := TestProgramConcurrency()
51+
assert.Assert(t, c.CheckerCount(10000) > 0)
52+
})
53+
}

0 commit comments

Comments
 (0)