Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
amyangfei committed Sep 14, 2022
1 parent 36504be commit 4e9fbe9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
18 changes: 12 additions & 6 deletions engine/framework/fake/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package fake

import (
libErrors "errors"
"fmt"
"regexp"

Expand All @@ -32,14 +33,13 @@ func NewJobUnRetryableError(errIn error) *JobUnRetryableError {
}
}

// Message returns raw error message of JobUnRetryableError
func (e *JobUnRetryableError) Message() string {
return "fake job unretryable error"
}

// Error implements error interface
func (e *JobUnRetryableError) Error() string {
return fmt.Sprintf("%s: %s", e.Message(), e.errIn)
return fmt.Sprintf("%s: %s", e.message(), e.errIn)
}

func (e *JobUnRetryableError) message() string {
return "fake job unretryable error"
}

const fakeJobErrorFormat = "fake job unretryable error: (.*)"
Expand All @@ -48,9 +48,15 @@ var fakeJobErrorRegexp = regexp.MustCompile(fakeJobErrorFormat)

// ToFakeJobError tries best to construct a fake job error from an error object
func ToFakeJobError(err error) error {
var errOut *JobUnRetryableError
if libErrors.As(err, &errOut) {
return err
}

subMatch := fakeJobErrorRegexp.FindStringSubmatch(err.Error())
if len(subMatch) > 1 {
return NewJobUnRetryableError(errors.New(subMatch[1]))
}

return err
}
33 changes: 33 additions & 0 deletions engine/framework/fake/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2022 PingCAP, Inc.
//
// 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
//
// http://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,
// See the License for the specific language governing permissions and
// limitations under the License.

package fake

import (
"testing"

"github.com/pingcap/errors"
"github.com/stretchr/testify/require"
)

func TestToFakeJobError(t *testing.T) {
t.Parallel()

normalErr := errors.New("normal error")
fakeJobErr := NewJobUnRetryableError(normalErr)
errFromPlainText := errors.New(fakeJobErr.Error())

require.Equal(t, normalErr, ToFakeJobError(normalErr))
require.Equal(t, fakeJobErr, ToFakeJobError(fakeJobErr))
require.EqualError(t, ToFakeJobError(errFromPlainText), fakeJobErr.Error())
}

0 comments on commit 4e9fbe9

Please sign in to comment.