From dd84b9d64fb98746a230cd24233ff50a562c39c9 Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Fri, 28 Apr 2023 01:36:44 +0800 Subject: --- cli/internal/util/parse_concurrency_test.go | 79 +++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 cli/internal/util/parse_concurrency_test.go (limited to 'cli/internal/util/parse_concurrency_test.go') diff --git a/cli/internal/util/parse_concurrency_test.go b/cli/internal/util/parse_concurrency_test.go new file mode 100644 index 0000000..b732724 --- /dev/null +++ b/cli/internal/util/parse_concurrency_test.go @@ -0,0 +1,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) + }) + } +} -- cgit v1.2.3-70-g09d2