waitfor 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/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ === 0.0.1 2010-01-24
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,13 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.rdoc
4
+ Rakefile
5
+ lib/waitfor.rb
6
+ lib/waitfor/fixnum.rb
7
+ lib/waitfor/timer.rb
8
+ lib/waitfor/version.rb
9
+ spec/waitfor_spec.rb
10
+ spec/timer_spec.rb
11
+ spec/fixnum_spec.rb
12
+ spec/spec.opts
13
+ spec/spec_helper.rb
data/README.rdoc ADDED
@@ -0,0 +1,76 @@
1
+ = waitfor
2
+
3
+ * http://github.com/jamesbobowski/waitfor
4
+
5
+ == DESCRIPTION:
6
+
7
+ Continuously executes a Block until the Block returns true or the time limit is reached, at which point an error is raised.
8
+ Useful for strengthening tests involving asynchronous or non-blocking operations.
9
+
10
+ == FEATURES/PROBLEMS:
11
+
12
+ * Accepts arguments of second / seconds or minute / minutes
13
+ * Allows for custom RuntimeError messages
14
+
15
+ * No custom Error support yet
16
+ * Resolution of delay between Block executions is currently 1 second, not yet configurable
17
+
18
+ == SYNOPSIS:
19
+
20
+ This gem is used with asynchronous or non-blocking operations where the action of doing something is separate from whether that action succeeded.
21
+
22
+ The example below executes an asynchronous operation, then waits for up to 30 seconds for the status of the object to change by continuously checking whether it was successful.
23
+ The Block will continue to be executed until it either returns true or 30 seconds elapses. If the time limit is reached, then a RuntimeError is raised.
24
+
25
+ object.some_async_operation
26
+
27
+ WaitFor.upto 30.seconds do
28
+ object.succeeded?
29
+ end
30
+
31
+ A custom error message is specified in the example below. ( Custom errors are not yet supported. )
32
+
33
+ WaitFor.upto( 2.minutes, "Timeout after 2 minutes!" ) do
34
+ event.happened?
35
+ end
36
+
37
+ Finally, a real world example taken from a FunFX test script I wrote. Here a button is clicked and we want to wait for the new screen to appear before moving on.
38
+
39
+ button.click
40
+
41
+ WaitFor.upto 1.minute do
42
+ Check.whether( new_screen ).is :visible
43
+ end
44
+
45
+ == REQUIREMENTS:
46
+
47
+ * Ruby >= 1.8.6
48
+
49
+ == INSTALL:
50
+
51
+ sudo gem install waitfor --source http://gemcutter.org
52
+
53
+ == LICENSE:
54
+
55
+ (The MIT License)
56
+
57
+ Copyright (c) 2010 James Bobowski
58
+
59
+ Permission is hereby granted, free of charge, to any person obtaining
60
+ a copy of this software and associated documentation files (the
61
+ 'Software'), to deal in the Software without restriction, including
62
+ without limitation the rights to use, copy, modify, merge, publish,
63
+ distribute, sublicense, and/or sell copies of the Software, and to
64
+ permit persons to whom the Software is furnished to do so, subject to
65
+ the following conditions:
66
+
67
+ The above copyright notice and this permission notice shall be
68
+ included in all copies or substantial portions of the Software.
69
+
70
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
71
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
72
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
73
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
74
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
75
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
76
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ require './lib/waitfor'
6
+ require './lib/waitfor/version'
7
+
8
+ Hoe.plugin :newgem
9
+ # Generate all the Rake tasks
10
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
11
+ $hoe = Hoe.spec 'waitfor' do
12
+ self.developer 'James Bobowski', 'james.bobowski@gmail.com'
13
+ self.rubyforge_name = self.name
14
+ self.version = WaitFor::VERSION::STRING
15
+ self.description = "Continuously executes a Block until the Block returns true or the time limit is reached, at which point an error is raised"
16
+ end
17
+
18
+ require 'newgem/tasks'
19
+ Dir['tasks/**/*.rake'].each { |t| load t }
@@ -0,0 +1,14 @@
1
+ class Fixnum #:nodoc:
2
+ def second
3
+ self.seconds
4
+ end
5
+ def seconds
6
+ self
7
+ end
8
+ def minute
9
+ self.minutes
10
+ end
11
+ def minutes
12
+ self * 60
13
+ end
14
+ end
@@ -0,0 +1,13 @@
1
+ class Timer #:nodoc:
2
+ def initialize( seconds, exception_message = "Timeout Error Waiting for Event" )
3
+ @seconds = seconds
4
+ @exception_message = exception_message
5
+ @start_time = Time.now
6
+ end
7
+
8
+ def out_of_time?
9
+ out_of_time = ( Time.now - @start_time ) > @seconds
10
+ raise @exception_message if out_of_time
11
+ out_of_time
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ module WaitFor #:nodoc:
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ TINY = 1
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
data/lib/waitfor.rb ADDED
@@ -0,0 +1,16 @@
1
+ $:.unshift( File.dirname( __FILE__ ) ) unless
2
+ $:.include?( File.dirname( __FILE__ ) ) || $:.include?( File.expand_path( File.dirname( __FILE__ ) ) )
3
+
4
+ require 'waitfor/fixnum'
5
+ require 'waitfor/timer'
6
+
7
+ module WaitFor
8
+ class << self
9
+ def upto( length_of_time )
10
+ timer = Timer.new length_of_time
11
+ until yield or timer.out_of_time?
12
+ sleep 1
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,29 @@
1
+ require File.dirname( __FILE__ ) + '/spec_helper.rb'
2
+
3
+ describe Fixnum, "Convert Seconds to Seconds" do
4
+ it "should take the unit of 'second' ( singular ) with the amount of 1 and return 1" do
5
+ 1 == 1.second
6
+ end
7
+
8
+ it "should take the unit of 'seconds' ( plural ) with the amount of 1 and return 1" do
9
+ 1 == 1.seconds
10
+ end
11
+
12
+ it "should take the unit of 'seconds' with the amount of 60 and return 60" do
13
+ 60 == 60.seconds
14
+ end
15
+ end
16
+
17
+ describe Fixnum, "Convert Minutes to Seconds" do
18
+ it "should take the unit of 'minute' ( singular ) with the amount of 1 and return 60" do
19
+ 60 == 1.minute
20
+ end
21
+
22
+ it "should take the unit of 'minutes' ( plural ) with the amount of 1 and return 60" do
23
+ 60 == 1.minutes
24
+ end
25
+
26
+ it "should take the unit of 'minutes' with the amount of 5 and return 300" do
27
+ 300 == 5.minutes
28
+ end
29
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --colour
@@ -0,0 +1,18 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
5
+ gem 'rspec'
6
+ require 'spec'
7
+ end
8
+
9
+ $:.unshift( File.dirname( __FILE__ ) + '/../lib' )
10
+ require File.dirname( __FILE__ ) + '/../lib/waitfor.rb'
11
+ require File.dirname( __FILE__ ) + '/../lib/waitfor/fixnum.rb'
12
+ require File.dirname( __FILE__ ) + '/../lib/waitfor/timer.rb'
13
+
14
+ def timer
15
+ start_time = Time.now
16
+ yield
17
+ Time.now - start_time
18
+ end
@@ -0,0 +1,25 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe Timer do
4
+ it "should not raise an error when set for two seconds and checked after one second" do
5
+ timer = Timer.new 2.seconds
6
+ sleep 1
7
+ timer.out_of_time?.should be( false )
8
+ end
9
+
10
+ it "should raise an error when set for two seconds and checked after one second" do
11
+ timer = Timer.new 1.seconds
12
+ sleep 2
13
+ lambda {
14
+ timer.out_of_time?.should be( true )
15
+ }.should raise_error( RuntimeError, "Timeout Error Waiting for Event" )
16
+ end
17
+
18
+ it "should raise a custom error when set for one second and checked after two second" do
19
+ timer = Timer.new( 1.seconds, "Custom Error" )
20
+ sleep 2
21
+ lambda {
22
+ timer.out_of_time?.should be( true )
23
+ }.should raise_error( RuntimeError, "Custom Error" )
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ require File.dirname( __FILE__ ) + '/spec_helper.rb'
2
+
3
+ describe WaitFor do
4
+ it "should pass in under one second if block returns true immediately" do
5
+ elapsed_time = timer {
6
+ WaitFor.upto 1.second do
7
+ true
8
+ end
9
+ }
10
+
11
+ elapsed_time.should be < 1.second
12
+ end
13
+
14
+ it "should take at least two seconds before raising an error" do
15
+ elapsed_time = timer {
16
+ lambda {
17
+ WaitFor.upto 2.seconds do
18
+ false
19
+ end
20
+ }.should raise_error
21
+ }
22
+
23
+ elapsed_time.should be_close( 2, 0.25 )
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: waitfor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - James Bobowski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-24 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.3.3
24
+ version:
25
+ description: Continuously executes a Block until the Block returns true or the time limit is reached, at which point an error is raised
26
+ email:
27
+ - james.bobowski@gmail.com
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - History.txt
34
+ - Manifest.txt
35
+ files:
36
+ - History.txt
37
+ - Manifest.txt
38
+ - README.rdoc
39
+ - Rakefile
40
+ - lib/waitfor.rb
41
+ - lib/waitfor/fixnum.rb
42
+ - lib/waitfor/timer.rb
43
+ - lib/waitfor/version.rb
44
+ - spec/waitfor_spec.rb
45
+ - spec/timer_spec.rb
46
+ - spec/fixnum_spec.rb
47
+ - spec/spec.opts
48
+ - spec/spec_helper.rb
49
+ has_rdoc: true
50
+ homepage: http://github.com/jamesbobowski/waitfor
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options:
55
+ - --main
56
+ - README.rdoc
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ version:
71
+ requirements: []
72
+
73
+ rubyforge_project: waitfor
74
+ rubygems_version: 1.3.5
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Generic 'wait for' method
78
+ test_files: []
79
+