fibre 0.10.0 → 1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 62efb12907acc747b90571c138cc84dea3b32c17
4
- data.tar.gz: 1f0ce684a13f150071dfe450f8c29f7c66e21d02
3
+ metadata.gz: db1d530ae64a23f807393667c363468029793a08
4
+ data.tar.gz: 1e3beff3e7217e7d0342ed286a26d8582bd2f83e
5
5
  SHA512:
6
- metadata.gz: 3db368b4a942f99e19ef9d2505b0133137e3214ffc54b12cc6b36d352fdf66293fa358250a435ec741c7a4d00bd34b151aa7eb29325bee1b2df3c7a2914cedff
7
- data.tar.gz: 3065781a747648ccaa13850f579d1fadcc35cd9558b69b3bb88536c6abe13d70b0af20e85d3758f95bca3a61bff7d52bfa10281f963f13daebfbadaffdc65adf
6
+ metadata.gz: f9aeeb9131aa92c7e6b9dc635ff9febc58e8aef886efb7f4b193f2fcab9af56307cec183c516d5ebfc26cac33f5a9d39eac3cd029fecc723f85bee22a0230cff
7
+ data.tar.gz: 900961fecc9cf43a44e09bec69d66d9dc4eae5293765e51bdd314aa4e89ef99a601acc44ab48f5ca923a2ec96431d38c9701da942f51c3a93f9f92b84843343e
data/fibre.gemspec CHANGED
@@ -4,27 +4,27 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'fibre/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "fibre"
7
+ spec.name = 'fibre'
8
8
  spec.version = Fibre::VERSION
9
- spec.authors = ["inre"]
10
- spec.email = ["inre.storm@gmail.com"]
11
- spec.summary = %q{Fibre - fiber pool, mock and scoping fibres}
12
- #spec.description = %q{TODO: Write a longer description. Optional.}
13
- spec.homepage = ""
14
- spec.license = "MIT"
9
+ spec.authors = ['inre']
10
+ spec.email = ['inre.storm@gmail.com']
11
+ spec.summary = 'Fiber pool for Ruby'
12
+ spec.description = spec.summary
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
19
+ spec.require_paths = ['lib']
20
20
 
21
21
  spec.required_ruby_version = '>= 2.1.0'
22
22
  spec.required_rubygems_version = '>= 2.3.0'
23
23
 
24
- spec.add_development_dependency "eventmachine", ">= 1.2.0"
25
- spec.add_development_dependency "bundler", "~> 1.6"
26
- spec.add_development_dependency "rake", "~> 10.0"
27
- spec.add_development_dependency "rspec", "~> 3"
24
+ spec.add_dependency 'event_object', '~> 1'
28
25
 
29
- spec.add_dependency "event_object", ">= 0.9"
26
+ spec.add_development_dependency 'eventmachine', '~> 1.2'
27
+ spec.add_development_dependency 'bundler', '~> 1.12'
28
+ spec.add_development_dependency 'rake', '~> 10'
29
+ spec.add_development_dependency 'rspec', '~> 3'
30
30
  end
data/lib/fibre.rb CHANGED
@@ -1,16 +1,16 @@
1
- require "event_object"
2
- require "fibre/version"
3
- require "fibre/core_ext/fiber"
4
- require "fibre/core_ext/synchrony"
5
- require "fibre/fiber_error"
6
- require "fibre/fiber_pool"
1
+ require 'event_object'
2
+ require 'fibre/version'
3
+ require 'fibre/core_ext/fiber'
4
+ require 'fibre/core_ext/synchrony'
5
+ require 'fibre/fiber_error'
6
+ require 'fibre/fiber_pool'
7
7
 
8
8
  module Fibre
9
- autoload :Mock, 'fibre/mock'
10
- autoload :Scope, 'fibre/scope'
9
+ autoload :Mock, 'fibre/mock'
10
+ autoload :Scope, 'fibre/scope'
11
11
 
12
12
  module Rack
13
- autoload :FiberPool, 'fibre/rack/fiber_pool'
13
+ autoload :FiberPool, 'fibre/rack/fiber_pool'
14
14
  end
