concurrent-polling 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 06d620fe5303099e5e33bb1c5f0cc9aa63db4887
4
+ data.tar.gz: a55dca6423e435f84a7fd0ca04f65a728a2df9f9
5
+ SHA512:
6
+ metadata.gz: e88c768dba9788757b6fe18eb7073243f99dff8332f2d3d45689234dae888c16711448dec5d3ea7fd68d8009ea27b4f05137653e39cc8bb99eca74af36f4da4b
7
+ data.tar.gz: c7599a9a65899afa9dbe782216eb32fb586d6ba773cedf7f01572ab9985101476445c03506c7f92b71398f4ecc245c06977c6cf79e8906f8617ed25c863adf65
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --warnings
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.1
4
+ - 2.0.0
5
+ - jruby-head
6
+ deploy:
7
+ provider: rubygems
8
+ api_key:
9
+ secure: Dd0l16iSFErUc9ClkPfStyqMKFY1PpA1qX5+59kTwF6+3srgNiDLzlLnxxAj7ESNS5JCRSbt0URWIotRApNTz0qAdPww5radc+9y6iDtRU7OGh1N3aFJZDtYv+0gs0dzm5tQjrPBwePQOLnzN5x/3iHv9Ei1avk3cqQwoWPrhIg=
10
+ gem: concurrent-polling
11
+ on:
12
+ tags: false
13
+ repo: falti/concurrent-polling
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in concurrent-polling.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Frank Falkenberg
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Concurrent::Polling
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'concurrent-polling'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install concurrent-polling
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/concurrent-polling/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'concurrent/polling/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "concurrent-polling"
8
+ spec.version = Concurrent::Polling::VERSION
9
+ spec.authors = ["Frank Falkenberg"]
10
+ spec.email = ["faltibrain@gmail.com"]
11
+ spec.summary = %q{Concurrency based polling.}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "concurrent-ruby", '~> 0.6.1'
21
+ spec.add_dependency "values", '~> 1.5'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ spec.add_development_dependency "rake"
26
+ end
@@ -0,0 +1,70 @@
1
+ require "concurrent"
2
+ require "concurrent/polling/version"
3
+ require "values"
4
+
5
+ module Concurrent
6
+
7
+ module Polling
8
+ DEFAULT_OPTIONS = {
9
+ retries: 1,
10
+ success_condition: lambda {|polled_value| true },
11
+ poll: lambda { :default_options_polling_result }
12
+ }
13
+
14
+
15
+ TimeoutError = Class.new(StandardError)
16
+ ConditionError = Class.new(StandardError)
17
+
18
+ class TempResult < Value.new(:ok, :retries, :reason); end
19
+ class FinalResult < Value.new(:ok, :reason); end
20
+
21
+ class Solver
22
+
23
+ def initialize(poll_function, check_function)
24
+ @poll_function = poll_function
25
+ @check_function = check_function
26
+ end
27
+
28
+ def solve(result)
29
+
30
+ condition_met = begin
31
+ @check_function.call(result.ok)
32
+ rescue Exception => e
33
+ raise ConditionError.new(e)
34
+ end
35
+
36
+ if(condition_met)
37
+ result
38
+ elsif result.retries == 0
39
+ raise TimeoutError.new
40
+ else
41
+
42
+ future = Concurrent::Future.new(&@poll_function).execute
43
+
44
+ future_result = future.value # blocking
45
+
46
+ solve(TempResult.new(future_result, (result.retries - 1) , nil))
47
+ end
48
+ end
49
+ end
50
+
51
+ def self.poll(options = {})
52
+
53
+ opts = DEFAULT_OPTIONS.merge(options)
54
+
55
+ success_condition = opts[:success_condition]
56
+ poll = opts[:poll]
57
+
58
+
59
+ retries = opts[:retries]
60
+
61
+ solver = Solver.new(poll, success_condition )
62
+ promise = Promise.new{ solver.solve(TempResult.new(poll.call(), retries, nil)) }
63
+ .rescue { |reason| FinalResult.with(ok: nil, reason: reason)}
64
+ .then { |result| FinalResult.new(result.ok, result.reason) }
65
+
66
+ promise.execute
67
+ promise.value
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,5 @@
1
+ module Concurrent
2
+ module Polling
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,90 @@
1
+
2
+ RSpec.describe Concurrent::Polling do
3
+
4
+ it "should poll with default options" do
5
+ polling_result = Concurrent::Polling.poll()
6
+ expect(polling_result.ok).to eq(:default_options_polling_result)
7
+ end
8
+
9
+ it "should handle exceptions on the poll proc" do
10
+ polling_result = Concurrent::Polling.poll(poll: lambda{ raise "polling boom" })
11
+
12
+ expect(polling_result.ok).to be_nil
13
+ expect(polling_result.reason).to be_a RuntimeError
14
+ end
15
+
16
+
17
+
18
+ it "should handle expection on the success_condition proc" do
19
+ polling_result = Concurrent::Polling.poll(success_condition: lambda{|result| raise "some error" })
20
+ expect(polling_result.ok).to be_nil
21
+ expect(polling_result.reason).to be_a Concurrent::Polling::ConditionError
22
+ end
23
+
24
+ it "should run into timeout" do
25
+ polling_result = Concurrent::Polling.poll(success_condition: lambda{|result| false })
26
+
27
+
28
+ expect(polling_result.ok).to be_nil
29
+ expect(polling_result.reason).to be_a Concurrent::Polling::TimeoutError
30
+
31
+ end
32
+
33
+ it "should work with custom options" do
34
+
35
+ polling = Concurrent::Polling.poll(
36
+ retries: 10,
37
+ execution_interval: 0.1,
38
+ timeout_interval: 0.01,
39
+ success_condition: lambda {|result| result == "success" },
40
+ poll: Proc.new do
41
+ "success"
42
+ end
43
+ )
44
+
45
+ expect(polling.ok).to eq("success")
46
+
47
+ end
48
+
49
+ describe "Stress Test" do
50
+
51
+ it "should work with multiple polls" do
52
+ puts "running the multi test"
53
+ futures = (1..10000).map do |i|
54
+
55
+ f = Concurrent::Future.new do
56
+ polling = Concurrent::Polling.poll(
57
+ retries: 10,
58
+ success_condition: lambda {|result| true },
59
+ poll: Proc.new do
60
+ "success"
61
+ end
62
+ )
63
+
64
+ polling
65
+
66
+ #expect(polling.ok).to eq("success#{i}")
67
+ end
68
+
69
+ f.execute
70
+ f.value
71
+ end
72
+
73
+
74
+ sleep(1)
75
+
76
+ futures.each do |f|
77
+ expect(f.ok).to be_truthy
78
+ expect(f.ok).to eq("success")
79
+
80
+
81
+ end
82
+
83
+
84
+
85
+ end
86
+ end
87
+
88
+
89
+
90
+ end
@@ -0,0 +1,91 @@
1
+ $VERBOSE=nil
2
+ $LOAD_PATH.unshift File.expand_path("../lib",__FILE__)
3
+ require 'concurrent/polling'
4
+ # This file was generated by the `rspec --init` command. Conventionally, all
5
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
6
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
7
+ # file to always be loaded, without a need to explicitly require it in any files.
8
+ #
9
+ # Given that it is always loaded, you are encouraged to keep this file as
10
+ # light-weight as possible. Requiring heavyweight dependencies from this file
11
+ # will add to the boot time of your test suite on EVERY test run, even for an
12
+ # individual file that may not need all of that loaded. Instead, make a
13
+ # separate helper file that requires this one and then use it only in the specs
14
+ # that actually need it.
15
+ #
16
+ # The `.rspec` file also contains a few flags that are not defaults but that
17
+ # users commonly want.
18
+ #
19
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
20
+
21
+ RSpec.configure do |config|
22
+ # The settings below are suggested to provide a good initial experience
23
+ # with RSpec, but feel free to customize to your heart's content.
24
+ =begin
25
+ # These two settings work together to allow you to limit a spec run
26
+ # to individual examples or groups you care about by tagging them with
27
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
28
+ # get run.
29
+ config.filter_run :focus
30
+ config.run_all_when_everything_filtered = true
31
+
32
+ # Many RSpec users commonly either run the entire suite or an individual
33
+ # file, and it's useful to allow more verbose output when running an
34
+ # individual spec file.
35
+ if config.files_to_run.one?
36
+ # Use the documentation formatter for detailed output,
37
+ # unless a formatter has already been configured
38
+ # (e.g. via a command-line flag).
39
+ config.default_formatter = 'doc'
40
+ end
41
+
42
+ # Print the 10 slowest examples and example groups at the
43
+ # end of the spec run, to help surface which specs are running
44
+ # particularly slow.
45
+ config.profile_examples = 10
46
+
47
+ # Run specs in random order to surface order dependencies. If you find an
48
+ # order dependency and want to debug it, you can fix the order by providing
49
+ # the seed, which is printed after each run.
50
+ # --seed 1234
51
+ config.order = :random
52
+
53
+ # Seed global randomization in this process using the `--seed` CLI option.
54
+ # Setting this allows you to use `--seed` to deterministically reproduce
55
+ # test failures related to randomization by passing the same `--seed` value
56
+ # as the one that triggered the failure.
57
+ Kernel.srand config.seed
58
+
59
+ # rspec-expectations config goes here. You can use an alternate
60
+ # assertion/expectation library such as wrong or the stdlib/minitest
61
+ # assertions if you prefer.
62
+ config.expect_with :rspec do |expectations|
63
+ # Enable only the newer, non-monkey-patching expect syntax.
64
+ # For more details, see:
65
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
66
+ expectations.syntax = :expect
67
+ end
68
+
69
+ # rspec-mocks config goes here. You can use an alternate test double
70
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
71
+ config.mock_with :rspec do |mocks|
72
+ # Enable only the newer, non-monkey-patching expect syntax.
73
+ # For more details, see:
74
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
75
+ mocks.syntax = :expect
76
+
77
+ # Prevents you from mocking or stubbing a method that does not exist on
78
+ # a real object. This is generally recommended.
79
+ mocks.verify_partial_doubles = true
80
+ end
81
+ =end
82
+
83
+ config.expect_with :rspec do |expectations|
84
+ expectations.syntax = :expect
85
+ end
86
+
87
+ config.mock_with :rspec do |mocks|
88
+ mocks.syntax = :expect
89
+ mocks.verify_partial_doubles = true
90
+ end
91
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: concurrent-polling
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Frank Falkenberg
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: concurrent-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.6.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.6.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: values
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - faltibrain@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - concurrent-polling.gemspec
98
+ - lib/concurrent/polling.rb
99
+ - lib/concurrent/polling/version.rb
100
+ - spec/polling_spec.rb
101
+ - spec/spec_helper.rb
102
+ homepage: ''
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.2.2
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Concurrency based polling.
126
+ test_files:
127
+ - spec/polling_spec.rb
128
+ - spec/spec_helper.rb