futex 0.4.0 → 0.4.1

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: e19f93d6754a7535a3a7824b2ba55aa6fb8fcf00e63b04011af936865e26895f
4
- data.tar.gz: da61f106a45c5da30d76891f88f3adf389c1610c852604326b3d182eadfe1215
3
+ metadata.gz: 6fe725623e9222ab65747891baf33b18b02aa14b4b9839386b14b9b40689d677
4
+ data.tar.gz: a56328499e4fe088514b7c4bd6c8cd79165c58f7e18ce40e12db9997e9ac580b
5
5
  SHA512:
6
- metadata.gz: d1adb27a04e721ff4ba1356afd44e19731b7193938b4fba03805eab9722c1b986a20bd4e5f0343de3f685e71b95b0d4e94f34213a49007839fc4243090cc87a0
7
- data.tar.gz: 29748f308f7790b67d3e33cf1827bcaea200a2b2a9f7aa977db77f08e289176fc5f65b9927ef5f88309559f59886a414bc12a0988cc0a40a236ba52edc674cc5
6
+ metadata.gz: 8bd6f3ced41b81b3e91489e09c7e796e45b6633181d520d3cf0912f7a69c28568ab3320a3007eb58210e00eb1396a72966f79050fb7ce09a13accde44d8ab2b6
7
+ data.tar.gz: '091d37893523574dbb618c6ee55e9721b1e042732e87a318bd50ae2613b3dd0252aedf8ee8ecd7e48c2a24a4f0ce6b7d44edbe93cbd66a77d7041cfd90424bcd'
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.4.0'
34
+ s.version = '0.4.1'
35
35
  s.license = 'MIT'
36
36
  s.summary = 'File-based mutex'
37
37
  s.description = 'Ruby Gem for file-based locking'
data/lib/futex.rb CHANGED
@@ -77,28 +77,33 @@ class Futex
77
77
  sleep(@sleep)
78
78
  cycle += 1
79
79
  if Time.now - start > @timeout
80
- raise "##{Process.pid}/#{Thread.current.name} can't get \
81
- exclusive access to the file #{@path} \
80
+ raise "#{badge} can't get exclusive access to the file #{@path} \
82
81
  because of the lock at #{@lock}, after #{age(start)} \
83
82
  of waiting: #{IO.read(@lock)}"
84
83
  end
85
84
  if (cycle % step).zero? && Time.now - start > @timeout / 2
86
- debug("##{Process.pid}/#{Thread.current.name} still waiting for \
85
+ debug("#{badge} still waiting for \
87
86
  exclusive access to #{@path}, #{age(start)} already: #{IO.read(@lock)}")
88
87
  end
89
88
  end
90
- debug("Locked by \"#{Thread.current.name}\" in #{age(start)}: #{@path} \
89
+ debug("Locked by #{badge} in #{age(start)}: #{@path} \
91
90
  (attempt no.#{cycle})")
92
- File.write(@lock, "##{Process.pid}/#{Thread.current.name}")
91
+ File.write(@lock, badge)
93
92
  acq = Time.now
94
93
  res = yield(@path)
95
- debug("Unlocked by \"#{Thread.current.name}\" in #{age(acq)}: #{@path}")
94
+ debug("Unlocked by #{badge} in #{age(acq)}: #{@path}")
96
95
  res
97
96
  end
98
97
  end
99
98
 
100
99
  private
101
100
 
101
+ def badge
102
+ tname = Thread.current.name
103
+ tname = 'nil' if tname.nil?
104
+ "##{Process.pid}/#{tname}"
105
+ end
106
+
102
107
  def age(time)
103
108
  "#{((Time.now - time) * 1000).round}ms"
104
109
  end
data/test/test_futex.rb CHANGED
@@ -61,6 +61,24 @@ class FutexTest < Minitest::Test
61
61
  end
62
62
  end
63
63
 
64
+ def test_raises_if_cant_lock
65
+ Dir.mktmpdir do |dir|
66
+ path = File.join(dir, 'the/simple/file.txt')
67
+ Thread.start do
68
+ Futex.new(path).open do
69
+ sleep 10
70
+ end
71
+ end
72
+ sleep 0.1
73
+ ex = assert_raises do
74
+ Futex.new(path, timeout: 0.1).open do |f|
75
+ # Will never reach this point
76
+ end
77
+ end
78
+ assert(ex.message.include?('can\'t get exclusive access to the file'), ex)
79
+ end
80
+ end
81
+
64
82
  def test_non_exclusive_locking
65
83
  Dir.mktmpdir do |dir|
66
84
  path = File.join(dir, 'g/e/f/file.txt')
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.4.0
4
+ version: 0.4.1
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-11-15 00:00:00.000000000 Z
11
+ date: 2018-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest