Skip to content

Commit

Permalink
make sure failing works correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
mattcorby-eaglen committed Mar 23, 2023
1 parent d0f8418 commit a89f045
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
50 changes: 38 additions & 12 deletions matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -894,25 +894,29 @@ func (s DelayedReader) Read(p []byte) (int, error) {
func TestEventuallyWithDelayedReader(t *testing.T) {
slowReader := DelayedReader{
R: bytes.NewBuffer([]byte("abcdefghijklmnopqrstuv")),
D: time.Second * 3,
D: time.Second,
}
then.WithinFiveSeconds(t, func(eventually gocrest.TestingT) {
then.AssertThat(eventually, by.Reading(slowReader, 1024), is.EqualTo([]byte("abcdefghijklmnopqrstuv")))
})
}
func TestEventuallyChannel(t *testing.T) {
channel := make(chan int, 1)
go func() {
for i := 0; i < 10; i++ {
time.Sleep(time.Second)
channel <- i
}
}()
func TestEventuallyChannels(t *testing.T) {
channel := firstTestChannel()
then.Eventually(t, time.Second*5, time.Second, func(eventually gocrest.TestingT) {
then.AssertThat(eventually, by.Channelling(channel), is.EqualTo(3))
then.AssertThat(eventually, by.Channelling(channel), is.EqualTo(3).Reason("should not fail"))
})
}

func TestEventuallyChannelsShouldFail(t *testing.T) {
channel := firstTestChannel()
channelTwo := secondTestChannel()
stubbedTesting := new(StubTestingT)
then.WithinTenSeconds(stubbedTesting, func(eventually gocrest.TestingT) {
then.AssertThat(eventually, by.Channelling(channel), is.EqualTo(3).Reason("should not fail"))
then.AssertThat(eventually, by.Channelling(channelTwo), is.EqualTo("11").Reason("This is unreachable"))
})
then.AssertThat(t, stubbedTesting.failed, is.EqualTo(true))
then.AssertThat(t, stubbedTesting.MockTestOutput, is.ValueContaining("This is unreachable"))
}
func TestEventuallyChannelInterface(t *testing.T) {
type MyType struct {
F string
Expand All @@ -922,7 +926,7 @@ func TestEventuallyChannelInterface(t *testing.T) {
channel := make(chan *MyType, 1)
go func() {
for i := 0; i < 10; i++ {
time.Sleep(time.Second)
time.Sleep(time.Millisecond * 500)
m := new(MyType)
m.F = fmt.Sprintf("hi - %d", i)
m.B = fmt.Sprintf("bye - %d", i)
Expand All @@ -936,3 +940,25 @@ func TestEventuallyChannelInterface(t *testing.T) {
}))
})
}

func firstTestChannel() chan int {
channel := make(chan int, 1)
go func() {
for i := 0; i < 10; i++ {
time.Sleep(time.Second)
channel <- i
}
}()
return channel
}

func secondTestChannel() chan string {
channelTwo := make(chan string, 1)
go func() {
for i := 0; i < 10; i++ {
time.Sleep(time.Second)
channelTwo <- fmt.Sprintf("%d", i)
}
}()
return channelTwo
}
16 changes: 15 additions & 1 deletion then/eventually.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package then
import (
"fmt"
"github.com/corbym/gocrest"
"sync"
"time"
)

Expand All @@ -27,21 +28,33 @@ func (t *RecordingTestingT) FailNow() {
t.failed = true
}

type Latest struct {
mu sync.Mutex
latestValue RecordingTestingT
}

func (l *Latest) Get() RecordingTestingT {
l.mu.Lock()
defer l.mu.Unlock()
return l.latestValue
}
func Eventually(t gocrest.TestingT, waitFor time.Duration, tick time.Duration, assertions func(eventually gocrest.TestingT)) {

t.Helper()
channel := make(chan RecordingTestingT, 1)
defer close(channel)

timer := time.NewTimer(waitFor)
defer timer.Stop()

ticker := time.NewTicker(tick)
defer ticker.Stop()

var latestValue = new(Latest)
for tick := ticker.C; ; {
select {
case <-timer.C:
t.Errorf((<-channel).TestOutput)
t.Errorf(latestValue.Get().TestOutput)
return
case <-tick:
tick = nil
Expand All @@ -57,6 +70,7 @@ func Eventually(t gocrest.TestingT, waitFor time.Duration, tick time.Duration, a
if !value.failed {
return
}
latestValue.latestValue = value
tick = ticker.C
}
}
Expand Down

0 comments on commit a89f045

Please sign in to comment.