github.com/holochain/holochain-proto@v0.1.0-alpha-26.0.20200915073418-5c83169c9b5b/action_bundle_test.go (about) 1 package holochain 2 3 import ( 4 "testing" 5 . "github.com/smartystreets/goconvey/convey" 6 . "github.com/holochain/holochain-proto/hash" 7 ) 8 9 func TestActionBundle(t *testing.T) { 10 d, _, h := PrepareTestChain("test") 11 defer CleanupTestChain(h, d) 12 Convey("bundle action constructor should set timeout", t, func() { 13 a := NewStartBundleAction(0, "myBundle") 14 So(a.timeout, ShouldEqual, DefaultBundleTimeout) 15 So(a.userParam, ShouldEqual, "myBundle") 16 a = NewStartBundleAction(123, "myBundle") 17 So(a.timeout, ShouldEqual, 123) 18 }) 19 20 Convey("starting a bundle should set the bundle start point", t, func() { 21 c := h.Chain() 22 So(c.BundleStarted(), ShouldBeNil) 23 a := NewStartBundleAction(100, "myBundle") 24 _, err := a.Call(h) 25 So(err, ShouldBeNil) 26 So(c.BundleStarted().idx, ShouldEqual, c.Length()-1) 27 }) 28 var hash Hash 29 Convey("commit actions should commit to bundle after it's started", t, func() { 30 So(h.chain.Length(), ShouldEqual, 2) 31 So(h.chain.bundle.chain.Length(), ShouldEqual, 0) 32 hash = commit(h, "oddNumbers", "99") 33 34 So(h.chain.Length(), ShouldEqual, 2) 35 So(h.chain.bundle.chain.Length(), ShouldEqual, 1) 36 }) 37 Convey("but those commits should not show in the DHT", t, func() { 38 _, _, _, _, err := h.dht.Get(hash, StatusDefault, GetMaskDefault) 39 So(err, ShouldEqual, ErrHashNotFound) 40 }) 41 42 Convey("closing a bundle should commit its entries to the chain", t, func() { 43 So(h.chain.Length(), ShouldEqual, 2) 44 a := &APIFnCloseBundle{commit: true} 45 So(a.commit, ShouldEqual, true) 46 _, err := a.Call(h) 47 So(err, ShouldBeNil) 48 So(h.chain.Length(), ShouldEqual, 3) 49 }) 50 Convey("and those commits should now show in the DHT", t, func() { 51 data, _, _, _, err := h.dht.Get(hash, StatusDefault, GetMaskDefault) 52 So(err, ShouldBeNil) 53 var e GobEntry 54 err = e.Unmarshal(data) 55 56 So(e.C, ShouldEqual, "99") 57 }) 58 59 Convey("canceling a bundle should not commit entries to chain and should execute the bundleCanceled callback", t, func() { 60 So(h.chain.Length(), ShouldEqual, 3) 61 62 _, err := NewStartBundleAction(0, "debugit").Call(h) 63 So(err, ShouldBeNil) 64 commit(h, "oddNumbers", "7") 65 66 a := &APIFnCloseBundle{commit: false} 67 So(a.commit, ShouldEqual, false) 68 ShouldLog(h.nucleus.alog, func() { 69 _, err = a.Call(h) 70 So(err, ShouldBeNil) 71 }, `debug message during bundleCanceled with reason: userCancel`) 72 So(h.chain.Length(), ShouldEqual, 3) 73 So(h.chain.BundleStarted(), ShouldBeNil) 74 }) 75 Convey("canceling a bundle should still commit entries if bundleCanceled returns BundleCancelResponseCommit", t, func() { 76 So(h.chain.Length(), ShouldEqual, 3) 77 78 _, err := NewStartBundleAction(0, "cancelit").Call(h) 79 So(err, ShouldBeNil) 80 commit(h, "oddNumbers", "7") 81 a := &APIFnCloseBundle{commit: false} 82 So(a.commit, ShouldEqual, false) 83 ShouldLog(h.nucleus.alog, func() { 84 _, err = a.Call(h) 85 So(err, ShouldBeNil) 86 }, `debug message during bundleCanceled: canceling cancel!`) 87 So(h.chain.BundleStarted(), ShouldNotBeNil) 88 }) 89 }