You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beam.apache.org by lo...@apache.org on 2022/02/24 22:02:12 UTC
[beam] branch master updated: [BEAM-13866] Add small unit tests to errorx, make boolean assignment more clear (#16943)
This is an automated email from the ASF dual-hosted git repository.
lostluck pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/beam.git
The following commit(s) were added to refs/heads/master by this push:
new bf020ac [BEAM-13866] Add small unit tests to errorx, make boolean assignment more clear (#16943)
bf020ac is described below
commit bf020ac1c620c0367244acc6161f228772559f70
Author: Jack McCluskey <34...@users.noreply.github.com>
AuthorDate: Thu Feb 24 17:01:00 2022 -0500
[BEAM-13866] Add small unit tests to errorx, make boolean assignment more clear (#16943)
---
sdks/go/pkg/beam/util/errorx/guarded.go | 2 +-
.../util/errorx/{guarded.go => guarded_test.go} | 48 +++++++++++-----------
2 files changed, 24 insertions(+), 26 deletions(-)
diff --git a/sdks/go/pkg/beam/util/errorx/guarded.go b/sdks/go/pkg/beam/util/errorx/guarded.go
index cc0b07b..186885b 100644
--- a/sdks/go/pkg/beam/util/errorx/guarded.go
+++ b/sdks/go/pkg/beam/util/errorx/guarded.go
@@ -39,7 +39,7 @@ func (g *GuardedError) TrySetError(err error) bool {
g.mu.Lock()
defer g.mu.Unlock()
- upd := g.err == nil
+ upd := (g.err == nil)
if upd {
g.err = err
}
diff --git a/sdks/go/pkg/beam/util/errorx/guarded.go b/sdks/go/pkg/beam/util/errorx/guarded_test.go
similarity index 53%
copy from sdks/go/pkg/beam/util/errorx/guarded.go
copy to sdks/go/pkg/beam/util/errorx/guarded_test.go
index cc0b07b..1e9c9b2 100644
--- a/sdks/go/pkg/beam/util/errorx/guarded.go
+++ b/sdks/go/pkg/beam/util/errorx/guarded_test.go
@@ -13,35 +13,33 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-// Package errorx contains utilities for handling errors.
package errorx
-import "sync"
+import (
+ "errors"
+ "testing"
+)
-// GuardedError is a concurrency-safe error wrapper. It is sticky
-// in that the first error won't be overwritten.
-type GuardedError struct {
- err error
- mu sync.Mutex
-}
-
-// Error returns the guarded error.
-func (g *GuardedError) Error() error {
- g.mu.Lock()
- defer g.mu.Unlock()
-
- return g.err
+func TestTrySetError(t *testing.T) {
+ var gu GuardedError
+ setErr := errors.New("attempted error")
+ success := gu.TrySetError(setErr)
+ if !success {
+ t.Fatal("got false when trying to set error, want true")
+ }
+ if got, want := gu.Error(), setErr; got != want {
+ t.Errorf("got error %v when checking message, want %v", got, want)
+ }
}
-// TrySetError sets the error, if not already set. Returns true iff the
-// error was set.
-func (g *GuardedError) TrySetError(err error) bool {
- g.mu.Lock()
- defer g.mu.Unlock()
-
- upd := g.err == nil
- if upd {
- g.err = err
+func TestTrySetError_bad(t *testing.T) {
+ setErr := errors.New("old error")
+ gu := &GuardedError{err: setErr}
+ success := gu.TrySetError(setErr)
+ if success {
+ t.Fatal("got true when trying to set error, want false")
+ }
+ if got, want := gu.Error(), setErr; got != want {
+ t.Errorf("got error %v when checking message, want %v", got, want)
}
- return upd
}