Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check mock #9

Merged
merged 5 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion checkers/checkers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package checkers
import (
"context"
"encoding/json"
"sync"

"github.com/pkg/errors"
)
Expand All @@ -11,9 +12,15 @@ type Checker interface {
Check(ctx context.Context) error
}

var registry = map[string]func(in json.RawMessage) (Checker, error){}
var (
registry = map[string]func(in json.RawMessage) (Checker, error){}
registryMutex = &sync.RWMutex{}
)

func NewCheckerByKind(kind string, spec json.RawMessage) (Checker, error) {
registryMutex.RLock()
defer registryMutex.RUnlock()

c, ok := registry[kind]
if !ok {
return nil, errors.Errorf("checker with kind `%s` is not registered", kind)
Expand All @@ -23,6 +30,9 @@ func NewCheckerByKind(kind string, spec json.RawMessage) (Checker, error) {
}

func Register(kind string, fn func(in json.RawMessage) (Checker, error)) error {
registryMutex.Lock()
defer registryMutex.Unlock()

if _, ok := registry[kind]; ok {
return errors.Errorf("checker with kind `%s` already registered", kind)
}
Expand Down
22 changes: 22 additions & 0 deletions checkers/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package checkers

import (
"context"

"github.com/stretchr/testify/mock"
)

var _ Checker = (*Mock)(nil)

type Mock struct {
mock.Mock
}

func NewMock() *Mock {
return &Mock{}
}

func (m *Mock) Check(context.Context) error {
args := m.Called()
return args.Error(0)
}
Loading