now using a testsuite for the throttler tests.

This commit is contained in:
Erik Brakkee 2024-08-19 19:42:51 +02:00
parent a62a513105
commit 91ea4632a3
4 changed files with 43 additions and 32 deletions

View File

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

View File

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