PxyUp / verifiers

Small library for verify async function response

Home Page:https://pkg.go.dev/github.com/PxyUp/verifiers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Verifiers

codecov

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

For Go v1.18+(with generics)

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...))
}

About

Small library for verify async function response

https://pkg.go.dev/github.com/PxyUp/verifiers

License:MIT License


Languages

Language:Go 100.0%