Skip to content

Commit

Permalink
Add missing tests and update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
bayraktugrul committed Aug 18, 2024
1 parent 36007cd commit c4bfd35
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 5 deletions.
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
go-await lets you use to synchronize async operations. It is inspired from popular Java library 'awaitility [https://github.com/awaitility/awaitility]'

## Install
With [Go's module support](https://go.dev/wiki/Modules#how-to-use-modules), `go [build|run|test]` automatically fetches the necessary dependencies when you add the import in your code:

```bash
go get github.com/bayraktugrul/go-await
```sh
import "github.com/bayraktugrul/go-await"
```

Alternatively, use `go get`:

```sh
go get -u github.com/bayraktugrul/go-await
```

## Examples
Expand Down Expand Up @@ -37,6 +44,27 @@ err := await.New().PollInterval(500 * time.Millisecond).AtMost(2 * time.Second).
if err != nil {}
```

### Await with custom poll interval strategy

The default polling strategy is Fixed, returning the same interval for each iteration. You can customize this by setting a different poll interval strategy.
```go

producer.publishMessage(orderCreatedMessage)

// waits with double poll interval strategy
// for up to 2 seconds until repo.Exist returns true,
// using a polling interval of 500ms
err := await.New().
PollStrategy(poll.Double).
PollInterval(100 * time.Millisecond).
AtMost(2 * time.Second).
Await(func() bool {
return repo.Exist(id) // any condition you want to wait for until it returns true
})

if err != nil {}
```

## Credits

* [Tuğrul Bayrak](https://github.com/bayraktugrul)
2 changes: 1 addition & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (a *app) Await(waitFunc func() bool) error {

go until(waitFunc, result)

iter := 0
iter := 1
for {
select {
case finish := <-result:
Expand Down
14 changes: 13 additions & 1 deletion strategy/poll/double_poll_interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,19 @@ import (
"time"
)

// DoublePollInterval TODO:write explanation
// DoublePollInterval calculates the poll interval for a given iteration
// using an exponential backoff strategy. The interval is doubled with each iteration,
// starting from the initial interval value provided.
//
// Iterations start from 0 (zero). At each step, the interval is multiplied by 2.
//
// Example1: For iteration: 3 and interval: 100 * time.Millisecond
//
// Step 1: 100ms * 2 = 200ms
// Step 2: 200ms * 2 = 400ms
// Step 3: 400ms * 2 = 800ms
//
// Result: 800ms
func DoublePollInterval(iteration int, interval time.Duration) time.Duration {
return utils.MultiplyDuration(iteration, interval)
}
22 changes: 22 additions & 0 deletions strategy/poll/double_poll_interval_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package poll_test

import (
"github.com/bayraktugrul/go-await/strategy/poll"
"github.com/stretchr/testify/assert"
"testing"
"time"
)

func Test_DoubleInterval(t *testing.T) {
t.Run("it should return 800ms ms when iteration is 3 and interval is 100ms", func(t *testing.T) {
//given
iteration := 3
interval := 100 * time.Millisecond

//when
result := poll.DoublePollInterval(iteration, interval)

//then
assert.Equal(t, 800*time.Millisecond, result)
})
}
14 changes: 13 additions & 1 deletion strategy/poll/fixed_poll_interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@ package poll

import "time"

// FixedPollInterval poll interval strategy always returns given interval
// FixedPollInterval is a poll interval strategy that consistently returns the same fixed interval,
// regardless of the iteration count. This strategy ensures that the polling frequency remains constant
// throughout all iterations, with no backoff or variation.
//
// Example: For iteration: 0 to N and interval: 100 * time.Millisecond
//
// Iteration 0: 100ms
// Iteration 1: 100ms
// Iteration 2: 100ms
// ...
// Iteration N: 100ms
//
// Result: The interval remains fixed at 100ms for every iteration.
func FixedPollInterval(iteration int, interval time.Duration) time.Duration {
return interval
}
22 changes: 22 additions & 0 deletions strategy/poll/fixed_poll_interval_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package poll_test

import (
"github.com/bayraktugrul/go-await/strategy/poll"
"github.com/stretchr/testify/assert"
"testing"
"time"
)

func Test_FixedInterval(t *testing.T) {
t.Run("it should return 100ms ms when iteration is 3 and interval is 100ms", func(t *testing.T) {
//given
iteration := 3
interval := 100 * time.Millisecond

//when
result := poll.FixedPollInterval(iteration, interval)

//then
assert.Equal(t, 100*time.Millisecond, result)
})
}

0 comments on commit c4bfd35

Please sign in to comment.