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

Implement internal/errors/worker test #952

Merged
merged 4 commits into from
Feb 4, 2021
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
6 changes: 6 additions & 0 deletions internal/errors/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,28 @@
package errors

var (

// ErrWorkerIsNotRunning represents a function to generate worker is not running error.
ErrWorkerIsNotRunning = func(name string) error {
return Errorf("worker %s is not running", name)
}

// ErrWorkerIsAlreadyRunning represents a function to generate worker is already running error.
ErrWorkerIsAlreadyRunning = func(name string) error {
return Errorf("worker %s is already running", name)
}

// ErrQueueIsNotRunning represents a function to generate the queue is not running error.
ErrQueueIsNotRunning = func() error {
return New("queue is not running")
}

// ErrQueueIsAlreadyRunning represents a function to generate the queue is already running error.
ErrQueueIsAlreadyRunning = func() error {
return New("queue is already running")
}

// ErrJobFuncIsNil represents a function to generate job function is nil error.
ErrJobFuncIsNil = func() error {
return New("JobFunc is nil")
}
Expand Down
306 changes: 306 additions & 0 deletions internal/errors/worker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,306 @@
//
// Copyright (C) 2019-2021 vdaas.org vald team <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

// Package errors provides error types and function
package errors

import (
"testing"

"go.uber.org/goleak"
)

func TestErrWorkerIsNotRunning(t *testing.T) {
t.Parallel()
type args struct {
name string
}
type want struct {
err error
}
type test struct {
name string
args args
want want
checkFunc func(want, error) error
beforeFunc func(args)
afterFunc func(args)
}
defaultCheckFunc := func(w want, err error) error {
if !Is(err, w.err) {
return Errorf("got_error: \"%#v\",\n\t\t\t\twant: \"%#v\"", err, w.err)
}
return nil
}
tests := []test{
{
name: "return an ErrWorkerIsNotRunning error when name is index",
args: args{
name: "index",
},
want: want{
err: New("worker index is not running"),
},
},
{
name: "return an ErrWorkerIsNotRunning error when name is empty",
args: args{
name: "",
},
want: want{
err: New("worker is not running"),
},
},
}

for _, tc := range tests {
test := tc
t.Run(test.name, func(tt *testing.T) {
tt.Parallel()
defer goleak.VerifyNone(tt, goleak.IgnoreCurrent())
if test.beforeFunc != nil {
test.beforeFunc(test.args)
}
if test.afterFunc != nil {
defer test.afterFunc(test.args)
}
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}

err := ErrWorkerIsNotRunning(test.args.name)
if err := test.checkFunc(test.want, err); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}

func TestErrWorkerIsAlreadyRunning(t *testing.T) {
t.Parallel()
type args struct {
name string
}
type want struct {
err error
}
type test struct {
name string
args args
want want
checkFunc func(want, error) error
beforeFunc func(args)
afterFunc func(args)
}
defaultCheckFunc := func(w want, err error) error {
if !Is(err, w.err) {
return Errorf("got_error: \"%#v\",\n\t\t\t\twant: \"%#v\"", err, w.err)
}
return nil
}
tests := []test{
{
name: "return an ErrWorkerIsAlreadyRunning error when name is index",
args: args{
name: "index",
},
want: want{
err: New("worker index is already running"),
},
},
{
name: "return an ErrWorkerIsAlreadyRunning error when name is empty",
args: args{
name: "",
},
want: want{
err: New("worker is already running"),
},
},
}

for _, tc := range tests {
test := tc
t.Run(test.name, func(tt *testing.T) {
tt.Parallel()
defer goleak.VerifyNone(tt, goleak.IgnoreCurrent())
if test.beforeFunc != nil {
test.beforeFunc(test.args)
}
if test.afterFunc != nil {
defer test.afterFunc(test.args)
}
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}

err := ErrWorkerIsAlreadyRunning(test.args.name)
if err := test.checkFunc(test.want, err); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}

func TestErrQueueIsNotRunning(t *testing.T) {
t.Parallel()
type want struct {
err error
}
type test struct {
name string
want want
checkFunc func(want, error) error
beforeFunc func()
afterFunc func()
}
defaultCheckFunc := func(w want, err error) error {
if !Is(err, w.err) {
return Errorf("got_error: \"%#v\",\n\t\t\t\twant: \"%#v\"", err, w.err)
}
return nil
}
tests := []test{
{
name: "return an ErrQueueIsNotRunning error",
want: want{
err: New("queue is not running"),
},
},
}

for _, tc := range tests {
test := tc
t.Run(test.name, func(tt *testing.T) {
tt.Parallel()
defer goleak.VerifyNone(tt, goleak.IgnoreCurrent())
if test.beforeFunc != nil {
test.beforeFunc()
}
if test.afterFunc != nil {
defer test.afterFunc()
}
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}

err := ErrQueueIsNotRunning()
if err := test.checkFunc(test.want, err); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}

func TestErrQueueIsAlreadyRunning(t *testing.T) {
t.Parallel()
type want struct {
err error
}
type test struct {
name string
want want
checkFunc func(want, error) error
beforeFunc func()
afterFunc func()
}
defaultCheckFunc := func(w want, err error) error {
if !Is(err, w.err) {
return Errorf("got_error: \"%#v\",\n\t\t\t\twant: \"%#v\"", err, w.err)
}
return nil
}
tests := []test{
{
name: "return an ErrQueueIsAlreadyRunning error",
want: want{
err: New("queue is already running"),
},
},
}

for _, tc := range tests {
test := tc
t.Run(test.name, func(tt *testing.T) {
tt.Parallel()
defer goleak.VerifyNone(tt, goleak.IgnoreCurrent())
if test.beforeFunc != nil {
test.beforeFunc()
}
if test.afterFunc != nil {
defer test.afterFunc()
}
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}

err := ErrQueueIsAlreadyRunning()
if err := test.checkFunc(test.want, err); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}

func TestErrJobFuncIsNil(t *testing.T) {
t.Parallel()
type want struct {
err error
}
type test struct {
name string
want want
checkFunc func(want, error) error
beforeFunc func()
afterFunc func()
}
defaultCheckFunc := func(w want, err error) error {
if !Is(err, w.err) {
return Errorf("got_error: \"%#v\",\n\t\t\t\twant: \"%#v\"", err, w.err)
}
return nil
}
tests := []test{
{
name: "return an ErrJobFuncIsNil error",
want: want{
err: New("JobFunc is nil"),
},
},
}

for _, tc := range tests {
test := tc
t.Run(test.name, func(tt *testing.T) {
tt.Parallel()
defer goleak.VerifyNone(tt, goleak.IgnoreCurrent())
if test.beforeFunc != nil {
test.beforeFunc()
}
if test.afterFunc != nil {
defer test.afterFunc()
}
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}

err := ErrJobFuncIsNil()
if err := test.checkFunc(test.want, err); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}