...
Run Format

Source file src/net/conn_test.go

Documentation: net

     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 will never have a build
     6  // tag.
     7  
     8  // +build !js
     9  
    10  package net
    11  
    12  import (
    13  	"testing"
    14  	"time"
    15  )
    16  
    17  // someTimeout is used just to test that net.Conn implementations
    18  // don't explode when their SetFooDeadline methods are called.
    19  // It isn't actually used for testing timeouts.
    20  const someTimeout = 10 * time.Second
    21  
    22  func TestConnAndListener(t *testing.T) {
    23  	for i, network := range []string{"tcp", "unix", "unixpacket"} {
    24  		if !testableNetwork(network) {
    25  			t.Logf("skipping %s test", network)
    26  			continue
    27  		}
    28  
    29  		ls, err := newLocalServer(network)
    30  		if err != nil {
    31  			t.Fatal(err)
    32  		}
    33  		defer ls.teardown()
    34  		ch := make(chan error, 1)
    35  		handler := func(ls *localServer, ln Listener) { transponder(ln, ch) }
    36  		if err := ls.buildup(handler); err != nil {
    37  			t.Fatal(err)
    38  		}
    39  		if ls.Listener.Addr().Network() != network {
    40  			t.Fatalf("got %s; want %s", ls.Listener.Addr().Network(), network)
    41  		}
    42  
    43  		c, err := Dial(ls.Listener.Addr().Network(), ls.Listener.Addr().String())
    44  		if err != nil {
    45  			t.Fatal(err)
    46  		}
    47  		defer c.Close()
    48  		if c.LocalAddr().Network() != network || c.RemoteAddr().Network() != network {
    49  			t.Fatalf("got %s->%s; want %s->%s", c.LocalAddr().Network(), c.RemoteAddr().Network(), network, network)
    50  		}
    51  		c.SetDeadline(time.Now().Add(someTimeout))
    52  		c.SetReadDeadline(time.Now().Add(someTimeout))
    53  		c.SetWriteDeadline(time.Now().Add(someTimeout))
    54  
    55  		if _, err := c.Write([]byte("CONN AND LISTENER TEST")); err != nil {
    56  			t.Fatal(err)
    57  		}
    58  		rb := make([]byte, 128)
    59  		if _, err := c.Read(rb); err != nil {
    60  			t.Fatal(err)
    61  		}
    62  
    63  		for err := range ch {
    64  			t.Errorf("#%d: %v", i, err)
    65  		}
    66  	}
    67  }
    68  

View as plain text