Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
jacobsa-fuse
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
jacobsa-fuse
Commits
63888f4d
Commit
63888f4d
authored
May 21, 2015
by
Aaron Jacobs
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PosixTest.CreateInParallel_Exclusive
parent
25ded77e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
77 additions
and
1 deletion
+77
-1
samples/memfs/posix_test.go
samples/memfs/posix_test.go
+77
-1
No files found.
samples/memfs/posix_test.go
View file @
63888f4d
...
...
@@ -24,6 +24,7 @@ import (
"os"
"path"
"runtime"
"sync/atomic"
"testing"
"time"
...
...
@@ -192,6 +193,81 @@ func runCreateInParallelTest_Truncate(
}
}
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
)
}
}
////////////////////////////////////////////////////////////////////////
// Boilerplate
////////////////////////////////////////////////////////////////////////
...
...
@@ -583,5 +659,5 @@ func (t *PosixTest) CreateInParallel_Truncate() {
}
func
(
t
*
PosixTest
)
CreateInParallel_Exclusive
()
{
AssertFalse
(
true
,
"TODO"
)
runCreateInParallelTest_Exclusive
(
t
.
ctx
,
t
.
dir
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment