host_source_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // +build all integration
  2. package gocql
  3. import (
  4. "net"
  5. "testing"
  6. )
  7. func TestUnmarshalCassVersion(t *testing.T) {
  8. tests := [...]struct {
  9. data string
  10. version cassVersion
  11. }{
  12. {"3.2", cassVersion{3, 2, 0}},
  13. {"2.10.1-SNAPSHOT", cassVersion{2, 10, 1}},
  14. {"1.2.3", cassVersion{1, 2, 3}},
  15. }
  16. for i, test := range tests {
  17. v := &cassVersion{}
  18. if err := v.UnmarshalCQL(nil, []byte(test.data)); err != nil {
  19. t.Errorf("%d: %v", i, err)
  20. } else if *v != test.version {
  21. t.Errorf("%d: expected %#+v got %#+v", i, test.version, *v)
  22. }
  23. }
  24. }
  25. func TestCassVersionBefore(t *testing.T) {
  26. tests := [...]struct {
  27. version cassVersion
  28. major, minor, patch int
  29. }{
  30. {cassVersion{1, 0, 0}, 0, 0, 0},
  31. {cassVersion{0, 1, 0}, 0, 0, 0},
  32. {cassVersion{0, 0, 1}, 0, 0, 0},
  33. {cassVersion{1, 0, 0}, 0, 1, 0},
  34. {cassVersion{0, 1, 0}, 0, 0, 1},
  35. }
  36. for i, test := range tests {
  37. if !test.version.Before(test.major, test.minor, test.patch) {
  38. t.Errorf("%d: expected v%d.%d.%d to be before %v", i, test.major, test.minor, test.patch, test.version)
  39. }
  40. }
  41. }
  42. func TestIsValidPeer(t *testing.T) {
  43. host := &HostInfo{
  44. rpcAddress: net.ParseIP("0.0.0.0"),
  45. rack: "myRack",
  46. hostId: "0",
  47. dataCenter: "datacenter",
  48. tokens: []string{"0", "1"},
  49. }
  50. if !isValidPeer(host) {
  51. t.Errorf("expected %+v to be a valid peer", host)
  52. }
  53. host.rack = ""
  54. if isValidPeer(host) {
  55. t.Errorf("expected %+v to NOT be a valid peer", host)
  56. }
  57. }
  58. func TestGetHosts(t *testing.T) {
  59. cluster := createCluster()
  60. session := createSessionFromCluster(cluster, t)
  61. hosts, partitioner, err := session.hostSource.GetHosts()
  62. assertTrue(t, "err == nil", err == nil)
  63. assertEqual(t, "len(hosts)", len(clusterHosts), len(hosts))
  64. assertTrue(t, "len(partitioner) != 0", len(partitioner) != 0)
  65. }
  66. func TestHostInfo_ConnectAddress(t *testing.T) {
  67. var localhost = net.IPv4(127, 0, 0, 1)
  68. tests := []struct {
  69. name string
  70. connectAddr net.IP
  71. rpcAddr net.IP
  72. broadcastAddr net.IP
  73. peer net.IP
  74. }{
  75. {name: "rpc_address", rpcAddr: localhost},
  76. {name: "connect_address", connectAddr: localhost},
  77. {name: "broadcast_address", broadcastAddr: localhost},
  78. {name: "peer", peer: localhost},
  79. }
  80. for _, test := range tests {
  81. t.Run(test.name, func(t *testing.T) {
  82. host := &HostInfo{
  83. connectAddress: test.connectAddr,
  84. rpcAddress: test.rpcAddr,
  85. broadcastAddress: test.broadcastAddr,
  86. peer: test.peer,
  87. }
  88. if addr := host.ConnectAddress(); !addr.Equal(localhost) {
  89. t.Fatalf("expected ConnectAddress to be %s got %s", localhost, addr)
  90. }
  91. })
  92. }
  93. }