parallizer 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile ADDED
@@ -0,0 +1,21 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ gem "bundler", ">= 1.1.5"
9
+ gem "jeweler", ">= 1.8.4"
10
+
11
+ gem "work_queue", ">= 0"
12
+
13
+ group :test do
14
+ gem "minitest", "2.12.1"
15
+ gem "minitest-extra-assertions", "0.1.0", :require => nil # guarantee it gets loaded after minitest
16
+ gem "minitest-reporters", "0.7.0"
17
+ gem "always_execute", "0.0.2", :require => nil
18
+ gem "shoulda", "2.11.3", :require => nil
19
+ gem "mocha", "0.9.8", :require => nil
20
+ end
21
+
data/Gemfile.lock ADDED
@@ -0,0 +1,43 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ always_execute (0.0.2)
5
+ shoulda
6
+ ansi (1.4.3)
7
+ builder (2.1.2)
8
+ git (1.2.5)
9
+ jeweler (1.8.4)
10
+ bundler (~> 1.0)
11
+ git (>= 1.2.5)
12
+ rake
13
+ rdoc
14
+ json (1.7.4)
15
+ minitest (2.12.1)
16
+ minitest-extra-assertions (0.1.0)
17
+ minitest-reporters (0.7.0)
18
+ ansi
19
+ builder
20
+ minitest (~> 2.0)
21
+ ruby-progressbar
22
+ mocha (0.9.8)
23
+ rake
24
+ rake (0.9.2.2)
25
+ rdoc (3.12)
26
+ json (~> 1.4)
27
+ ruby-progressbar (0.0.10)
28
+ shoulda (2.11.3)
29
+ work_queue (2.5.2)
30
+
31
+ PLATFORMS
32
+ ruby
33
+
34
+ DEPENDENCIES
35
+ always_execute (= 0.0.2)
36
+ bundler (>= 1.1.5)
37
+ jeweler (>= 1.8.4)
38
+ minitest (= 2.12.1)
39
+ minitest-extra-assertions (= 0.1.0)
40
+ minitest-reporters (= 0.7.0)
41
+ mocha (= 0.9.8)
42
+ shoulda (= 2.11.3)
43
+ work_queue
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Michael Pearce
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.md ADDED
@@ -0,0 +1,14 @@
1
+ # Parallizer - Execute your service layer in parallel.
2
+
3
+ ## Examples
4
+
5
+ # Credits
6
+
7
+ Always Execute is maintained by [Michael Pearce](http://github.com/michaelgpearce) and is funded by [BookRenter.com](http://www.bookrenter.com "BookRenter.com").
8
+
9
+ ![BookRenter.com Logo](http://assets0.bookrenter.com/images/header/bookrenter_logo.gif "BookRenter.com")
10
+
11
+ # Copyright
12
+
13
+ Copyright (c) 2012 Michael Pearce, Bookrenter.com. See LICENSE.txt for further details.
14
+
data/Rakefile ADDED
@@ -0,0 +1,36 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "parallizer"
16
+ gem.homepage = "http://github.com/michaelgpearce/parallizer"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Execute your service layer in parallel.}
19
+ gem.description = %Q{Execute your service layer in parallel.}
20
+ gem.email = "michael.pearce@bookrenter.com"
21
+ gem.authors = ["Michael Pearce"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ gem.add_runtime_dependency 'work_queue', '>= 0'
25
+ gem.add_development_dependency 'shoulda', '>= 0'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/*_test.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ task :default => :test
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/lib/parallizer.rb ADDED
@@ -0,0 +1,49 @@
1
+ require 'set'
2
+ require 'work_queue'
3
+ require 'parallizer/proxy'
4
+
5
+ class Parallizer
6
+ attr_accessor :calls, :client
7
+
8
+ def initialize(client)
9
+ self.client = client
10
+ self.calls = Set.new
11
+ end
12
+
13
+ def add_call(method_name, *args)
14
+ calls.add([method_name.to_sym, *args])
15
+ end
16
+
17
+ def execute
18
+ Parallizer.execute_all(self).first
19
+ end
20
+
21
+ def self.work_queue
22
+ # TODO: share the work queue among calling threads
23
+ Thread.current[:parallizer_work_queue] ||= WorkQueue.new(10)
24
+ end
25
+
26
+ def self.execute_all(*parallizers)
27
+ parallizers_execution_results = {}
28
+ parallizers.each do |parallizer|
29
+ execution_results = {}
30
+ parallizers_execution_results[parallizer] = execution_results
31
+
32
+ parallizer.calls.each do |name_and_args|
33
+ Parallizer.work_queue.enqueue_b do
34
+ begin
35
+ execution_results[name_and_args] = {:result => parallizer.client.send(*name_and_args)}
36
+ rescue Exception => e
37
+ execution_results[name_and_args] = {:exception => e}
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ Parallizer.work_queue.join
44
+
45
+ return parallizers_execution_results.collect do |parallizer, execution_results|
46
+ Parallizer::Proxy.new(parallizer.client, execution_results)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,33 @@
1
+ require 'set'
2
+
3
+ class Parallizer
4
+ class Proxy
5
+ def initialize(client, execution_results)
6
+ @client = client
7
+ @execution_results = execution_results
8
+ end
9
+
10
+ def method_missing(name, *args, &block)
11
+ if @execution_results.key?([name, *args])
12
+ value = @execution_results[[name, *args]]
13
+ if value[:exception]
14
+ raise value[:exception]
15
+ else
16
+ value[:result]
17
+ end
18
+ else
19
+ @client.send(*[name, *args], &block)
20
+ end
21
+ end
22
+
23
+ def respond_to?(name, include_private = false, &block)
24
+ @execution_methods ||= Set.new(@execution_results.keys.collect(&:first))
25
+
26
+ if @execution_methods.include?(name.to_sym)
27
+ true
28
+ else
29
+ super
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,63 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "parallizer"
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Michael Pearce"]
12
+ s.date = "2012-07-28"
13
+ s.description = "Execute your service layer in parallel."
14
+ s.email = "michael.pearce@bookrenter.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ "Gemfile",
21
+ "Gemfile.lock",
22
+ "LICENSE.txt",
23
+ "README.md",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/parallizer.rb",
27
+ "lib/parallizer/proxy.rb",
28
+ "parallizer.gemspec",
29
+ "test/parallizer/proxy_test.rb",
30
+ "test/parallizer_test.rb",
31
+ "test/test_helper.rb"
32
+ ]
33
+ s.homepage = "http://github.com/michaelgpearce/parallizer"
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = "1.8.24"
37
+ s.summary = "Execute your service layer in parallel."
38
+
39
+ if s.respond_to? :specification_version then
40
+ s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
+ s.add_runtime_dependency(%q<bundler>, [">= 1.1.5"])
44
+ s.add_runtime_dependency(%q<jeweler>, [">= 1.8.4"])
45
+ s.add_runtime_dependency(%q<work_queue>, [">= 0"])
46
+ s.add_runtime_dependency(%q<work_queue>, [">= 0"])
47
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
48
+ else
49
+ s.add_dependency(%q<bundler>, [">= 1.1.5"])
50
+ s.add_dependency(%q<jeweler>, [">= 1.8.4"])
51
+ s.add_dependency(%q<work_queue>, [">= 0"])
52
+ s.add_dependency(%q<work_queue>, [">= 0"])
53
+ s.add_dependency(%q<shoulda>, [">= 0"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<bundler>, [">= 1.1.5"])
57
+ s.add_dependency(%q<jeweler>, [">= 1.8.4"])
58
+ s.add_dependency(%q<work_queue>, [">= 0"])
59
+ s.add_dependency(%q<work_queue>, [">= 0"])
60
+ s.add_dependency(%q<shoulda>, [">= 0"])
61
+ end
62
+ end
63
+
@@ -0,0 +1,90 @@
1
+ require 'test_helper'
2
+
3
+ class Parallizer::ProxyTest < Test::Unit::TestCase
4
+ DEFAULT_RETURN_VALUE = "return value"
5
+
6
+ class TestObject
7
+ def a_method(arg)
8
+ DEFAULT_RETURN_VALUE
9
+ end
10
+ end
11
+
12
+ context ".method_missing" do
13
+ setup do
14
+ @client = TestObject.new
15
+ end
16
+
17
+ context "with method that exists on client" do
18
+ context "with method and arg in execute results" do
19
+ setup do
20
+ @method_result = "some value"
21
+ @method_arg = "some arg"
22
+ @execution_results = {[:a_method, @method_arg] => {:result => @method_result}}
23
+ end
24
+
25
+ should "return value from execution_results" do
26
+ assert_equal @method_result, Parallizer::Proxy.new(@client, @execution_results).a_method(@method_arg)
27
+ end
28
+
29
+ context "with exception in execute results" do
30
+ setup do
31
+ @execution_results = {[:a_method, @method_arg] => {:exception => Exception.new("an exception")}}
32
+ end
33
+
34
+ should "raise exception" do
35
+ assert_raises Exception, "an exception" do
36
+ Parallizer::Proxy.new(@client, @execution_results).a_method(@method_arg)
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ context "with method and arg not in execute result" do
43
+ setup do
44
+ @execution_results = {[:a_method, :unknown] => "unknown"}
45
+ end
46
+
47
+ should "return value from client object" do
48
+ assert_equal DEFAULT_RETURN_VALUE, Parallizer::Proxy.new(@client, @execution_results).a_method(:not_unknown)
49
+ end
50
+ end
51
+ end
52
+
53
+ context "with method that does not exist on client" do
54
+ setup do
55
+ @execution_results = {[:a_method, @method_arg] => {:result => @method_result}}
56
+ end
57
+
58
+ should "raise exception" do
59
+ assert_raises NoMethodError, "an exception" do
60
+ Parallizer::Proxy.new(@client, @execution_results).unknown_method()
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ context ".respond_to?" do
67
+ setup do
68
+ client = TestObject.new
69
+ execution_results = {[:a_method, 'valid argument'] => {:result => 'valid result'}}
70
+ @proxy = Parallizer::Proxy.new(client, execution_results)
71
+ end
72
+
73
+ should "respond to proxy method as symbol" do
74
+ assert @proxy.respond_to?(:a_method)
75
+ end
76
+
77
+ should "respond to proxy method as string" do
78
+ assert @proxy.respond_to?('a_method')
79
+ end
80
+
81
+ should "respond to methods inherited from object" do
82
+ assert @proxy.respond_to?(:to_s)
83
+ end
84
+
85
+ should "not respond to methods that do not exist" do
86
+ assert !@proxy.respond_to?(:invalid_method)
87
+ end
88
+ end
89
+
90
+ end
@@ -0,0 +1,104 @@
1
+ require 'test_helper'
2
+
3
+ class ParallizerTest < Test::Unit::TestCase
4
+ class TestObject
5
+ def a_method(arg)
6
+ Thread.current
7
+ end
8
+
9
+ def another_method
10
+ Thread.current
11
+ end
12
+ end
13
+
14
+ class AnotherTestObject
15
+ def a_method
16
+ Thread.current
17
+ end
18
+
19
+ def another_method
20
+ Thread.current
21
+ end
22
+ end
23
+
24
+ context ".add_call" do
25
+ setup do
26
+ @client = TestObject.new
27
+ @parallizer = Parallizer.new(@client)
28
+ end
29
+
30
+ execute do
31
+ @parallizer.add_call(@method, 'arg')
32
+ end
33
+
34
+ context "with string method name added" do
35
+ setup do
36
+ @method = 'a_method'
37
+ end
38
+
39
+ should "add call to calls" do
40
+ assert_equal [:a_method, 'arg'], @parallizer.calls.first
41
+ end
42
+ end
43
+
44
+ context "with symbol method name" do
45
+ setup do
46
+ @method = 'a_method'
47
+ end
48
+
49
+ should "add call to calls" do
50
+ assert_equal [:a_method, 'arg'], @parallizer.calls.first
51
+ end
52
+ end
53
+ end
54
+
55
+ context ".execute" do
56
+ setup do
57
+ @client = TestObject.new
58
+ @parallizer = Parallizer.new(@client)
59
+ end
60
+
61
+ execute do
62
+ @proxy = @parallizer.execute
63
+ end
64
+
65
+ context "with existing method on client" do
66
+ setup do
67
+ @parallizer.add_call(:a_method, 'arg')
68
+ end
69
+
70
+ should "execute method with add_call in a separate thread" do
71
+ assert_not_equal Thread.current, @proxy.a_method('arg')
72
+ end
73
+
74
+ should "execute method not added with add_call in current thread" do
75
+ assert_equal Thread.current, @proxy.another_method
76
+ end
77
+ end
78
+ end
79
+
80
+ context ".execute_all" do
81
+ setup do
82
+ @client1 = TestObject.new
83
+ @parallizer1 = Parallizer.new(@client1)
84
+ @parallizer1.add_call(:a_method, 'arg')
85
+ @client2 = AnotherTestObject.new
86
+ @parallizer2 = Parallizer.new(@client2)
87
+ @parallizer2.add_call(:a_method)
88
+ end
89
+
90
+ execute do
91
+ @proxy1, @proxy2 = Parallizer.execute_all(@parallizer1, @parallizer2)
92
+ end
93
+
94
+ should "execute methods with add_call in a separate thread" do
95
+ assert_not_equal Thread.current, @proxy1.a_method('arg')
96
+ assert_not_equal Thread.current, @proxy2.a_method
97
+ end
98
+
99
+ should "execute methods not added with add_call in current thread" do
100
+ assert_equal Thread.current, @proxy1.another_method
101
+ assert_equal Thread.current, @proxy2.another_method
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+ require 'always_execute'
13
+ require 'mocha'
14
+
15
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
16
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
17
+
18
+ require 'parallizer'
19
+
20
+ class Test::Unit::TestCase
21
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: parallizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michael Pearce
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.1.5
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.1.5
30
+ - !ruby/object:Gem::Dependency
31
+ name: jeweler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 1.8.4
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.8.4
46
+ - !ruby/object:Gem::Dependency
47
+ name: work_queue
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: work_queue
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: shoulda
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Execute your service layer in parallel.
95
+ email: michael.pearce@bookrenter.com
96
+ executables: []
97
+ extensions: []
98
+ extra_rdoc_files:
99
+ - LICENSE.txt
100
+ - README.md
101
+ files:
102
+ - Gemfile
103
+ - Gemfile.lock
104
+ - LICENSE.txt
105
+ - README.md
106
+ - Rakefile
107
+ - VERSION
108
+ - lib/parallizer.rb
109
+ - lib/parallizer/proxy.rb
110
+ - parallizer.gemspec
111
+ - test/parallizer/proxy_test.rb
112
+ - test/parallizer_test.rb
113
+ - test/test_helper.rb
114
+ homepage: http://github.com/michaelgpearce/parallizer
115
+ licenses:
116
+ - MIT
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ! '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ segments:
128
+ - 0
129
+ hash: 3246225958344608883
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ none: false
132
+ requirements:
133
+ - - ! '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 1.8.24
139
+ signing_key:
140
+ specification_version: 3
141
+ summary: Execute your service layer in parallel.
142
+ test_files: []