Skip to content

Commit

Permalink
Implement internal/errors/worker test (#952)
Browse files Browse the repository at this point in the history
* implement internal/errors/worker test

* add comments to internal/errors/worker

* Apply suggestions from code review

Co-authored-by: Kiichiro YUKAWA <kyukawa315@gmail.com>

* Apply suggestions from code review

Co-authored-by: Kiichiro YUKAWA <kyukawa315@gmail.com>

Co-authored-by: Kiichiro YUKAWA <kyukawa315@gmail.com>
  • Loading branch information
kevindiu and vankichi authored Feb 4, 2021
1 parent c1c6dc0 commit 9c405ba
Show file tree
Hide file tree
Showing 2 changed files with 312 additions and 0 deletions.
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 <vald@vdaas.org>
//
// 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)
}
})
}
}

0 comments on commit 9c405ba

Please sign in to comment.