Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make some tests run faster by removing time.Sleeps and modifying implementation #99

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 28 additions & 15 deletions pipe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,10 @@ func TestDialTimesOutByDefault(t *testing.T) {
t.Fatal(err)
}
defer l.Close()
_, err = DialPipe(testPipeName, nil)
if err != ErrTimeout {
start := time.Now()
timeout := time.Millisecond
_, err = DialPipe(testPipeName, &timeout)
if err != ErrTimeout || time.Since(start) < time.Millisecond {
t.Fatalf("expected ErrTimeout, got %v", err)
}
}
Expand All @@ -274,14 +276,15 @@ func TestTimeoutPendingRead(t *testing.T) {
defer l.Close()

serverDone := make(chan struct{})

isReading := make(chan struct{})
go func() {
s, err := l.Accept()
if err != nil {
t.Fatal(err)
}
time.Sleep(1 * time.Second)
s.Close()
isReading <- struct{}{}

close(serverDone)
}()

Expand All @@ -294,11 +297,10 @@ func TestTimeoutPendingRead(t *testing.T) {
clientErr := make(chan error)
go func() {
buf := make([]byte, 10)
<-isReading
_, err = client.Read(buf)
clientErr <- err
}()

time.Sleep(100 * time.Millisecond) // make *sure* the pipe is reading before we set the deadline
client.SetReadDeadline(aLongTimeAgo)

select {
Expand All @@ -322,12 +324,14 @@ func TestTimeoutPendingWrite(t *testing.T) {

serverDone := make(chan struct{})

isReading := make(chan struct{})
wrote := make(chan struct{})
go func() {
s, err := l.Accept()
if err != nil {
t.Fatal(err)
}
time.Sleep(1 * time.Second)
isReading <- struct{}{}
s.Close()
close(serverDone)
}()
Expand All @@ -340,12 +344,14 @@ func TestTimeoutPendingWrite(t *testing.T) {

clientErr := make(chan error)
go func() {
<-isReading // allow it to close
_, err = client.Write([]byte("this should timeout"))
wrote <- struct{}{}
clientErr <- err
}()

time.Sleep(100 * time.Millisecond) // make *sure* the pipe is writing before we set the deadline
client.SetWriteDeadline(aLongTimeAgo)
<-wrote

select {
case err = <-clientErr:
Expand Down Expand Up @@ -385,12 +391,12 @@ func TestEchoWithMessaging(t *testing.T) {
}
defer conn.Close()

time.Sleep(500 * time.Millisecond) // make *sure* we don't begin to read before eof signal is sent
time.Sleep(200 * time.Millisecond) // make *sure* we don't begin to read before eof signal is sent
io.Copy(conn, conn)
conn.(CloseWriter).CloseWrite()
close(listenerDone)
}()
timeout := 1 * time.Second
timeout := 500 * time.Second
client, err := DialPipe(testPipeName, &timeout)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -446,13 +452,20 @@ func TestConnectRace(t *testing.T) {
}
}()

// Dial all in background
var wg sync.WaitGroup
for i := 0; i < 1000; i++ {
c, err := DialPipe(testPipeName, nil)
if err != nil {
t.Fatal(err)
}
c.Close()
wg.Add(1)
go func() {
defer wg.Done()
c, err := DialPipe(testPipeName, nil)
if err != nil {
t.Fatal(err)
}
c.Close()
}()
}
wg.Wait() // wait for all to finish
}

func TestMessageReadMode(t *testing.T) {
Expand Down