summaryrefslogtreecommitdiffhomepage
path: root/pkg/tcpip/adapters/gonet/gonet_test.go
diff options
context:
space:
mode:
authorIan Gudger <igudger@google.com>2019-05-07 14:26:24 -0700
committerShentubot <shentubot@google.com>2019-05-07 14:27:36 -0700
commit20862f0db27efac0eed3bb23d01b22b09bddfa27 (patch)
tree2bb9450cb50fc0e307826c7be94de2571daac940 /pkg/tcpip/adapters/gonet/gonet_test.go
parente5432fa1b365edcebf9c8c01e2c40ade3014f282 (diff)
Add gonet.DialContextTCP.
Allows cancellation and timeouts. PiperOrigin-RevId: 247090428 Change-Id: I91907f12e218677dcd0e0b6d72819deedbd9f20c
Diffstat (limited to 'pkg/tcpip/adapters/gonet/gonet_test.go')
-rw-r--r--pkg/tcpip/adapters/gonet/gonet_test.go43
1 files changed, 43 insertions, 0 deletions
diff --git a/pkg/tcpip/adapters/gonet/gonet_test.go b/pkg/tcpip/adapters/gonet/gonet_test.go
index 2c81c5697..2552004a9 100644
--- a/pkg/tcpip/adapters/gonet/gonet_test.go
+++ b/pkg/tcpip/adapters/gonet/gonet_test.go
@@ -15,6 +15,7 @@
package gonet
import (
+ "context"
"fmt"
"io"
"net"
@@ -595,6 +596,48 @@ func TestTCPDialError(t *testing.T) {
}
}
+func TestDialContextTCPCanceled(t *testing.T) {
+ s, err := newLoopbackStack()
+ if err != nil {
+ t.Fatalf("newLoopbackStack() = %v", err)
+ }
+
+ addr := tcpip.FullAddress{NICID, tcpip.Address(net.IPv4(169, 254, 10, 1).To4()), 11211}
+ s.AddAddress(NICID, ipv4.ProtocolNumber, addr.Addr)
+
+ ctx := context.Background()
+ ctx, cancel := context.WithCancel(ctx)
+ cancel()
+
+ if _, err := DialContextTCP(ctx, s, addr, ipv4.ProtocolNumber); err != context.Canceled {
+ t.Errorf("got DialContextTCP(...) = %v, want = %v", err, context.Canceled)
+ }
+}
+
+func TestDialContextTCPTimeout(t *testing.T) {
+ s, err := newLoopbackStack()
+ if err != nil {
+ t.Fatalf("newLoopbackStack() = %v", err)
+ }
+
+ addr := tcpip.FullAddress{NICID, tcpip.Address(net.IPv4(169, 254, 10, 1).To4()), 11211}
+ s.AddAddress(NICID, ipv4.ProtocolNumber, addr.Addr)
+
+ fwd := tcp.NewForwarder(s, 30000, 10, func(r *tcp.ForwarderRequest) {
+ time.Sleep(time.Second)
+ r.Complete(true)
+ })
+ s.SetTransportProtocolHandler(tcp.ProtocolNumber, fwd.HandlePacket)
+
+ ctx := context.Background()
+ ctx, cancel := context.WithDeadline(ctx, time.Now().Add(100*time.Millisecond))
+ defer cancel()
+
+ if _, err := DialContextTCP(ctx, s, addr, ipv4.ProtocolNumber); err != context.DeadlineExceeded {
+ t.Errorf("got DialContextTCP(...) = %v, want = %v", err, context.DeadlineExceeded)
+ }
+}
+
func TestNetTest(t *testing.T) {
nettest.TestConn(t, makePipe)
}