Skip to content

Commit

Permalink
modernize mocks & test harness
Browse files Browse the repository at this point in the history
  • Loading branch information
mredolatti committed Mar 28, 2024
1 parent c250cf0 commit 3dcce66
Show file tree
Hide file tree
Showing 27 changed files with 616 additions and 2,507 deletions.
7 changes: 7 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
v6.0.0 (TBD):
- updated common helpers to be generic
- updated datastructures to be generic
- cleanup package structre and remove deprecated ones
- updated logger with formatting functionality
- modernized test harness & mocks

5.4.0 (Jan 10, 2024)
- Added `Scan` operation to Redis

Expand Down
48 changes: 14 additions & 34 deletions asynctask/asynctasks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/splitio/go-toolkit/v6/logging"
"github.com/stretchr/testify/assert"
)

func TestAsyncTaskNormalOperation(t *testing.T) {
Expand All @@ -29,27 +30,14 @@ func TestAsyncTaskNormalOperation(t *testing.T) {

task1.Start()
time.Sleep(1 * time.Second)
if !task1.IsRunning() {
t.Error("Task should be running")
}
time.Sleep(1 * time.Second)
assert.True(t, task1.IsRunning())

time.Sleep(1 * time.Second)
task1.Stop(true)
if task1.IsRunning() {
t.Error("Task should be stopped")
}

if !onInit.Load().(bool) {
t.Error("Initialization hook not executed")
}

if !onExecution.Load().(bool) {
t.Error("Main task function not executed")
}

if !onStop.Load().(bool) {
t.Error("After execution function not executed")
}
assert.False(t, task1.IsRunning())
assert.True(t, onInit.Load().(bool))
assert.True(t, onExecution.Load().(bool))
assert.True(t, onStop.Load().(bool))
}

func TestAsyncTaskPanics(t *testing.T) {
Expand Down Expand Up @@ -94,15 +82,10 @@ func TestAsyncTaskPanics(t *testing.T) {
task3.Start()
time.Sleep(time.Second * 2)
task3.Stop(true)
if task1.IsRunning() {
t.Error("Task1 is running and should be stopped")
}
if task2.IsRunning() {
t.Error("Task2 is running and should be stopped")
}
if task3.IsRunning() {
t.Error("Task3 is running and should be stopped")
}

assert.False(t, task1.IsRunning())
assert.False(t, task2.IsRunning())
assert.False(t, task3.IsRunning())
}

func TestAsyncTaskErrors(t *testing.T) {
Expand Down Expand Up @@ -138,9 +121,8 @@ func TestAsyncTaskErrors(t *testing.T) {

task2.Start()
time.Sleep(2 * time.Second)
if res.Load().(int) != 0 {
t.Error("Task should have never executed if there was an error when calling onInit()")
}

assert.Equal(t, int(0), res.Load().(int))
}

func TestAsyncTaskWakeUp(t *testing.T) {
Expand All @@ -163,7 +145,5 @@ func TestAsyncTaskWakeUp(t *testing.T) {
_ = task1.WakeUp()
_ = task1.Stop(true)

if atomic.LoadInt32(&res) != 3 {
t.Errorf("Task shuld have executed 4 times. It ran %d times", res)
}
assert.Equal(t, int32(3), atomic.LoadInt32(&res))
}
30 changes: 9 additions & 21 deletions backoff/backoff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,20 @@ package backoff
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)

func TestBackoff(t *testing.T) {
base := int64(10)
maxAllowed := 60 * time.Second
backoff := New(base, maxAllowed)
if backoff.base != base {
t.Error("It should be equals to 10")
}
if backoff.maxAllowed != maxAllowed {
t.Error("It should be equals to 60")
}
if backoff.Next() != 1*time.Second {
t.Error("It should be 1 second")
}
if backoff.Next() != 10*time.Second {
t.Error("It should be 10 seconds")
}
if backoff.Next() != 60*time.Second {
t.Error("It should be 60 seconds")
}
assert.Equal(t, base, backoff.base)
assert.Equal(t, maxAllowed, backoff.maxAllowed)
assert.Equal(t, 1*time.Second, backoff.Next())
assert.Equal(t, 10*time.Second, backoff.Next())
assert.Equal(t, 60*time.Second, backoff.Next())

backoff.Reset()
if backoff.current != 0 {
t.Error("It should be zero")
}
if backoff.Next() != 1*time.Second {
t.Error("It should be 1 second")
}
assert.Equal(t, int64(0), backoff.current)
assert.Equal(t, 1*time.Second, backoff.Next())
}
18 changes: 13 additions & 5 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ func Ref[T any](x T) *T {

// RefOrNil returns a pointer to the value supplied if it's not the default value, nil otherwise
func RefOrNil[T comparable](x T) *T {
var t T
if x == t {
return nil
}
return &x
var t T
if x == t {
return nil
}
return &x
}

// PointerOf performs a type-assertion to T and returns a pointer if successful, nil otherwise.
Expand Down Expand Up @@ -93,3 +93,11 @@ func Min[T cmp.Ordered](i1 T, rest ...T) T {
}
return min
}

func AsInterfaceSlice[T any](in []T) []interface{} {
out := make([]interface{}, len(in))
for idx := range in {
out[idx] = in[idx]
}
return out
}
151 changes: 55 additions & 96 deletions datastructures/boolslice/boolslice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,115 +3,74 @@ package boolslice
import (
"math"
"testing"

"github.com/stretchr/testify/assert"
)

func TestBoolSlice(t *testing.T) {
_, err := NewBoolSlice(12)
if err == nil {
t.Error("It should return err")
}
assert.NotNil(t, err)

b, err := NewBoolSlice(int(math.Pow(2, 15)))
if err != nil {
t.Error("It should not return err", err)
}
assert.Nil(t, err)

i1 := 12
i2 := 20
i3 := 123
i4 := 2000
i5 := 8192

if err := b.Set(int(math.Pow(2, 15)) + 1); err == nil {
t.Error("It should return err")
}
if err := b.Set(i1); err != nil {
t.Error("It should not return err")
}
if err := b.Set(i2); err != nil {
t.Error("It should not return err")
}
if err := b.Set(i3); err != nil {
t.Error("It should not return err")
}
if err := b.Set(i4); err != nil {
t.Error("It should not return err")
}
if err := b.Set(i5); err != nil {
t.Error("It should not return err")
}

if _, err := b.Get(int(math.Pow(2, 15)) + 1); err == nil {
t.Error("It should return err")
}
if v, _ := b.Get(i1); !v {
t.Error("It should match", i1)
}
if v, _ := b.Get(i2); !v {
t.Error("It should match", i2)
}
if v, _ := b.Get(i3); !v {
t.Error("It should match", i3)
}
if v, _ := b.Get(i4); !v {
t.Error("It should match", i4)
}
if v, _ := b.Get(i5); !v {
t.Error("It should match", i5)
}
if v, _ := b.Get(200); v {
t.Error("It should not match 200")
}
if v, _ := b.Get(5000); v {
t.Error("It should not match 5000")
}

if len(b.Bytes()) != int(math.Pow(2, 15)/8) {
t.Error("Len should be 4096")
}

if err := b.Clear(int(math.Pow(2, 15)) + 1); err == nil {
t.Error("It should return err")
}
if err := b.Clear(i1); err != nil {
t.Error("It should not return err")
}

if v, _ := b.Get(i1); v {
t.Error("It should not match after cleared", i1)
}

if _, err := Rebuild(1, nil); err.Error() != "size must be a multiple of 8" {
t.Error("It should return err")
}

if _, err := Rebuild(8, nil); err.Error() != "data cannot be empty" {
t.Error("It should return err")
}
assert.Equal(t, ErrorOutOfBounds, b.Set(int(math.Pow(2, 15)) + 1))
assert.Nil(t, b.Set(i1))
assert.Nil(t, b.Set(i2))
assert.Nil(t, b.Set(i3))
assert.Nil(t, b.Set(i4))
assert.Nil(t, b.Set(i5))

set, err := b.Get(int(math.Pow(2, 15)) + 1)
assert.False(t, set)
assert.Equal(t, ErrorOutOfBounds, err)

for _, i := range []int{i1, i2, i3, i4, i5} {
res, err := b.Get(i)
assert.Nil(t, err)
assert.True(t, res)
}

for _, i := range []int{200, 500} {
res, err := b.Get(i)
assert.Nil(t, err)
assert.False(t, res)
}

assert.Equal(t, math.Pow(2, 15)/8, float64(len(b.Bytes())))
assert.Equal(t, ErrorOutOfBounds, b.Clear(int(math.Pow(2, 15)) + 1))
assert.Nil(t, b.Clear(i1))

v, err := b.Get(i1)
assert.Nil(t, err)
assert.False(t, v)

res, err := Rebuild(1, nil)
assert.Nil(t, res)
assert.NotNil(t, err)

res, err = Rebuild(8, nil)
assert.Nil(t, res)
assert.NotNil(t, err)

rebuilt, err := Rebuild(int(math.Pow(2, 15)), b.Bytes())
if err != nil {
t.Error("It should not return err")
}
if v, _ := rebuilt.Get(i2); !v {
t.Error("It should match", i2)
}
if v, _ := rebuilt.Get(i3); !v {
t.Error("It should match", i3)
}
if v, _ := rebuilt.Get(i4); !v {
t.Error("It should match", i4)
}
if v, _ := rebuilt.Get(i5); !v {
t.Error("It should match", i5)
}
if v, _ := rebuilt.Get(i1); v {
t.Error("It should not match 12")
}
if v, _ := rebuilt.Get(200); v {
t.Error("It should not match 200")
}
if v, _ := rebuilt.Get(5000); v {
t.Error("It should not match 5000")
}
assert.Nil(t, err)

for _, i := range []int{i2, i3, i4, i5} {
res, err := rebuilt.Get(i)
assert.Nil(t, err)
assert.True(t, res)
}

for _, i := range []int{200, 5000} {
res, err := rebuilt.Get(i)
assert.Nil(t, err)
assert.False(t, res)
}
}
Loading

0 comments on commit 3dcce66

Please sign in to comment.