|
| 1 | +package tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "net" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +// TestDriverListenAccept tests the full driver Listen → Accept → Read/Write path |
| 10 | +// and verifies Listener.Addr, Conn.LocalAddr, Conn.RemoteAddr, pilotAddr.Network. |
| 11 | +func TestDriverListenAccept(t *testing.T) { |
| 12 | + t.Parallel() |
| 13 | + env := NewTestEnv(t) |
| 14 | + a := env.AddDaemon() |
| 15 | + b := env.AddDaemon() |
| 16 | + |
| 17 | + // A listens on a custom port |
| 18 | + const testPort uint16 = 2000 |
| 19 | + ln, err := a.Driver.Listen(testPort) |
| 20 | + if err != nil { |
| 21 | + t.Fatalf("listen: %v", err) |
| 22 | + } |
| 23 | + |
| 24 | + // Verify Listener.Addr() |
| 25 | + addr := ln.Addr() |
| 26 | + if addr == nil { |
| 27 | + t.Fatal("Addr() returned nil") |
| 28 | + } |
| 29 | + if addr.Network() != "pilot" { |
| 30 | + t.Errorf("expected network 'pilot', got %q", addr.Network()) |
| 31 | + } |
| 32 | + addrStr := addr.String() |
| 33 | + if addrStr == "" { |
| 34 | + t.Error("Addr().String() returned empty") |
| 35 | + } |
| 36 | + t.Logf("Listener addr: %s (network=%s)", addrStr, addr.Network()) |
| 37 | + |
| 38 | + // B dials A on the test port |
| 39 | + connDone := make(chan net.Conn, 1) |
| 40 | + errDone := make(chan error, 1) |
| 41 | + go func() { |
| 42 | + conn, err := b.Driver.DialAddr(a.Daemon.Addr(), testPort) |
| 43 | + if err != nil { |
| 44 | + errDone <- err |
| 45 | + return |
| 46 | + } |
| 47 | + connDone <- conn |
| 48 | + }() |
| 49 | + |
| 50 | + // A accepts the connection |
| 51 | + acceptDone := make(chan net.Conn, 1) |
| 52 | + acceptErr := make(chan error, 1) |
| 53 | + go func() { |
| 54 | + accepted, err := ln.Accept() |
| 55 | + if err != nil { |
| 56 | + acceptErr <- err |
| 57 | + return |
| 58 | + } |
| 59 | + acceptDone <- accepted |
| 60 | + }() |
| 61 | + |
| 62 | + // Wait for both dial and accept to complete |
| 63 | + var dialConn, acceptConn net.Conn |
| 64 | + select { |
| 65 | + case dialConn = <-connDone: |
| 66 | + case err := <-errDone: |
| 67 | + t.Fatalf("dial: %v", err) |
| 68 | + case <-time.After(5 * time.Second): |
| 69 | + t.Fatal("dial timed out") |
| 70 | + } |
| 71 | + |
| 72 | + select { |
| 73 | + case acceptConn = <-acceptDone: |
| 74 | + case err := <-acceptErr: |
| 75 | + t.Fatalf("accept: %v", err) |
| 76 | + case <-time.After(5 * time.Second): |
| 77 | + t.Fatal("accept timed out") |
| 78 | + } |
| 79 | + |
| 80 | + // Verify conn addresses |
| 81 | + localAddr := acceptConn.LocalAddr() |
| 82 | + if localAddr == nil { |
| 83 | + t.Fatal("LocalAddr() returned nil") |
| 84 | + } |
| 85 | + if localAddr.Network() != "pilot" { |
| 86 | + t.Errorf("expected network 'pilot', got %q", localAddr.Network()) |
| 87 | + } |
| 88 | + t.Logf("Accepted conn: local=%s remote=%s", localAddr, acceptConn.RemoteAddr()) |
| 89 | + |
| 90 | + remoteAddr := acceptConn.RemoteAddr() |
| 91 | + if remoteAddr == nil { |
| 92 | + t.Fatal("RemoteAddr() returned nil") |
| 93 | + } |
| 94 | + if remoteAddr.Network() != "pilot" { |
| 95 | + t.Errorf("expected remote network 'pilot', got %q", remoteAddr.Network()) |
| 96 | + } |
| 97 | + |
| 98 | + // Test write/read through accepted connection |
| 99 | + testData := []byte("hello from B") |
| 100 | + if _, err := dialConn.Write(testData); err != nil { |
| 101 | + t.Fatalf("write: %v", err) |
| 102 | + } |
| 103 | + |
| 104 | + buf := make([]byte, 256) |
| 105 | + acceptConn.SetReadDeadline(time.Now().Add(2 * time.Second)) |
| 106 | + n, err := acceptConn.Read(buf) |
| 107 | + if err != nil { |
| 108 | + t.Fatalf("read: %v", err) |
| 109 | + } |
| 110 | + if string(buf[:n]) != "hello from B" { |
| 111 | + t.Errorf("expected 'hello from B', got %q", string(buf[:n])) |
| 112 | + } |
| 113 | + |
| 114 | + // Clean up |
| 115 | + dialConn.Close() |
| 116 | + acceptConn.Close() |
| 117 | + ln.Close() |
| 118 | +} |
| 119 | + |
| 120 | +// TestDriverListenerCloseUnblocksAccept verifies that closing a listener |
| 121 | +// unblocks a pending Accept call. |
| 122 | +func TestDriverListenerCloseUnblocksAccept(t *testing.T) { |
| 123 | + t.Parallel() |
| 124 | + env := NewTestEnv(t) |
| 125 | + a := env.AddDaemon() |
| 126 | + |
| 127 | + const testPort uint16 = 2001 |
| 128 | + ln, err := a.Driver.Listen(testPort) |
| 129 | + if err != nil { |
| 130 | + t.Fatalf("listen: %v", err) |
| 131 | + } |
| 132 | + |
| 133 | + errCh := make(chan error, 1) |
| 134 | + go func() { |
| 135 | + _, err := ln.Accept() |
| 136 | + errCh <- err |
| 137 | + }() |
| 138 | + |
| 139 | + // Brief pause then close listener |
| 140 | + time.Sleep(50 * time.Millisecond) |
| 141 | + ln.Close() |
| 142 | + |
| 143 | + select { |
| 144 | + case err := <-errCh: |
| 145 | + if err == nil { |
| 146 | + t.Error("expected error from Accept after close, got nil") |
| 147 | + } |
| 148 | + t.Logf("Accept after close: %v", err) |
| 149 | + case <-time.After(3 * time.Second): |
| 150 | + t.Fatal("Accept did not unblock after Close") |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +// TestDriverConnSetDeadline tests SetDeadline and SetWriteDeadline. |
| 155 | +func TestDriverConnSetDeadline(t *testing.T) { |
| 156 | + t.Parallel() |
| 157 | + env := NewTestEnv(t) |
| 158 | + a := env.AddDaemon() |
| 159 | + b := env.AddDaemon() |
| 160 | + |
| 161 | + // Connect A → B on echo port |
| 162 | + conn, err := a.Driver.DialAddr(b.Daemon.Addr(), 7) |
| 163 | + if err != nil { |
| 164 | + t.Fatalf("dial: %v", err) |
| 165 | + } |
| 166 | + defer conn.Close() |
| 167 | + |
| 168 | + // SetDeadline should not error |
| 169 | + if err := conn.SetDeadline(time.Now().Add(time.Second)); err != nil { |
| 170 | + t.Errorf("SetDeadline: %v", err) |
| 171 | + } |
| 172 | + |
| 173 | + // SetWriteDeadline should not error (it's a no-op) |
| 174 | + if err := conn.SetWriteDeadline(time.Now().Add(time.Second)); err != nil { |
| 175 | + t.Errorf("SetWriteDeadline: %v", err) |
| 176 | + } |
| 177 | + |
| 178 | + // SetReadDeadline with expired time should cause Read to timeout |
| 179 | + conn.SetReadDeadline(time.Now().Add(-time.Second)) |
| 180 | + buf := make([]byte, 16) |
| 181 | + _, err = conn.Read(buf) |
| 182 | + if err == nil { |
| 183 | + t.Error("expected deadline exceeded error, got nil") |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +// TestTestEnvSocketPath tests the SocketPath utility method. |
| 188 | +func TestTestEnvSocketPath(t *testing.T) { |
| 189 | + t.Parallel() |
| 190 | + env := NewTestEnv(t) |
| 191 | + path := env.SocketPath("test-service") |
| 192 | + if path == "" { |
| 193 | + t.Fatal("SocketPath returned empty") |
| 194 | + } |
| 195 | + t.Logf("SocketPath: %s", path) |
| 196 | +} |
0 commit comments