filters_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package gocql
  2. import "testing"
  3. func TestFilter_WhiteList(t *testing.T) {
  4. f := WhiteListHostFilter("addr1", "addr2")
  5. tests := [...]struct {
  6. addr string
  7. accept bool
  8. }{
  9. {"addr1", true},
  10. {"addr2", true},
  11. {"addr3", false},
  12. }
  13. for i, test := range tests {
  14. if f.Accept(&HostInfo{peer: test.addr}) {
  15. if !test.accept {
  16. t.Errorf("%d: should not have been accepted but was", i)
  17. }
  18. } else if test.accept {
  19. t.Errorf("%d: should have been accepted but wasn't", i)
  20. }
  21. }
  22. }
  23. func TestFilter_AllowAll(t *testing.T) {
  24. f := AcceptAllFilter()
  25. tests := [...]struct {
  26. addr string
  27. accept bool
  28. }{
  29. {"addr1", true},
  30. {"addr2", true},
  31. {"addr3", true},
  32. }
  33. for i, test := range tests {
  34. if f.Accept(&HostInfo{peer: test.addr}) {
  35. if !test.accept {
  36. t.Errorf("%d: should not have been accepted but was", i)
  37. }
  38. } else if test.accept {
  39. t.Errorf("%d: should have been accepted but wasn't", i)
  40. }
  41. }
  42. }
  43. func TestFilter_DenyAll(t *testing.T) {
  44. f := DenyAllFilter()
  45. tests := [...]struct {
  46. addr string
  47. accept bool
  48. }{
  49. {"addr1", false},
  50. {"addr2", false},
  51. {"addr3", false},
  52. }
  53. for i, test := range tests {
  54. if f.Accept(&HostInfo{peer: test.addr}) {
  55. if !test.accept {
  56. t.Errorf("%d: should not have been accepted but was", i)
  57. }
  58. } else if test.accept {
  59. t.Errorf("%d: should have been accepted but wasn't", i)
  60. }
  61. }
  62. }
  63. func TestFilter_DataCentre(t *testing.T) {
  64. f := DataCentreHostFilter("dc1")
  65. tests := [...]struct {
  66. dc string
  67. accept bool
  68. }{
  69. {"dc1", true},
  70. {"dc2", false},
  71. }
  72. for i, test := range tests {
  73. if f.Accept(&HostInfo{dataCenter: test.dc}) {
  74. if !test.accept {
  75. t.Errorf("%d: should not have been accepted but was", i)
  76. }
  77. } else if test.accept {
  78. t.Errorf("%d: should have been accepted but wasn't", i)
  79. }
  80. }
  81. }