diff --git a/pkg/support/throttling/async_throttler_test.go b/pkg/support/throttling/async_throttler_test.go index 1977803..c50449c 100644 --- a/pkg/support/throttling/async_throttler_test.go +++ b/pkg/support/throttling/async_throttler_test.go @@ -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) } diff --git a/pkg/support/throttling/cloek_test.go b/pkg/support/throttling/cloek_test.go index 1acbd49..4c80aae 100644 --- a/pkg/support/throttling/cloek_test.go +++ b/pkg/support/throttling/cloek_test.go @@ -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) -} diff --git a/pkg/support/throttling/suite_test.go b/pkg/support/throttling/suite_test.go new file mode 100644 index 0000000..392d35e --- /dev/null +++ b/pkg/support/throttling/suite_test.go @@ -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{}) +} diff --git a/pkg/support/throttling/throtller_test.go b/pkg/support/throttling/throtller_test.go index 9672809..c7442a2 100644 --- a/pkg/support/throttling/throtller_test.go +++ b/pkg/support/throttling/throtller_test.go @@ -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) }