Merge pull request #1477 from nak3/add-syslog_server-test

Add Syslog server start shutdown test
This commit is contained in:
Diptanu Choudhury 2016-08-05 12:01:24 -07:00 committed by GitHub
commit fb178a1f5f
1 changed files with 63 additions and 0 deletions

View File

@ -0,0 +1,63 @@
package logging
import (
"io/ioutil"
"net"
"os"
"path"
"testing"
"time"
)
func TestSyslogServer_Start_Shutdown(t *testing.T) {
dir, err := ioutil.TempDir("", "sock")
if err != nil {
t.Fatalf("Failed to create temporary direcotry: %v", err)
}
sock := path.Join(dir, "socket")
defer os.Remove(sock)
l, err := net.Listen("unix", sock)
if err != nil {
t.Fatalf("Failed to listen unix socket: %v", err)
}
s := NewSyslogServer(l, make(chan *SyslogMessage, 2048), nil)
go s.Start()
if s.done {
t.Fatalf("expected running SyslogServer, but not running")
}
received := false
go func() {
for _ = range s.messages {
received = true
}
}()
conn, err := net.Dial("unix", sock)
if err != nil {
t.Fatalf("expected access to SyslogServer, but %v", err)
}
_, err = conn.Write([]byte("syslog server test\n"))
if err != nil {
t.Fatalf("expected send data to SyslogServer but: %v", err)
}
// Need to wait until SyslogServer received the data certainly
time.Sleep(1000 * time.Millisecond)
if !received {
t.Fatalf("expected SyslogServer received data, but not received")
}
defer conn.Close()
s.Shutdown()
if !s.done {
t.Fatalf("expected SyslogServer done, but running")
}
}