15
15
 
16
16
  extend self
@@ -19,22 +19,21 @@ module Fibre
19
19
  attr_accessor :root
20
20
  self.root = Fiber.current
21
21
 
22
- # The pool size must be defined before the pool is called
23
- attr_accessor :pool_size
24
- self.pool_size = 20
22
+ DEFAULT_POOL_SIZE = 50
23
+ DEFAULT_POOL_QUEUE_SIZE = 1000
24
+ FIBER_POOL_THREADED = '__fiber_pool'
25
25
 
26
- # Can be changed at any time
27
- attr_accessor :max_pool_queue_size
28
- self.max_pool_queue_size = 1000
26
+ def make_pool(pool_size: DEFAULT_POOL_SIZE, pool_queue_size: DEFAULT_POOL_QUEUE_SIZE)
27
+ FiberPool.new(pool_size: pool_size, pool_queue_size: pool_queue_size)
28
+ end
29
29
 
30
30
  # Auto-initialize at first call and each thread has own fiber pool
31
- attr_writer :pool
32
31
  def pool
33
- Thread.current[:__fiber_pool] ||= FiberPool.new(pool_size)
32
+ Thread.current[FIBER_POOL_THREADED] ||= make_pool
34
33
  end
35
34
 
36
35
  def reset
37
- Thread.current[:__fiber_pool] = nil
36
+ Thread.current[FIBER_POOL_THREADED] = nil
38
37
  pool
39
38
  end
40
39
  end
@@ -2,7 +2,7 @@
2
2
  #
3
3
  # Outline,
4
4
  #
5
- # Fiber.sync do |fiber|
5
+ # Fiber.await do |fiber|
6
6
  # fiber.resume response
7
7
  # fiber.leave StandardError, "Something"
8
8
  # end
@@ -26,14 +26,18 @@ class Fiber
26
26
  attributes[key] = value
27
27
  end
28
28
 
29
+ def leave(exception, message=nil)
30
+ resume exception.is_a?(Class) ? exception.new(message) : exception
31
+ end
32
+
29
33
  class <<self
30
34
 
31
35
  def scope(*a, &b)
32
36
  Fibre::Scope.scope(*a, &b)
33
37
  end
34
38
 
35
- def sync(*a, &b)
36
- Fibre::Scope.scope? ? Fibre::Scope.sync(*a, &b) : wait(*a, &b)
39
+ def await(*a, &b)
40
+ Fibre::Scope.in_scope? ? Fibre::Scope.await(*a, &b) : await_only(*a, &b)
37
41
  end
38
42
 
39
43
  # raise exception if we catch exception
@@ -43,13 +47,9 @@ class Fiber
43
47
  end
44
48
  end
45
49
 
46
- def wait
50
+ def await_only
47
51
  yield(Fiber.current) if block_given?
48
52
  Fiber.yield!
49
53
  end
50
54
  end
51
-
52
- def leave(exception, message=nil)
53
- resume exception.is_a?(Class) ? exception.new(message) : exception
54
- end
55
55
  end
@@ -9,14 +9,14 @@
9
9
  module Fibre::Synchrony
10
10
 
11
11
  refine Array do
12
- def sync
13
- res = Fiber.scope { collect(&:sync) }
12
+ def await
13
+ res = Fiber.scope { collect(&:await) }
14
14
  res.collect(&:result)
15
15
  end
16
16
 
17
- def sync!
17
+ def await!
18
18
  res = Fiber.scope do
19
- deep_sync_scoped
19
+ await_deep_scoped
20
20
  end
21
21
 
22
22
  res.each do |mock|
@@ -30,15 +30,15 @@ module Fibre::Synchrony
30
30
  self
31
31
  end
32
32
 
33
- def deep_sync_scoped(path: [])
33
+ def await_deep_scoped(path: [])
34
34
  each_with_index do |item, index|
35
35
  # deeeep
36
36
  if item.is_a?(Array) || item.is_a?(Hash) # item.respond_to?(:deep_sync_scoped) not works in ruby 2.1.2
37
- item.deep_sync_scoped(path: path + [index])
37
+ item.await_deep_scoped(path: path + [index])
38
38
  next
39
39
  end
40
40
 
41
- item.sync.tap do |mock|
41
+ item.await.tap do |mock|
42
42
  mock.path = path + [index]
43
43
  end
44
44
  end
@@ -46,8 +46,8 @@ module Fibre::Synchrony
46
46
  end
47
47
 
48
48
  refine Hash do
49
- def sync
50
- res = Fiber.scope { values.collect(&:sync) }
49
+ def await
50
+ res = Fiber.scope { values.collect(&:await) }
51
51
  hash = {}
52
52
  res.each_with_index do |mock, index|
53
53
  hash[keys[index]] = mock.result
@@ -55,9 +55,9 @@ module Fibre::Synchrony
55
55
  hash
56
56
  end
57
57
 
58
- def sync!
58
+ def await!
59
59
  res = Fiber.scope do
60
- deep_sync_scoped
60
+ await_deep_scoped
61
61
  end
62
62
 
63
63
  res.each do |mock|
@@ -71,14 +71,14 @@ module Fibre::Synchrony
71
71
  self
72
72
  end
73
73
 
74
- def deep_sync_scoped(path: [])
74
+ def await_deep_scoped(path: [])
75
75
  each do |key, item|
76
76
  # deeeep
77
77
  if item.is_a?(Array) || item.is_a?(Hash) # item.respond_to?(:deep_sync_scoped) not works in ruby 2.1.2
78
- item.deep_sync_scoped(path: path + [key])
78
+ item.await_deep_scoped(path: path + [key])
79
79
  next
80
80
  end
81
- item.sync.tap do |mock|
81
+ item.await.tap do |mock|
82
82
  mock.path = path + [key]
83
83
  end
84
84
  end
@@ -13,11 +13,18 @@ module Fibre
13
13
  class FiberPool
14
14
  events :error, :before, :after
15
15
 
16
- # Initialize fibers pool
17
- def initialize(size)
18
- @pool = size.times.collect { ::Fiber.new(&self.method(:fiber_entry)) }
16
+ attr_reader :pool_size
17
+ attr_reader :pool_queue_size
18
+ attr_reader :reserved
19
+ attr_reader :queue
20
+
21
+ # Initialize fiber's pool
22
+ def initialize(pool_size: DEFAULT_POOL_SIZE, pool_queue_size: DEFAULT_POOL_QUEUE_SIZE)
23
+ @pool_size = pool_size
24
+ @pool_queue_size = pool_queue_size
19
25
  @reserved = {}
20
26
  @queue = []
27
+ @pool = @pool_size.times.collect { ::Fiber.new(&self.method(:fiber_entry)) }
21
28
  end
22
29
 
23
30
  # Borrow fiber from the pool and call the block inside
@@ -25,7 +32,7 @@ module Fibre
25
32
  spec = { block: b, parent: ::Fiber.current }
26
33
 
27
34
  if @pool.empty?
28
- raise "The fiber queue has been overflowed" if @queue.size > Fibre.max_pool_queue_size
35
+ raise "The fiber queue has been overflowed" if @queue.size > @pool_queue_size
29
36
  @queue.push spec
30
37
  return
31
38
  end
@@ -38,19 +45,6 @@ module Fibre
38
45
  self
39
46
  end
40
47
 
41
- # The size of pool
42
- def size
43
- @pool.size
44
- end
45
-
46
- def reserved
47
- @reserved
48
- end
49
-
50
- def queue
51
- @queue
52
- end
53
-
54
48
  private
55
49
 
56
50
  # There is entrypoint running fibers
data/lib/fibre/scope.rb CHANGED
@@ -6,7 +6,7 @@ module Fibre
6
6
  class <<self
7
7
 
8
8
  def scope
9
- raise "nested scopes" if Fiber.current[:scope]
9
+ raise 'nested scopes' if Fiber.current[:scope]
10
10
  scope = self.new(Fiber.current)
11
11
  Fiber.current[:scope] = scope
12
12
  yield
@@ -15,11 +15,11 @@ module Fibre
15
15
  scope.mocks
16
16
  end
17
17
 
18
- def scope?
18
+ def in_scope?
19
19
  !!Fiber.current[:scope]
20
20
  end
21
21
 
22
- def sync
22
+ def await
23
23
  scope = Fiber.current[:scope]
24
24
  mock = Fibre::Mock.new(scope)
25
25
  scope.mocks << mock
data/lib/fibre/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Fibre
2
- VERSION = "0.10.0"
2
+ VERSION = '1.0.1'
3
3
  end
@@ -0,0 +1,59 @@
1
+ require "spec_helper"
2
+
3
+ describe Fibre do
4
+ using EventObject
5
+ using Fibre::Synchrony
6
+
7
+ before { Fibre.reset }
8
+ let(:probe) { lambda {} }
9
+ let(:pool) { Fibre.make_pool }
10
+
11
+ it "creates fiber's pool" do
12
+ expect(Fibre.pool.pool_size).to be(50)
13
+ expect(Fibre.pool.pool_queue_size).to be(1000)
14
+ end
15
+
16
+ it "has the root fiber" do
17
+ expect(Fibre.root).to equal(Fiber.current)
18
+ expect(Fiber.current.root?).to be true
19
+ end
20
+
21
+ it "checks out the fiber" do
22
+ expect(probe).to receive(:call)
23
+ pool.checkout(&probe)
24
+ end
25
+
26
+ it "calls in scope" do
27
+ expect(probe).to receive(:call)
28
+ pool.checkout do
29
+ Fiber.scope do
30
+ probe.call
31
+ end
32
+ end
33
+ end
34
+
35
+ def raise_method
36
+ raise "test exception"
37
+ end
38
+
39
+ it "raises an exception in the fiber" do
40
+ expect {
41
+ pool.checkout do
42
+ raise_method
43
+ end
44
+ }.to raise_error(Fibre::FiberError)
45
+ end
46
+
47
+ it "catches up an exception with `error` event" do
48
+ expect(probe).to receive(:call)
49
+ pool.on :error do |error|
50
+ probe.call
51
+ end
52
+
53
+ expect {
54
+ pool.checkout do
55
+ raise
56
+ end
57
+ }.to_not raise_error
58
+ end
59
+ end
@@ -0,0 +1,107 @@
1
+ require "spec_helper"
2
+
3
+ describe Fibre do
4
+ using EventObject
5
+ using Fibre::Synchrony
6
+
7
+ before { Fibre.reset }
8
+ let(:probe) { lambda {} }
9
+ let(:pool) { Fibre.make_pool }
10
+
11
+ around do |examples|
12
+ EventMachine.run_block do
13
+ pool.checkout(&examples)
14
+ end
15
+ end
16
+
17
+ class FibreTestOperation
18
+ def initialize(number)
19
+ @number = number
20
+ end
21
+
22
+ def await
23
+ Fiber.await do |fiber|
24
+ EM.next_tick do
25
+ fiber.resume @number
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ class FibreTestOperationWithException
32
+ def initialize(number)
33
+ @number = number
34
+ end
35
+
36
+ def await
37
+ Fiber.await { |f|
38
+ Fibre.pool.checkout do
39
+ raise "test"
40
+ end
41
+ }
42
+ end
43
+ end
44
+
45
+ it "waits a successful answer" do
46
+ result = Fiber.await do |fiber|
47
+ EM.next_tick do
48
+ fiber.resume :success
49
+ end
50
+ end
51
+ expect(result).to be :success
52
+ end
53
+
54
+ it "raises an expcetion and catches Fibre error" do
55
+ expect {
56
+ pool.checkout do
57
+ raise "test"
58
+ end
59
+ }.to raise_error Fibre::FiberError
60
+ end
61
+
62
+ it "raises an exception during await" do
63
+ expect {
64
+ op = FibreTestOperationWithException.new(4)
65
+ op.await
66
+ }.to raise_error Fibre::FiberError
67
+ end
68
+
69
+ it "waits couple async requests wrapped up with Array" do
70
+ op1 = FibreTestOperation.new(4)
71
+ op2 = FibreTestOperation.new(7)
72
+ res = [op1, op2].await
73
+ expect(res[0]).to be(4)
74
+ expect(res[1]).to be(7)
75
+ end
76
+
77
+ it "waits three async requests wrapped up with Hash" do
78
+ op1 = FibreTestOperation.new(3)
79
+ op2 = FibreTestOperation.new(13)
80
+ op3 = FibreTestOperation.new(5)
81
+ res = {op1: op1, op2: op2, op3: op3}.await
82
+ expect(res).to include(op1: 3, op2: 13, op3: 5)
83
+ end
84
+
85
+ it "runs a lot of asynchronous operations" do
86
+ op1 = FibreTestOperation.new(3)
87
+ op2 = FibreTestOperation.new(13)
88
+ op3 = FibreTestOperation.new(5)
89
+ op4 = FibreTestOperation.new(8)
90
+ op5 = FibreTestOperation.new(9)
91
+ res = {
92
+ ops: [op1, op2],
93
+ op3: op3,
94
+ child: {
95
+ op45: [op4, { op5: op5 }]
96
+ }
97
+ }.await!
98
+
99
+ expect(res).to include(
100
+ op3: 5,
101
+ ops: match_array([3,13]),
102
+ child: include(
103
+ op45: match_array([8, include(op5: 9)])
104
+ )
105
+ )
106
+ end
107
+ end
metadata CHANGED
@@ -1,86 +1,86 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fibre
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - inre
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-02 00:00:00.000000000 Z
11
+ date: 2016-11-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: eventmachine
14
+ name: event_object
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 1.2.0
20
- type: :development
19
+ version: '1'
20
+ type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 1.2.0
26
+ version: '1'
27
27
  - !ruby/object:Gem::Dependency
