host_source_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // +build all cassandra
  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. {cassVersion{4, 1, 0}, 3, 1, 2},
  36. }
  37. for i, test := range tests {
  38. if test.version.Before(test.major, test.minor, test.patch) {
  39. t.Errorf("%d: expected v%d.%d.%d to be before %v", i, test.major, test.minor, test.patch, test.version)
  40. }
  41. }
  42. }
  43. func TestIsValidPeer(t *testing.T) {
  44. host := &HostInfo{
  45. rpcAddress: net.ParseIP("0.0.0.0"),
  46. rack: "myRack",
  47. hostId: "0",
  48. dataCenter: "datacenter",
  49. tokens: []string{"0", "1"},
  50. }
  51. if !isValidPeer(host) {
  52. t.Errorf("expected %+v to be a valid peer", host)
  53. }
  54. host.rack = ""
  55. if isValidPeer(host) {
  56. t.Errorf("expected %+v to NOT be a valid peer", host)
  57. }
  58. }
  59. func TestHostInfo_ConnectAddress(t *testing.T) {
  60. var localhost = net.IPv4(127, 0, 0, 1)
  61. tests := []struct {
  62. name string
  63. connectAddr net.IP
  64. rpcAddr net.IP
  65. broadcastAddr net.IP
  66. peer net.IP
  67. }{
  68. {name: "rpc_address", rpcAddr: localhost},
  69. {name: "connect_address", connectAddr: localhost},
  70. {name: "broadcast_address", broadcastAddr: localhost},
  71. {name: "peer", peer: localhost},
  72. }
  73. for _, test := range tests {
  74. t.Run(test.name, func(t *testing.T) {
  75. host := &HostInfo{
  76. connectAddress: test.connectAddr,
  77. rpcAddress: test.rpcAddr,
  78. broadcastAddress: test.broadcastAddr,
  79. peer: test.peer,
  80. }
  81. if addr := host.ConnectAddress(); !addr.Equal(localhost) {
  82. t.Fatalf("expected ConnectAddress to be %s got %s", localhost, addr)
  83. }
  84. })
  85. }
  86. }