From 3167e052db966b02e8bb22495fe23a962bbef3f8 Mon Sep 17 00:00:00 2001 From: Benjamin Wang Date: Tue, 5 Jul 2022 10:17:32 +0800 Subject: [PATCH] add unit test for pkg/flags/Uint32FromFlag Signed-off-by: Benjamin Wang --- pkg/flags/uint32_test.go | 43 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/pkg/flags/uint32_test.go b/pkg/flags/uint32_test.go index 6e7d38df2d0f..60d015793171 100644 --- a/pkg/flags/uint32_test.go +++ b/pkg/flags/uint32_test.go @@ -15,6 +15,7 @@ package flags import ( + "flag" "testing" "github.com/stretchr/testify/assert" @@ -66,3 +67,45 @@ func TestUint32Value(t *testing.T) { }) } } + +func TestUint32FromFlag(t *testing.T) { + const flagName = "max-concurrent-streams" + + cases := []struct { + name string + defaultVal uint32 + arguments []string + expectedVal uint32 + }{ + { + name: "only default value", + defaultVal: 15, + arguments: []string{}, + expectedVal: 15, + }, + { + name: "argument has different value from the default one", + defaultVal: 16, + arguments: []string{"--max-concurrent-streams", "200"}, + expectedVal: 200, + }, + { + name: "argument has the same value from the default one", + defaultVal: 105, + arguments: []string{"--max-concurrent-streams", "105"}, + expectedVal: 105, + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + fs := flag.NewFlagSet("etcd", flag.ContinueOnError) + fs.Var(NewUint32Value(tc.defaultVal), flagName, "Maximum concurrent streams that a client can open at a time.") + if err := fs.Parse(tc.arguments); err != nil { + t.Fatalf("Unexpected error: %v\n", err) + } + actualMaxStream := Uint32FromFlag(fs, flagName) + assert.Equal(t, actualMaxStream, tc.expectedVal) + }) + } +}