node_extern_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package store
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. import "github.com/coreos/etcd/Godeps/_workspace/src/github.com/stretchr/testify/assert"
  7. func TestNodeExternClone(t *testing.T) {
  8. var eNode *NodeExtern
  9. if g := eNode.Clone(); g != nil {
  10. t.Fatalf("nil.Clone=%v, want nil", g)
  11. }
  12. const (
  13. key string = "/foo/bar"
  14. ttl int64 = 123456789
  15. ci uint64 = 123
  16. mi uint64 = 321
  17. )
  18. var (
  19. val = "some_data"
  20. valp = &val
  21. exp = time.Unix(12345, 67890)
  22. expp = &exp
  23. )
  24. eNode = &NodeExtern{
  25. Key: key,
  26. TTL: ttl,
  27. CreatedIndex: ci,
  28. ModifiedIndex: mi,
  29. Value: valp,
  30. Expiration: expp,
  31. }
  32. gNode := eNode.Clone()
  33. // Check the clone is as expected
  34. assert.Equal(t, gNode.Key, key)
  35. assert.Equal(t, gNode.TTL, ttl)
  36. assert.Equal(t, gNode.CreatedIndex, ci)
  37. assert.Equal(t, gNode.ModifiedIndex, mi)
  38. // values should be the same
  39. assert.Equal(t, *gNode.Value, val)
  40. assert.Equal(t, *gNode.Expiration, exp)
  41. // but pointers should differ
  42. if gNode.Value == eNode.Value {
  43. t.Fatalf("expected value pointers to differ, but got same!")
  44. }
  45. if gNode.Expiration == eNode.Expiration {
  46. t.Fatalf("expected expiration pointers to differ, but got same!")
  47. }
  48. // Original should be the same
  49. assert.Equal(t, eNode.Key, key)
  50. assert.Equal(t, eNode.TTL, ttl)
  51. assert.Equal(t, eNode.CreatedIndex, ci)
  52. assert.Equal(t, eNode.ModifiedIndex, mi)
  53. assert.Equal(t, eNode.Value, valp)
  54. assert.Equal(t, eNode.Expiration, expp)
  55. // Change the clone and ensure the original is not affected
  56. gNode.Key = "/baz"
  57. gNode.TTL = 0
  58. assert.Equal(t, eNode.Key, key)
  59. assert.Equal(t, eNode.TTL, ttl)
  60. assert.Equal(t, eNode.CreatedIndex, ci)
  61. assert.Equal(t, eNode.ModifiedIndex, mi)
  62. // Change the original and ensure the clone is not affected
  63. eNode.Key = "/wuf"
  64. assert.Equal(t, eNode.Key, "/wuf")
  65. assert.Equal(t, gNode.Key, "/baz")
  66. }