thread_so_safe 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +22 -0
- data/LICENSE +20 -0
- data/README.textile +30 -0
- data/Rakefile +45 -0
- data/lib/thread_so_safe.rb +62 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/thread_so_safe_spec.rb +93 -0
- data/thread_so_safe.gemspec +52 -0
- metadata +84 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Dane Harrigan
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.textile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
h1. thread_so_safe
|
2
|
+
|
3
|
+
thread_so_safe is a very simple gem to help keep multi-threaded environments synced.
|
4
|
+
|
5
|
+
h2. Examples
|
6
|
+
|
7
|
+
<code><pre>
|
8
|
+
# Starting thread-safety is easy!
|
9
|
+
ThreadSoSafe.safeguard('My.Application')
|
10
|
+
ThreadSoSafe.safeguard('My.Second.Application')
|
11
|
+
|
12
|
+
# Time to check if our data is till up to date
|
13
|
+
unless ThreadSoSafe.safe?('My.Application')
|
14
|
+
puts "Updating data..."
|
15
|
+
ThreadSoSafe.update!('My.Application')
|
16
|
+
end
|
17
|
+
|
18
|
+
unless ThreadSoSafe.safe?('My.Second.Application')
|
19
|
+
# update second set of data
|
20
|
+
ThreadSoSafe.update!('My.Second.Application')
|
21
|
+
end
|
22
|
+
|
23
|
+
# If you're working with only one application you can use the slightly shorter-hand
|
24
|
+
unless ThreadSoSafe.safe?
|
25
|
+
# bring your code up to date
|
26
|
+
ThreadSoSafe.update!
|
27
|
+
end
|
28
|
+
</pre></code>
|
29
|
+
|
30
|
+
Copyright (c) 2010 Dane Harrigan. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'spec/rake/spectask'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'jeweler'
|
7
|
+
Jeweler::Tasks.new do |gem|
|
8
|
+
gem.name = "thread_so_safe"
|
9
|
+
gem.version = "0.1"
|
10
|
+
gem.summary = %Q{thread_so_safe is a very simple gem to help keep multi-threaded environments synced.}
|
11
|
+
gem.email = "dane.harrigan@gmail.com"
|
12
|
+
gem.homepage = "http://github.com/daneharrigan/thread_so_safe"
|
13
|
+
gem.authors = ["Dane Harrigan"]
|
14
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
22
|
+
spec.libs << 'lib' << 'spec'
|
23
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
24
|
+
end
|
25
|
+
|
26
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
27
|
+
spec.libs << 'lib' << 'spec'
|
28
|
+
spec.pattern = 'spec/*_spec.rb'
|
29
|
+
spec.rcov = true
|
30
|
+
spec.rcov_opts = ['--exclude', 'gem,spec']
|
31
|
+
end
|
32
|
+
|
33
|
+
task :spec => :check_dependencies
|
34
|
+
|
35
|
+
task :default => :spec
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "thread_so_safe #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'digest/md5'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
class ThreadSoSafe
|
5
|
+
@@threads = {}
|
6
|
+
@@current_thread = nil
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def safeguard(name)
|
10
|
+
@@current_thread = name
|
11
|
+
name = file_name(name)
|
12
|
+
|
13
|
+
file = File.new(full_path(name),'w+')
|
14
|
+
@@threads[name] = file.mtime
|
15
|
+
|
16
|
+
file.close
|
17
|
+
return
|
18
|
+
end
|
19
|
+
|
20
|
+
def safe?(name=@@current_thread)
|
21
|
+
@@current_thread = name
|
22
|
+
name = file_name(name)
|
23
|
+
|
24
|
+
@@threads[name] == File.mtime("#{full_path name}")
|
25
|
+
end
|
26
|
+
|
27
|
+
def update!(name=@@current_thread)
|
28
|
+
name = file_name(name)
|
29
|
+
file = full_path(name)
|
30
|
+
|
31
|
+
FileUtils.touch(file) if @@threads[name] == File.mtime(file)
|
32
|
+
@@threads[name] = File.mtime(file)
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
def file_name(name)
|
37
|
+
Digest::MD5.hexdigest(name)
|
38
|
+
end
|
39
|
+
|
40
|
+
def full_path(name)
|
41
|
+
"#{directory}/#{name}"
|
42
|
+
end
|
43
|
+
|
44
|
+
def directory
|
45
|
+
use_default_directory? ? default_directory : gem_directory
|
46
|
+
end
|
47
|
+
|
48
|
+
def use_default_directory?
|
49
|
+
File.writable?(default_directory)
|
50
|
+
end
|
51
|
+
|
52
|
+
#:nordoc:
|
53
|
+
def default_directory
|
54
|
+
'/tmp/thread_so_safe'
|
55
|
+
end
|
56
|
+
|
57
|
+
#:nordoc:
|
58
|
+
def gem_directory
|
59
|
+
File.expand_path(File.dirname(__FILE__)+'/../tmp')
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ThreadSoSafe do
|
4
|
+
before(:all) do
|
5
|
+
@app_name = 'My.Application'
|
6
|
+
@default_directory = '/tmp/thread_so_safe'
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should be safe without passing the application name" do
|
10
|
+
ThreadSoSafe.safeguard(@app_name)
|
11
|
+
ThreadSoSafe.safe?.should == true
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should be safe with the application name passed" do
|
15
|
+
ThreadSoSafe.safeguard(@app_name)
|
16
|
+
ThreadSoSafe.safe?(@app_name).should == true
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should encode the application name with md5" do
|
20
|
+
ThreadSoSafe.send(:file_name,@app_name).should == Digest::MD5.hexdigest(@app_name)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should return /tmp/thread_so_safe as the default directory" do
|
24
|
+
ThreadSoSafe.send(:default_directory).should == @default_directory
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should return <gem-path>/tmp as the default directory" do
|
28
|
+
path_from_class = File.expand_path ThreadSoSafe.send(:gem_directory)
|
29
|
+
path_from_test = File.expand_path(File.dirname(__FILE__) + '/../tmp')
|
30
|
+
path_from_class.should == path_from_test
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should return default directory" do
|
34
|
+
ThreadSoSafe.send(:directory).should == @default_directory
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should return the full path to the /tmp file" do
|
38
|
+
file_name = ThreadSoSafe.send(:file_name, @app_name)
|
39
|
+
ThreadSoSafe.send(:full_path, file_name).should == "#{@default_directory}/#{file_name}"
|
40
|
+
end
|
41
|
+
|
42
|
+
context "when another thread updates the thread-safe token" do
|
43
|
+
before(:each) do
|
44
|
+
ThreadSoSafe.safeguard(@app_name)
|
45
|
+
|
46
|
+
file_name = ThreadSoSafe.send(:file_name, @app_name)
|
47
|
+
full_path = ThreadSoSafe.send(:full_path, file_name)
|
48
|
+
|
49
|
+
File.stub!(:mtime).and_return(Time.now+100)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should not be safe without passing the application name" do
|
53
|
+
ThreadSoSafe.safe?.should == false
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should be safe with the application name passed" do
|
57
|
+
ThreadSoSafe.safe?(@app_name).should == false
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context "when updating the thread-safe token" do
|
62
|
+
before(:each) do
|
63
|
+
ThreadSoSafe.safeguard(@app_name)
|
64
|
+
file_name = ThreadSoSafe.send(:file_name, @app_name)
|
65
|
+
@full_path = ThreadSoSafe.send(:full_path, file_name)
|
66
|
+
|
67
|
+
@mtime = File.mtime @full_path
|
68
|
+
File.stub!(:mtime).and_return(Time.now+100)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should update the mtime on the /tmp file with the application name passed" do
|
72
|
+
ThreadSoSafe.update!(@app_name)
|
73
|
+
File.mtime(@full_path).should_not == @mtime
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should update the mtime on the /tmp file without the application name passed" do
|
77
|
+
ThreadSoSafe.update!
|
78
|
+
File.mtime(@full_path).should_not == @mtime
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
context "when the /tmp directory is not available or writable" do
|
83
|
+
before(:each) do
|
84
|
+
ThreadSoSafe.stub!(:use_default_directory?).and_return(false)
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should return gem directory" do
|
88
|
+
path_from_class = ThreadSoSafe.send(:directory)
|
89
|
+
path_from_test = File.expand_path(File.dirname(__FILE__) + '/../tmp')
|
90
|
+
path_from_class.should == path_from_test
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{thread_so_safe}
|
8
|
+
s.version = "0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Dane Harrigan"]
|
12
|
+
s.date = %q{2010-06-27}
|
13
|
+
s.email = %q{dane.harrigan@gmail.com}
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"LICENSE",
|
16
|
+
"README.textile"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".document",
|
20
|
+
".gitignore",
|
21
|
+
"LICENSE",
|
22
|
+
"README.textile",
|
23
|
+
"Rakefile",
|
24
|
+
"lib/thread_so_safe.rb",
|
25
|
+
"spec/spec_helper.rb",
|
26
|
+
"spec/thread_so_safe_spec.rb",
|
27
|
+
"thread_so_safe.gemspec"
|
28
|
+
]
|
29
|
+
s.homepage = %q{http://github.com/daneharrigan/thread_so_safe}
|
30
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
31
|
+
s.require_paths = ["lib"]
|
32
|
+
s.rubygems_version = %q{1.3.6}
|
33
|
+
s.summary = %q{thread_so_safe is a very simple gem to help keep multi-threaded environments synced.}
|
34
|
+
s.test_files = [
|
35
|
+
"spec/spec_helper.rb",
|
36
|
+
"spec/thread_so_safe_spec.rb"
|
37
|
+
]
|
38
|
+
|
39
|
+
if s.respond_to? :specification_version then
|
40
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
41
|
+
s.specification_version = 3
|
42
|
+
|
43
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
44
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
45
|
+
else
|
46
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
47
|
+
end
|
48
|
+
else
|
49
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: thread_so_safe
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
version: "0.1"
|
9
|
+
platform: ruby
|
10
|
+
authors:
|
11
|
+
- Dane Harrigan
|
12
|
+
autorequire:
|
13
|
+
bindir: bin
|
14
|
+
cert_chain: []
|
15
|
+
|
16
|
+
date: 2010-06-27 00:00:00 -04:00
|
17
|
+
default_executable:
|
18
|
+
dependencies:
|
19
|
+
- !ruby/object:Gem::Dependency
|
20
|
+
name: rspec
|
21
|
+
prerelease: false
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
segments:
|
27
|
+
- 1
|
28
|
+
- 2
|
29
|
+
- 9
|
30
|
+
version: 1.2.9
|
31
|
+
type: :development
|
32
|
+
version_requirements: *id001
|
33
|
+
description:
|
34
|
+
email: dane.harrigan@gmail.com
|
35
|
+
executables: []
|
36
|
+
|
37
|
+
extensions: []
|
38
|
+
|
39
|
+
extra_rdoc_files:
|
40
|
+
- LICENSE
|
41
|
+
- README.textile
|
42
|
+
files:
|
43
|
+
- .document
|
44
|
+
- .gitignore
|
45
|
+
- LICENSE
|
46
|
+
- README.textile
|
47
|
+
- Rakefile
|
48
|
+
- lib/thread_so_safe.rb
|
49
|
+
- spec/spec_helper.rb
|
50
|
+
- spec/thread_so_safe_spec.rb
|
51
|
+
- thread_so_safe.gemspec
|
52
|
+
has_rdoc: true
|
53
|
+
homepage: http://github.com/daneharrigan/thread_so_safe
|
54
|
+
licenses: []
|
55
|
+
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options:
|
58
|
+
- --charset=UTF-8
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
segments:
|
66
|
+
- 0
|
67
|
+
version: "0"
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
requirements: []
|
76
|
+
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 1.3.6
|
79
|
+
signing_key:
|
80
|
+
specification_version: 3
|
81
|
+
summary: thread_so_safe is a very simple gem to help keep multi-threaded environments synced.
|
82
|
+
test_files:
|
83
|
+
- spec/spec_helper.rb
|
84
|
+
- spec/thread_so_safe_spec.rb
|