now using a testsuite for the throttler tests.

This commit is contained in:
Erik Brakkee 2024-08-19 19:42:51 +02:00
parent 6abcf0a7af
commit 9f90f9a2b8
4 changed files with 43 additions and 32 deletions

View File

@ -1,12 +1,10 @@
package throttling package throttling
import ( import (
"github.com/stretchr/testify/assert"
"testing"
"time" "time"
) )
func Test_AsyncDeliverOneValue(t *testing.T) { func (suite *ThrottlerTestSuite) Test_AsyncDeliverOneValue() {
value := 0 value := 0
pollInterval := 10 * time.Millisecond pollInterval := 10 * time.Millisecond
@ -20,15 +18,15 @@ func Test_AsyncDeliverOneValue(t *testing.T) {
currentTime.now = t0 currentTime.now = t0
throttler.Notify(&v) throttler.Notify(&v)
time.Sleep(2 * pollInterval) time.Sleep(2 * pollInterval)
assert.Equal(t, v, value) suite.Equal(v, value)
// subsequent ping will not lead to a notification // subsequent ping will not lead to a notification
value = 0 value = 0
time.Sleep(2 * pollInterval) time.Sleep(2 * pollInterval)
assert.Equal(t, 0, value) suite.Equal(0, value)
} }
func Test_AsyncTwoNotificationsInSHortSucessionSecondOneIsDeliverdWithDelay(t *testing.T) { func (suite *ThrottlerTestSuite) Test_AsyncTwoNotificationsInSHortSucessionSecondOneIsDeliverdWithDelay() {
value := 0 value := 0
delayMs := 1000 delayMs := 1000
@ -47,24 +45,24 @@ func Test_AsyncTwoNotificationsInSHortSucessionSecondOneIsDeliverdWithDelay(t *t
currentTime.now = t0 currentTime.now = t0
throttler.Notify(&v1) throttler.Notify(&v1)
time.Sleep(2 * pollInterval) time.Sleep(2 * pollInterval)
assert.Equal(t, v1, value) suite.Equal(v1, value)
throttler.Notify(&v2) throttler.Notify(&v2)
throttler.Notify(&v3) throttler.Notify(&v3)
time.Sleep(2 * pollInterval) time.Sleep(2 * pollInterval)
assert.Equal(t, v1, value) suite.Equal(v1, value)
currentTime.now = t0.Add(time.Duration(delayMs-1) * time.Millisecond) currentTime.now = t0.Add(time.Duration(delayMs-1) * time.Millisecond)
time.Sleep(2 * pollInterval) time.Sleep(2 * pollInterval)
assert.Equal(t, v1, value) suite.Equal(v1, value)
currentTime.now = t0.Add(time.Duration(delayMs) * time.Millisecond) currentTime.now = t0.Add(time.Duration(delayMs) * time.Millisecond)
time.Sleep(2 * pollInterval) time.Sleep(2 * pollInterval)
assert.Equal(t, v3, value) suite.Equal(v3, value)
// another ping won't deliver the same value again. // another ping won't deliver the same value again.
value = 0 value = 0
currentTime.now = t0.Add(time.Duration(delayMs) * time.Millisecond) currentTime.now = t0.Add(time.Duration(delayMs) * time.Millisecond)
time.Sleep(2 * pollInterval) time.Sleep(2 * pollInterval)
assert.Equal(t, 0, value) suite.Equal(0, value)
} }

View File

@ -1,8 +1,6 @@
package throttling package throttling
import ( import (
"os"
"testing"
"time" "time"
) )
@ -19,11 +17,3 @@ func (t *testClock) time() time.Time {
// //
// Simply: currentTime.now = .... // Simply: currentTime.now = ....
var currentTime = &testClock{} var currentTime = &testClock{}
func TestMain(m *testing.M) {
oldclock := clock
clock = currentTime
exitCode := m.Run()
clock = oldclock
os.Exit(exitCode)
}

View File

@ -0,0 +1,25 @@
package throttling
import (
"github.com/stretchr/testify/suite"
"testing"
)
type ThrottlerTestSuite struct {
suite.Suite
oldclock _clock
}
func (suite *ThrottlerTestSuite) SetupTest() {
suite.oldclock = clock
clock = currentTime
}
func (suite *ThrottlerTestSuite) TearDownTest() {
clock = suite.oldclock
}
func TestThrottlerTestSuite(t *testing.T) {
suite.Run(t, &ThrottlerTestSuite{})
}

View File

@ -1,12 +1,10 @@
package throttling package throttling
import ( import (
"github.com/stretchr/testify/assert"
"testing"
"time" "time"
) )
func Test_throttlerImmediateNotificationAfterInitialized(t *testing.T) { func (suite *ThrottlerTestSuite) Test_throttlerImmediateNotificationAfterInitialized() {
value := 0 value := 0
throttler := NewThrottler[int](func(v *int) { throttler := NewThrottler[int](func(v *int) {
value = *v value = *v
@ -16,15 +14,15 @@ func Test_throttlerImmediateNotificationAfterInitialized(t *testing.T) {
v := 1 v := 1
currentTime.now = t0 currentTime.now = t0
throttler.Notify(&v) throttler.Notify(&v)
assert.Equal(t, v, value) suite.Equal(v, value)
value = 0 value = 0
// subsequent ping will not lead to a notification // subsequent ping will not lead to a notification
currentTime.now = t0.Add(10 * time.Second) currentTime.now = t0.Add(10 * time.Second)
throttler.Ping() throttler.Ping()
assert.Equal(t, 0, value) suite.Equal(0, value)
} }
func Test_TwoNotificationsInSHortSucessionSecondOneIsDeliverdWithDelay(t *testing.T) { func (suite *ThrottlerTestSuite) Test_TwoNotificationsInSHortSucessionSecondOneIsDeliverdWithDelay() {
value := 0 value := 0
delayMs := 1000 delayMs := 1000
throttler := NewThrottler[int](func(v *int) { throttler := NewThrottler[int](func(v *int) {
@ -38,22 +36,22 @@ func Test_TwoNotificationsInSHortSucessionSecondOneIsDeliverdWithDelay(t *testin
v3 := 3 v3 := 3
currentTime.now = t0 currentTime.now = t0
throttler.Notify(&v1) throttler.Notify(&v1)
assert.Equal(t, v1, value) suite.Equal(v1, value)
throttler.Notify(&v2) throttler.Notify(&v2)
throttler.Notify(&v3) throttler.Notify(&v3)
assert.Equal(t, v1, value) suite.Equal(v1, value)
currentTime.now = t0.Add(time.Duration(delayMs-1) * time.Millisecond) currentTime.now = t0.Add(time.Duration(delayMs-1) * time.Millisecond)
throttler.Ping() throttler.Ping()
assert.Equal(t, v1, value) suite.Equal(v1, value)
currentTime.now = t0.Add(time.Duration(delayMs) * time.Millisecond) currentTime.now = t0.Add(time.Duration(delayMs) * time.Millisecond)
throttler.Ping() throttler.Ping()
assert.Equal(t, v3, value) suite.Equal(v3, value)
// another ping won't deliver the same value again. // another ping won't deliver the same value again.
value = 0 value = 0
currentTime.now = t0.Add(time.Duration(delayMs) * time.Millisecond) currentTime.now = t0.Add(time.Duration(delayMs) * time.Millisecond)
throttler.Ping() throttler.Ping()
assert.Equal(t, 0, value) suite.Equal(0, value)
} }