-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created a network monitor to support multiple request counters
- Loading branch information
1 parent
19f83b6
commit 724a5ce
Showing
2 changed files
with
279 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors. | ||
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 reqcounter_test | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
rc "sigs.k8s.io/k8s-container-image-promoter/legacy/reqcounter" | ||
) | ||
|
||
func TestInit(t *testing.T) { | ||
rc.Init() | ||
// Ensure request counting is enabled. | ||
if rc.EnableCounting == false { | ||
fmt.Println("Init did not enable counting") | ||
t.Fail() | ||
} | ||
// Ensure counters are created. | ||
if len(rc.Monitor.Counters) == 0 { | ||
fmt.Println("Init did not create any request counters") | ||
t.Fail() | ||
} | ||
// Ensure at least one counter uses the MeasurementWindow | ||
found := false | ||
for _, counter := range rc.Monitor.Counters { | ||
if counter.Interval == rc.MeasurementWindow { | ||
found = true | ||
break | ||
} | ||
} | ||
if !found { | ||
fmt.Println("No request counters are using the 10min Interval: MeasurementWindow") | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestIncrement(t *testing.T) { | ||
// Create several unique request counts. | ||
requests := []uint64{0, 9, 2839} | ||
// Create counters which use these request counts and | ||
// populate the Monitor global variable. | ||
rc.Monitor = &rc.NetworkMonitor{ | ||
Counters: []*rc.RequestCounter{ | ||
{ | ||
Mutex: sync.Mutex{}, | ||
Requests: requests[0], | ||
Since: time.Now(), | ||
Interval: time.Second, | ||
}, | ||
{ | ||
Mutex: sync.Mutex{}, | ||
Requests: requests[1], | ||
Since: time.Now(), | ||
Interval: time.Second, | ||
}, | ||
{ | ||
Mutex: sync.Mutex{}, | ||
Requests: requests[2], | ||
Since: time.Now(), | ||
Interval: time.Second, | ||
}}, | ||
} | ||
// Ensure that modifying counters can only occur when counting is enabled. | ||
rc.EnableCounting = false | ||
rc.Increment() | ||
for i, counter := range rc.Monitor.Counters { | ||
if counter.Requests != requests[i] { | ||
fmt.Println("Counters were modified while counting was disabled") | ||
t.Fail() | ||
} | ||
} | ||
// Ensure the Increment function actually increments each counter's requests field. | ||
rc.EnableCounting = true | ||
rc.Increment() | ||
for i, counter := range rc.Monitor.Counters { | ||
if counter.Requests != requests[i]+1 { | ||
fmt.Printf("Counters were not incremented correctly. Expected: %d Got: %d\n", requests[i]+1, counter.Requests) | ||
t.Fail() | ||
} | ||
} | ||
} | ||
|
||
func TestFlush(t *testing.T) { | ||
// Create a local invocation of time. | ||
currentTime := time.Now() | ||
counter := &rc.RequestCounter{ | ||
Mutex: sync.Mutex{}, | ||
Requests: 100, | ||
Since: currentTime, | ||
Interval: time.Second, | ||
} | ||
// Mock the logrus.Debug function. | ||
debugCalls := 0 | ||
rc.Debug = func(args ...interface{}) { | ||
debugCalls++ | ||
} | ||
// Flush the request counter. | ||
counter.Flush() | ||
// Ensure logrus.Debug was called. | ||
if debugCalls != 1 { | ||
fmt.Println("Flush failed to trigger a debug statement.") | ||
t.Fail() | ||
} | ||
// Ensure the counter is reset, where time advances and the requests are zeroed. | ||
counter.Flush() | ||
if counter.Requests != 0 || !currentTime.Before(counter.Since) { | ||
fmt.Println("Calling Flush did not reset the counter.") | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestLog(t *testing.T) { | ||
// Create two counters with different logging intervals. Durring testing, only one | ||
// of the counters should fire logrus.Debug statements. To determine which counter | ||
// was logged, we can use the Requests number to identify them. | ||
unexpected := uint64(80808) | ||
expected := uint64(33333) | ||
netMonitor := &rc.NetworkMonitor{ | ||
Counters: []*rc.RequestCounter{ | ||
{ | ||
Mutex: sync.Mutex{}, | ||
Requests: unexpected, | ||
Since: time.Time{}, | ||
Interval: time.Hour, | ||
}, | ||
{ | ||
Mutex: sync.Mutex{}, | ||
Requests: expected, | ||
Since: time.Time{}, | ||
Interval: time.Second, | ||
}, | ||
}, | ||
} | ||
// Mock the logrus.Debug function, ensuring the correct counter is logged. | ||
sawExpectedLog := false | ||
rc.Debug = func(args ...interface{}) { | ||
fmt.Println(args) | ||
for _, arg := range args { | ||
// The arguments should be passed as strings. | ||
s := arg.(string) | ||
if strings.Contains(s, fmt.Sprint(unexpected)) { | ||
fmt.Println("The counter with 'Interval: 1hr' was prematurely logged after waiting only 3 seconds.") | ||
t.Fail() | ||
} else if strings.Contains(s, fmt.Sprint(expected)) { | ||
sawExpectedLog = true | ||
} | ||
} | ||
} | ||
// Begin logging each counter. | ||
netMonitor.Log() | ||
// Allow enough time to ensure ONLY the expected counter can log itself. | ||
time.Sleep(time.Second * 3) | ||
// Ensure the expected log was found. | ||
if !sawExpectedLog { | ||
fmt.Println("The counter with 'Interval: 1sec' was not logged a single time after waiting 3 seconds.") | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestRequestCounterIncrement(t *testing.T) { | ||
req := uint64(36) | ||
counter := &rc.RequestCounter{ | ||
Mutex: sync.Mutex{}, | ||
Requests: req, | ||
Since: time.Time{}, | ||
Interval: time.Second, | ||
} | ||
// Increment the counter. | ||
counter.Increment() | ||
// Ensure the counter is now one more than before. | ||
if counter.Requests != req+1 { | ||
fmt.Printf("The request counter failed to increment. Expected: %d Got: %d\n", req+1, counter.Requests) | ||
t.Fail() | ||
} | ||
} |