2018-09-17 17:15:48 +00:00
|
|
|
package fifo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2019-04-01 19:38:39 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-09-17 17:15:48 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2019-03-31 16:50:06 +00:00
|
|
|
// TestFIFO tests basic behavior, and that reader closes when writer closes
|
2018-09-17 17:15:48 +00:00
|
|
|
func TestFIFO(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
var path string
|
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
path = "//./pipe/fifo"
|
|
|
|
} else {
|
|
|
|
dir, err := ioutil.TempDir("", "")
|
|
|
|
require.NoError(err)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
path = filepath.Join(dir, "fifo")
|
|
|
|
}
|
|
|
|
|
2019-04-01 19:56:43 +00:00
|
|
|
readerOpenFn, err := CreateAndRead(path)
|
2018-09-17 17:15:48 +00:00
|
|
|
require.NoError(err)
|
|
|
|
|
2019-03-31 16:50:06 +00:00
|
|
|
var reader io.ReadCloser
|
|
|
|
|
2018-09-17 17:15:48 +00:00
|
|
|
toWrite := [][]byte{
|
|
|
|
[]byte("abc\n"),
|
|
|
|
[]byte(""),
|
|
|
|
[]byte("def\n"),
|
|
|
|
[]byte("nomad"),
|
|
|
|
[]byte("\n"),
|
|
|
|
}
|
|
|
|
|
|
|
|
var readBuf bytes.Buffer
|
|
|
|
var wait sync.WaitGroup
|
|
|
|
wait.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wait.Done()
|
2019-03-31 16:50:06 +00:00
|
|
|
|
2019-05-21 13:32:58 +00:00
|
|
|
var err error
|
2019-03-31 16:50:06 +00:00
|
|
|
reader, err = readerOpenFn()
|
2019-04-01 19:38:39 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-03-31 16:50:06 +00:00
|
|
|
|
|
|
|
_, err = io.Copy(&readBuf, reader)
|
2019-04-01 19:38:39 +00:00
|
|
|
assert.NoError(t, err)
|
2018-09-17 17:15:48 +00:00
|
|
|
}()
|
|
|
|
|
2019-04-01 19:56:43 +00:00
|
|
|
writer, err := OpenWriter(path)
|
2018-09-17 17:15:48 +00:00
|
|
|
require.NoError(err)
|
|
|
|
for _, b := range toWrite {
|
|
|
|
n, err := writer.Write(b)
|
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(n, len(b))
|
|
|
|
}
|
|
|
|
require.NoError(writer.Close())
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
|
|
|
|
wait.Wait()
|
2019-03-31 16:50:06 +00:00
|
|
|
require.NoError(reader.Close())
|
2018-09-17 17:15:48 +00:00
|
|
|
|
|
|
|
expected := "abc\ndef\nnomad\n"
|
|
|
|
require.Equal(expected, readBuf.String())
|
|
|
|
|
|
|
|
require.NoError(Remove(path))
|
|
|
|
}
|
|
|
|
|
2019-03-31 16:50:06 +00:00
|
|
|
// TestWriteClose asserts that when writer closes, subsequent Write() fails
|
2018-09-17 17:15:48 +00:00
|
|
|
func TestWriteClose(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
var path string
|
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
path = "//./pipe/" + uuid.Generate()[:4]
|
|
|
|
} else {
|
|
|
|
dir, err := ioutil.TempDir("", "")
|
|
|
|
require.NoError(err)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
path = filepath.Join(dir, "fifo")
|
|
|
|
}
|
|
|
|
|
2019-04-01 19:56:43 +00:00
|
|
|
readerOpenFn, err := CreateAndRead(path)
|
2018-09-17 17:15:48 +00:00
|
|
|
require.NoError(err)
|
2019-03-31 16:50:06 +00:00
|
|
|
var reader io.ReadCloser
|
2018-09-17 17:15:48 +00:00
|
|
|
|
|
|
|
var readBuf bytes.Buffer
|
|
|
|
var wait sync.WaitGroup
|
|
|
|
wait.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wait.Done()
|
2019-03-31 16:50:06 +00:00
|
|
|
|
2019-05-21 13:32:58 +00:00
|
|
|
var err error
|
2019-03-31 16:50:06 +00:00
|
|
|
reader, err = readerOpenFn()
|
2019-04-01 19:38:39 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-03-31 16:50:06 +00:00
|
|
|
|
|
|
|
_, err = io.Copy(&readBuf, reader)
|
2019-04-01 19:38:39 +00:00
|
|
|
assert.NoError(t, err)
|
2018-09-17 17:15:48 +00:00
|
|
|
}()
|
|
|
|
|
2019-04-01 19:56:43 +00:00
|
|
|
writer, err := OpenWriter(path)
|
2018-09-17 17:15:48 +00:00
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
var count int
|
|
|
|
wait.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wait.Done()
|
|
|
|
for count = 0; count < int(^uint16(0)); count++ {
|
|
|
|
_, err := writer.Write([]byte(","))
|
|
|
|
if err != nil && IsClosedErr(err) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
|
|
|
time.Sleep(5 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
require.NoError(writer.Close())
|
|
|
|
wait.Wait()
|
|
|
|
|
|
|
|
require.Equal(count, len(readBuf.String()))
|
|
|
|
}
|