threadlimiter 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +9 -0
- data/VERSION +1 -1
- data/lib/threadlimiter/enumerable.rb +5 -5
- data/lib/threadlimiter/threadlimiter.rb +1 -1
- data/test/test.rb +54 -79
- metadata +6 -4
data/CHANGELOG
ADDED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
@@ -29,13 +29,13 @@ module Enumerable
|
|
29
29
|
threaded_collect(number_of_clusters, &block)
|
30
30
|
else
|
31
31
|
clusters = [] # One cluster per thread.
|
32
|
-
last_pos =
|
32
|
+
last_pos = -1
|
33
33
|
res = []
|
34
34
|
|
35
|
-
self.
|
36
|
-
|
35
|
+
self.each do |object|
|
36
|
+
last_pos += 1
|
37
37
|
|
38
|
-
last_pos
|
38
|
+
(clusters[last_pos%number_of_clusters] ||= []) << object
|
39
39
|
end
|
40
40
|
|
41
41
|
clusters.threaded_collect(-1) do |cluster|
|
@@ -50,7 +50,7 @@ module Enumerable
|
|
50
50
|
res.concat(array)
|
51
51
|
end
|
52
52
|
|
53
|
-
res[0..last_pos] # Remove padding
|
53
|
+
res[0..last_pos] # Remove padding nil.
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
@@ -4,7 +4,7 @@
|
|
4
4
|
# ThreadLimiter isn't a thread pool. Each fork really starts a new thread.
|
5
5
|
|
6
6
|
class ThreadLimiter
|
7
|
-
# Initialize the
|
7
|
+
# Initialize the ThreadLimiter.
|
8
8
|
# The optional parameter <i>limit</i> is the maximum number of concurrently running threads.
|
9
9
|
# Set <i>limit</i> to -1 or 0 to fork threads without limiting the number of concurrently running threads.
|
10
10
|
|
data/test/test.rb
CHANGED
@@ -17,9 +17,8 @@ class ThreadLimiterTest < Test::Unit::TestCase
|
|
17
17
|
|
18
18
|
assert_equal([Thread], threads.collect{|t| t.class}.uniq)
|
19
19
|
|
20
|
-
output = threads.collect{|t| t.value}
|
21
20
|
|
22
|
-
assert_equal(input.to_a ,
|
21
|
+
assert_equal(input.to_a , threads.collect{|t| t.value})
|
23
22
|
assert_equal(0 , threadlimiter.instance_eval{@running})
|
24
23
|
assert_equal(limit , threadlimiter.instance_eval{@limit})
|
25
24
|
end
|
@@ -41,140 +40,116 @@ class ThreadLimiterEnumerableTest < Test::Unit::TestCase
|
|
41
40
|
def test_with_empty_enumerables
|
42
41
|
assert_equal([], [].threaded_collect(10){2})
|
43
42
|
assert_equal([], [].threaded_collect(-1){2})
|
44
|
-
assert_equal([], [].threaded_collect(0){2})
|
45
|
-
end
|
46
|
-
|
47
|
-
def test_threaded_collect_with_no_arguments_with_no_limit
|
48
|
-
input = (1..100).zip(101..200)
|
49
|
-
output = input.threaded_collect(-1){2}
|
50
|
-
should_be = input.collect{2}
|
51
|
-
|
52
|
-
assert_equal(should_be, output)
|
53
|
-
end
|
43
|
+
assert_equal([], [].threaded_collect( 0){2})
|
54
44
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
should_be = input.collect{2}
|
59
|
-
|
60
|
-
assert_equal(should_be, output)
|
45
|
+
assert_equal([], [].clustered_threaded_collect(10){2})
|
46
|
+
assert_equal([], [].clustered_threaded_collect(-1){2})
|
47
|
+
assert_equal([], [].clustered_threaded_collect( 0){2})
|
61
48
|
end
|
62
49
|
|
63
|
-
def
|
64
|
-
input = (1..100).
|
65
|
-
output = input.threaded_collect(0){2}
|
50
|
+
def test_threaded_collect_with_no_arguments
|
51
|
+
input = (1..100).to_a
|
66
52
|
should_be = input.collect{2}
|
67
53
|
|
68
|
-
assert_equal(should_be,
|
54
|
+
assert_equal(should_be, input.threaded_collect(10){2})
|
55
|
+
assert_equal(should_be, input.threaded_collect(-1){2})
|
56
|
+
assert_equal(should_be, input.threaded_collect( 0){2})
|
69
57
|
end
|
70
58
|
|
71
|
-
def
|
72
|
-
input = (1..100).
|
73
|
-
output = input.threaded_collect(-1){|x| x * 2}
|
59
|
+
def test_threaded_collect_with_one_argument
|
60
|
+
input = (1..100).to_a
|
74
61
|
should_be = input.collect{|x| x * 2}
|
75
62
|
|
76
|
-
assert_equal(should_be,
|
63
|
+
assert_equal(should_be, input.threaded_collect(10){|x| x * 2})
|
64
|
+
assert_equal(should_be, input.threaded_collect(-1){|x| x * 2})
|
65
|
+
assert_equal(should_be, input.threaded_collect( 0){|x| x * 2})
|
77
66
|
end
|
78
67
|
|
79
|
-
def
|
68
|
+
def test_threaded_collect_with_two_arguments
|
80
69
|
input = (1..100).zip(101..200)
|
81
|
-
|
82
|
-
should_be = input.collect{|x| x * 2}
|
70
|
+
should_be = input.collect{|x, y| (x*y) % 2 == 0}
|
83
71
|
|
84
|
-
assert_equal(should_be,
|
85
|
-
|
86
|
-
|
87
|
-
def test_threaded_collect_with_one_argument_with_zero_limit
|
88
|
-
input = (1..100).zip(101..200)
|
89
|
-
output = input.threaded_collect(0){|x| x * 2}
|
90
|
-
should_be = input.collect{|x| x * 2}
|
91
|
-
|
92
|
-
assert_equal(should_be, output)
|
93
|
-
end
|
94
|
-
|
95
|
-
def test_threaded_collect_with_two_arguments_with_no_limit
|
96
|
-
input = (1..100).zip(101..200)
|
97
|
-
output = input.threaded_collect(-1){|x, y| x * y}
|
98
|
-
should_be = input.collect{|x, y| x * y}
|
99
|
-
|
100
|
-
assert_equal(should_be, output)
|
101
|
-
end
|
102
|
-
|
103
|
-
def test_threaded_collect_with_two_arguments_with_limit
|
104
|
-
input = (1..100).zip(101..200)
|
105
|
-
output = input.threaded_collect(10){|x, y| x * y}
|
106
|
-
should_be = input.collect{|x, y| x * y}
|
107
|
-
|
108
|
-
assert_equal(should_be, output)
|
109
|
-
end
|
110
|
-
|
111
|
-
def test_threaded_collect_with_two_arguments_with_zero_limit
|
112
|
-
input = (1..100).zip(101..200)
|
113
|
-
output = input.threaded_collect(0){|x, y| x * y}
|
114
|
-
should_be = input.collect{|x, y| x * y}
|
115
|
-
|
116
|
-
assert_equal(should_be, output)
|
72
|
+
assert_equal(should_be, input.threaded_collect(10){|x, y| (x*y) % 2 == 0})
|
73
|
+
assert_equal(should_be, input.threaded_collect(-1){|x, y| (x*y) % 2 == 0})
|
74
|
+
assert_equal(should_be, input.threaded_collect( 0){|x, y| (x*y) % 2 == 0})
|
117
75
|
end
|
118
76
|
|
119
77
|
def test_threaded_select
|
120
78
|
input = (1..100).zip(101..200)
|
121
|
-
output = input.threaded_select{|x, y| (x*y) % 2 == 0}
|
122
79
|
should_be = input.select{|x, y| (x*y) % 2 == 0}
|
123
80
|
|
124
|
-
assert_equal(should_be,
|
81
|
+
assert_equal(should_be, input.threaded_select(10){|x, y| (x*y) % 2 == 0})
|
82
|
+
assert_equal(should_be, input.threaded_select(-1){|x, y| (x*y) % 2 == 0})
|
83
|
+
assert_equal(should_be, input.threaded_select( 0){|x, y| (x*y) % 2 == 0})
|
125
84
|
end
|
126
85
|
|
127
86
|
def test_threaded_reject
|
128
87
|
input = (1..100).zip(101..200)
|
129
|
-
output = input.threaded_reject{|x, y| (x*y) % 2 == 0}
|
130
88
|
should_be = input.reject{|x, y| (x*y) % 2 == 0}
|
131
89
|
|
132
|
-
assert_equal(should_be,
|
90
|
+
assert_equal(should_be, input.threaded_reject(10){|x, y| (x*y) % 2 == 0})
|
91
|
+
assert_equal(should_be, input.threaded_reject(-1){|x, y| (x*y) % 2 == 0})
|
92
|
+
assert_equal(should_be, input.threaded_reject( 0){|x, y| (x*y) % 2 == 0})
|
133
93
|
end
|
134
94
|
|
135
95
|
def test_threaded_each
|
136
96
|
input = (1..100).zip(101..200)
|
137
|
-
output = input.threaded_each{|x, y| (x*y) % 2 == 0}
|
138
97
|
|
139
|
-
assert_equal(input,
|
98
|
+
assert_equal(input, input.threaded_each(10){|x, y| (x*y) % 2 == 0})
|
99
|
+
assert_equal(input, input.threaded_each(-1){|x, y| (x*y) % 2 == 0})
|
100
|
+
assert_equal(input, input.threaded_each( 0){|x, y| (x*y) % 2 == 0})
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_clustered_threaded_collect_with_no_argument
|
104
|
+
input = (1..100).to_a
|
105
|
+
should_be = input.collect{2}
|
106
|
+
|
107
|
+
assert_equal(should_be, input.clustered_threaded_collect(10){2})
|
108
|
+
assert_equal(should_be, input.clustered_threaded_collect(-1){2})
|
109
|
+
assert_equal(should_be, input.clustered_threaded_collect( 0){2})
|
140
110
|
end
|
141
111
|
|
142
112
|
def test_clustered_threaded_collect_with_one_argument
|
143
|
-
input = (1..100).
|
144
|
-
output = input.clustered_threaded_collect(10){|x| x * 2}
|
113
|
+
input = (1..100).to_a
|
145
114
|
should_be = input.collect{|x| x * 2}
|
146
115
|
|
147
|
-
assert_equal(should_be,
|
116
|
+
assert_equal(should_be, input.clustered_threaded_collect(10){|x| x * 2})
|
117
|
+
assert_equal(should_be, input.clustered_threaded_collect(-1){|x| x * 2})
|
118
|
+
assert_equal(should_be, input.clustered_threaded_collect( 0){|x| x * 2})
|
148
119
|
end
|
149
120
|
|
150
121
|
def test_clustered_threaded_collect_with_two_arguments
|
151
122
|
input = (1..100).zip(101..200)
|
152
|
-
output = input.clustered_threaded_collect(10){|x, y| x * y}
|
153
123
|
should_be = input.collect{|x, y| x * y}
|
154
124
|
|
155
|
-
assert_equal(should_be,
|
125
|
+
assert_equal(should_be, input.clustered_threaded_collect(10){|x, y| x * y})
|
126
|
+
assert_equal(should_be, input.clustered_threaded_collect(-1){|x, y| x * y})
|
127
|
+
assert_equal(should_be, input.clustered_threaded_collect( 0){|x, y| x * y})
|
156
128
|
end
|
157
129
|
|
158
130
|
def test_clustered_threaded_select
|
159
131
|
input = (1..100).zip(101..200)
|
160
|
-
output = input.clustered_threaded_select(10){|x, y| (x*y) % 2 == 0}
|
161
132
|
should_be = input.select{|x, y| (x*y) % 2 == 0}
|
162
133
|
|
163
|
-
assert_equal(should_be,
|
134
|
+
assert_equal(should_be, input.clustered_threaded_select(10){|x, y| (x*y) % 2 == 0})
|
135
|
+
assert_equal(should_be, input.clustered_threaded_select(-1){|x, y| (x*y) % 2 == 0})
|
136
|
+
assert_equal(should_be, input.clustered_threaded_select( 0){|x, y| (x*y) % 2 == 0})
|
164
137
|
end
|
165
138
|
|
166
139
|
def test_clustered_threaded_reject
|
167
140
|
input = (1..100).zip(101..200)
|
168
|
-
output = input.clustered_threaded_reject(10){|x, y| (x*y) % 2 == 0}
|
169
141
|
should_be = input.reject{|x, y| (x*y) % 2 == 0}
|
170
142
|
|
171
|
-
assert_equal(should_be,
|
143
|
+
assert_equal(should_be, input.clustered_threaded_reject(10){|x, y| (x*y) % 2 == 0})
|
144
|
+
assert_equal(should_be, input.clustered_threaded_reject(-1){|x, y| (x*y) % 2 == 0})
|
145
|
+
assert_equal(should_be, input.clustered_threaded_reject( 0){|x, y| (x*y) % 2 == 0})
|
172
146
|
end
|
173
147
|
|
174
148
|
def test_clustered_threaded_each
|
175
149
|
input = (1..100).zip(101..200)
|
176
|
-
output = input.clustered_threaded_each(10){|x, y| (x*y) % 2 == 0}
|
177
150
|
|
178
|
-
assert_equal(input,
|
151
|
+
assert_equal(input, input.clustered_threaded_each(10){|x, y| (x*y) % 2 == 0})
|
152
|
+
assert_equal(input, input.clustered_threaded_each(-1){|x, y| (x*y) % 2 == 0})
|
153
|
+
assert_equal(input, input.clustered_threaded_each( 0){|x, y| (x*y) % 2 == 0})
|
179
154
|
end
|
180
155
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: threadlimiter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Erik Veenstra
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-07-
|
12
|
+
date: 2008-07-27 00:00:00 +02:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -29,6 +29,7 @@ files:
|
|
29
29
|
- README
|
30
30
|
- LICENSE
|
31
31
|
- VERSION
|
32
|
+
- CHANGELOG
|
32
33
|
has_rdoc: true
|
33
34
|
homepage: http://www.erikveen.dds.nl/threadlimiter/index.html
|
34
35
|
post_install_message:
|
@@ -36,8 +37,9 @@ rdoc_options:
|
|
36
37
|
- README
|
37
38
|
- LICENSE
|
38
39
|
- VERSION
|
40
|
+
- CHANGELOG
|
39
41
|
- --title
|
40
|
-
- threadlimiter (0.1.
|
42
|
+
- threadlimiter (0.1.1)
|
41
43
|
- --main
|
42
44
|
- README
|
43
45
|
require_paths:
|
@@ -57,7 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
57
59
|
requirements: []
|
58
60
|
|
59
61
|
rubyforge_project: threadlimiter
|
60
|
-
rubygems_version: 1.
|
62
|
+
rubygems_version: 1.2.0
|
61
63
|
signing_key:
|
62
64
|
specification_version: 2
|
63
65
|
summary: Fork threads like Thread.fork, but limit the number of concurrently running threads.
|