-
Notifications
You must be signed in to change notification settings - Fork 846
/
suite_test.go
174 lines (149 loc) · 5.47 KB
/
suite_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
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 batcher_test
import (
"context"
"sync/atomic"
"testing"
"time"
"github.com/samber/lo"
"sigs.k8s.io/karpenter/pkg/test"
"sigs.k8s.io/karpenter/pkg/test/expectations"
"github.com/aws/karpenter-provider-aws/pkg/batcher"
"github.com/aws/karpenter-provider-aws/pkg/fake"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "knative.dev/pkg/logging/testing"
)
var fakeEC2API *fake.EC2API
var ctx context.Context
func TestAWS(t *testing.T) {
ctx = TestContextWithLogger(t)
RegisterFailHandler(Fail)
RunSpecs(t, "Batcher")
}
var _ = BeforeSuite(func() {
fakeEC2API = &fake.EC2API{}
})
var _ = Describe("Batcher", func() {
var cancelCtx context.Context
var cancel context.CancelFunc
var fakeBatcher *FakeBatcher
BeforeEach(func() {
cancelCtx, cancel = context.WithCancel(ctx)
})
AfterEach(func() {
// Cancel the context to make sure that we properly clean-up
cancel()
})
Context("Concurrency", func() {
It("should limit the number of threads that run concurrently from the batcher", func() {
// This batcher will get canceled at the end of the test run
fakeBatcher = NewFakeBatcher(cancelCtx, time.Minute, 100)
// Generate 300 items that add to the batcher
for i := 0; i < 300; i++ {
go func() {
fakeBatcher.batcher.Add(cancelCtx, lo.ToPtr(test.RandomName()))
}()
}
// Check that we get to 100 threads, and we stay at 100 threads
Eventually(fakeBatcher.activeBatches.Load).Should(BeNumerically("==", 100))
Consistently(fakeBatcher.activeBatches.Load, time.Second*10).Should(BeNumerically("==", 100))
})
It("should process 300 items in parallel to get quicker batching", func() {
// This batcher will get canceled at the end of the test run
fakeBatcher = NewFakeBatcher(cancelCtx, time.Second, 300)
// Generate 300 items that add to the batcher
for i := 0; i < 300; i++ {
go func() {
fakeBatcher.batcher.Add(cancelCtx, lo.ToPtr(test.RandomName()))
}()
}
Eventually(fakeBatcher.activeBatches.Load).Should(BeNumerically("==", 300))
Eventually(fakeBatcher.completedBatches.Load, time.Second*3).Should(BeNumerically("==", 300))
})
})
Context("Metrics", func() {
It("should create a batch_size metric when a batch is run", func() {
// This batcher will get canceled at the end of the test run
fakeBatcher = NewFakeBatcher(cancelCtx, time.Minute, 100)
// Generate 300 items that add to the batcher
for i := 0; i < 100; i++ {
go func() {
fakeBatcher.batcher.Add(cancelCtx, lo.ToPtr(test.RandomName()))
}()
}
Eventually(fakeBatcher.activeBatches.Load).Should(BeNumerically("==", 100))
metric, ok := expectations.FindMetricWithLabelValues("karpenter_cloudprovider_batcher_batch_size", map[string]string{
"batcher": "fake",
})
Expect(ok).To(BeTrue())
Expect(metric.GetHistogram().GetSampleCount()).To(BeNumerically(">=", 100))
})
It("should create a batch_window_duration metric when a batch is run", func() {
// This batcher will get canceled at the end of the test run
fakeBatcher = NewFakeBatcher(cancelCtx, time.Minute, 100)
// Generate 300 items that add to the batcher
for i := 0; i < 100; i++ {
go func() {
fakeBatcher.batcher.Add(cancelCtx, lo.ToPtr(test.RandomName()))
}()
}
Eventually(fakeBatcher.activeBatches.Load).Should(BeNumerically("==", 100))
_, ok := expectations.FindMetricWithLabelValues("karpenter_cloudprovider_batcher_batch_time_seconds", map[string]string{
"batcher": "fake",
})
Expect(ok).To(BeTrue())
})
})
})
// FakeBatcher is a batcher with a mocked request that takes a long time to execute that also ref-counts the number
// of active requests that are running at a given time
type FakeBatcher struct {
activeBatches *atomic.Int64
completedBatches *atomic.Int64
batcher *batcher.Batcher[string, string]
}
func NewFakeBatcher(ctx context.Context, requestLength time.Duration, maxRequestWorkers int) *FakeBatcher {
activeBatches := &atomic.Int64{}
completedBatches := &atomic.Int64{}
options := batcher.Options[string, string]{
Name: "fake",
IdleTimeout: 100 * time.Millisecond,
MaxTimeout: 1 * time.Second,
MaxRequestWorkers: maxRequestWorkers,
RequestHasher: batcher.DefaultHasher[string],
BatchExecutor: func(ctx context.Context, items []*string) []batcher.Result[string] {
// Keep a ref count of the number of batches that we are currently running
activeBatches.Add(1)
defer activeBatches.Add(-1)
defer completedBatches.Add(1)
// Wait for an arbitrary request length while running this call
select {
case <-ctx.Done():
case <-time.After(requestLength):
}
// Return back request responses
return lo.Map(items, func(i *string, _ int) batcher.Result[string] {
return batcher.Result[string]{
Output: lo.ToPtr[string](""),
Err: nil,
}
})
},
}
return &FakeBatcher{
activeBatches: activeBatches,
completedBatches: completedBatches,
batcher: batcher.NewBatcher(ctx, options),
}
}