From ae740a459ef40f63d3a2864b70c89e316efd0c8b Mon Sep 17 00:00:00 2001 From: David du Colombier <0intro@gmail.com> Date: Sun, 12 Apr 2015 19:36:23 +0200 Subject: [PATCH] os: add TestProgWideChdir This test checks the working directory is always consistent after Chdir in a Go program. Fixes #10035. Change-Id: I6abf0e4fcd40680ee572c6b40fc52ab17ef38d54 Reviewed-on: https://go-review.googlesource.com/6382 Reviewed-by: Brad Fitzpatrick Run-TryBot: David du Colombier <0intro@gmail.com> TryBot-Result: Gobot Gobot --- src/os/os_test.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/os/os_test.go b/src/os/os_test.go index a2c181da22..f65845af86 100644 --- a/src/os/os_test.go +++ b/src/os/os_test.go @@ -1040,6 +1040,57 @@ func TestChdirAndGetwd(t *testing.T) { fd.Close() } +// Test that Chdir+Getwd is program-wide. +func TestProgWideChdir(t *testing.T) { + const N = 10 + c := make(chan bool) + cpwd := make(chan string) + for i := 0; i < N; i++ { + go func(i int) { + // Lock half the goroutines in their own operating system + // thread to exercise more scheduler possibilities. + if i%2 == 1 { + // On Plan 9, after calling LockOSThread, the goroutines + // run on different processes which don't share the working + // directory. This used to be an issue because Go expects + // the working directory to be program-wide. + // See issue 9428. + runtime.LockOSThread() + } + <-c + pwd, err := Getwd() + if err != nil { + t.Fatal("Getwd: %v", err) + } + cpwd <- pwd + }(i) + } + oldwd, err := Getwd() + if err != nil { + t.Fatal("Getwd: %v", err) + } + d, err := ioutil.TempDir("", "test") + if err != nil { + t.Fatal("TempDir: %v", err) + } + defer func() { + if err := Chdir(oldwd); err != nil { + t.Fatal("Chdir: %v", err) + } + RemoveAll(d) + }() + if err := Chdir(d); err != nil { + t.Fatal("Chdir: %v", err) + } + close(c) + for i := 0; i < N; i++ { + pwd := <-cpwd + if pwd != d { + t.Errorf("Getwd returned %q want %q", pwd, d) + } + } +} + func TestSeek(t *testing.T) { f := newFile("TestSeek", t) defer Remove(f.Name()) -- 2.48.1