github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/client/driver/logging/syslog_server_unix_test.go (about)

     1  package logging
     2  
     3  import (
     4  	"io/ioutil"
     5  	"net"
     6  	"os"
     7  	"path"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  func TestSyslogServer_Start_Shutdown(t *testing.T) {
    13  	t.Parallel()
    14  	dir, err := ioutil.TempDir("", "sock")
    15  	if err != nil {
    16  		t.Fatalf("Failed to create temporary direcotry: %v", err)
    17  	}
    18  
    19  	sock := path.Join(dir, "socket")
    20  	defer os.Remove(sock)
    21  
    22  	l, err := net.Listen("unix", sock)
    23  	if err != nil {
    24  		t.Fatalf("Failed to listen unix socket: %v", err)
    25  	}
    26  
    27  	s := NewSyslogServer(l, make(chan *SyslogMessage, 2048), nil)
    28  
    29  	go s.Start()
    30  	if s.done {
    31  		t.Fatalf("expected running SyslogServer, but not running")
    32  	}
    33  
    34  	received := false
    35  	go func() {
    36  		for _ = range s.messages {
    37  			received = true
    38  		}
    39  	}()
    40  
    41  	conn, err := net.Dial("unix", sock)
    42  	if err != nil {
    43  		t.Fatalf("expected access to SyslogServer, but %v", err)
    44  	}
    45  
    46  	_, err = conn.Write([]byte("syslog server test\n"))
    47  	if err != nil {
    48  		t.Fatalf("expected send data to SyslogServer but: %v", err)
    49  	}
    50  
    51  	// Need to wait until SyslogServer received the data certainly
    52  	time.Sleep(1000 * time.Millisecond)
    53  
    54  	if !received {
    55  		t.Fatalf("expected SyslogServer received data, but not received")
    56  	}
    57  
    58  	defer conn.Close()
    59  
    60  	s.Shutdown()
    61  	if !s.done {
    62  		t.Fatalf("expected SyslogServer done, but running")
    63  	}
    64  }