|
| 1 | +package tcp |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +// setupTestChecker creates a checker with CheckingLoop running and waits until ready |
| 10 | +func setupTestChecker(t *testing.T) (*Checker, context.CancelFunc) { |
| 11 | + checker := NewChecker() |
| 12 | + ctx, cancel := context.WithCancel(context.Background()) |
| 13 | + go func() { |
| 14 | + _ = checker.CheckingLoop(ctx) |
| 15 | + }() |
| 16 | + <-checker.WaitReady() |
| 17 | + return checker, cancel |
| 18 | +} |
| 19 | + |
| 20 | +// testWithChecker runs a test function with a prepared checker and test server |
| 21 | +func testWithChecker(t *testing.T, testFunc func(*testing.T, *Checker, string)) { |
| 22 | + checker, cancel := setupTestChecker(t) |
| 23 | + defer cancel() |
| 24 | + |
| 25 | + testAddr, stopServer := StartTestServer() |
| 26 | + defer stopServer() |
| 27 | + |
| 28 | + testFunc(t, checker, testAddr) |
| 29 | +} |
| 30 | + |
| 31 | +func TestOptions(t *testing.T) { |
| 32 | + // Test default options |
| 33 | + opts := DefaultOptions() |
| 34 | + if opts.Network != "tcp" { |
| 35 | + t.Errorf("expected default network to be 'tcp', got %s", opts.Network) |
| 36 | + } |
| 37 | + if opts.Timeout != 3*time.Second { |
| 38 | + t.Errorf("expected default timeout to be 3s, got %v", opts.Timeout) |
| 39 | + } |
| 40 | + if !opts.ZeroLinger { |
| 41 | + t.Error("expected default ZeroLinger to be true") |
| 42 | + } |
| 43 | + if opts.Mark != 0 { |
| 44 | + t.Errorf("expected default Mark to be 0, got %d", opts.Mark) |
| 45 | + } |
| 46 | + |
| 47 | + // Test fluent interface |
| 48 | + customOpts := DefaultOptions(). |
| 49 | + WithTimeout(5 * time.Second). |
| 50 | + WithNetwork("tcp6"). |
| 51 | + WithZeroLinger(false). |
| 52 | + WithMark(100) |
| 53 | + |
| 54 | + if customOpts.Network != "tcp6" { |
| 55 | + t.Errorf("expected network to be 'tcp6', got %s", customOpts.Network) |
| 56 | + } |
| 57 | + if customOpts.Timeout != 5*time.Second { |
| 58 | + t.Errorf("expected timeout to be 5s, got %v", customOpts.Timeout) |
| 59 | + } |
| 60 | + if customOpts.ZeroLinger { |
| 61 | + t.Error("expected ZeroLinger to be false") |
| 62 | + } |
| 63 | + if customOpts.Mark != 100 { |
| 64 | + t.Errorf("expected Mark to be 100, got %d", customOpts.Mark) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestCheckerWithOptions(t *testing.T) { |
| 69 | + testWithChecker(t, func(t *testing.T, checker *Checker, testAddr string) { |
| 70 | + // Test basic functionality with default options |
| 71 | + opts := DefaultOptions().WithTimeout(2 * time.Second) |
| 72 | + err := checker.CheckAddrWithOptions(testAddr, opts) |
| 73 | + if err != nil { |
| 74 | + t.Errorf("Connection to test server failed: %v", err) |
| 75 | + } |
| 76 | + |
| 77 | + // Test IPv4 specific |
| 78 | + opts4 := DefaultOptions().WithTimeout(2 * time.Second).WithNetwork("tcp4") |
| 79 | + err = checker.CheckAddrWithOptions(testAddr, opts4) |
| 80 | + if err != nil { |
| 81 | + t.Errorf("IPv4 connection to test server failed: %v", err) |
| 82 | + } |
| 83 | + }) |
| 84 | +} |
| 85 | + |
| 86 | +func TestIPv6Support(t *testing.T) { |
| 87 | + checker, cancel := setupTestChecker(t) |
| 88 | + defer cancel() |
| 89 | + |
| 90 | + // Try to start IPv6 server |
| 91 | + testAddr6, stopServer6, err := StartTestServerIPv6() |
| 92 | + if err != nil { |
| 93 | + t.Skipf("Skipping IPv6 test: %v", err) |
| 94 | + return |
| 95 | + } |
| 96 | + defer stopServer6() |
| 97 | + |
| 98 | + // Test IPv6 connection with tcp6 network |
| 99 | + opts6 := DefaultOptions().WithTimeout(2 * time.Second).WithNetwork("tcp6") |
| 100 | + err = checker.CheckAddrWithOptions(testAddr6, opts6) |
| 101 | + if err != nil { |
| 102 | + t.Errorf("IPv6 connection to test server failed: %v", err) |
| 103 | + } |
| 104 | + |
| 105 | + // Test IPv6 connection with tcp network (should also work) |
| 106 | + optsGeneral := DefaultOptions().WithTimeout(2 * time.Second).WithNetwork("tcp") |
| 107 | + err = checker.CheckAddrWithOptions(testAddr6, optsGeneral) |
| 108 | + if err != nil { |
| 109 | + t.Errorf("General TCP connection to IPv6 test server failed: %v", err) |
| 110 | + } |
| 111 | + |
| 112 | + // Test that tcp4 fails on IPv6 address (should fail) |
| 113 | + opts4 := DefaultOptions().WithTimeout(1 * time.Second).WithNetwork("tcp4") |
| 114 | + err = checker.CheckAddrWithOptions(testAddr6, opts4) |
| 115 | + if err == nil { |
| 116 | + t.Error("Expected tcp4 connection to IPv6 address to fail, but it succeeded") |
| 117 | + } else { |
| 118 | + t.Logf("tcp4 connection to IPv6 address failed as expected: %v", err) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +func TestMarkOption(t *testing.T) { |
| 123 | + testWithChecker(t, func(t *testing.T, checker *Checker, testAddr string) { |
| 124 | + // Test with mark set (only meaningful on Linux) |
| 125 | + opts := DefaultOptions().WithTimeout(2 * time.Second).WithMark(42) |
| 126 | + err := checker.CheckAddrWithOptions(testAddr, opts) |
| 127 | + if err != nil { |
| 128 | + t.Errorf("Connection with mark to test server failed: %v", err) |
| 129 | + } |
| 130 | + }) |
| 131 | +} |
| 132 | + |
| 133 | +func TestBackwardCompatibility(t *testing.T) { |
| 134 | + testWithChecker(t, func(t *testing.T, checker *Checker, testAddr string) { |
| 135 | + // Old API should still work |
| 136 | + err := checker.CheckAddr(testAddr, 2*time.Second) |
| 137 | + if err != nil { |
| 138 | + t.Errorf("Old API connection to test server failed: %v", err) |
| 139 | + } |
| 140 | + |
| 141 | + // CheckAddrZeroLinger should use new implementation internally |
| 142 | + err = checker.CheckAddrZeroLinger(testAddr, 2*time.Second, true) |
| 143 | + if err != nil { |
| 144 | + t.Errorf("CheckAddrZeroLinger connection to test server failed: %v", err) |
| 145 | + } |
| 146 | + }) |
| 147 | +} |
0 commit comments