Debian Codegraph @

maxprocs/maxprocs_test.go

raw symbols SCIP
1 // Copyright (c) 2017 Uber Technologies, Inc.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20
21 package maxprocs
22
23 import (
24 "bytes"
25 "errors"
26 "fmt"
27 "log"
28 "os"
29 "strconv"
30 "testing"
31
32 iruntime "go.uber.org/automaxprocs/internal/runtime"
33
34 "github.com/stretchr/testify/assert"
35 "github.com/stretchr/testify/require"
36 )
37
38 func withMax(t testing.TB, n int, f func()) {
39 prevStr, ok := os.LookupEnv(_maxProcsKey)
40 want := strconv.FormatInt(int64(n), 10)
41 require.NoError(t, os.Setenv(_maxProcsKey, want), "couldn't set GOMAXPROCS")
42 f()
43 if ok {
44 require.NoError(t, os.Setenv(_maxProcsKey, prevStr), "couldn't restore original GOMAXPROCS value")
45 return
46 }
47 require.NoError(t, os.Unsetenv(_maxProcsKey), "couldn't clear GOMAXPROCS")
48 }
49
50 func testLogger() (*bytes.Buffer, Option) {
51 buf := bytes.NewBuffer(nil)
52 printf := func(template string, args ...interface{}) {
53 fmt.Fprintf(buf, template, args...)
54 }
55 return buf, Logger(printf)
56 }
57
58 func stubProcs(f func(int) (int, iruntime.CPUQuotaStatus, error)) Option {
59 return optionFunc(func(cfg *config) {
60 cfg.procs = f
61 })
62 }
63
64 func TestLogger(t *testing.T) {
65 t.Run("default", func(t *testing.T) {
66 // Calling Set without options should be safe.
67 undo, err := Set()
68 defer undo()
69 require.NoError(t, err, "Set failed")
70 })
71
72 t.Run("override", func(t *testing.T) {
73 buf, opt := testLogger()
74 undo, err := Set(opt)
75 defer undo()
76 require.NoError(t, err, "Set failed")
77 assert.True(t, buf.Len() > 0, "didn't capture log output")
78 })
79 }
80
81 func TestSet(t *testing.T) {
82 // Ensure that we've undone any modifications correctly.
83 prev := currentMaxProcs()
84 defer func() {
85 require.Equal(t, prev, currentMaxProcs(), "didn't undo GOMAXPROCS changes")
86 }()
87
88 t.Run("EnvVarPresent", func(t *testing.T) {
89 withMax(t, 42, func() {
90 prev := currentMaxProcs()
91 undo, err := Set()
92 defer undo()
93 require.NoError(t, err, "Set failed")
94 assert.Equal(t, prev, currentMaxProcs(), "shouldn't alter GOMAXPROCS")
95 })
96 })
97
98 t.Run("ErrorReadingQuota", func(t *testing.T) {
99 opt := stubProcs(func(int) (int, iruntime.CPUQuotaStatus, error) {
100 return 0, iruntime.CPUQuotaUndefined, errors.New("failed")
101 })
102 prev := currentMaxProcs()
103 undo, err := Set(opt)
104 defer undo()
105 require.Error(t, err, "Set should have failed")
106 assert.Equal(t, "failed", err.Error(), "should pass errors up the stack")
107 assert.Equal(t, prev, currentMaxProcs(), "shouldn't alter GOMAXPROCS")
108 })
109
110 t.Run("QuotaUndefined", func(t *testing.T) {
111 buf, logOpt := testLogger()
112 quotaOpt := stubProcs(func(int) (int, iruntime.CPUQuotaStatus, error) {
113 return 0, iruntime.CPUQuotaUndefined, nil
114 })
115 prev := currentMaxProcs()
116 undo, err := Set(logOpt, quotaOpt)
117 defer undo()
118 require.NoError(t, err, "Set failed")
119 assert.Equal(t, prev, currentMaxProcs(), "shouldn't alter GOMAXPROCS")
120 assert.Contains(t, buf.String(), "quota undefined", "unexpected log output")
121 })
122
123 t.Run("QuotaUndefined return maxProcs=7", func(t *testing.T) {
124 buf, logOpt := testLogger()
125 quotaOpt := stubProcs(func(int) (int, iruntime.CPUQuotaStatus, error) {
126 return 7, iruntime.CPUQuotaUndefined, nil
127 })
128 prev := currentMaxProcs()
129 undo, err := Set(logOpt, quotaOpt)
130 defer undo()
131 require.NoError(t, err, "Set failed")
132 assert.Equal(t, prev, currentMaxProcs(), "shouldn't alter GOMAXPROCS")
133 assert.Contains(t, buf.String(), "quota undefined", "unexpected log output")
134 })
135
136 t.Run("QuotaTooSmall", func(t *testing.T) {
137 buf, logOpt := testLogger()
138 quotaOpt := stubProcs(func(min int) (int, iruntime.CPUQuotaStatus, error) {
139 return min, iruntime.CPUQuotaMinUsed, nil
140 })
141 undo, err := Set(logOpt, quotaOpt, Min(5))
142 defer undo()
143 require.NoError(t, err, "Set failed")
144 assert.Equal(t, 5, currentMaxProcs(), "should use min allowed GOMAXPROCS")
145 assert.Contains(t, buf.String(), "using minimum allowed", "unexpected log output")
146 })
147
148 t.Run("Min unused", func(t *testing.T) {
149 buf, logOpt := testLogger()
150 quotaOpt := stubProcs(func(min int) (int, iruntime.CPUQuotaStatus, error) {
151 return min, iruntime.CPUQuotaMinUsed, nil
152 })
153 // Min(-1) should be ignored.
154 undo, err := Set(logOpt, quotaOpt, Min(5), Min(-1))
155 defer undo()
156 require.NoError(t, err, "Set failed")
157 assert.Equal(t, 5, currentMaxProcs(), "should use min allowed GOMAXPROCS")
158 assert.Contains(t, buf.String(), "using minimum allowed", "unexpected log output")
159 })
160
161 t.Run("QuotaUsed", func(t *testing.T) {
162 opt := stubProcs(func(min int) (int, iruntime.CPUQuotaStatus, error) {
163 assert.Equal(t, 1, min, "Default minimum value should be 1")
164 return 42, iruntime.CPUQuotaUsed, nil
165 })
166 undo, err := Set(opt)
167 defer undo()
168 require.NoError(t, err, "Set failed")
169 assert.Equal(t, 42, currentMaxProcs(), "should change GOMAXPROCS to match quota")
170 })
171 }
172
173 func TestMain(m *testing.M) {
174 if err := os.Unsetenv(_maxProcsKey); err != nil {
175 log.Fatalf("Couldn't clear %s: %v\n", _maxProcsKey, err)
176 }
177 os.Exit(m.Run())
178 }