123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- // Copyright 2015 Google Inc. All Rights Reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package fusetesting
- import (
- "fmt"
- "io/ioutil"
- "os"
- "path"
- "runtime"
- "sync/atomic"
- "time"
- . "github.com/jacobsa/ogletest"
- "github.com/jacobsa/syncutil"
- "golang.org/x/net/context"
- )
- // Run an ogletest test that checks expectations for parallel calls to open(2)
- // with O_CREAT.
- func RunCreateInParallelTest_NoTruncate(
- 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 opens the file with O_CREATE and then appends a
- // byte to it.
- worker := func(id byte) (err error) {
- f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
- if err != nil {
- err = fmt.Errorf("Worker %d: Open: %v", id, err)
- return
- }
- defer f.Close()
- _, err = f.Write([]byte{id})
- if err != nil {
- err = fmt.Errorf("Worker %d: Write: %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)
- // Read the contents of the file. We should see each worker's ID once.
- contents, err := ioutil.ReadFile(filename)
- AssertEq(nil, err)
- idsSeen := make(map[byte]struct{})
- for i, _ := range contents {
- id := contents[i]
- AssertLt(id, numWorkers)
- if _, ok := idsSeen[id]; ok {
- AddFailure("Duplicate ID: %d", id)
- }
- idsSeen[id] = struct{}{}
- }
- AssertEq(numWorkers, len(idsSeen))
- // Delete the file.
- err = os.Remove(filename)
- AssertEq(nil, err)
- }
- }
- // Run an ogletest test that checks expectations for parallel calls to open(2)
- // with O_CREAT|O_TRUNC.
- func RunCreateInParallelTest_Truncate(
- 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 opens the file with O_CREATE and O_TRUNC and then
- // appends a byte to it.
- worker := func(id byte) (err error) {
- f, err := os.OpenFile(
- filename,
- os.O_CREATE|os.O_WRONLY|os.O_APPEND|os.O_TRUNC,
- 0600)
- if err != nil {
- err = fmt.Errorf("Worker %d: Open: %v", id, err)
- return
- }
- defer f.Close()
- _, err = f.Write([]byte{id})
- if err != nil {
- err = fmt.Errorf("Worker %d: Write: %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)
- // Read the contents of the file. We should see at least one ID (the last
- // one that truncated), and at most all of them.
- contents, err := ioutil.ReadFile(filename)
- AssertEq(nil, err)
- idsSeen := make(map[byte]struct{})
- for i, _ := range contents {
- id := contents[i]
- AssertLt(id, numWorkers)
- if _, ok := idsSeen[id]; ok {
- AddFailure("Duplicate ID: %d", id)
- }
- idsSeen[id] = struct{}{}
- }
- AssertGe(len(idsSeen), 1)
- AssertLe(len(idsSeen), numWorkers)
- // Delete the file.
- err = os.Remove(filename)
- AssertEq(nil, err)
- }
- }
- // Run an ogletest test that checks expectations for parallel calls to open(2)
- // with O_CREAT|O_EXCL.
- func RunCreateInParallelTest_Exclusive(
- 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 opens the file with O_CREATE and O_EXCL, and then
- // appends a byte to it if it was successfully opened.
- var openCount uint64
- worker := func(id byte) (err error) {
- f, err := os.OpenFile(
- filename,
- os.O_CREATE|os.O_EXCL|os.O_WRONLY|os.O_APPEND,
- 0600)
- // If we failed to open due to the file already existing, just leave.
- if os.IsExist(err) {
- err = nil
- return
- }
- // Propgate other errors.
- if err != nil {
- err = fmt.Errorf("Worker %d: Open: %v", id, err)
- return
- }
- atomic.AddUint64(&openCount, 1)
- defer f.Close()
- _, err = f.Write([]byte{id})
- if err != nil {
- err = fmt.Errorf("Worker %d: Write: %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)
- // Exactly one worker should have opened successfully.
- AssertEq(1, openCount)
- // Read the contents of the file. It should contain that one worker's ID.
- contents, err := ioutil.ReadFile(filename)
- AssertEq(nil, err)
- AssertEq(1, len(contents))
- AssertLt(contents[0], numWorkers)
- // Delete the file.
- err = os.Remove(filename)
- AssertEq(nil, err)
- }
- }
- // Run an ogletest test that checks expectations for parallel calls to mkdir(2).
- 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 := ReadDirPicky(dir)
- AssertEq(nil, err)
- AssertEq(1, len(entries))
- AssertEq("foo", entries[0].Name())
- // Delete the directory.
- err = os.Remove(filename)
- AssertEq(nil, err)
- }
- }
- // Run an ogletest test that checks expectations for parallel calls to
- // symlink(2).
- func RunSymlinkInParallelTest(
- 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 symlink, ignoring EEXIST errors.
- worker := func(id byte) (err error) {
- err = os.Symlink("blah", filename)
- if os.IsExist(err) {
- err = nil
- }
- if err != nil {
- err = fmt.Errorf("Worker %d: Symlink: %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 symlink should have been created, once.
- entries, err := ReadDirPicky(dir)
- AssertEq(nil, err)
- AssertEq(1, len(entries))
- AssertEq("foo", entries[0].Name())
- // Delete the directory.
- err = os.Remove(filename)
- AssertEq(nil, err)
- }
- }
|