-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement internal/errors/worker test (#952)
* 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
Showing
2 changed files
with
312 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |