Skip to content

Instantly share code, notes, and snippets.

@frankywahl
Last active November 16, 2019 23:56
Show Gist options
  • Save frankywahl/3bef696d249fd8f31de720a7f859272e to your computer and use it in GitHub Desktop.
Save frankywahl/3bef696d249fd8f31de720a7f859272e to your computer and use it in GitHub Desktop.
package main_test
import (
"fmt"
"io"
"runtime"
"testing"
"time"
)
type Error string
func (e Error) Error() string { return string(e) }
const CustomError = Error("CustomError")
func init() {
fmt.Printf("Version: %s - GOOS: %s - Arch: %s\n", runtime.Version(), runtime.GOOS, runtime.GOARCH)
}
// This works as expected
func TestNoError(t *testing.T) {
pr, pw := io.Pipe()
go func() {
defer pw.Close()
time.Sleep(1 * time.Second)
if _, err := pw.Write([]byte("hello")); err != nil {
panic(err)
}
}()
var buffer []byte
if _, err := pr.Read(buffer); err != nil {
t.Errorf("expected: nil, got: %v", err)
}
}
// This also works as expected
func TestOneErrorBad(t *testing.T) {
pr, pw := io.Pipe()
go func() {
defer pw.Close()
pw.CloseWithError(CustomError)
time.Sleep(1 * time.Second)
}()
var buffer []byte
if _, err := pr.Read(buffer); err != CustomError {
t.Errorf("expected: %v, got: %v", CustomError, err)
}
if _, err := pr.Read(buffer); err != CustomError {
t.Errorf("expected: %v, got: %v", CustomError, err)
}
}
// This returns two different errors
func TestOneErrorGoodAfterLongTime(t *testing.T) {
pr, pw := io.Pipe()
go func() {
pw.CloseWithError(CustomError)
time.Sleep(1 * time.Second)
defer pw.Close()
}()
var buffer []byte
if _, err := pr.Read(buffer); err != CustomError {
t.Errorf("expected: %v, got: %v", CustomError, err)
}
time.Sleep(3 * time.Second) /* May represent expensive work */
if _, err := pr.Read(buffer); err != CustomError {
t.Errorf("expected: %v, got: %v", CustomError, err)
}
}
// This returns the EOF error which also shows it's not just the first error
// encountered
func TestOneErrorGoodAfterOtherLongTime(t *testing.T) {
pr, pw := io.Pipe()
go func() {
pw.CloseWithError(CustomError)
time.Sleep(1 * time.Second)
defer pw.Close()
}()
var buffer []byte
time.Sleep(3 * time.Second) /* May represent expensive work */
if _, err := pr.Read(buffer); err != CustomError {
t.Errorf("expected: %v, got: %v", CustomError, err)
}
}
Version: go1.13.4 - GOOS: darwin - Arch: amd64
=== RUN TestNoError
--- PASS: TestNoError (1.05s)
=== RUN TestOneErrorBad
--- PASS: TestOneErrorBad (0.00s)
=== RUN TestOneErrorGoodAfterLongTime
--- FAIL: TestOneErrorGoodAfterLongTime (3.07s)
main_test.go:71: expected: CustomError, got: EOF
=== RUN TestOneErrorGoodAfterOtherLongTime
--- FAIL: TestOneErrorGoodAfterOtherLongTime (3.07s)
main_test.go:88: expected: CustomError, got: EOF
FAIL
FAIL github.com/frankywahl/tmp 7.200s
FAIL
Version: go1.13.4 - GOOS: linux - Arch: amd64
=== RUN TestNoError
--- PASS: TestNoError (1.00s)
=== RUN TestOneErrorBad
--- PASS: TestOneErrorBad (0.00s)
=== RUN TestOneErrorGoodAfterLongTime
--- FAIL: TestOneErrorGoodAfterLongTime (3.00s)
main_test.go:71: expected: CustomError, got: EOF
=== RUN TestOneErrorGoodAfterOtherLongTime
--- FAIL: TestOneErrorGoodAfterOtherLongTime (3.00s)
main_test.go:88: expected: CustomError, got: EOF
FAIL
FAIL _/go 7.014s
FAIL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment