github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/gossip/state/metastate_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package state 8 9 import ( 10 "testing" 11 12 "github.com/hyperledger/fabric/gossip/util" 13 "github.com/stretchr/testify/assert" 14 ) 15 16 func init() { 17 util.SetupTestLogging() 18 } 19 20 func TestNewNodeMetastate(t *testing.T) { 21 metastate := NewNodeMetastate(0) 22 assert.Equal(t, metastate.Height(), uint64(0)) 23 } 24 25 func TestNodeMetastateImpl_Update(t *testing.T) { 26 metastate := NewNodeMetastate(0) 27 assert.Equal(t, metastate.Height(), uint64(0)) 28 metastate.Update(10) 29 assert.Equal(t, metastate.Height(), uint64(10)) 30 } 31 32 // Test node metastate encoding 33 func TestNodeMetastateImpl_Bytes(t *testing.T) { 34 metastate := NewNodeMetastate(0) 35 // Encode state into bytes and check there is no errors 36 _, err := metastate.Bytes() 37 assert.NoError(t, err) 38 } 39 40 // Check the deserialization of the meta stats structure 41 func TestNodeMetastate_FromBytes(t *testing.T) { 42 metastate := NewNodeMetastate(0) 43 // Serialize into bytes array 44 bytes, err := metastate.Bytes() 45 assert.NoError(t, err) 46 if bytes == nil { 47 t.Fatal("Was not able to serialize meta state into byte array.") 48 } 49 50 // Deserialize back and check, that state still have same 51 // height value 52 state, err := FromBytes(bytes) 53 assert.NoError(t, err) 54 55 assert.Equal(t, state.Height(), uint64(0)) 56 57 // Update state to the new height and serialize it again 58 state.Update(17) 59 bytes, err = state.Bytes() 60 assert.NoError(t, err) 61 if bytes == nil { 62 t.Fatal("Was not able to serialize meta state into byte array.") 63 } 64 65 // Restore state from byte array and validate 66 // that stored height is still the same 67 updatedState, err := FromBytes(bytes) 68 assert.NoError(t, err) 69 assert.Equal(t, updatedState.Height(), uint64(17)) 70 }