futex 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.0pdd.yml +9 -0
- data/.gitignore +5 -0
- data/.pdd +5 -0
- data/.rubocop.yml +16 -0
- data/.rultor.yml +23 -0
- data/.travis.yml +12 -0
- data/Gemfile +24 -0
- data/README.md +82 -0
- data/Rakefile +52 -0
- data/futex.gemspec +51 -0
- data/lib/futex.rb +95 -0
- data/test/test_futex.rb +71 -0
- metadata +142 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f8cbb61dfb861233a3f0883cd1ce83858bbbad9ec2b71f17f286edbbe2d0c57b
|
4
|
+
data.tar.gz: 6ef08599e9100ae73d0706270ce3c7aa6cb1b58f9c64ceb9f54026f07ac547f1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bc33d6417050639fd839530b820de078406b112d8d6ffc914c9956ce6b84b511be82678a2b438f9b9fa13311487a3033d6ac3e0efbe6262836b3c2c06443824d
|
7
|
+
data.tar.gz: 383ced7c5df759d14d494a183562771cc0cf3904b33bc9152c0870cea24e2683d966b660e11751a01aa32ab0137f538aaef7bc2eac9a53fa94149e46aa06dc84
|
data/.0pdd.yml
ADDED
data/.gitignore
ADDED
data/.pdd
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
AllCops:
|
2
|
+
DisplayCopNames: true
|
3
|
+
TargetRubyVersion: 2.3.3
|
4
|
+
|
5
|
+
Layout/MultilineMethodCallIndentation:
|
6
|
+
Enabled: false
|
7
|
+
Metrics/AbcSize:
|
8
|
+
Max: 50
|
9
|
+
Metrics/MethodLength:
|
10
|
+
Max: 30
|
11
|
+
Metrics/CyclomaticComplexity:
|
12
|
+
Max: 10
|
13
|
+
Metrics/PerceivedComplexity:
|
14
|
+
Max: 10
|
15
|
+
Layout/AlignParameters:
|
16
|
+
Enabled: false
|
data/.rultor.yml
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
assets:
|
2
|
+
rubygems.yml: zerocracy/home#assets/rubygems.yml
|
3
|
+
install: |-
|
4
|
+
export GEM_HOME=~/.ruby
|
5
|
+
export GEM_PATH=$GEM_HOME:$GEM_PATH
|
6
|
+
release:
|
7
|
+
script: |-
|
8
|
+
bundle install
|
9
|
+
rake
|
10
|
+
rm -rf *.gem
|
11
|
+
sed -i "s/0\.0\.0/${tag}/g" futex.gemspec
|
12
|
+
git add futex.gemspec
|
13
|
+
git commit -m "Version set to ${tag}"
|
14
|
+
gem build futex.gemspec
|
15
|
+
chmod 0600 ../rubygems.yml
|
16
|
+
gem push *.gem --config-file ../rubygems.yml
|
17
|
+
commanders:
|
18
|
+
- yegor256
|
19
|
+
architect:
|
20
|
+
- yegor256
|
21
|
+
merge:
|
22
|
+
commanders: []
|
23
|
+
deploy: {}
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright (c) 2018 Yegor Bugayenko
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the 'Software'), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in all
|
13
|
+
# copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
# SOFTWARE.
|
22
|
+
|
23
|
+
source 'https://rubygems.org'
|
24
|
+
gemspec
|
data/README.md
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
[![DevOps By Rultor.com](http://www.rultor.com/b/yegor256/futex)](http://www.rultor.com/p/yegor256/futex)
|
2
|
+
[![We recommend RubyMine](http://www.elegantobjects.org/rubymine.svg)](https://www.jetbrains.com/ruby/)
|
3
|
+
|
4
|
+
[![Build Status](https://travis-ci.org/yegor256/futex.svg)](https://travis-ci.org/yegor256/futex)
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/futex.svg)](http://badge.fury.io/rb/futex)
|
6
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/5528e182bb5e4a2ecc1f/maintainability)](https://codeclimate.com/github/yegor256/futex/maintainability)
|
7
|
+
|
8
|
+
Sometimes you need to synchronize your block of code, but `Mutex` is too coarse-grained,
|
9
|
+
because it _always locks_, no matter what objects your code accesses. The
|
10
|
+
`Futex` (from "file mutex") is more fine-grained and uses a file as an
|
11
|
+
entrance lock to your code.
|
12
|
+
|
13
|
+
First, install it:
|
14
|
+
|
15
|
+
```bash
|
16
|
+
$ gem install futex
|
17
|
+
```
|
18
|
+
|
19
|
+
Then, use it like this:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
require 'futex'
|
23
|
+
Futex.new('/tmp/my-file.txt').open |f|
|
24
|
+
File.write(f, 'Hello, world!')
|
25
|
+
end
|
26
|
+
```
|
27
|
+
|
28
|
+
The file `/tmp/my-file.txt.lock` will be created and used as an entrance lock.
|
29
|
+
It will be deleted afterwards.
|
30
|
+
|
31
|
+
For better traceability you can provide a few arguments to the
|
32
|
+
constructor of the `Futex` class, including:
|
33
|
+
|
34
|
+
* `log`: an object that implements `debug()` method, which will
|
35
|
+
receive supplementary messages from the locking mechanism;
|
36
|
+
|
37
|
+
* `timeout`: the number of seconds to wait for the lock availability
|
38
|
+
(an exception is raised when the wait is expired);
|
39
|
+
|
40
|
+
* `sleep`: the number of seconds to wait between attempts to acquire
|
41
|
+
the lock file (the smaller the number, the more reactive is the software,
|
42
|
+
but the higher the load for the file system and the CPU).
|
43
|
+
|
44
|
+
That's it.
|
45
|
+
|
46
|
+
# How to contribute
|
47
|
+
|
48
|
+
Read [these guidelines](https://www.yegor256.com/2014/04/15/github-guidelines.html).
|
49
|
+
Make sure you build is green before you contribute
|
50
|
+
your pull request. You will need to have [Ruby](https://www.ruby-lang.org/en/) 2.3+ and
|
51
|
+
[Bundler](https://bundler.io/) installed. Then:
|
52
|
+
|
53
|
+
```
|
54
|
+
$ bundle update
|
55
|
+
$ rake
|
56
|
+
```
|
57
|
+
|
58
|
+
If it's clean and you don't see any error messages, submit your pull request.
|
59
|
+
|
60
|
+
# License
|
61
|
+
|
62
|
+
(The MIT License)
|
63
|
+
|
64
|
+
Copyright (c) 2018 Yegor Bugayenko
|
65
|
+
|
66
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
67
|
+
of this software and associated documentation files (the 'Software'), to deal
|
68
|
+
in the Software without restriction, including without limitation the rights
|
69
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
70
|
+
copies of the Software, and to permit persons to whom the Software is
|
71
|
+
furnished to do so, subject to the following conditions:
|
72
|
+
|
73
|
+
The above copyright notice and this permission notice shall be included in all
|
74
|
+
copies or substantial portions of the Software.
|
75
|
+
|
76
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
77
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
78
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
79
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
80
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
81
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
82
|
+
SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright (c) 2018 Yegor Bugayenko
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the 'Software'), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in all
|
13
|
+
# copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
# SOFTWARE.
|
22
|
+
|
23
|
+
require 'rubygems'
|
24
|
+
require 'rake'
|
25
|
+
require 'rake/clean'
|
26
|
+
|
27
|
+
CLEAN = FileList['coverage']
|
28
|
+
|
29
|
+
def name
|
30
|
+
@name ||= File.basename(Dir['*.gemspec'].first, '.*')
|
31
|
+
end
|
32
|
+
|
33
|
+
def version
|
34
|
+
Gem::Specification.load(Dir['*.gemspec'].first).version
|
35
|
+
end
|
36
|
+
|
37
|
+
task default: %i[clean test rubocop]
|
38
|
+
|
39
|
+
require 'rake/testtask'
|
40
|
+
desc 'Run all unit tests'
|
41
|
+
Rake::TestTask.new(:test) do |test|
|
42
|
+
test.libs << 'lib' << 'test'
|
43
|
+
test.pattern = 'test/**/test_*.rb'
|
44
|
+
test.verbose = false
|
45
|
+
end
|
46
|
+
|
47
|
+
require 'rubocop/rake_task'
|
48
|
+
desc 'Run RuboCop on all directories'
|
49
|
+
RuboCop::RakeTask.new(:rubocop) do |task|
|
50
|
+
task.fail_on_error = true
|
51
|
+
task.requires << 'rubocop-rspec'
|
52
|
+
end
|
data/futex.gemspec
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# (The MIT License)
|
4
|
+
#
|
5
|
+
# Copyright (c) 2018 Yegor Bugayenko
|
6
|
+
#
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
# of this software and associated documentation files (the 'Software'), to deal
|
9
|
+
# in the Software without restriction, including without limitation the rights
|
10
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
# copies of the Software, and to permit persons to whom the Software is
|
12
|
+
# furnished to do so, subject to the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be included in all
|
15
|
+
# copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
23
|
+
# SOFTWARE.
|
24
|
+
|
25
|
+
require 'English'
|
26
|
+
Gem::Specification.new do |s|
|
27
|
+
s.specification_version = 2 if s.respond_to? :specification_version=
|
28
|
+
if s.respond_to? :required_rubygems_version=
|
29
|
+
s.required_rubygems_version = Gem::Requirement.new('>= 0')
|
30
|
+
end
|
31
|
+
s.rubygems_version = '2.3.3'
|
32
|
+
s.required_ruby_version = '>=2.3'
|
33
|
+
s.name = 'futex'
|
34
|
+
s.version = '0.1.0'
|
35
|
+
s.license = 'MIT'
|
36
|
+
s.summary = 'File-based mutex'
|
37
|
+
s.description = 'Ruby Gem for file-based locking'
|
38
|
+
s.authors = ['Yegor Bugayenko']
|
39
|
+
s.email = 'yegor256@gmail.com'
|
40
|
+
s.homepage = 'http://github.com/yegor256/futex'
|
41
|
+
s.files = `git ls-files`.split($RS)
|
42
|
+
s.test_files = s.files.grep(%r{^(test)/})
|
43
|
+
s.rdoc_options = ['--charset=UTF-8']
|
44
|
+
s.extra_rdoc_files = ['README.md']
|
45
|
+
s.add_development_dependency 'minitest', '~>5'
|
46
|
+
s.add_development_dependency 'rake', '~>12'
|
47
|
+
s.add_development_dependency 'rdoc', '~>4'
|
48
|
+
s.add_development_dependency 'rubocop', '0.58.1'
|
49
|
+
s.add_development_dependency 'rubocop-rspec', '~>1'
|
50
|
+
s.add_development_dependency 'threads', '~>0'
|
51
|
+
end
|
data/lib/futex.rb
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# (The MIT License)
|
4
|
+
#
|
5
|
+
# Copyright (c) 2018 Yegor Bugayenko
|
6
|
+
#
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
# of this software and associated documentation files (the 'Software'), to deal
|
9
|
+
# in the Software without restriction, including without limitation the rights
|
10
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
# copies of the Software, and to permit persons to whom the Software is
|
12
|
+
# furnished to do so, subject to the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be included in all
|
15
|
+
# copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
23
|
+
# SOFTWARE.
|
24
|
+
|
25
|
+
require 'fileutils'
|
26
|
+
require 'time'
|
27
|
+
|
28
|
+
# Futex.
|
29
|
+
# Author:: Yegor Bugayenko (yegor256@gmail.com)
|
30
|
+
# Copyright:: Copyright (c) 2018 Yegor Bugayenko
|
31
|
+
# License:: MIT
|
32
|
+
class Futex
|
33
|
+
def initialize(path, log: STDOUT, timeout: 16, sleep: 0.005,
|
34
|
+
lock: path + '.lock')
|
35
|
+
raise "File path can't be nil" if path.nil?
|
36
|
+
@path = path
|
37
|
+
raise "Log can't be nil" if log.nil?
|
38
|
+
@log = log
|
39
|
+
raise "Timeout can't be nil" if timeout.nil?
|
40
|
+
raise "Timeout must be positive: #{timeout}" unless timeout.positive?
|
41
|
+
@timeout = timeout
|
42
|
+
raise "Sleep can't be nil" if sleep.nil?
|
43
|
+
raise "Sleep can't be negative or zero: #{sleep}" unless sleep.positive?
|
44
|
+
@sleep = sleep
|
45
|
+
raise "Lock path can't be nil" if lock.nil?
|
46
|
+
@lock = lock
|
47
|
+
end
|
48
|
+
|
49
|
+
def open
|
50
|
+
FileUtils.mkdir_p(File.dirname(@lock))
|
51
|
+
step = (1 / @sleep).to_i
|
52
|
+
start = Time.now
|
53
|
+
cycle = 0
|
54
|
+
loop do
|
55
|
+
if File.new(@lock, File::CREAT | File::RDWR)
|
56
|
+
.flock(File::LOCK_EX | File::LOCK_NB)
|
57
|
+
break
|
58
|
+
end
|
59
|
+
sleep(@sleep)
|
60
|
+
cycle += 1
|
61
|
+
if Time.now - start > @timeout
|
62
|
+
raise "##{Process.pid}/#{Thread.current.name} can't get \
|
63
|
+
exclusive access to the file #{@path} \
|
64
|
+
because of the lock at #{@lock}, after #{age(start)} \
|
65
|
+
of waiting: #{IO.read(@lock)}"
|
66
|
+
end
|
67
|
+
if (cycle % step).zero? && Time.now - start > @timeout / 2
|
68
|
+
debug("##{Process.pid}/#{Thread.current.name} still waiting for \
|
69
|
+
exclusive access to #{@path}, #{age(start)} already: #{IO.read(@lock)}")
|
70
|
+
end
|
71
|
+
end
|
72
|
+
debug("Locked by \"#{Thread.current.name}\" in #{age(start)}: #{@path} \
|
73
|
+
(attempt no.#{cycle})")
|
74
|
+
File.write(@lock, "##{Process.pid}/#{Thread.current.name}")
|
75
|
+
acq = Time.now
|
76
|
+
res = yield(@path)
|
77
|
+
FileUtils.rm(@lock)
|
78
|
+
puts("Unlocked by \"#{Thread.current.name}\" in #{age(acq)}: #{@path}")
|
79
|
+
res
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def age(time)
|
85
|
+
"#{((Time.now - time) * 1000).round}ms"
|
86
|
+
end
|
87
|
+
|
88
|
+
def debug(msg)
|
89
|
+
if @log.respond_to?(:debug)
|
90
|
+
@log.debug(msg)
|
91
|
+
elsif @log.respond_to?(:puts)
|
92
|
+
@log.puts(msg)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
data/test/test_futex.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# (The MIT License)
|
4
|
+
#
|
5
|
+
# Copyright (c) 2018 Yegor Bugayenko
|
6
|
+
#
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
# of this software and associated documentation files (the 'Software'), to deal
|
9
|
+
# in the Software without restriction, including without limitation the rights
|
10
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
# copies of the Software, and to permit persons to whom the Software is
|
12
|
+
# furnished to do so, subject to the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be included in all
|
15
|
+
# copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
23
|
+
# SOFTWARE.
|
24
|
+
|
25
|
+
require 'minitest/autorun'
|
26
|
+
require 'tmpdir'
|
27
|
+
require 'threads'
|
28
|
+
require_relative '../lib/futex'
|
29
|
+
|
30
|
+
# Futex test.
|
31
|
+
# Author:: Yegor Bugayenko (yegor256@gmail.com)
|
32
|
+
# Copyright:: Copyright (c) 2018 Yegor Bugayenko
|
33
|
+
# License:: MIT
|
34
|
+
class FutexTest < Minitest::Test
|
35
|
+
def test_syncs_access_to_file
|
36
|
+
Dir.mktmpdir do |dir|
|
37
|
+
path = File.join(dir, 'a/b/c/file.txt')
|
38
|
+
Threads.new(5).assert(100) do |_, r|
|
39
|
+
Futex.new(path).open do |f|
|
40
|
+
text = "op no.#{r}"
|
41
|
+
IO.write(f, text)
|
42
|
+
assert_equal(text, IO.read(f))
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_syncs_access_to_file_in_slow_motion
|
49
|
+
Dir.mktmpdir do |dir|
|
50
|
+
path = File.join(dir, 'a/b/c/file.txt')
|
51
|
+
Threads.new(2).assert(4) do |_, r|
|
52
|
+
Futex.new(path).open do |f|
|
53
|
+
text = "op no.#{r}"
|
54
|
+
IO.write(f, text)
|
55
|
+
sleep 0.2
|
56
|
+
assert_equal(text, IO.read(f))
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_cleans_up_the_mess
|
63
|
+
Dir.mktmpdir do |dir|
|
64
|
+
Futex.new(File.join(dir, 'hey.txt')).open do |f|
|
65
|
+
IO.write(f, 'hey')
|
66
|
+
FileUtils.rm(f)
|
67
|
+
end
|
68
|
+
assert_equal(2, Dir.new(dir).count)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
metadata
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: futex
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yegor Bugayenko
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-10-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: minitest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '12'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '12'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rdoc
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '4'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '4'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.58.1
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.58.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop-rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: threads
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Ruby Gem for file-based locking
|
98
|
+
email: yegor256@gmail.com
|
99
|
+
executables: []
|
100
|
+
extensions: []
|
101
|
+
extra_rdoc_files:
|
102
|
+
- README.md
|
103
|
+
files:
|
104
|
+
- ".0pdd.yml"
|
105
|
+
- ".gitignore"
|
106
|
+
- ".pdd"
|
107
|
+
- ".rubocop.yml"
|
108
|
+
- ".rultor.yml"
|
109
|
+
- ".travis.yml"
|
110
|
+
- Gemfile
|
111
|
+
- README.md
|
112
|
+
- Rakefile
|
113
|
+
- futex.gemspec
|
114
|
+
- lib/futex.rb
|
115
|
+
- test/test_futex.rb
|
116
|
+
homepage: http://github.com/yegor256/futex
|
117
|
+
licenses:
|
118
|
+
- MIT
|
119
|
+
metadata: {}
|
120
|
+
post_install_message:
|
121
|
+
rdoc_options:
|
122
|
+
- "--charset=UTF-8"
|
123
|
+
require_paths:
|
124
|
+
- lib
|
125
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '2.3'
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - ">="
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
requirements: []
|
136
|
+
rubyforge_project:
|
137
|
+
rubygems_version: 2.7.6
|
138
|
+
signing_key:
|
139
|
+
specification_version: 2
|
140
|
+
summary: File-based mutex
|
141
|
+
test_files:
|
142
|
+
- test/test_futex.rb
|