aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/cli/internal/util/parse_concurrency_test.go
blob: b732724bd77014130071e3fa8989ac8e8ee1ffdb (plain) (blame)
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
package util

import (
	"fmt"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestParseConcurrency(t *testing.T) {
	cases := []struct {
		Input    string
		Expected int
	}{
		{
			"12",
			12,
		},
		{
			"200%",
			20,
		},
		{
			"100%",
			10,
		},
		{
			"50%",
			5,
		},
		{
			"25%",
			2,
		},
		{
			"1%",
			1,
		},
		{
			"0644", // we parse in base 10
			644,
		},
	}

	// mock runtime.NumCPU() to 10
	runtimeNumCPU = func() int {
		return 10
	}

	for i, tc := range cases {
		t.Run(fmt.Sprintf("%d) '%s' should be parsed at '%d'", i, tc.Input, tc.Expected), func(t *testing.T) {
			if result, err := ParseConcurrency(tc.Input); err != nil {
				t.Fatalf("invalid parse: %#v", err)
			} else {
				assert.EqualValues(t, tc.Expected, result)
			}
		})
	}
}

func TestInvalidPercents(t *testing.T) {
	inputs := []string{
		"asdf",
		"-1",
		"-l%",
		"infinity%",
		"-infinity%",
		"nan%",
		"0b01",
		"0o644",
		"0xFF",
	}
	for _, tc := range inputs {
		t.Run(tc, func(t *testing.T) {
			val, err := ParseConcurrency(tc)
			assert.Error(t, err, "input %v got %v", tc, val)
		})
	}
}