github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/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 ) 24 25 func TestNewNodeMetastate(t *testing.T) { 26 metastate := NewNodeMetastate(0) 27 assert.Equal(t, metastate.Height(), uint64(0)) 28 } 29 30 func TestNodeMetastateImpl_Update(t *testing.T) { 31 metastate := NewNodeMetastate(0) 32 assert.Equal(t, metastate.Height(), uint64(0)) 33 metastate.Update(10) 34 assert.Equal(t, metastate.Height(), uint64(10)) 35 } 36 37 // Test node metastate encoding 38 func TestNodeMetastateImpl_Bytes(t *testing.T) { 39 metastate := NewNodeMetastate(0) 40 // Encode state into bytes and check there is no errors 41 _, err := metastate.Bytes() 42 assert.NilError(t, err) 43 } 44 45 // Check the deserialization of the meta stats structure 46 func TestNodeMetastate_FromBytes(t *testing.T) { 47 metastate := NewNodeMetastate(0) 48 // Serialize into bytes array 49 bytes, err := metastate.Bytes() 50 assert.NilError(t, err) 51 if bytes == nil { 52 t.Fatal("Was not able to serialize meta state into byte array.") 53 } 54 55 // Deserialize back and check, that state still have same 56 // height value 57 state, err := FromBytes(bytes) 58 assert.NilError(t, err) 59 60 assert.Equal(t, state.Height(), uint64(0)) 61 62 // Update state to the new height and serialize it again 63 state.Update(17) 64 bytes, err = state.Bytes() 65 assert.NilError(t, err) 66 if bytes == nil { 67 t.Fatal("Was not able to serialize meta state into byte array.") 68 } 69 70 // Restore state from byte array and validate 71 // that stored height is still the same 72 updatedState, err := FromBytes(bytes) 73 assert.NilError(t, err) 74 assert.Equal(t, updatedState.Height(), uint64(17)) 75 }