github.com/Schaudge/grailbase@v0.0.0-20240223061707-44c758a471c0/file/localdev_test.go (about) 1 //go:build !unit 2 // +build !unit 3 4 package file_test 5 6 import ( 7 "context" 8 "io/ioutil" 9 "os/exec" 10 "path/filepath" 11 "runtime" 12 "sync" 13 "testing" 14 15 "github.com/Schaudge/grailbase/file" 16 "github.com/grailbio/testutil" 17 "github.com/grailbio/testutil/assert" 18 "github.com/stretchr/testify/require" 19 ) 20 21 // Write to /dev/stdout. This test only checks that the write succeeds. 22 func TestStdout(t *testing.T) { 23 if runtime.GOOS == "darwin" { 24 t.Skip("This test does not consistently work on macOS") 25 } 26 ctx := context.Background() 27 w, err := file.Create(ctx, "/dev/stdout") 28 assert.Nil(t, err) 29 _, err = w.Writer(ctx).Write([]byte("Hello\n")) 30 assert.Nil(t, err) 31 require.NoError(t, w.Close(ctx)) 32 } 33 34 // Read and write a FIFO. 35 func TestDevice(t *testing.T) { 36 mkfifo, err := exec.LookPath("mkfifo") 37 if err != nil { 38 t.Skipf("mkfifo not found, skipping the test: %v", err) 39 } 40 tempDir, cleanup := testutil.TempDir(t, "", "") 41 defer cleanup() 42 43 fifoPath := filepath.Join(tempDir, "fifo") 44 require.NoError(t, exec.Command(mkfifo, fifoPath).Run()) 45 46 ctx := context.Background() 47 wg := sync.WaitGroup{} 48 wg.Add(2) 49 go func() { 50 w, err := file.Create(ctx, fifoPath) 51 require.NoError(t, err) 52 _, err = w.Writer(ctx).Write([]byte("Hello\n")) 53 require.NoError(t, err) 54 require.NoError(t, w.Close(ctx)) 55 wg.Done() 56 }() 57 58 var data []byte 59 go func() { 60 r, err := file.Open(ctx, fifoPath) 61 require.NoError(t, err) 62 data, err = ioutil.ReadAll(r.Reader(ctx)) 63 require.NoError(t, err) 64 wg.Done() 65 }() 66 wg.Wait() 67 require.Equal(t, "Hello\n", string(data)) 68 }