Skip to content

Commit

Permalink
wip: reproduce macos pipe readiness problem in unit test
Browse files Browse the repository at this point in the history
Signed-off-by: thediveo <thediveo@gmx.eu>
  • Loading branch information
thediveo committed Dec 15, 2023
1 parent 135fd8d commit 7788cb5
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions pipe/checker_notwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
package pipe

import (
"io"
"os"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/thediveo/success"
"golang.org/x/sys/unix"
)

var _ = Describe("pipes", func() {
Expand All @@ -21,13 +23,52 @@ var _ = Describe("pipes", func() {
// packet capture data into it. For this test we simulate Wireshark
// closing its reading end and we must properly detect this situation on
// our writing end of the pipe.
r, w := Successful2R(os.Pipe())
By("creating a temporary named pipe/fifo and opening its ends")
tmpfifodir := Successful(os.MkdirTemp("", "test-fifo-*"))
defer os.RemoveAll(tmpfifodir)

fifoname := tmpfifodir + "/fifo"
unix.Mkfifo(fifoname, 0660)
wch := make(chan *os.File)
go func() {
defer GinkgoRecover()
wch <- Successful(os.OpenFile(fifoname, os.O_WRONLY, os.ModeNamedPipe))
}()

rch := make(chan *os.File)
go func() {
defer GinkgoRecover()
rch <- Successful(os.OpenFile(fifoname, os.O_RDONLY, os.ModeNamedPipe))
}()

var r, w *os.File
Eventually(rch).Should(Receive(&r))
Eventually(wch).Should(Receive(&w))
defer w.Close()

go func() {
GinkgoHelper()
defer GinkgoRecover()
By("continously draining the read end of the pipe into /dev/null")
null := Successful(os.OpenFile("/dev/null", os.O_WRONLY, 0))
defer null.Close()
io.Copy(null, r)
By("pipe draining done")
}()

go func() {
defer GinkgoRecover()
time.Sleep(2 * time.Second)
By("closing read end of pipe")
Expect(r.Close()).To(Succeed())
}()

go func() {
defer GinkgoRecover()
time.Sleep(300 * time.Microsecond)
By("writing some data into the pipe")
w.WriteString("Wireshark rulez")
}()

start := time.Now()
WaitTillBreak(w)
Expect(time.Since(start).Milliseconds()).To(BeNumerically(">", 1900))
Expand Down

0 comments on commit 7788cb5

Please sign in to comment.