github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/gossip/state/metastate_test.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package state 18 19 import ( 20 "testing" 21 22 "github.com/docker/docker/pkg/testutil/assert" 23 "github.com/hyperledger/fabric/gossip/util" 24 ) 25 26 func init() { 27 util.SetupTestLogging() 28 } 29 30 func TestNewNodeMetastate(t *testing.T) { 31 metastate := NewNodeMetastate(0) 32 assert.Equal(t, metastate.Height(), uint64(0)) 33 } 34 35 func TestNodeMetastateImpl_Update(t *testing.T) { 36 metastate := NewNodeMetastate(0) 37 assert.Equal(t, metastate.Height(), uint64(0)) 38 metastate.Update(10) 39 assert.Equal(t, metastate.Height(), uint64(10)) 40 } 41 42 // Test node metastate encoding 43 func TestNodeMetastateImpl_Bytes(t *testing.T) { 44 metastate := NewNodeMetastate(0) 45 // Encode state into bytes and check there is no errors 46 _, err := metastate.Bytes() 47 assert.NilError(t, err) 48 } 49 50 // Check the deserialization of the meta stats structure 51 func TestNodeMetastate_FromBytes(t *testing.T) { 52 metastate := NewNodeMetastate(0) 53 // Serialize into bytes array 54 bytes, err := metastate.Bytes() 55 assert.NilError(t, err) 56 if bytes == nil { 57 t.Fatal("Was not able to serialize meta state into byte array.") 58 } 59 60 // Deserialize back and check, that state still have same 61 // height value 62 state, err := FromBytes(bytes) 63 assert.NilError(t, err) 64 65 assert.Equal(t, state.Height(), uint64(0)) 66 67 // Update state to the new height and serialize it again 68 state.Update(17) 69 bytes, err = state.Bytes() 70 assert.NilError(t, err) 71 if bytes == nil { 72 t.Fatal("Was not able to serialize meta state into byte array.") 73 } 74 75 // Restore state from byte array and validate 76 // that stored height is still the same 77 updatedState, err := FromBytes(bytes) 78 assert.NilError(t, err) 79 assert.Equal(t, updatedState.Height(), uint64(17)) 80 }