github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/consensus/changelog_test.go (about)

     1  package consensus
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/NebulousLabs/Sia/modules"
     7  	"github.com/NebulousLabs/Sia/types"
     8  )
     9  
    10  // TestIntegrationChangeLog does a general test of the changelog by creating a
    11  // subscriber that subscribes partway into startup and checking that the
    12  // correct ordering of blocks are provided.
    13  func TestIntegrationChangeLog(t *testing.T) {
    14  	if testing.Short() {
    15  		t.SkipNow()
    16  	}
    17  	// Get a blank consensus set tester so that the mocked subscriber can join
    18  	// immediately after genesis.
    19  	cst, err := blankConsensusSetTester("TestIntegrationChangeLog")
    20  	if err != nil {
    21  		t.Fatal(err)
    22  	}
    23  
    24  	// Add a mocked subscriber and check that it receives the correct number of
    25  	// blocks.
    26  	ms := newMockSubscriber()
    27  	cst.cs.ConsensusSetSubscribe(&ms, modules.ConsensusChangeBeginning)
    28  	if ms.updates[0].AppliedBlocks[0].ID() != cst.cs.blockRoot.Block.ID() {
    29  		t.Fatal("subscription did not correctly receive the genesis block")
    30  	}
    31  	if len(ms.updates) != 1 {
    32  		t.Fatal("subscription resulted in the wrong number of blocks being sent")
    33  	}
    34  
    35  	// Create a copy of the subscriber that will subscribe to the consensus at
    36  	// the tail of the updates.
    37  	tailSubscriber := ms.copySub()
    38  	cst.cs.ConsensusSetSubscribe(&tailSubscriber, tailSubscriber.updates[len(tailSubscriber.updates)-1].ID)
    39  	if len(tailSubscriber.updates) != 1 {
    40  		t.Fatal("subscription resulted in the wrong number of blocks being sent")
    41  	}
    42  
    43  	// Create a copy of the subscriber that will join when it is not at 0, but it is behind.
    44  	behindSubscriber := ms.copySub()
    45  	cst.addSiafunds()
    46  	cst.mineSiacoins()
    47  	cst.cs.ConsensusSetSubscribe(&behindSubscriber, behindSubscriber.updates[len(behindSubscriber.updates)-1].ID)
    48  	if types.BlockHeight(len(behindSubscriber.updates)) != cst.cs.dbBlockHeight()+1 {
    49  		t.Fatal("subscription resulted in the wrong number of blocks being sent")
    50  	}
    51  	if len(ms.updates) != len(tailSubscriber.updates) {
    52  		t.Error("subscribers have inconsistent update chains")
    53  	}
    54  	if len(ms.updates) != len(behindSubscriber.updates) {
    55  		t.Error("subscribers have inconsisitent update chains")
    56  	}
    57  }