github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/pkg/net/conn_test.go (about)

     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  package net
     9  
    10  import (
    11  	"os"
    12  	"runtime"
    13  	"testing"
    14  	"time"
    15  )
    16  
    17  var connTests = []struct {
    18  	net  string
    19  	addr func() string
    20  }{
    21  	{"tcp", func() string { return "127.0.0.1:0" }},
    22  	{"unix", testUnixAddr},
    23  	{"unixpacket", testUnixAddr},
    24  }
    25  
    26  // someTimeout is used just to test that net.Conn implementations
    27  // don't explode when their SetFooDeadline methods are called.
    28  // It isn't actually used for testing timeouts.
    29  const someTimeout = 10 * time.Second
    30  
    31  func TestConnAndListener(t *testing.T) {
    32  	for _, tt := range connTests {
    33  		switch tt.net {
    34  		case "unix", "unixpacket":
    35  			switch runtime.GOOS {
    36  			case "plan9", "windows":
    37  				continue
    38  			}
    39  			if tt.net == "unixpacket" && runtime.GOOS != "linux" {
    40  				continue
    41  			}
    42  		}
    43  
    44  		addr := tt.addr()
    45  		ln, err := Listen(tt.net, addr)
    46  		if err != nil {
    47  			t.Fatalf("Listen failed: %v", err)
    48  		}
    49  		defer func(ln Listener, net, addr string) {
    50  			ln.Close()
    51  			switch net {
    52  			case "unix", "unixpacket":
    53  				os.Remove(addr)
    54  			}
    55  		}(ln, tt.net, addr)
    56  		ln.Addr()
    57  
    58  		done := make(chan int)
    59  		go transponder(t, ln, done)
    60  
    61  		c, err := Dial(tt.net, ln.Addr().String())
    62  		if err != nil {
    63  			t.Fatalf("Dial failed: %v", err)
    64  		}
    65  		defer c.Close()
    66  		c.LocalAddr()
    67  		c.RemoteAddr()
    68  		c.SetDeadline(time.Now().Add(someTimeout))
    69  		c.SetReadDeadline(time.Now().Add(someTimeout))
    70  		c.SetWriteDeadline(time.Now().Add(someTimeout))
    71  
    72  		if _, err := c.Write([]byte("CONN TEST")); err != nil {
    73  			t.Fatalf("Conn.Write failed: %v", err)
    74  		}
    75  		rb := make([]byte, 128)
    76  		if _, err := c.Read(rb); err != nil {
    77  			t.Fatalf("Conn.Read failed: %v", err)
    78  		}
    79  
    80  		<-done
    81  	}
    82  }
    83  
    84  func transponder(t *testing.T, ln Listener, done chan<- int) {
    85  	defer func() { done <- 1 }()
    86  
    87  	switch ln := ln.(type) {
    88  	case *TCPListener:
    89  		ln.SetDeadline(time.Now().Add(someTimeout))
    90  	case *UnixListener:
    91  		ln.SetDeadline(time.Now().Add(someTimeout))
    92  	}
    93  	c, err := ln.Accept()
    94  	if err != nil {
    95  		t.Errorf("Listener.Accept failed: %v", err)
    96  		return
    97  	}
    98  	defer c.Close()
    99  	c.LocalAddr()
   100  	c.RemoteAddr()
   101  	c.SetDeadline(time.Now().Add(someTimeout))
   102  	c.SetReadDeadline(time.Now().Add(someTimeout))
   103  	c.SetWriteDeadline(time.Now().Add(someTimeout))
   104  
   105  	b := make([]byte, 128)
   106  	n, err := c.Read(b)
   107  	if err != nil {
   108  		t.Errorf("Conn.Read failed: %v", err)
   109  		return
   110  	}
   111  	if _, err := c.Write(b[:n]); err != nil {
   112  		t.Errorf("Conn.Write failed: %v", err)
   113  		return
   114  	}
   115  }