summaryrefslogtreecommitdiff
path: root/vendor/github.com/getlantern/ops/ops_test.go
diff options
context:
space:
mode:
authorKali Kaneko (leap communications) <kali@leap.se>2019-01-12 18:39:45 +0100
committerRuben Pollan <meskio@sindominio.net>2019-01-17 12:30:32 +0100
commitb1247d2d0d51108c910a73891ff3116e5f032ab1 (patch)
treee9948964f0bfb1ad2df3bc7bad02aa1f41ccfbd8 /vendor/github.com/getlantern/ops/ops_test.go
parentefcb8312e31b5c2261b1a1e95ace55b322cfcc27 (diff)
[pkg] all your deps are vendored to us
Diffstat (limited to 'vendor/github.com/getlantern/ops/ops_test.go')
-rw-r--r--vendor/github.com/getlantern/ops/ops_test.go76
1 files changed, 76 insertions, 0 deletions
diff --git a/vendor/github.com/getlantern/ops/ops_test.go b/vendor/github.com/getlantern/ops/ops_test.go
new file mode 100644
index 0000000..1c16e73
--- /dev/null
+++ b/vendor/github.com/getlantern/ops/ops_test.go
@@ -0,0 +1,76 @@
+package ops_test
+
+import (
+ "sync"
+ "testing"
+
+ "github.com/getlantern/errors"
+ "github.com/getlantern/ops"
+ "github.com/stretchr/testify/assert"
+)
+
+func TestSuccess(t *testing.T) {
+ var reportedFailure error
+ var reportedCtx map[string]interface{}
+ report := func(failure error, ctx map[string]interface{}) {
+ reportedFailure = failure
+ reportedCtx = ctx
+ }
+
+ ops.RegisterReporter(report)
+ ops.SetGlobal("g", "g1")
+ op := ops.Begin("test_success").Set("a", 1).SetDynamic("b", func() interface{} { return 2 })
+ defer op.End()
+ innerOp := op.Begin("inside")
+ innerOp.FailIf(nil)
+ innerOp.End()
+
+ assert.Nil(t, reportedFailure)
+ expectedCtx := map[string]interface{}{
+ "op": "inside",
+ "root_op": "test_success",
+ "g": "g1",
+ "a": 1,
+ "b": 2,
+ }
+ assert.Equal(t, expectedCtx, reportedCtx)
+}
+
+func TestFailure(t *testing.T) {
+ doTestFailure(t, false)
+}
+
+func TestCancel(t *testing.T) {
+ doTestFailure(t, true)
+}
+
+func doTestFailure(t *testing.T, cancel bool) {
+ var reportedFailure error
+ var reportedCtx map[string]interface{}
+ report := func(failure error, ctx map[string]interface{}) {
+ reportedFailure = failure
+ reportedCtx = ctx
+ }
+
+ ops.RegisterReporter(report)
+ op := ops.Begin("test_failure")
+ var wg sync.WaitGroup
+ wg.Add(1)
+ op.Go(func() {
+ op.FailIf(errors.New("I failed").With("errorcontext", 5))
+ wg.Done()
+ })
+ wg.Wait()
+ if cancel {
+ op.Cancel()
+ }
+ op.End()
+
+ if cancel {
+ assert.Nil(t, reportedFailure)
+ assert.Nil(t, reportedCtx)
+ } else {
+ assert.Contains(t, reportedFailure.Error(), "I failed")
+ assert.Equal(t, 5, reportedCtx["errorcontext"])
+ }
+}