Skip to content

Instantly share code, notes, and snippets.

@avagin
Created March 12, 2015 21:12
Show Gist options
  • Save avagin/b40fbc224a62aa5ee8eb to your computer and use it in GitHub Desktop.
Save avagin/b40fbc224a62aa5ee8eb to your computer and use it in GitHub Desktop.
go test vs pipes
[avagin@localhost ~]$ cat test_test.go
package main
import "time"
import "testing"
import "bufio"
import "fmt"
import "os"
func asd() {
rp, wp, err := os.Pipe()
if err != nil {
return
}
go func() {
rpfd := rp.Fd()
fmt.Fprintf(os.Stderr, "\n%d %d\n", rp.Fd(), wp.Fd())
sc := bufio.NewScanner(rp)
for sc.Scan() {
fmt.Fprintf(os.Stderr, "%s\n", sc.Text())
}
rp.Close()
fmt.Fprintf(os.Stderr, "The end %d\n", rpfd)
}()
fmt.Fprintf(wp, "start\n")
time.Sleep(1000 * time.Millisecond)
fmt.Fprintf(wp, "end\n")
time.Sleep(1000 * time.Millisecond)
}
func TestA(t *testing.T) {
asd()
}
func TestB(t *testing.T) {
asd()
}
func TestC(t *testing.T) {
asd()
}
[avagin@localhost ~]$ go test -v test_test.go
=== RUN TestA
3 4
start
end
The end 3
--- PASS: TestA (2.00 seconds)
=== RUN TestB
3 4
start
end
--- PASS: TestB (2.00 seconds)
=== RUN TestC
5 6
start
end
--- PASS: TestC (2.00 seconds)
PASS
ok command-line-arguments 6.005s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment