11package connectioncounter_test
22
33import (
4- "context"
54 "sync"
65 "testing"
76 "time"
@@ -24,9 +23,9 @@ type mockObsSender struct {
2423 lastOpenConns , lastNewlyOpenedConns int
2524}
2625
27- func (m * mockObsSender ) AddMetrics (_ ... map [string ]interface {} ) {}
26+ func (m * mockObsSender ) AddMetrics (_ ... map [string ]any ) {}
2827
29- func (m * mockObsSender ) AddDistinctMetrics (_ interface {} , _ ... map [string ]interface {} ) {}
28+ func (m * mockObsSender ) AddDistinctMetrics (_ any , _ ... map [string ]any ) {}
3029
3130func (m * mockObsSender ) AddIMAPConnectionsExceededThresholdMetric (openConns , newlyOpenedConns int ) {
3231 m .mu .Lock ()
@@ -48,18 +47,20 @@ func (m *mockObsSender) LastValues() (int, int) {
4847 return m .lastOpenConns , m .lastNewlyOpenedConns
4948}
5049
51- func TestRollingCounter_ThresholdNotExceeded (t * testing.T ) {
50+ func TestRollingCounter_ObservabilityThresholdNotExceeded (t * testing.T ) {
51+ observabilityThreshold := 5
52+ connectionLimitThreshold := 5
5253 rc := connectioncounter .NewRollingCounter (
53- 5 ,
54+ connectionLimitThreshold ,
55+ observabilityThreshold ,
5456 3 ,
5557 100 * time .Millisecond ,
5658 )
5759
5860 mockSender := & mockObsSender {}
5961 mockProvider := & mockConnProvider {openSessions : 10 }
6062
61- ctx , cancel := context .WithCancel (context .Background ())
62- defer cancel ()
63+ ctx := t .Context ()
6364
6465 rc .Start (ctx , mockSender , mockProvider )
6566
@@ -76,24 +77,25 @@ func TestRollingCounter_ThresholdNotExceeded(t *testing.T) {
7677 rc .Stop ()
7778}
7879
79- func TestRollingCounter_ThresholdExceeded (t * testing.T ) {
80- newConnThreshold := 3
80+ func TestRollingCounter_ObservabilityThresholdExceeded (t * testing.T ) {
81+ observabilityThreshold := 3
82+ connectionLimitThreshold := 5
8183 rc := connectioncounter .NewRollingCounter (
82- newConnThreshold ,
84+ connectionLimitThreshold ,
85+ observabilityThreshold ,
8386 3 ,
8487 100 * time .Millisecond ,
8588 )
8689
8790 mockSender := & mockObsSender {}
8891 mockProvider := & mockConnProvider {openSessions : 7 }
8992
90- ctx , cancel := context .WithCancel (context .Background ())
91- defer cancel ()
93+ ctx := t .Context ()
9294
9395 rc .Start (ctx , mockSender , mockProvider )
9496
95- newConnsOpened := newConnThreshold * 5
96- for i := 0 ; i < newConnsOpened ; i ++ {
97+ newConnsOpened := observabilityThreshold * 5
98+ for range newConnsOpened {
9799 rc .NewConnection ()
98100 }
99101
@@ -108,23 +110,50 @@ func TestRollingCounter_ThresholdExceeded(t *testing.T) {
108110 rc .Stop ()
109111}
110112
113+ func TestRollingCounter_ConnectionLimitThresholdExceeded (t * testing.T ) {
114+ observabilityThreshold := 3
115+ connectionLimitThreshold := 3
116+ rc := connectioncounter .NewRollingCounter (
117+ connectionLimitThreshold ,
118+ observabilityThreshold ,
119+ 3 ,
120+ 100 * time .Millisecond ,
121+ )
122+
123+ mockSender := & mockObsSender {}
124+ mockProvider := & mockConnProvider {openSessions : 7 }
125+
126+ ctx := t .Context ()
127+
128+ rc .Start (ctx , mockSender , mockProvider )
129+
130+ newConnsOpened := observabilityThreshold * 5
131+ for range newConnsOpened {
132+ rc .NewConnection ()
133+ }
134+ assert .Equal (t , rc .GetRollingCount (), newConnsOpened )
135+ assert .True (t , rc .OverConnectionLimitThreshold ())
136+
137+ rc .Stop ()
138+ }
139+
111140func TestRollingCounter_BucketRotation (t * testing.T ) {
112141 jitterPeriod := 50 * time .Millisecond
113142 bucketRotationInterval := 500 * time .Millisecond
114143 rc := connectioncounter .NewRollingCounter (
144+ 100 ,
115145 100 ,
116146 3 ,
117147 bucketRotationInterval ,
118148 )
119149
120150 post10Connections := func () {
121- for i := 0 ; i < 10 ; i ++ {
151+ for range 10 {
122152 rc .NewConnection ()
123153 }
124154 }
125155
126- ctx , cancel := context .WithCancel (context .Background ())
127- defer cancel ()
156+ ctx := t .Context ()
128157
129158 mockSender := & mockObsSender {}
130159 mockProvider := & mockConnProvider {}
0 commit comments