node_extern_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package store
  2. import (
  3. "reflect"
  4. "testing"
  5. "time"
  6. "unsafe"
  7. )
  8. import "github.com/coreos/etcd/Godeps/_workspace/src/github.com/stretchr/testify/assert"
  9. func TestNodeExternClone(t *testing.T) {
  10. var eNode *NodeExtern
  11. if g := eNode.Clone(); g != nil {
  12. t.Fatalf("nil.Clone=%v, want nil", g)
  13. }
  14. const (
  15. key string = "/foo/bar"
  16. ttl int64 = 123456789
  17. ci uint64 = 123
  18. mi uint64 = 321
  19. )
  20. var (
  21. val = "some_data"
  22. valp = &val
  23. exp = time.Unix(12345, 67890)
  24. expp = &exp
  25. child = NodeExtern{}
  26. childp = &child
  27. childs = []*NodeExtern{childp}
  28. )
  29. eNode = &NodeExtern{
  30. Key: key,
  31. TTL: ttl,
  32. CreatedIndex: ci,
  33. ModifiedIndex: mi,
  34. Value: valp,
  35. Expiration: expp,
  36. Nodes: childs,
  37. }
  38. gNode := eNode.Clone()
  39. // Check the clone is as expected
  40. assert.Equal(t, gNode.Key, key)
  41. assert.Equal(t, gNode.TTL, ttl)
  42. assert.Equal(t, gNode.CreatedIndex, ci)
  43. assert.Equal(t, gNode.ModifiedIndex, mi)
  44. // values should be the same
  45. assert.Equal(t, *gNode.Value, val)
  46. assert.Equal(t, *gNode.Expiration, exp)
  47. assert.Equal(t, len(gNode.Nodes), len(childs))
  48. assert.Equal(t, *gNode.Nodes[0], child)
  49. // but pointers should differ
  50. if gNode.Value == eNode.Value {
  51. t.Fatalf("expected value pointers to differ, but got same!")
  52. }
  53. if gNode.Expiration == eNode.Expiration {
  54. t.Fatalf("expected expiration pointers to differ, but got same!")
  55. }
  56. if sameSlice(gNode.Nodes, eNode.Nodes) {
  57. t.Fatalf("expected nodes pointers to differ, but got same!")
  58. }
  59. // Original should be the same
  60. assert.Equal(t, eNode.Key, key)
  61. assert.Equal(t, eNode.TTL, ttl)
  62. assert.Equal(t, eNode.CreatedIndex, ci)
  63. assert.Equal(t, eNode.ModifiedIndex, mi)
  64. assert.Equal(t, eNode.Value, valp)
  65. assert.Equal(t, eNode.Expiration, expp)
  66. if !sameSlice(eNode.Nodes, childs) {
  67. t.Fatalf("expected nodes pointer to same, but got different!")
  68. }
  69. // Change the clone and ensure the original is not affected
  70. gNode.Key = "/baz"
  71. gNode.TTL = 0
  72. gNode.Nodes[0].Key = "uno"
  73. assert.Equal(t, eNode.Key, key)
  74. assert.Equal(t, eNode.TTL, ttl)
  75. assert.Equal(t, eNode.CreatedIndex, ci)
  76. assert.Equal(t, eNode.ModifiedIndex, mi)
  77. assert.Equal(t, *eNode.Nodes[0], child)
  78. // Change the original and ensure the clone is not affected
  79. eNode.Key = "/wuf"
  80. assert.Equal(t, eNode.Key, "/wuf")
  81. assert.Equal(t, gNode.Key, "/baz")
  82. }
  83. func sameSlice(a, b []*NodeExtern) bool {
  84. ah := (*reflect.SliceHeader)(unsafe.Pointer(&a))
  85. bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
  86. return *ah == *bh
  87. }