diff --git a/drr.go b/drr.go index ef5dd95..51c9ab5 100644 --- a/drr.go +++ b/drr.go @@ -4,20 +4,20 @@ package drr import ( "context" - "fmt" + "errors" "reflect" ) var ( - // InvalidPriorityValueError error is returned by Input method when + // ErrInvalidPriorityValue error is returned by Input method when // priority value is less than or equal to 0. - InvalidPriorityValueError = fmt.Errorf("InvalidPriorityValueError") - // ChannelIsNilError error is returned by NewDRR and Input methods + ErrInvalidPriorityValue = errors.New("ErrInvalidPriorityValue") + // ErrChannelIsNil error is returned by NewDRR and Input methods // when channel is nil. - ChannelIsNilError = fmt.Errorf("ChannelIsNilError") - // ContextIsNilError is returned by Start method when context.Context + ErrChannelIsNil = errors.New("ErrChannelIsNil") + // ErrContextIsNil is returned by Start method when context.Context // is nil - ContextIsNilError = fmt.Errorf("ContextIsNil") + ErrContextIsNil = errors.New("ContextIsNil") ) type flow struct { @@ -36,10 +36,10 @@ type DRR struct { // NewDRR creates a new DRR with indicated output channel. // // The outChan must be non-nil, otherwise NewDRR returns -// ChannelIsNilError error. +// ErrChannelIsNil error. func NewDRR(outChan chan interface{}) (*DRR, error) { if outChan == nil { - return nil, ChannelIsNilError + return nil, ErrChannelIsNil } return &DRR{ outChan: outChan, @@ -49,15 +49,15 @@ func NewDRR(outChan chan interface{}) (*DRR, error) { // Input registers a new ingress flow, that is a channel with // priority. // -// Input returns ChannelIsNilError if input channel is nil. +// Input returns ErrChannelIsNil if input channel is nil. // Priority must be greater than 0, otherwise Input returns -// InvalidPriorityValueError error. +// ErrInvalidPriorityValue error. func (d *DRR) Input(prio int, in <-chan interface{}) error { if prio <= 0 { - return InvalidPriorityValueError + return ErrInvalidPriorityValue } if in == nil { - return ChannelIsNilError + return ErrChannelIsNil } d.flows = append(d.flows, flow{c: in, prio: prio}) return nil @@ -73,7 +73,7 @@ func (d *DRR) Input(prio int, in <-chan interface{}) error { // channels are closed. DRR goroutine closes the output channel upon termination. func (d *DRR) Start(ctx context.Context) error { if ctx == nil { - return ContextIsNilError + return ErrContextIsNil } go func() { defer close(d.outChan) diff --git a/drr_test.go b/drr_test.go index c434dce..7cf2619 100644 --- a/drr_test.go +++ b/drr_test.go @@ -111,7 +111,7 @@ func getFlowID(s string) int { idStr := strings.Split(s, ":")[0] id, err := strconv.Atoi(idStr) if err != nil { - panic(fmt.Errorf("convert of string %s failed: %v", s, err)) + panic(fmt.Errorf("convert of string %s failed: %w", s, err)) } return id } @@ -164,19 +164,19 @@ func TestErrorInput(t *testing.T) { Convey("Create DRR by passing nil output chan", t, func() { drr, err := NewDRR(nil) So(drr, ShouldEqual, nil) - So(err, ShouldEqual, ChannelIsNilError) + So(err, ShouldEqual, ErrChannelIsNil) }) Convey("Create DRR and pass wrong values in Input API", t, func() { drr, _ := NewDRR(make(chan interface{})) err := drr.Input(0, make(chan interface{})) - So(err, ShouldEqual, InvalidPriorityValueError) + So(err, ShouldEqual, ErrInvalidPriorityValue) err = drr.Input(1, nil) - So(err, ShouldEqual, ChannelIsNilError) + So(err, ShouldEqual, ErrChannelIsNil) }) Convey("Create DRR and pass wrong values in Input API", t, func() { drr, _ := NewDRR(make(chan interface{})) err := drr.Start(nil) - So(err, ShouldEqual, ContextIsNilError) + So(err, ShouldEqual, ErrContextIsNil) }) }