28
- name: bundler
28
+ name: eventmachine
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.6'
33
+ version: '1.2'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.6'
40
+ version: '1.2'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rake
42
+ name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '10.0'
47
+ version: '1.12'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '10.0'
54
+ version: '1.12'
55
55
  - !ruby/object:Gem::Dependency
56
- name: rspec
56
+ name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '3'
61
+ version: '10'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '3'
68
+ version: '10'
69
69
  - !ruby/object:Gem::Dependency
70
- name: event_object
70
+ name: rspec
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '0.9'
76
- type: :runtime
75
+ version: '3'
76
+ type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ">="
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '0.9'
83
- description:
82
+ version: '3'
83
+ description: Fiber pool for Ruby
84
84
  email:
85
85
  - inre.storm@gmail.com
86
86
  executables: []
@@ -102,8 +102,9 @@ files:
102
102
  - lib/fibre/rack/fiber_pool.rb
103
103
  - lib/fibre/scope.rb
104
104
  - lib/fibre/version.rb
105
- - spec/fibre_spec.rb
105
+ - spec/fiber_pool_spec.rb
106
106
  - spec/spec_helper.rb
107
+ - spec/synchrony_spec.rb
107
108
  homepage: ''
108
109
  licenses:
109
110
  - MIT
@@ -124,10 +125,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
125
  version: 2.3.0
125
126
  requirements: []
126
127
  rubyforge_project:
127
- rubygems_version: 2.5.1
128
+ rubygems_version: 2.6.6
128
129
  signing_key:
129
130
  specification_version: 4
130
- summary: Fibre - fiber pool, mock and scoping fibres
131
+ summary: Fiber pool for Ruby
131
132
  test_files:
132
- - spec/fibre_spec.rb
133
+ - spec/fiber_pool_spec.rb
133
134
  - spec/spec_helper.rb
