@@ -299,6 +299,58 @@ func TestCompatibility(t *testing.T) {
299
299
})
300
300
}
301
301
302
+ func TestTimeout (t * testing.T ) {
303
+ t .Parallel ()
304
+
305
+ td , client := createTestTunneld (t , & tunneld.Options {
306
+ BaseURL : & url.URL {
307
+ Scheme : "http" ,
308
+ Host : "localhost.com" ,
309
+ },
310
+ WireguardEndpoint : "" , // generated automatically
311
+ WireguardPort : 0 , // generated automatically
312
+ WireguardKey : tunnelsdk.Key {}, // generated automatically
313
+ WireguardServerIP : tunneld .DefaultWireguardServerIP ,
314
+ WireguardNetworkPrefix : tunneld .DefaultWireguardNetworkPrefix ,
315
+ PeerDialTimeout : time .Second ,
316
+ })
317
+ require .NotNil (t , td )
318
+
319
+ // Start a tunnel.
320
+ key , err := tunnelsdk .GeneratePrivateKey ()
321
+ require .NoError (t , err , "generate private key" )
322
+ tunnel , err := client .LaunchTunnel (context .Background (), tunnelsdk.TunnelConfig {
323
+ Log : slogtest .
324
+ Make (t , & slogtest.Options {IgnoreErrors : true }).
325
+ Named ("tunnel_client" ),
326
+ PrivateKey : key ,
327
+ })
328
+ require .NoError (t , err , "launch tunnel" )
329
+
330
+ // Close the tunnel.
331
+ err = tunnel .Close ()
332
+ require .NoError (t , err , "close tunnel" )
333
+ <- tunnel .Wait ()
334
+
335
+ // Requests should fail in roughly 1 second.
336
+ c := tunnelHTTPClient (client )
337
+ ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
338
+ defer cancel ()
339
+
340
+ u := * tunnel .URL
341
+ u .Path = "/test/1"
342
+
343
+ req , err := http .NewRequestWithContext (ctx , http .MethodGet , u .String (), nil )
344
+ require .NoError (t , err )
345
+
346
+ now := time .Now ()
347
+ res , err := c .Do (req )
348
+ require .NoError (t , err )
349
+ require .WithinDuration (t , now .Add (time .Second ), time .Now (), 2 * time .Second )
350
+ defer res .Body .Close ()
351
+ require .Equal (t , http .StatusBadGateway , res .StatusCode )
352
+ }
353
+
302
354
func freeUDPPort (t * testing.T ) uint16 {
303
355
t .Helper ()
304
356
0 commit comments