This repository has been archived by the owner on Apr 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathgubernator_pool_internal_test.go
83 lines (68 loc) · 2.12 KB
/
gubernator_pool_internal_test.go
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
80
81
82
83
/*
Copyright 2018-2022 Mailgun Technologies Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gubernator
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
type MockPoolHasher struct {
mock.Mock
}
func (m *MockPoolHasher) ComputeHash63(input string) uint64 {
args := m.Called(input)
retval, _ := args.Get(0).(uint64)
return retval
}
func TestGubernatorPoolInternal(t *testing.T) {
t.Run("getWorker()", func(t *testing.T) {
const concurrency = 32
const cacheSize = 1000
conf := &Config{}
conf.SetDefaults()
// Test that getWorker() interpolates the hash to find the expected worker.
testCases := []struct {
Name string
Hash uint64
ExpectedIdx int
}{
{"Hash 0%", 0, 0},
{"Hash 50%", 0x3fff_ffff_ffff_ffff, (concurrency / 2) - 1},
{"Hash 50% + 1", 0x4000_0000_0000_0000, (concurrency / 2)},
{"Hash 100%", 0x7fff_ffff_ffff_ffff, concurrency - 1},
}
for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
pool := NewGubernatorPool(conf, concurrency, cacheSize)
defer pool.Close()
mockHasher := &MockPoolHasher{}
pool.hasher = mockHasher
// Setup mocks.
mockHasher.On("ComputeHash63", mock.Anything).Once().Return(testCase.Hash)
// Call code.
worker := pool.getWorker("Foobar")
// Verify
require.NotNil(t, worker)
var actualIdx int
for ; actualIdx < len(pool.workers); actualIdx++ {
if pool.workers[actualIdx] == worker {
break
}
}
assert.Equal(t, testCase.ExpectedIdx, actualIdx)
mockHasher.AssertExpectations(t)
})
}
})
}