Skip to content

Commit

Permalink
fifo: Close connections and cleanup lock handling
Browse files Browse the repository at this point in the history
  • Loading branch information
endocrimes committed Jul 1, 2019
1 parent 2e5aba9 commit 8148466
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 27 deletions.
10 changes: 8 additions & 2 deletions client/lib/fifo/fifo_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ type winFIFO struct {

func (f *winFIFO) Read(p []byte) (n int, err error) {
f.connLock.Lock()
defer f.connLock.Unlock()
if f.conn == nil {
c, err := f.listener.Accept()
if err != nil {
Expand All @@ -32,6 +31,7 @@ func (f *winFIFO) Read(p []byte) (n int, err error) {

f.conn = c
}
f.connLock.Unlock()

// If the connection is closed then we need to close the listener
// to emulate unix fifo behavior
Expand All @@ -44,7 +44,6 @@ func (f *winFIFO) Read(p []byte) (n int, err error) {

func (f *winFIFO) Write(p []byte) (n int, err error) {
f.connLock.Lock()
defer f.connLock.Unlock()
if f.conn == nil {
c, err := f.listener.Accept()
if err != nil {
Expand All @@ -53,18 +52,25 @@ func (f *winFIFO) Write(p []byte) (n int, err error) {

f.conn = c
}
f.connLock.Unlock()

// If the connection is closed then we need to close the listener
// to emulate unix fifo behavior
n, err = f.conn.Write(p)
if err == io.EOF {
f.conn.Close()
f.listener.Close()
}
return n, err

}

func (f *winFIFO) Close() error {
f.connLock.Lock()
if f.conn != nil {
f.conn.Close()
}
f.connLock.Unlock()
return f.listener.Close()
}

Expand Down
41 changes: 16 additions & 25 deletions client/logmon/logmon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ import (

func TestLogmon_Start_rotate(t *testing.T) {
require := require.New(t)

stdoutLog := "stdout"
stderrLog := "stderr"

var stdoutFifoPath, stderrFifoPath string

dir, err := ioutil.TempDir("", "nomadtest")
Expand All @@ -37,9 +33,9 @@ func TestLogmon_Start_rotate(t *testing.T) {

cfg := &LogConfig{
LogDir: dir,
StdoutLogFile: stdoutLog,
StdoutLogFile: "stdout",
StdoutFifo: stdoutFifoPath,
StderrLogFile: stderrLog,
StderrLogFile: "stderr",
StderrFifo: stderrFifoPath,
MaxFiles: 2,
MaxFileSizeMB: 1,
Expand Down Expand Up @@ -78,17 +74,13 @@ func TestLogmon_Start_rotate(t *testing.T) {
}

// asserts that calling Start twice restarts the log rotator and that any logs
// published while the listener was unavailable are recieved.
// published while the listener was unavailable are received.
func TestLogmon_Start_restart_flusheslogs(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("windows does not support pushing data to a pipe with no servers")
}

require := require.New(t)

stdoutLog := "stdout"
stderrLog := "stderr"

var stdoutFifoPath, stderrFifoPath string

dir, err := ioutil.TempDir("", "nomadtest")
Expand All @@ -105,9 +97,9 @@ func TestLogmon_Start_restart_flusheslogs(t *testing.T) {

cfg := &LogConfig{
LogDir: dir,
StdoutLogFile: stdoutLog,
StdoutLogFile: "stdout",
StdoutFifo: stdoutFifoPath,
StderrLogFile: stderrLog,
StderrLogFile: "stderr",
StderrFifo: stderrFifoPath,
MaxFiles: 2,
MaxFileSizeMB: 1,
Expand Down Expand Up @@ -148,11 +140,6 @@ func TestLogmon_Start_restart_flusheslogs(t *testing.T) {
require.NoError(err)
})

require.NoError(lm.Stop())

// Start logmon again and assert that it appended to the file
require.NoError(lm.Start(cfg))

stdout, err = fifo.OpenWriter(stdoutFifoPath)
require.NoError(err)
stderr, err = fifo.OpenWriter(stderrFifoPath)
Expand All @@ -171,6 +158,14 @@ func TestLogmon_Start_restart_flusheslogs(t *testing.T) {
require.NoError(err)
})

// Start logmon again and assert that it appended to the file
require.NoError(lm.Start(cfg))

stdout, err = fifo.OpenWriter(stdoutFifoPath)
require.NoError(err)
stderr, err = fifo.OpenWriter(stderrFifoPath)
require.NoError(err)

_, err = stdout.Write([]byte("st\n"))
require.NoError(err)
testutil.WaitForResult(func() (bool, error) {
Expand All @@ -189,10 +184,6 @@ func TestLogmon_Start_restart_flusheslogs(t *testing.T) {
// asserts that calling Start twice restarts the log rotator
func TestLogmon_Start_restart(t *testing.T) {
require := require.New(t)

stdoutLog := "stdout"
stderrLog := "stderr"

var stdoutFifoPath, stderrFifoPath string

dir, err := ioutil.TempDir("", "nomadtest")
Expand All @@ -209,9 +200,9 @@ func TestLogmon_Start_restart(t *testing.T) {

cfg := &LogConfig{
LogDir: dir,
StdoutLogFile: stdoutLog,
StdoutLogFile: "stdout",
StdoutFifo: stdoutFifoPath,
StderrLogFile: stderrLog,
StderrLogFile: "stderr",
StderrFifo: stderrFifoPath,
MaxFiles: 2,
MaxFileSizeMB: 1,
Expand Down Expand Up @@ -252,7 +243,7 @@ func TestLogmon_Start_restart(t *testing.T) {
require.NoError(err)
})

// Start logmon again and assert that it can recieve logs again
// Start logmon again and assert that it can receive logs again
require.NoError(lm.Start(cfg))

stdout, err = fifo.OpenWriter(stdoutFifoPath)
Expand Down

0 comments on commit 8148466

Please sign in to comment.