...

Source file src/go.etcd.io/etcd/server/v3/etcdserver/api/v2store/node_extern_test.go

Documentation: go.etcd.io/etcd/server/v3/etcdserver/api/v2store

     1  // Copyright 2015 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package v2store
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  	"time"
    21  
    22  	"go.etcd.io/etcd/client/pkg/v3/testutil"
    23  )
    24  
    25  func TestNodeExternClone(t *testing.T) {
    26  	var eNode *NodeExtern
    27  	if g := eNode.Clone(); g != nil {
    28  		t.Fatalf("nil.Clone=%v, want nil", g)
    29  	}
    30  
    31  	const (
    32  		key string = "/foo/bar"
    33  		ttl int64  = 123456789
    34  		ci  uint64 = 123
    35  		mi  uint64 = 321
    36  	)
    37  	var (
    38  		val    = "some_data"
    39  		valp   = &val
    40  		exp    = time.Unix(12345, 67890)
    41  		expp   = &exp
    42  		child  = NodeExtern{}
    43  		childp = &child
    44  		childs = []*NodeExtern{childp}
    45  	)
    46  
    47  	eNode = &NodeExtern{
    48  		Key:           key,
    49  		TTL:           ttl,
    50  		CreatedIndex:  ci,
    51  		ModifiedIndex: mi,
    52  		Value:         valp,
    53  		Expiration:    expp,
    54  		Nodes:         childs,
    55  	}
    56  
    57  	gNode := eNode.Clone()
    58  	// Check the clone is as expected
    59  	testutil.AssertEqual(t, gNode.Key, key)
    60  	testutil.AssertEqual(t, gNode.TTL, ttl)
    61  	testutil.AssertEqual(t, gNode.CreatedIndex, ci)
    62  	testutil.AssertEqual(t, gNode.ModifiedIndex, mi)
    63  	// values should be the same
    64  	testutil.AssertEqual(t, *gNode.Value, val)
    65  	testutil.AssertEqual(t, *gNode.Expiration, exp)
    66  	testutil.AssertEqual(t, len(gNode.Nodes), len(childs))
    67  	testutil.AssertEqual(t, *gNode.Nodes[0], child)
    68  	// but pointers should differ
    69  	if gNode.Value == eNode.Value {
    70  		t.Fatalf("expected value pointers to differ, but got same!")
    71  	}
    72  	if gNode.Expiration == eNode.Expiration {
    73  		t.Fatalf("expected expiration pointers to differ, but got same!")
    74  	}
    75  	if sameSlice(gNode.Nodes, eNode.Nodes) {
    76  		t.Fatalf("expected nodes pointers to differ, but got same!")
    77  	}
    78  	// Original should be the same
    79  	testutil.AssertEqual(t, eNode.Key, key)
    80  	testutil.AssertEqual(t, eNode.TTL, ttl)
    81  	testutil.AssertEqual(t, eNode.CreatedIndex, ci)
    82  	testutil.AssertEqual(t, eNode.ModifiedIndex, mi)
    83  	testutil.AssertEqual(t, eNode.Value, valp)
    84  	testutil.AssertEqual(t, eNode.Expiration, expp)
    85  	if !sameSlice(eNode.Nodes, childs) {
    86  		t.Fatalf("expected nodes pointer to same, but got different!")
    87  	}
    88  	// Change the clone and ensure the original is not affected
    89  	gNode.Key = "/baz"
    90  	gNode.TTL = 0
    91  	gNode.Nodes[0].Key = "uno"
    92  	testutil.AssertEqual(t, eNode.Key, key)
    93  	testutil.AssertEqual(t, eNode.TTL, ttl)
    94  	testutil.AssertEqual(t, eNode.CreatedIndex, ci)
    95  	testutil.AssertEqual(t, eNode.ModifiedIndex, mi)
    96  	testutil.AssertEqual(t, *eNode.Nodes[0], child)
    97  	// Change the original and ensure the clone is not affected
    98  	eNode.Key = "/wuf"
    99  	testutil.AssertEqual(t, eNode.Key, "/wuf")
   100  	testutil.AssertEqual(t, gNode.Key, "/baz")
   101  }
   102  
   103  func sameSlice(a, b []*NodeExtern) bool {
   104  	va := reflect.ValueOf(a)
   105  	vb := reflect.ValueOf(b)
   106  	return va.Len() == vb.Len() && va.Pointer() == vb.Pointer()
   107  }
   108  

View as plain text