futex 0.8.2 → 0.8.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8a40f777d578131870588931a9f18fde1960ba7a5219b5b5cd50dd97a7b9fc07
4
- data.tar.gz: 0bf2196fca978a7a75b63faccd92ca4115510fcedf24c639a2d1c1251bf420b4
3
+ metadata.gz: 621fe1e76d09dd787dc6e5a76b7137d44f981ac303d5c0e825a0af6d885695ca
4
+ data.tar.gz: 0ab8b3899131e5e5514e2ede85cc7f18825a74915fa59b26e50e4712c1e0f0c8
5
5
  SHA512:
6
- metadata.gz: 49791caf45a0b5c250aee714870bd6315c359b6c5e32e93a69663d15490ec0d56b9ede3d0726e1fd90c5f8231b46bd29a3accc8a2bd6d4407834e8498174543c
7
- data.tar.gz: 85296003d7fb88544f1365ca1b9f026ece2fc1eebf2f532bf82fe7a947f14f92b1d45054fff516cf1eabc2f800817c60b949773cdb686abee58d06cae8202f2a
6
+ metadata.gz: 8b2a86b5382799a2b3f7591d0408bae3a1fc3f9b2f01b99b24e18b0598de50d9a04bf2904619ad135322346de41ee72e3b96a1de71fd9b74046495f1203e2ed6
7
+ data.tar.gz: a9fa21e4a920d835454fa47f458933e1810344677e870a8df7c45f1919db4dfc3e6324b5a6876e6ffa9bbbbce4bf0506e755afbab10e25e6fb7d426195ab6c93
data/.rubocop.yml CHANGED
@@ -13,7 +13,7 @@ Metrics/BlockLength:
13
13
  Metrics/MethodLength:
14
14
  Max: 50
15
15
  Metrics/ClassLength:
16
- Max: 150
16
+ Max: 180
17
17
  Metrics/CyclomaticComplexity:
18
18
  Max: 10
19
19
  Metrics/PerceivedComplexity:
data/futex.gemspec CHANGED
@@ -31,7 +31,7 @@ Gem::Specification.new do |s|
31
31
  s.rubygems_version = '2.3.3'
32
32
  s.required_ruby_version = '>=2.3'
33
33
  s.name = 'futex'
34
- s.version = '0.8.2'
34
+ s.version = '0.8.3'
35
35
  s.license = 'MIT'
36
36
  s.summary = 'File-based mutex'
37
37
  s.description = 'Ruby Gem for file-based locking'
@@ -46,7 +46,7 @@ Gem::Specification.new do |s|
46
46
  s.add_development_dependency 'openssl', '2.1.2'
47
47
  s.add_development_dependency 'rake', '12.3.1'
48
48
  s.add_development_dependency 'rdoc', '4.3.0'
49
- s.add_development_dependency 'rubocop', '0.60.0'
50
- s.add_development_dependency 'rubocop-rspec', '1.30.1'
49
+ s.add_development_dependency 'rubocop', '0.62.0'
50
+ s.add_development_dependency 'rubocop-rspec', '1.31.0'
51
51
  s.add_development_dependency 'threads', '0.3.0'
52
52
  end
data/lib/futex.rb CHANGED
@@ -67,6 +67,9 @@ class Futex
67
67
  end
68
68
  end
69
69
 
70
+ # Global file for locks counting
71
+ COUNTS = File.join(Dir.tmpdir, 'futex.lock').freeze
72
+
70
73
  # Creates a new instance of the class.
71
74
  def initialize(path, log: STDOUT, timeout: 16, sleep: 0.005,
72
75
  lock: path + '.lock', logging: false)
@@ -163,41 +166,40 @@ access to #{@path}, #{age(start)} already: #{IO.read(@lock)} \
163
166
  def open_synchronized(path)
164
167
  path = File.absolute_path(path)
165
168
  file = nil
166
- synchronized do |ref_counts_file|
169
+ synchronized do |counts|
167
170
  file = File.open(path, File::CREAT | File::RDWR)
168
- ref_counts = deserialize(File.read(ref_counts_file.path))
169
- ref_counts[path] = (ref_counts[path] || 0) + 1
170
- File.write(ref_counts_file.path, serialize(ref_counts))
171
+ refs = deserialize(File.read(counts.path))
172
+ refs[path] = (refs[path] || 0) + 1
173
+ File.write(counts.path, serialize(refs))
171
174
  end
172
175
  yield file
173
176
  ensure
174
- synchronized do |ref_counts_file|
177
+ synchronized do |counts|
175
178
  file.close
176
- ref_counts = deserialize(File.read(ref_counts_file.path))
177
- ref_counts[path] -= 1
178
- if ref_counts[path].zero?
179
+ refs = deserialize(File.read(counts.path))
180
+ refs[path] -= 1
181
+ if refs[path].zero?
179
182
  FileUtils.rm(path, force: true)
180
- ref_counts.delete(path)
183
+ refs.delete(path)
181
184
  end
182
- File.write(ref_counts_file.path, serialize(ref_counts))
185
+ File.write(counts.path, serialize(refs))
183
186
  end
184
187
  end
185
188
 
186
189
  def synchronized
187
- ref_counts_file = File.join(Dir.tmpdir, '.futex.lock')
188
- File.open(ref_counts_file, File::CREAT | File::RDWR) do |f|
190
+ File.open(COUNTS, File::CREAT | File::RDWR) do |f|
189
191
  f.flock(File::LOCK_EX)
190
192
  yield f
191
193
  end
192
194
  end
193
195
 
194
196
  def serialize(data)
195
- # NOTE: JSON is just an example,
196
- # use whatever serialization is more appropriate here
197
197
  data.to_json
198
198
  end
199
199
 
200
200
  def deserialize(data)
201
201
  data.empty? ? {} : JSON.parse(data)
202
+ rescue JSON::ParserError
203
+ {}
202
204
  end
203
205
  end
data/test/test_futex.rb CHANGED
@@ -191,6 +191,15 @@ class FutexTest < Minitest::Test
191
191
  end
192
192
  end
193
193
 
194
+ def test_works_with_broken_counts_file
195
+ IO.write(Futex::COUNTS, 'fds')
196
+ Dir.mktmpdir do |dir|
197
+ Futex.new(File.join(dir, 'hey.txt')).open do |f|
198
+ assert(!File.exist?(f))
199
+ end
200
+ end
201
+ end
202
+
194
203
  private
195
204
 
196
205
  def hash(text)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: futex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.2
4
+ version: 0.8.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-25 00:00:00.000000000 Z
11
+ date: 2019-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -72,28 +72,28 @@ dependencies:
72
72
  requirements:
73
73
  - - '='
74
74
  - !ruby/object:Gem::Version
75
- version: 0.60.0
75
+ version: 0.62.0
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - '='
81
81
  - !ruby/object:Gem::Version
82
- version: 0.60.0
82
+ version: 0.62.0
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rubocop-rspec
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - '='
88
88
  - !ruby/object:Gem::Version
89
- version: 1.30.1
89
+ version: 1.31.0
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - '='
95
95
  - !ruby/object:Gem::Version
96
- version: 1.30.1
96
+ version: 1.31.0
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: threads
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -148,8 +148,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
148
  - !ruby/object:Gem::Version
149
149
  version: '0'
150
150
  requirements: []
151
- rubyforge_project:
152
- rubygems_version: 2.7.6
151
+ rubygems_version: 3.0.1
153
152
  signing_key:
154
153
  specification_version: 2
155
154
  summary: File-based mutex