-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
And add hints for any future Windows implementation.
- Loading branch information
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package twin | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"gotest.tools/v3/assert" | ||
) | ||
|
||
// This test should replace TestInterruptableReader_blockedOnRead if or when | ||
// Windows catches up with the shutdown implementation. | ||
func TestInterruptableReader_blockedOnReadImmediate(t *testing.T) { | ||
// Make a pipe to read from and write to | ||
pipeReader, pipeWriter, err := os.Pipe() | ||
assert.NilError(t, err) | ||
|
||
// Make an interruptable reader | ||
testMe, err := newInterruptableReader(pipeReader) | ||
assert.NilError(t, err) | ||
assert.Assert(t, testMe != nil) | ||
|
||
// Start a thread that reads from the pipe | ||
type readResult struct { | ||
n int | ||
err error | ||
} | ||
readResultChan := make(chan readResult) | ||
go func() { | ||
buffer := make([]byte, 1) | ||
n, err := testMe.Read(buffer) | ||
readResultChan <- readResult{n, err} | ||
}() | ||
|
||
// Give the reader thread some time to start waiting | ||
time.Sleep(100 * time.Millisecond) | ||
|
||
// Interrupt the reader | ||
testMe.Interrupt() | ||
|
||
// Wait for the reader thread to finish | ||
result := <-readResultChan | ||
|
||
// Check the result | ||
assert.Equal(t, result.n, 0) | ||
assert.Equal(t, result.err, io.EOF) | ||
|
||
// Another read should return EOF immediately | ||
buffer := make([]byte, 1) | ||
n, err := testMe.Read(buffer) | ||
assert.Equal(t, err, io.EOF) | ||
assert.Equal(t, n, 0) | ||
|
||
// Even if there are bytes, the interrupted reader should still return EOF | ||
n, err = pipeWriter.Write([]byte{42}) | ||
assert.NilError(t, err) | ||
assert.Equal(t, n, 1) | ||
|
||
n, err = testMe.Read(buffer) | ||
assert.Equal(t, err, io.EOF) | ||
assert.Equal(t, n, 0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters