Source file src/net/conn_test.go

     1  // Copyright 2012 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // This file implements API tests across platforms and should never have a build
     6  // constraint.
     7  
     8  package net
     9  
    10  import (
    11  	"testing"
    12  	"time"
    13  )
    14  
    15  // someTimeout is used just to test that net.Conn implementations
    16  // don't explode when their SetFooDeadline methods are called.
    17  // It isn't actually used for testing timeouts.
    18  const someTimeout = 1 * time.Hour
    19  
    20  func TestConnAndListener(t *testing.T) {
    21  	for i, network := range []string{"tcp", "unix", "unixpacket"} {
    22  		i, network := i, network
    23  		t.Run(network, func(t *testing.T) {
    24  			if !testableNetwork(network) {
    25  				t.Skipf("skipping %s test", network)
    26  			}
    27  
    28  			ls := newLocalServer(t, network)
    29  			defer ls.teardown()
    30  			ch := make(chan error, 1)
    31  			handler := func(ls *localServer, ln Listener) { ls.transponder(ln, ch) }
    32  			if err := ls.buildup(handler); err != nil {
    33  				t.Fatal(err)
    34  			}
    35  			if ls.Listener.Addr().Network() != network {
    36  				t.Fatalf("got %s; want %s", ls.Listener.Addr().Network(), network)
    37  			}
    38  
    39  			c, err := Dial(ls.Listener.Addr().Network(), ls.Listener.Addr().String())
    40  			if err != nil {
    41  				t.Fatal(err)
    42  			}
    43  			defer c.Close()
    44  			if c.LocalAddr().Network() != network || c.RemoteAddr().Network() != network {
    45  				t.Fatalf("got %s->%s; want %s->%s", c.LocalAddr().Network(), c.RemoteAddr().Network(), network, network)
    46  			}
    47  			c.SetDeadline(time.Now().Add(someTimeout))
    48  			c.SetReadDeadline(time.Now().Add(someTimeout))
    49  			c.SetWriteDeadline(time.Now().Add(someTimeout))
    50  
    51  			if _, err := c.Write([]byte("CONN AND LISTENER TEST")); err != nil {
    52  				t.Fatal(err)
    53  			}
    54  			rb := make([]byte, 128)
    55  			if _, err := c.Read(rb); err != nil {
    56  				t.Fatal(err)
    57  			}
    58  
    59  			for err := range ch {
    60  				t.Errorf("#%d: %v", i, err)
    61  			}
    62  		})
    63  	}
    64  }
    65  

View as plain text