Verifiers
Small GO library for verify async function response.
Provide some basic functionality for conditional check
Usage
go get github.com/PxyUp/verifiers
Important: all function will be finished if condition are matched (it is mean all child routine will be stopped)
Methods
- verifier.All(...Verifier) - is equal verifier.Exact(len(fns), fns ...Verifier)
- verifier.OneOf(...Verifier) - is equal verifier.AtLeast(1, ...Verifier)
- verifier.AtLeast(int, ...Verifier)
- verifier.Exact(int, ...Verifier)
- verifier.OnlyOne(...Verifier) - is equal verifier.Exact(1, ...Verifier)
- verifier.NoOne(...Verifier) - is equal verifier.Exact(0, ...Verifier)
For Go v1.18+(with generics)
- verifiers.FromArray[T any](arr []T, cmp func(context.Context, T) error) - generate Verifier from static array
List of errors
// ErrCountMoreThanLength is configuration error.
// Will return if we expect more function than we provide for verifier.AtLeast or verifier.Exact
verifiers.ErrCountMoreThanLength = errors.New("cant wait more than exists")
// ErrMaxAmountOfError wii be returned some function which we not expect return error
verifiers.ErrMaxAmountOfError = errors.New("verifier reach max amount of error")
// ErrMaxAmountOfFinished will be returned if some other function(which we not expect) return success
verifiers.ErrMaxAmountOfFinished = errors.New("verifier reach max amount success jobs")
verifier.All
type Verifier func(ctx context.Context) error
All(fns ...Verifier) error
Method verifies is all function finished without error in given context timeout/deadline
Example success:
verifier := verifiers.New(ctx)
startTime := time.Now()
err := verifier.All(
func(ctx context.Context) error {
time.Sleep(time.Second)
return nil
},
func(ctx context.Context) error {
time.Sleep(time.Second * 2)
return nil
},
func(ctx context.Context) error {
time.Sleep(time.Second * 3)
return nil
},
)
// Because we should wait latest function
assert.True(t, time.Now().Sub(startTime) >= time.Second*3)
Example error:
verifier := verifiers.New(ctx)
startTime := time.Now()
err := verifier.All(
func(ctx context.Context) error {
time.Sleep(time.Second)
return errors.New("")
},
func(ctx context.Context) error {
time.Sleep(time.Second * 2)
return nil
},
func(ctx context.Context) error {
time.Sleep(time.Second * 3)
return nil
},
)
// Because we will throw error after first return it
assert.True(t, time.Now().Sub(startTime) < time.Second*2)
assert.Err(t, verifiers.ErrMaxAmountOfError)
verifier.OneOf
type Verifier func(ctx context.Context) error
OneOf(fns ...Verifier) error
Method verifies is at least one function finished without error in given context timeout/deadline
verifier := verifiers.New(ctx)
startTime := time.Now()
err := verifier.OneOf(
func(ctx context.Context) error {
time.Sleep(time.Second)
return nil
},
func(ctx context.Context) error {
time.Sleep(time.Second * 2)
return errors.New("")
},
func(ctx context.Context) error {
time.Sleep(time.Second * 3)
return nil
},
)
// Because we should wait second function
assert.True(t, time.Now().Sub(startTime) >= time.Second*1)
assert.True(t, time.Now().Sub(startTime) < time.Second*2)
assert.Nil(t, err)
verifier.AtLeast
type Verifier func(ctx context.Context) error
AtLeast(count int, fns ...Verifier) error
Method verifies is at least provided amount of functions will be finished without error in given context timeout/deadline
verifier := verifiers.New(ctx)
startTime := time.Now()
err := verifier.AtLeast(
2,
func(ctx context.Context) error {
time.Sleep(time.Second)
return nil
},
func(ctx context.Context) error {
time.Sleep(time.Second * 2)
return nil
},
func(ctx context.Context) error {
time.Sleep(time.Second * 3)
return errors.New("")
},
)
// Because we should wait second function
assert.True(t, time.Now().Sub(startTime) > time.Second*1)
assert.True(t, time.Now().Sub(startTime) <= time.Second*3)
assert.Nil(t, err)
verifier.Exact
type Verifier func(ctx context.Context) error
Exact(count int, fns ...Verifier) error
Method verify exactly provided amount of functions finished without error in given context timeout/deadline
verifier := verifiers.New(ctx)
startTime := time.Now()
err := verifier.Exact(
2,
func(ctx context.Context) error {
time.Sleep(time.Second)
return nil
},
func(ctx context.Context) error {
time.Sleep(time.Second * 2)
return nil
},
func(ctx context.Context) error {
time.Sleep(time.Second * 3)
return errors.New("")
},
)
// Because we should wait three function(all functions should be finished)
assert.True(t, time.Now().Sub(startTime) >= time.Second*3)
assert.Nil(t, err)
verifier.OnlyOne
type Verifier func(ctx context.Context) error
OnyOne(fns ...Verifier) error
Method verify exactly one function finished without error in given context timeout/deadline
verifier := verifiers.New(ctx)
startTime := time.Now()
err := verifier.OnlyOne(
func(ctx context.Context) error {
time.Sleep(time.Second)
return nil
},
func(ctx context.Context) error {
time.Sleep(time.Second * 2)
return errors.New("")
},
func(ctx context.Context) error {
time.Sleep(time.Second * 3)
return errors.New("")
},
)
// Because we should wait three function(all functions should be finished)
assert.True(t, time.Now().Sub(startTime) >= time.Second*3)
assert.Nil(t, err)
verifier.NoOne
type Verifier func(ctx context.Context) error
NoOne(fns ...Verifier) error
Method verifies no one from functions finished without error in given context timeout/deadline
verifier := verifiers.New(ctx)
startTime := time.Now()
err := verifier.NoOne(
func(ctx context.Context) error {
time.Sleep(time.Second)
return errors.New("")
},
func(ctx context.Context) error {
time.Sleep(time.Second * 2)
return errors.New("")
},
func(ctx context.Context) error {
time.Sleep(time.Second * 3)
return errors.New("")
},
)
// Because we should wait three function(all functions should be finished)
assert.True(t, time.Now().Sub(startTime) >= time.Second*3)
assert.Nil(t, err)
verifiers.FromArray
JUST FOR Go v1.18+(GENERIC)
type Verifier func(ctx context.Context) error
func FromArray[T any](arr []T, cmp func(context.Context, T) error) []Verifier
Method FromArray generate Verifier from static generic array. With that method you can also verify static array
package verifiers_test
import (
"context"
"errors"
"github.com/PxyUp/verifiers"
"github.com/stretchr/testify/assert"
"testing"
)
func Test(t *testing.T) {
type People struct {
Name string
Age int
}
people := []*People{
{
Age: 20,
Name: "First",
},
{
Age: 25,
Name: "Second",
},
{
Age: 30,
Name: "Third",
},
}
fns := verifiers.FromArray(people, func(ctx context.Context, p *People) error {
if p.Age >= 25 {
return nil
}
return errors.New("to old")
})
v := verifiers.New(context.Background())
assert.Equal(t, verifiers.ErrMaxAmountOfError, v.All(fns...))
assert.Equal(t, verifiers.ErrMaxAmountOfFinished, v.NoOne(fns...))
assert.Equal(t, nil, v.OneOf(fns...))
assert.Equal(t, nil, v.Exact(2, fns...))
assert.Equal(t, verifiers.ErrMaxAmountOfFinished, v.OnlyOne(fns...))
}