concurrent-sequential 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,32 @@
1
+ # concurrent/sequential - Sequenced asynchronous actions
2
+ #
3
+ # Copyright 2008 MenTaLguY <mental@rydia.net>
4
+ #
5
+ # All rights reserved.
6
+ #
7
+ # Redistribution and use in source and binary forms, with or without
8
+ # modification, are permitted provided that the following conditions are met:
9
+ #
10
+ # * Redistributions of source code must retain the above copyright notice,
11
+ # this list of conditions and the following disclaimer.
12
+ # * Redistributions in binary form must reproduce the above copyright notice
13
+ # this list of conditions and the following disclaimer in the documentation
14
+ # and/or other materials provided with the distribution.
15
+ # * The names of the authors may not be used to endorse or promote products
16
+ # derived from this software without specific prior written permission.
17
+ #
18
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
+ # POSSIBILITY OF SUCH DAMAGE.
29
+
30
+ require 'concurrent/sequenced/mock-sequencer'
31
+ require 'concurrent/sequenced/thread-sequencer'
32
+
@@ -0,0 +1,51 @@
1
+ # concurrent/sequential/mock-sequencer - Single-threaded sequencer for testing
2
+ #
3
+ # Copyright 2008 MenTaLguY <mental@rydia.net>
4
+ #
5
+ # All rights reserved.
6
+ #
7
+ # Redistribution and use in source and binary forms, with or without
8
+ # modification, are permitted provided that the following conditions are met:
9
+ #
10
+ # * Redistributions of source code must retain the above copyright notice,
11
+ # this list of conditions and the following disclaimer.
12
+ # * Redistributions in binary form must reproduce the above copyright notice
13
+ # this list of conditions and the following disclaimer in the documentation
14
+ # and/or other materials provided with the distribution.
15
+ # * The names of the authors may not be used to endorse or promote products
16
+ # derived from this software without specific prior written permission.
17
+ #
18
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
+ # POSSIBILITY OF SUCH DAMAGE.
29
+
30
+ module Concurrent
31
+ module Sequential
32
+
33
+ class MockSequencer
34
+ def initialize
35
+ @queue = []
36
+ end
37
+
38
+ def soon(&block)
39
+ raise ArgumentError, "No block given" unless block
40
+ @queue << block
41
+ self
42
+ end
43
+
44
+ def run
45
+ @queue.shift.call until @queue.empty?
46
+ self
47
+ end
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,71 @@
1
+ # concurrent/sequential/thread-sequencer - Sequencer with own scheduling thread
2
+ #
3
+ # Copyright 2008 MenTaLguY <mental@rydia.net>
4
+ #
5
+ # All rights reserved.
6
+ #
7
+ # Redistribution and use in source and binary forms, with or without
8
+ # modification, are permitted provided that the following conditions are met:
9
+ #
10
+ # * Redistributions of source code must retain the above copyright notice,
11
+ # this list of conditions and the following disclaimer.
12
+ # * Redistributions in binary form must reproduce the above copyright notice
13
+ # this list of conditions and the following disclaimer in the documentation
14
+ # and/or other materials provided with the distribution.
15
+ # * The names of the authors may not be used to endorse or promote products
16
+ # derived from this software without specific prior written permission.
17
+ #
18
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
+ # POSSIBILITY OF SUCH DAMAGE.
29
+
30
+ require 'thread'
31
+ begin
32
+ require 'fastthread'
33
+ rescue LoadError
34
+ end
35
+
36
+ module Concurrent
37
+ module Sequential
38
+
39
+ class ThreadSequencer
40
+ def initialize
41
+ @queue = Queue.new
42
+ @running = true
43
+ @thread = Thread.new { run }
44
+ end
45
+
46
+ def soon(&block)
47
+ raise ArgumentError, "No block given" unless block
48
+ @queue << block
49
+ self
50
+ end
51
+
52
+ def shutdown(timeout=nil)
53
+ soon { @running = false } if @thread.alive?
54
+ @thread.join(timeout)
55
+ self
56
+ end
57
+
58
+ def run
59
+ while @running
60
+ begin
61
+ @queue.shift.call
62
+ rescue Exception
63
+ end
64
+ end
65
+ self
66
+ end
67
+ private :run
68
+ end
69
+
70
+ end
71
+ end
@@ -0,0 +1,31 @@
1
+ require 'concurrent/sequential/mock-sequencer'
2
+
3
+ mock_sequencer = Concurrent::Sequential::MockSequencer
4
+
5
+ describe mock_sequencer do
6
+ before :each do
7
+ @sequencer = mock_sequencer.new
8
+ end
9
+
10
+ it "should accept blocks via #{mock_sequencer}#soon" do
11
+ @sequencer.soon { }
12
+ end
13
+
14
+ it "should execute submitted blocks after submission" do
15
+ calls = mock("calls")
16
+ @sequencer.soon { calls.a }
17
+ calls.should_receive(:a)
18
+ @sequencer.run
19
+ end
20
+
21
+ it "should execute reentrant blocks completely and in submission order" do
22
+ calls = mock("calls")
23
+ @sequencer.soon do
24
+ @sequencer.soon { calls.b }
25
+ calls.a
26
+ end
27
+ calls.should_receive(:a).ordered
28
+ calls.should_receive(:b).ordered
29
+ @sequencer.run
30
+ end
31
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: concurrent-sequential
5
+ version: !ruby/object:Gem::Version
6
+ version: "0.1"
7
+ date: 2008-06-03 00:00:00 -04:00
8
+ summary: Sequenced asynchronous actions
9
+ require_paths:
10
+ - lib
11
+ email:
12
+ homepage:
13
+ rubyforge_project:
14
+ description:
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - MenTaLguY <mental@rydia.net>
31
+ files:
32
+ - spec/sequential/mock-sequencer_spec.rb
33
+ - lib/concurrent/sequential.rb
34
+ - lib/concurrent/sequential/mock-sequencer.rb
35
+ - lib/concurrent/sequential/thread-sequencer.rb
36
+ test_files: []
37
+
38
+ rdoc_options: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ executables: []
43
+
44
+ extensions: []
45
+
46
+ requirements: []
47
+
48
+ dependencies: []
49
+