From 2b71f8cbe83fb88f1a2de14d54898087181228c8 Mon Sep 17 00:00:00 2001 From: Aaron Jacobs Date: Thu, 21 May 2015 15:19:39 +1000 Subject: [PATCH] PosixTest.MkdirInParallel --- samples/memfs/posix_test.go | 59 +++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/samples/memfs/posix_test.go b/samples/memfs/posix_test.go index 1052814..efb5da2 100644 --- a/samples/memfs/posix_test.go +++ b/samples/memfs/posix_test.go @@ -30,6 +30,7 @@ import ( "golang.org/x/net/context" + "github.com/jacobsa/fuse/fusetesting" "github.com/jacobsa/gcloud/syncutil" . "github.com/jacobsa/oglematchers" . "github.com/jacobsa/ogletest" @@ -268,6 +269,60 @@ func runCreateInParallelTest_Exclusive( } } +func runMkdirInParallelTest( + ctx context.Context, + dir string) { + // Ensure that we get parallelism for this test. + defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(runtime.NumCPU())) + + // Try for awhile to see if anything breaks. + const duration = 500 * time.Millisecond + startTime := time.Now() + for time.Since(startTime) < duration { + filename := path.Join(dir, "foo") + + // Set up a function that creates the directory, ignoring EEXIST errors. + worker := func(id byte) (err error) { + err = os.Mkdir(filename, 0700) + + if os.IsExist(err) { + err = nil + } + + if err != nil { + err = fmt.Errorf("Worker %d: Mkdir: %v", id, err) + return + } + + return + } + + // Run several workers in parallel. + const numWorkers = 16 + b := syncutil.NewBundle(ctx) + for i := 0; i < numWorkers; i++ { + id := byte(i) + b.Add(func(ctx context.Context) (err error) { + err = worker(id) + return + }) + } + + err := b.Join() + AssertEq(nil, err) + + // The directory should have been created, once. + entries, err := fusetesting.ReadDirPicky(dir) + AssertEq(nil, err) + AssertEq(1, len(entries)) + AssertEq("foo", entries[0].Name()) + + // Delete the directory. + err = os.Remove(filename) + AssertEq(nil, err) + } +} + //////////////////////////////////////////////////////////////////////// // Boilerplate //////////////////////////////////////////////////////////////////////// @@ -661,3 +716,7 @@ func (t *PosixTest) CreateInParallel_Truncate() { func (t *PosixTest) CreateInParallel_Exclusive() { runCreateInParallelTest_Exclusive(t.ctx, t.dir) } + +func (t *PosixTest) MkdirInParallel() { + runMkdirInParallelTest(t.ctx, t.dir) +}