ipv6_socket_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package upstream
  2. import (
  3. "sync"
  4. "testing"
  5. )
  6. func TestFormatSocketAddress_IPv6(t *testing.T) {
  7. tests := []struct {
  8. name string
  9. host string
  10. port string
  11. expected string
  12. }{
  13. {
  14. name: "IPv6 all addresses",
  15. host: "::",
  16. port: "9001",
  17. expected: "[::]:9001",
  18. },
  19. {
  20. name: "IPv6 localhost",
  21. host: "::1",
  22. port: "8080",
  23. expected: "[::1]:8080",
  24. },
  25. {
  26. name: "IPv6 full address",
  27. host: "2001:db8::1",
  28. port: "9000",
  29. expected: "[2001:db8::1]:9000",
  30. },
  31. {
  32. name: "IPv6 with brackets already",
  33. host: "[::1]",
  34. port: "8080",
  35. expected: "[::1]:8080",
  36. },
  37. {
  38. name: "IPv4 address",
  39. host: "127.0.0.1",
  40. port: "9001",
  41. expected: "127.0.0.1:9001",
  42. },
  43. {
  44. name: "hostname",
  45. host: "example.com",
  46. port: "80",
  47. expected: "example.com:80",
  48. },
  49. }
  50. for _, tt := range tests {
  51. t.Run(tt.name, func(t *testing.T) {
  52. result := formatSocketAddress(tt.host, tt.port)
  53. if result != tt.expected {
  54. t.Errorf("formatSocketAddress(%q, %q) = %q, want %q", tt.host, tt.port, result, tt.expected)
  55. }
  56. })
  57. }
  58. }
  59. func TestAvailabilityTest_IPv6Socket(t *testing.T) {
  60. // Test that IPv6 socket addresses are properly formatted
  61. // This test verifies that the socket string passed to net.DialTimeout is correct
  62. // Test with properly formatted IPv6 addresses
  63. sockets := []string{
  64. "[::1]:8080", // IPv6 localhost with port
  65. "127.0.0.1:8080", // IPv4 for comparison
  66. }
  67. // This should not panic or cause parsing errors
  68. results := AvailabilityTest(sockets)
  69. // Verify we get results for both sockets (even if they're offline)
  70. if len(results) != 2 {
  71. t.Errorf("Expected 2 results, got %d", len(results))
  72. }
  73. // Check that the keys are preserved correctly
  74. for _, socket := range sockets {
  75. if _, exists := results[socket]; !exists {
  76. t.Errorf("Expected result for socket %q", socket)
  77. }
  78. }
  79. }
  80. func TestTCPLatency_IPv6Support(t *testing.T) {
  81. // Test that testTCPLatency can handle IPv6 addresses correctly
  82. // Note: This test verifies the function doesn't panic with IPv6 addresses
  83. // The actual connection will likely fail since we're testing non-existent services
  84. tests := []struct {
  85. name string
  86. socket string
  87. }{
  88. {
  89. name: "IPv6 localhost",
  90. socket: "[::1]:8080",
  91. },
  92. {
  93. name: "IPv6 all addresses",
  94. socket: "[::]:9001",
  95. },
  96. {
  97. name: "IPv4 for comparison",
  98. socket: "127.0.0.1:8080",
  99. },
  100. }
  101. for _, tt := range tests {
  102. t.Run(tt.name, func(t *testing.T) {
  103. var wg sync.WaitGroup
  104. status := &Status{}
  105. wg.Add(1)
  106. // This should not panic even if the connection fails
  107. defer func() {
  108. if r := recover(); r != nil {
  109. t.Errorf("testTCPLatency panicked with socket %q: %v", tt.socket, r)
  110. }
  111. }()
  112. testTCPLatency(&wg, tt.socket, status)
  113. wg.Wait()
  114. // We don't check if it's online since the service likely doesn't exist
  115. // We just verify the function completed without panicking
  116. })
  117. }
  118. }