github.com/rentongzhang/docker@v1.8.2-rc1/pkg/listenbuffer/listen_buffer_test.go (about)

     1  package listenbuffer
     2  
     3  import (
     4  	"io/ioutil"
     5  	"net"
     6  	"testing"
     7  )
     8  
     9  func TestListenBufferAllowsAcceptingWhenActivated(t *testing.T) {
    10  	lock := make(chan struct{})
    11  	buffer, err := NewListenBuffer("tcp", "", lock)
    12  	if err != nil {
    13  		t.Fatal("Unable to create listen buffer: ", err)
    14  	}
    15  
    16  	go func() {
    17  		conn, err := net.Dial("tcp", buffer.Addr().String())
    18  		if err != nil {
    19  			t.Fatal("Client failed to establish connection to server: ", err)
    20  		}
    21  
    22  		conn.Write([]byte("ping"))
    23  		conn.Close()
    24  	}()
    25  
    26  	close(lock)
    27  
    28  	client, err := buffer.Accept()
    29  	if err != nil {
    30  		t.Fatal("Failed to accept client: ", err)
    31  	}
    32  
    33  	response, err := ioutil.ReadAll(client)
    34  	if err != nil {
    35  		t.Fatal("Failed to read from client: ", err)
    36  	}
    37  
    38  	if string(response) != "ping" {
    39  		t.Fatal("Expected to receive ping from client, received: ", string(response))
    40  	}
    41  }