em-scenario 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +109 -2
- data/lib/scenario.rb +57 -5
- metadata +8 -8
data/README.md
CHANGED
@@ -10,16 +10,123 @@ Ruby 1.9.2 is used, it may work with ruby 1.8.x
|
|
10
10
|
Tools
|
11
11
|
-----
|
12
12
|
|
13
|
-
The specs provide nice example of usage.
|
14
|
-
|
15
13
|
### Quorum
|
16
14
|
|
17
15
|
Do something when n actions are done.
|
18
16
|
|
17
|
+
```ruby
|
18
|
+
EM.run do
|
19
|
+
quorum(5) do |nextStep|
|
20
|
+
5.times do |i|
|
21
|
+
EM.add_timer(Random.rand(0.1)) do
|
22
|
+
nextStep.call
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end.finally do
|
26
|
+
assert true
|
27
|
+
EM.stop
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
```
|
32
|
+
|
33
|
+
### AdNauseum
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
EM.run do
|
37
|
+
cpt = 0
|
38
|
+
adnauseum do |nextStep|
|
39
|
+
EM.add_timer(Random.rand(0.1)) do
|
40
|
+
cpt += 1
|
41
|
+
nextStep.call
|
42
|
+
end
|
43
|
+
end.until do
|
44
|
+
cpt > 5
|
45
|
+
end.finally do
|
46
|
+
assert true
|
47
|
+
EM.stop
|
48
|
+
end
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
19
52
|
### AbInitio
|
20
53
|
|
21
54
|
Act sequentialy, from the start.
|
22
55
|
|
56
|
+
```ruby
|
57
|
+
EM.run do
|
58
|
+
txt = ""
|
59
|
+
abinitio do |sequence|
|
60
|
+
sequence.then do |nextStep|
|
61
|
+
EM.add_timer(Random.rand(0.1)) do
|
62
|
+
txt = "Hello "
|
63
|
+
nextStep.call
|
64
|
+
end
|
65
|
+
end.then do |nextStep|
|
66
|
+
EM.add_timer(Random.rand(0.1)) do
|
67
|
+
txt += "World"
|
68
|
+
nextStep.call
|
69
|
+
end
|
70
|
+
end.then do |nextStep|
|
71
|
+
EM.add_timer(Random.rand(0.1)) do
|
72
|
+
txt.upcase!
|
73
|
+
nextStep.call
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end.finally do
|
77
|
+
assert "HELLO WORLD" == txt
|
78
|
+
EM.stop
|
79
|
+
end
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
23
83
|
### AdLib
|
24
84
|
|
25
85
|
Repeat an action, seqentillay.
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
EM.run do
|
89
|
+
stack = []
|
90
|
+
adlib(5) do |nextStep, i|
|
91
|
+
stack << i
|
92
|
+
EM.add_timer(Random.rand(0.1)) do
|
93
|
+
nextStep.call
|
94
|
+
end
|
95
|
+
end.finally do
|
96
|
+
assert true
|
97
|
+
assert [0,1,2,3,4] == stack
|
98
|
+
EM.stop
|
99
|
+
end
|
100
|
+
end
|
101
|
+
```
|
102
|
+
|
103
|
+
### QuantumSatis
|
104
|
+
|
105
|
+
Not so many parallel actions
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
EM.run do
|
109
|
+
stack = []
|
110
|
+
quantumsatis(5, 2) do |nextStep, i, workers|
|
111
|
+
assert workers <= 2
|
112
|
+
EM.add_timer(Random.rand(0.1)) do
|
113
|
+
stack << i
|
114
|
+
nextStep.call
|
115
|
+
end
|
116
|
+
end.finally do
|
117
|
+
assert (0..4).to_a == stack.sort
|
118
|
+
EM.stop
|
119
|
+
end
|
120
|
+
end
|
121
|
+
```
|
122
|
+
|
123
|
+
Todo
|
124
|
+
----
|
125
|
+
|
126
|
+
* Chain all commands
|
127
|
+
* Alias in plain old english
|
128
|
+
|
129
|
+
Licence
|
130
|
+
-------
|
131
|
+
|
132
|
+
Released under the LGPL license.
|
data/lib/scenario.rb
CHANGED
@@ -4,6 +4,10 @@ module EventMachine
|
|
4
4
|
# @see http://en.wikipedia.org/wiki/List_of_Latin_phrases
|
5
5
|
module Scenario
|
6
6
|
|
7
|
+
class Scenario
|
8
|
+
|
9
|
+
end
|
10
|
+
|
7
11
|
# from the start
|
8
12
|
class AbInitio
|
9
13
|
include EM::Deferrable
|
@@ -45,7 +49,7 @@ module EventMachine
|
|
45
49
|
end
|
46
50
|
|
47
51
|
#Trigger when a quota of actions is done
|
48
|
-
class Quorum
|
52
|
+
class Quorum < Scenario
|
49
53
|
include EM::Deferrable
|
50
54
|
|
51
55
|
def initialize times, &block
|
@@ -61,13 +65,56 @@ module EventMachine
|
|
61
65
|
|
62
66
|
protected
|
63
67
|
def nextStep
|
64
|
-
|
65
|
-
|
68
|
+
@times -= 1
|
69
|
+
self.succeed(self) if @times == 0
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# As much as enough.
|
74
|
+
# You wont lots of parralel workers, but not too much.
|
75
|
+
class QuantumSatis
|
76
|
+
include EM::Deferrable
|
77
|
+
|
78
|
+
def initialize times, throttle=nil, &block
|
79
|
+
@opened = 0
|
80
|
+
@finished = 0
|
81
|
+
@worker = 0
|
82
|
+
@times = times
|
83
|
+
@throttle = throttle
|
84
|
+
@loop = block
|
85
|
+
@debug = false
|
86
|
+
end
|
87
|
+
|
88
|
+
def finally &block
|
89
|
+
self.callback &block
|
90
|
+
if @throttle
|
91
|
+
@throttle.times{ call }
|
66
92
|
else
|
67
|
-
@times
|
68
|
-
@loop.call( Proc.new {nextStep} )
|
93
|
+
@times.times{ call }
|
69
94
|
end
|
70
95
|
end
|
96
|
+
|
97
|
+
protected
|
98
|
+
def call
|
99
|
+
@worker += 1
|
100
|
+
@loop.call Proc.new{nextStep}, @opened, @worker
|
101
|
+
@opened += 1
|
102
|
+
if @debug
|
103
|
+
puts "worker: #{@worker} opened: #{@opened} finished: #{@finished}"
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def nextStep
|
108
|
+
puts "ending" if @debug
|
109
|
+
@finished += 1
|
110
|
+
@worker -= 1
|
111
|
+
if @finished == @times
|
112
|
+
self.succeed
|
113
|
+
else
|
114
|
+
call if @opened < @times
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
71
118
|
end
|
72
119
|
|
73
120
|
# Repeat sequentially an action
|
@@ -96,6 +143,7 @@ module EventMachine
|
|
96
143
|
end
|
97
144
|
end
|
98
145
|
|
146
|
+
# Until sick. Act again and again, until criteria
|
99
147
|
class AdNauseum
|
100
148
|
include EM::Deferrable
|
101
149
|
|
@@ -141,3 +189,7 @@ end
|
|
141
189
|
def adnauseum(&block)
|
142
190
|
EventMachine::Scenario::AdNauseum.new &block
|
143
191
|
end
|
192
|
+
|
193
|
+
def quantumsatis(times, throttle=nil, &block)
|
194
|
+
EventMachine::Scenario::QuantumSatis.new times, throttle, &block
|
195
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: em-scenario
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-09-
|
12
|
+
date: 2011-09-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: eventmachine
|
16
|
-
requirement: &
|
16
|
+
requirement: &2152103240 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - =
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 1.0.0.beta3
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2152103240
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: minitest
|
27
|
-
requirement: &
|
27
|
+
requirement: &2152099300 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '2.0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2152099300
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rake
|
38
|
-
requirement: &
|
38
|
+
requirement: &2152093220 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *2152093220
|
47
47
|
description: Handling simpler story with event machine's callback
|
48
48
|
email: mathieu@garambrogne.net
|
49
49
|
executables: []
|