github.com/johnathanhowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/consensus/subscribe_test.go (about) 1 package consensus 2 3 import ( 4 "testing" 5 6 "github.com/NebulousLabs/Sia/modules" 7 ) 8 9 // mockSubscriber receives and holds changes to the consensus set, remembering 10 // the order in which changes were received. 11 type mockSubscriber struct { 12 updates []modules.ConsensusChange 13 } 14 15 // newMockSubscriber returns a mockSubscriber that is ready to subscribe to a 16 // consensus set. Currently blank, but can be expanded to support more features 17 // in the future. 18 func newMockSubscriber() mockSubscriber { 19 return mockSubscriber{} 20 } 21 22 // ProcessConsensusChange adds a consensus change to the mock subscriber. 23 func (ms *mockSubscriber) ProcessConsensusChange(cc modules.ConsensusChange) { 24 ms.updates = append(ms.updates, cc) 25 } 26 27 // copySub creates and returns a new mock subscriber that has identical 28 // internals to the input mockSubscriber. The copy will not be subscribed to 29 // the consensus set even if the original is. 30 func (ms *mockSubscriber) copySub() (cms mockSubscriber) { 31 cms.updates = make([]modules.ConsensusChange, len(ms.updates)) 32 copy(cms.updates, ms.updates) 33 return cms 34 } 35 36 // TestUnitInvalidConsensusChangeSubscription checks that the consensus set 37 // returns modules.ErrInvalidConsensusChangeID in the event of a subscriber 38 // using an unrecognized id. 39 func TestUnitInvalidConsensusChangeSubscription(t *testing.T) { 40 if testing.Short() { 41 t.Skip() 42 } 43 cst, err := createConsensusSetTester("TestUnitInvalidConsensusChangeSubscription") 44 if err != nil { 45 t.Fatal(err) 46 } 47 defer cst.Close() 48 49 ms := newMockSubscriber() 50 badCCID := modules.ConsensusChangeID{255, 255, 255} 51 err = cst.cs.ConsensusSetSubscribe(&ms, badCCID) 52 if err != modules.ErrInvalidConsensusChangeID { 53 t.Error("consensus set returning the wrong error during an invalid subscription:", err) 54 } 55 } 56 57 // TestUnitUnsubscribe checks that the consensus set correctly unsubscribes a 58 // subscriber if the Unsubscribe call is made. 59 func TestUnitUnsubscribe(t *testing.T) { 60 if testing.Short() { 61 t.Skip() 62 } 63 cst, err := createConsensusSetTester("TestUnitInvalidConsensusChangeSubscription") 64 if err != nil { 65 t.Fatal(err) 66 } 67 defer cst.Close() 68 69 // Subscribe the mock subscriber to the consensus set. 70 ms := newMockSubscriber() 71 err = cst.cs.ConsensusSetSubscribe(&ms, modules.ConsensusChangeBeginning) 72 if err != nil { 73 t.Fatal(err) 74 } 75 76 // Check that the subscriber is receiving updates. 77 msLen := len(ms.updates) 78 if msLen == 0 { 79 t.Error("mock subscriber is not receiving updates") 80 } 81 _, err = cst.miner.AddBlock() // should cause another update to be sent to the subscriber 82 if err != nil { 83 t.Fatal(err) 84 } 85 if len(ms.updates) != msLen+1 { 86 t.Error("mock subscriber did not receive the correct number of updates") 87 } 88 89 // Unsubscribe the subscriber and then check that it is no longer receiving 90 // updates. 91 cst.cs.Unsubscribe(&ms) 92 _, err = cst.miner.AddBlock() 93 if err != nil { 94 t.Fatal(err) 95 } 96 if len(ms.updates) != msLen+1 { 97 t.Error("mock subscriber was not correctly unsubscribed") 98 } 99 }