135
+ - spec/synchrony_spec.rb
data/spec/fibre_spec.rb DELETED
@@ -1,170 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Fibre do
4
- using EventObject
5
- using Fibre::Synchrony
6
-
7
- before { Fibre.reset }
8
- let(:probe) { lambda {} }
9
-
10
- it "should create default pool with default size" do
11
- expect(Fibre.pool.size).to be(20)
12
- expect(Fibre.max_pool_queue_size).to be(1000)
13
- end
14
-
15
- it "should have right root fiber" do
16
- expect(Fibre.root).to equal(Fiber.current)
17
- expect(Fiber.current.root?).to be true
18
- end
19
-
20
-
21
- it "should checkout fiber" do
22
- expect(probe).to receive(:call)
23
- Fibre.pool.checkout(&probe)
24
- end
25
-
26
- it "should rescue error in fiber" do
27
- expect(probe).to receive(:call)
28
- Fibre.pool.on(:error) do |error|
29
- probe.call
30
- end
31
-
32
- Fibre.pool.checkout do
33
- raise
34
- end
35
- end
36
-
37
-
38
- it "should scope" do
39
- expect(probe).to receive(:call)
40
- Fibre.pool.checkout do
41
- Fiber.scope do
42
- probe.call
43
- end
44
- end
45
- end
46
-
47
- def raise_method
48
- raise "test exception"
49
- end
50
-
51
- it "should raise uncatched exceptions" do
52
- expect {
53
- Fibre.pool.checkout do
54
- raise_method
55
- end
56
- }.to raise_error(Fibre::FiberError)
57
- end
58
-
59
- it "should catch exception" do
60
- Fibre.pool.on :error do |error|
61
- # catch exception here
62
- end
63
-
64
- expect {
65
- Fibre.pool.checkout do
66
- raise
67
- end
68
- }.to_not raise_error
69
- end
70
-
71
- describe "in fiber specs" do
72
-
73
- around do |examples|
74
- EventMachine.run_block do
75
- Fibre.pool.checkout(&examples)
76
- end
77
- end
78
-
79
- class FibreTestOperation
80
- def initialize(number)
81
- @number = number
82
- end
83
-
84
- def sync
85
- Fiber.sync do |fiber|
86
- EM.next_tick do
87
- fiber.resume @number
88
- end
89
- end
90
- end
91
- end
92
-
93
- class FibreTestOperationWithException
94
- def initialize(number)
95
- @number = number
96
- end
97
-
98
- def sync
99
- Fiber.sync do |f|
100
- Fibre.pool.checkout do
101
- raise "test"
102
- end
103
- end
104
- end
105
- end
106
-
107
- it "should Fiber.sync works well" do
108
- result = Fiber.sync do |fiber|
109
- EM.next_tick do
110
- fiber.resume :success
111
- end
112
- end
113
- expect(result).to be :success
114
- end
115
-
116
- it "should raise exception in fiber" do
117
- expect {
118
- Fibre.pool.checkout do
119
- raise "test"
120
- end
121
- }.to raise_error Fibre::FiberError
122
- end
123
-
124
- it "should sync with exception" do
125
- expect {
126
- op = FibreTestOperationWithException.new(4)
127
- op.sync
128
- }.to raise_error Fibre::FiberError
129
- end
130
-
131
- it "should sync array (scoping test)" do
132
- op1 = FibreTestOperation.new(4)
133
- op2 = FibreTestOperation.new(7)
134
- res = [op1, op2].sync
135
- expect(res[0]).to be(4)
136
- expect(res[1]).to be(7)
137
- end
138
-
139
- it "should sync hash" do
140
- op1 = FibreTestOperation.new(3)
141
- op2 = FibreTestOperation.new(13)
142
- op3 = FibreTestOperation.new(5)
143
- res = {op1: op1, op2: op2, op3: op3}.sync
144
- expect(res).to include(op1: 3, op2: 13, op3: 5)
145
- end
146
-
147
- it "should deep sync and sync! method (two in one)" do
148
- op1 = FibreTestOperation.new(3)
149
- op2 = FibreTestOperation.new(13)
150
- op3 = FibreTestOperation.new(5)
151
- op4 = FibreTestOperation.new(8)
152
- op5 = FibreTestOperation.new(9)
153
- res = {
154
- ops: [op1, op2],
155
- op3: op3,
156
- child: {
157
- op45: [op4, { op5: op5 }]
158
- }
159
- }.sync!
160
-
161
- expect(res).to include(
162
- op3: 5,
163
- ops: match_array([3,13]),
164
- child: include(
165
- op45: match_array([8, include(op5: 9)])
166
- )
167
- )
168
- end
169
- end
170
- end