Skip to content
This repository has been archived by the owner on Feb 17, 2025. It is now read-only.

Commit

Permalink
Refine function assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
pellared committed Oct 21, 2022
1 parent 08abbfe commit 38ba99c
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 280 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ The main additions are the new assertions for
### Added

- Add `True`, `False`, `NoError` assertion functions.
- Add `Panics`, `NoPanic` assertion functions.
- Add `Eventually`, `EventuallyChan` assertion functions.
- Add `Ordered` function which provides following assertions,
in addition to `Comparable`, via `FluentOrdered` type:
- `Lesser`
Expand Down
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ $ go test
TODO
```

### Asynchronous (periodic polling)
### Periodic polling

```go
package test
Expand All @@ -135,26 +135,23 @@ import (
"github.com/fluentassert/verify"
)

func TestAsync(t *testing.T) {
verify.Periodic(10*time.Second, time.Second, func() verify.FailureMessage {
func TestPeriodic(t *testing.T) {
verify.Eventually(10*time.Second, time.Second, func() verify.FailureMessage {
client := http.Client{Timeout: time.Second}
resp, err := client.Get("http://not-existing:1234")
if err != nil {
return verify.NoError(err)
}
return verify.Number(resp.StatusCode).Lesser(300)
}).Eventually().Assert(t)
}).Assert(t)
}
```

```sh
$ go test
--- FAIL: TestAsync (10.00s)
--- FAIL: TestPeriodic (10.00s)
async_test.go:19:
timeout
function always failed
last failure message:
non-nil error:
function never passed, last failure message:
Get "http://not-existing:1234": context deadline exceeded (Client.Timeout exceeded while awaiting headers)
```
Expand Down
111 changes: 0 additions & 111 deletions async.go

This file was deleted.

115 changes: 0 additions & 115 deletions async_test.go

This file was deleted.

49 changes: 49 additions & 0 deletions eventually.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package verify

import (
"time"
)

// Eventually executes the test function until it returns an empty FailureMessage
// or timeout elapses.
func Eventually(timeout, interval time.Duration, fn func() FailureMessage) FailureMessage {
timer := time.NewTimer(timeout)
defer timer.Stop()
ticker := time.NewTicker(interval)
defer ticker.Stop()
return EventuallyChan(timer.C, ticker.C, fn)
}

// EventuallyChan executes the test function until it returns an empty FailureMessage or timeout elapses.
func EventuallyChan[TTimerPayload, TTickPayload any](timeout <-chan (TTimerPayload), ticker <-chan (TTickPayload), fn func() FailureMessage) FailureMessage {
var err string
failMsg := func(cause string) FailureMessage {
return FailureMessage("function never passed, last failure message:\n" + err)
}

for {
select {
case <-timeout:
return failMsg("timeout")
default:
}

err = string(fn())

select {
case <-timeout:
return failMsg("timeout")
default:
}

if err == "" {
return ""
}

select {
case <-timeout:
return failMsg("timeout")
case <-ticker:
}
}
}
Loading

0 comments on commit 38ba99c

Please sign in to comment.