Skip to content

Commit

Permalink
unify the signature between Retry.Assert and T.Eventually
Browse files Browse the repository at this point in the history
  • Loading branch information
adamluzsi committed Mar 27, 2022
1 parent 23c494a commit 7806d50
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 35 deletions.
7 changes: 4 additions & 3 deletions Race_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !race
// +build !race

package testcase_test
Expand All @@ -20,7 +21,7 @@ func TestRace(t *testing.T) {
retry := testcase.Retry{Strategy: testcase.Waiter{WaitTimeout: time.Second}}

t.Run(`functions run in race against each other`, func(t *testing.T) {
retry.Assert(t, func(tb testing.TB) {
retry.Assert(t, func(tb assert.It) {
var counter, total int32
blk := func() {
atomic.AddInt32(&total, 1)
Expand All @@ -30,9 +31,9 @@ func TestRace(t *testing.T) {
}

testcase.Race(blk, blk, blk, blk)
assert.Must(t).Equal(int32(4), total)
tb.Must.Equal(int32(4), total)
tb.Log(`counter:`, counter, `total:`, total)
assert.Must(t).True(counter < total,
tb.Must.True(counter < total,
fmt.Sprintf(`counter was expected to be less that the total block run during race`))
})
})
Expand Down
9 changes: 3 additions & 6 deletions Retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"
"time"

"github.com/adamluzsi/testcase/assert"
"github.com/adamluzsi/testcase/internal"
)

Expand All @@ -30,7 +31,7 @@ func (fn RetryStrategyFunc) While(condition func() bool) { fn(condition) }
// In case expectations are failed, it will retry the assertion block using the RetryStrategy.
// The last failed assertion results would be published to the received testing.TB.
// Calling multiple times the assertion function block content should be a safe and repeatable operation.
func (r Retry) Assert(tb testing.TB, blk func(testing.TB)) {
func (r Retry) Assert(tb testing.TB, blk func(it assert.It)) {
tb.Helper()
var lastRecorder *internal.RecorderTB

Expand All @@ -39,7 +40,7 @@ func (r Retry) Assert(tb testing.TB, blk func(testing.TB)) {
lastRecorder = &internal.RecorderTB{TB: tb}
internal.RecoverExceptGoexit(func() {
tb.Helper()
blk(lastRecorder)
blk(assert.MakeIt(lastRecorder))
})
if lastRecorder.IsFailed {
lastRecorder.CleanupNow()
Expand All @@ -52,10 +53,6 @@ func (r Retry) Assert(tb testing.TB, blk func(testing.TB)) {
}
}

//func (r Retry) setup(s *Spec) {
// s.flaky = &r
//}

func RetryCount(times int) RetryStrategy {
return RetryStrategyFunc(func(condition func() bool) {
for i := 0; i < times+1; i++ {
Expand Down
10 changes: 5 additions & 5 deletions Retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func SpecRetry(tb testing.TB) {
s.Describe(`#Assert`, func(s *testcase.Spec) {
var (
stubTB = testcase.Let(s, func(t *testcase.T) *internal.StubTB { return &internal.StubTB{} })
blk = testcase.Let(s, func(t *testcase.T) func(testing.TB) { return func(testing.TB) {} })
blk = testcase.Let(s, func(t *testcase.T) func(assert.It) { return func(it assert.It) {} })
subject = func(t *testcase.T) {
helper.Get(t).Assert(stubTB.Get(t), blk.Get(t))
}
Expand All @@ -51,8 +51,8 @@ func SpecRetry(tb testing.TB) {
blkLet = func(s *testcase.Spec, fn func(*testcase.T, testing.TB)) {
blkCounterInc := func(t *testcase.T) { blkCounter.Set(t, blkCounter.Get(t)+1) }

blk.Let(s, func(t *testcase.T) func(testing.TB) {
return func(tb testing.TB) {
blk.Let(s, func(t *testcase.T) func(assert.It) {
return func(tb assert.It) {
blkCounterInc(t)
time.Sleep(blkDurationGet(t))
fn(t, tb)
Expand Down Expand Up @@ -322,7 +322,7 @@ func TestRetry_Assert_failsOnceButThenPass(t *testing.T) {
counter int
times int
)
w.Assert(stub, func(tb testing.TB) {
w.Assert(stub, func(tb assert.It) {
// run 42 times
// 41 times it will fail but create cleanup
// on the last it should pass
Expand Down Expand Up @@ -354,7 +354,7 @@ func TestRetry_Assert_panic(t *testing.T) {
expectedPanicValue := fixtures.Random.String()
actualPanicValue := func() (r interface{}) {
defer func() { r = recover() }()
w.Assert(t, func(tb testing.TB) {
w.Assert(t, func(tb assert.It) {
panic(expectedPanicValue)
})
return nil
Expand Down
3 changes: 2 additions & 1 deletion Spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"testing"

"github.com/adamluzsi/testcase/assert"
"github.com/adamluzsi/testcase/internal"
)

Expand Down Expand Up @@ -397,7 +398,7 @@ func (spec *Spec) runTB(tb testing.TB, blk func(*T)) {

retryHandler, ok := spec.lookupRetryFlaky()
if ok {
retryHandler.Assert(tb, test)
retryHandler.Assert(tb, func(it assert.It) { test(it) })
} else {
test(tb)
}
Expand Down
4 changes: 2 additions & 2 deletions Spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,7 @@ func TestSpec_executionOrder(t *testing.T) {
t.Skip(`WIP`)

t.Run(`Non parallel testCase will run in randomized order`, func(t *testing.T) {
testcase.Retry{Strategy: testcase.Waiter{WaitDuration: time.Second}}.Assert(t, func(tb testing.TB) {
testcase.Retry{Strategy: testcase.Waiter{WaitDuration: time.Second}}.Assert(t, func(tb assert.It) {
var m sync.Mutex
total := fixtures.Random.IntBetween(32, 128)
out := make([]int, 0, total)
Expand All @@ -1201,7 +1201,7 @@ func TestSpec_executionOrder(t *testing.T) {
}
})

assert.Must(tb).True(!sort.IsSorted(sort.IntSlice(out)))
tb.Must.True(!sort.IsSorted(sort.IntSlice(out)))
})
})
}
Expand Down
4 changes: 1 addition & 3 deletions T.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,5 @@ func (t *T) Eventually(blk func(it assert.It), retryOpts ...interface{}) {
if !ok {
retry = DefaultEventuallyRetry
}
retry.Assert(t, func(tb testing.TB) {
blk(assert.MakeIt(tb))
})
retry.Assert(t, blk)
}
4 changes: 2 additions & 2 deletions T_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,8 @@ func TestT_HasTag(t *testing.T) {

func TestT_Random(t *testing.T) {
randomGenerationWorks := func(t *testcase.T) {
testcase.Retry{Strategy: testcase.Waiter{WaitDuration: time.Second}}.Assert(t, func(tb testing.TB) {
assert.Must(tb).True(0 < t.Random.Int())
testcase.Retry{Strategy: testcase.Waiter{WaitDuration: time.Second}}.Assert(t, func(tb assert.It) {
tb.Must.True(0 < t.Random.Int())
})
}

Expand Down
2 changes: 2 additions & 0 deletions assert/It.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import "testing"

func MakeIt(tb testing.TB) It {
return It{
TB: tb,
Must: Must(tb),
Should: Should(tb),
}
}

type It struct {
testing.TB
// Must Asserter will use FailNow on a failed assertion.
// This will make test exit early on.
Must Asserter
Expand Down
9 changes: 5 additions & 4 deletions example_Retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/adamluzsi/testcase"
"github.com/adamluzsi/testcase/assert"
)

func ExampleRetry_Assert() {
Expand All @@ -21,7 +22,7 @@ func ExampleRetry_Assert() {
// If the wait timeout reached, and there was no passing assertion run,
// the last failed assertion history is replied to the received testing.TB
// In this case the failure would be replied to the *testing.T.
w.Assert(t, func(tb testing.TB) {
w.Assert(t, func(tb assert.It) {
if rand.Intn(1) == 0 {
tb.Fatal(`boom`)
}
Expand All @@ -48,7 +49,7 @@ func ExampleRetry_byTimeout() {
}}

var t *testing.T
r.Assert(t, func(tb testing.TB) {
r.Assert(t, func(tb assert.It) {
if rand.Intn(1) == 0 {
tb.Fatal(`boom`)
}
Expand All @@ -59,7 +60,7 @@ func ExampleRetry_byCount() {
r := testcase.Retry{Strategy: testcase.RetryCount(42)}

var t *testing.T
r.Assert(t, func(tb testing.TB) {
r.Assert(t, func(tb assert.It) {
if rand.Intn(1) == 0 {
tb.Fatal(`boom`)
}
Expand All @@ -82,7 +83,7 @@ func ExampleRetry_byCustomRetryStrategy() {
r := testcase.Retry{Strategy: testcase.RetryStrategyFunc(while)}

var t *testing.T
r.Assert(t, func(tb testing.TB) {
r.Assert(t, func(tb assert.It) {
if rand.Intn(1) == 0 {
tb.Fatal(`boom`)
}
Expand Down
6 changes: 3 additions & 3 deletions fixtures/Factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestFactory(t *testing.T) {

thenItGeneratesVariousValues := func(s *testcase.Spec) {
s.Then(`it generates various results`, func(t *testcase.T) {
retry.Assert(t, func(tb testing.TB) {
retry.Assert(t, func(tb assert.It) {
var values []interface{}
for i := 0; i < 12; i++ {
v := subject(t)
Expand All @@ -68,7 +68,7 @@ func TestFactory(t *testing.T) {
})

s.Then(`it generates various results`, func(t *testcase.T) {
retry.Assert(t, func(tb testing.TB) {
retry.Assert(t, func(tb assert.It) {
var values []interface{}
for i := 0; i < 12; i++ {
ptr := subject(t)
Expand All @@ -82,7 +82,7 @@ func TestFactory(t *testing.T) {
}

hasValue := func(t *testcase.T, blk func(v interface{}) bool) {
retry.Assert(t, func(tb testing.TB) {
retry.Assert(t, func(tb assert.It) {
assert.Must(tb).True(blk(subject(t)))
})
}
Expand Down
12 changes: 6 additions & 6 deletions ordering_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,30 +131,30 @@ func TestRandomOrderer_Order(t *testing.T) {
})

s.Then(`different seed yield different shuffling`, func(t *T) {
Retry{Strategy: Waiter{WaitTimeout: time.Second}}.Assert(t, func(tb testing.TB) {
Retry{Strategy: Waiter{WaitTimeout: time.Second}}.Assert(t, func(tb assert.It) {
out := &[]int{}
ogIn := genOrdInput(out)
initial := runOrdInput(ogIn, out)
seed1 := int64(fixtures.Random.Int())
seed2 := int64(fixtures.Random.Int())
t.Must.NotEqual(seed1, seed2, `given the two seed that will be used is different`)
tb.Must.NotEqual(seed1, seed2, `given the two seed that will be used is different`)

// random order with a seed
in := cpyOrdInput(ogIn)

ord.Set(t, randomOrderer{Seed: seed1})
subject(t, in)
res1 := runOrdInput(in, out)
assert.Must(tb).ContainExactly(initial, res1)
assert.Must(tb).NotEqual(initial, res1)
tb.Must.ContainExactly(initial, res1)
tb.Must.NotEqual(initial, res1)

// random order with different seed
in = cpyOrdInput(ogIn)
ord.Set(t, randomOrderer{Seed: seed2})
subject(t, in)
res2 := runOrdInput(in, out)
assert.Must(tb).ContainExactly(initial, res2)
assert.Must(tb).NotEqual(initial, res2)
tb.Must.ContainExactly(initial, res2)
tb.Must.NotEqual(initial, res2)

tb.Logf(`the two ordering should be different because the different seeds`)
// the two random ordering with different seed because the different seed
Expand Down

0 comments on commit 7806d50

Please sign in to comment.