1
2
3
4
5
6
7
8
9
10
11
12
13
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
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
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
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
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
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
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