Repository: zeromicro/go-zero
Pull request #3359 (2023-06-16 15:47:29)

chore: add more tests

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/zeromicro/go-zero/pull/3359?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#3359](https://app.codecov.io/gh/zeromicro/go-zero/pull/3359?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (d1adf16) into [master](https://app.codecov.io/gh/zeromicro/go-zero/commit/92f6c4834912e9857a2d88ad99f601be95461987?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (92f6c48) will **increase** coverage by `0.17%`.
> The diff coverage is `100.00%`.


| [Impacted Files](https://app.codecov.io/gh/zeromicro/go-zero/pull/3359?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [core/stat/internal/cgroup\_linux.go](https://app.codecov.io/gh/zeromicro/go-zero/pull/3359?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9zdGF0L2ludGVybmFsL2Nncm91cF9saW51eC5nbw==) | `72.15% <100.00%> (+15.26%)` | :arrow_up: |

... and [1 file with indirect coverage changes](https://app.codecov.io/gh/zeromicro/go-zero/pull/3359/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)


Patch:
diff --git a/core/stat/internal/cgroup_linux.go b/core/stat/internal/cgroup_linux.go
index 5f53d9ed..6ac19853 100644
--- a/core/stat/internal/cgroup_linux.go
+++ b/core/stat/internal/cgroup_linux.go
@@ -219,6 +219,7 @@ func parseUints(val string) ([]uint64, error) {
 		return nil, nil
 	}
 
+	var sets []uint64
 	ints := make(map[uint64]lang.PlaceholderType)
 	cols := strings.Split(val, ",")
 	for _, r := range cols {
@@ -239,7 +240,10 @@ func parseUints(val string) ([]uint64, error) {
 			}
 
 			for i := min; i <= max; i++ {
-				ints[i] = lang.Placeholder
+				if _, ok := ints[i]; !ok {
+					ints[i] = lang.Placeholder
+					sets = append(sets, i)
+				}
 			}
 		} else {
 			v, err := parseUint(r)
@@ -247,19 +251,17 @@ func parseUints(val string) ([]uint64, error) {
 				return nil, err
 			}
 
-			ints[v] = lang.Placeholder
+			if _, ok := ints[v]; !ok {
+				ints[v] = lang.Placeholder
+				sets = append(sets, v)
+			}
 		}
 	}
 
-	var sets []uint64
-	for k := range ints {
-		sets = append(sets, k)
-	}
-
 	return sets, nil
 }
 
-// runningInUserNS detects whether we are currently running in an user namespace.
+// runningInUserNS detects whether we are currently running in a user namespace.
 func runningInUserNS() bool {
 	nsOnce.Do(func() {
 		file, err := os.Open("/proc/self/uid_map")


Test patch:
diff --git a/core/stat/internal/cgroup_linux_test.go b/core/stat/internal/cgroup_linux_test.go
index 06bc5ac9..15ab1d43 100644
--- a/core/stat/internal/cgroup_linux_test.go
+++ b/core/stat/internal/cgroup_linux_test.go
@@ -1,6 +1,7 @@
 package internal
 
 import (
+	"fmt"
 	"testing"
 
 	"github.com/stretchr/testify/assert"
@@ -25,3 +26,46 @@ func TestCgroupV1(t *testing.T) {
 		assert.Error(t, err)
 	}
 }
+
+func TestParseUint(t *testing.T) {
+	tests := []struct {
+		input string
+		want  uint64
+		err   error
+	}{
+		{"0", 0, nil},
+		{"123", 123, nil},
+		{"-1", 0, nil},
+		{"-18446744073709551616", 0, nil},
+		{"foo", 0, fmt.Errorf("cgroup: bad int format: foo")},
+	}
+
+	for _, tt := range tests {
+		got, err := parseUint(tt.input)
+		assert.Equal(t, tt.err, err)
+		assert.Equal(t, tt.want, got)
+	}
+}
+
+func TestParseUints(t *testing.T) {
+	tests := []struct {
+		input string
+		want  []uint64
+		err   error
+	}{
+		{"", nil, nil},
+		{"1,2,3", []uint64{1, 2, 3}, nil},
+		{"1-3", []uint64{1, 2, 3}, nil},
+		{"1-3,5,7-9", []uint64{1, 2, 3, 5, 7, 8, 9}, nil},
+		{"foo", nil, fmt.Errorf("cgroup: bad int format: foo")},
+		{"1-bar", nil, fmt.Errorf("cgroup: bad int list format: 1-bar")},
+		{"bar-3", nil, fmt.Errorf("cgroup: bad int list format: bar-3")},
+		{"3-1", nil, fmt.Errorf("cgroup: bad int list format: 3-1")},
+	}
+
+	for _, tt := range tests {
+		got, err := parseUints(tt.input)
+		assert.Equal(t, tt.err, err)
+		assert.Equal(t, tt.want, got)
+	}
+}