fibre 0.9.6 → 0.9.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 98cb9c7bec1b4bacca19c56a9c1f30749de6f2f2
4
- data.tar.gz: ec027579676fbdf631c7d5edf3292088d3be08fd
3
+ metadata.gz: 07320e52433387848e04df86513a2084afb47c5f
4
+ data.tar.gz: 00fcf61e035ed39c5dcde9307f42511891051785
5
5
  SHA512:
6
- metadata.gz: 7dce7bb0b82b76bc54a12d6073eca64c339cae076bb2be04392bbcf3f48e89a0d446f22ae88dea2e3315d79f262ac5482414b8b8692b5c78a58adb6fae718f6b
7
- data.tar.gz: ac1a20aec7a195cf7e22967289279737159ef414a56d3f979e39c64d4f5f2015fb705ee7a5a85fa4bb74db8651d0b4ae3502274cad6b650e4285a695c3ca4978
6
+ metadata.gz: 345d4ddf8891a06747a859fd8d63eec61239eab74d5f91e97cc99d8a573e901eab0da69f1f5a5f5cc319c8fe6d2b10b5b0c3fa7f68138389cd2c56fc915ebc09
7
+ data.tar.gz: bb604e4a82e42707b733b8f356b8fc6e91821d63cd202d6e7aa2a6e345dee4478732332a6b91ffec5ebdd9ad3a8cbbcfb809d0c6f5c9a6dffaba4a0ea42704b7
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
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.0.3"
24
25
  spec.add_development_dependency "bundler", "~> 1.6"
25
26
  spec.add_development_dependency "rake", "~> 10.0"
26
27
  spec.add_development_dependency "rspec", "~> 3"
@@ -12,6 +12,8 @@ module Fibre
12
12
  autoload :FiberPool, 'fibre/rack/fiber_pool'
13
13
  end
14
14
 
15
+ class LeaveError < StandardError; end
16
+
15
17
  # Configuration module
16
18
 
17
19
  extend self
@@ -1,10 +1,10 @@
1
1
  require 'fiber'
2
2
 
3
- #
4
3
  # Fiber.sync do |fiber|
5
4
  # fiber.resume response
6
5
  # fiber.leave StandardError, "Something"
7
6
  # end
7
+
8
8
  class Fiber
9
9
 
10
10
  def attributes
@@ -40,18 +40,14 @@ class Fiber
40
40
  end
41
41
  end
42
42
 
43
- #
44
- # Fiber.sync do |fiber|
45
- # fiber.resume # ...
46
- # end
47
- #
43
+
48
44
  def sync_it
49
45
  yield(Fiber.current) if block_given?
50
46
  Fiber.yield!
51
47
  end
52
48
  end
53
49
 
54
- def leave(exception, message=nil)
50
+ def leave(exception=Fibre::LeaveError, message=nil)
55
51
  resume exception.new(message)
56
52
  end
57
53
  end
@@ -5,6 +5,76 @@ module Fibre::Synchrony
5
5
  res = Fiber.scope { collect(&:sync) }
6
6
  res.collect(&:result)
7
7
  end
8
+
9
+ def sync!
10
+ res = Fiber.scope do
11
+ deep_sync_scoped
12
+ end
13
+
14
+ res.each do |mock|
15
+ node = self
16
+ path = mock.path.split(/./)
17
+ key = path.pop
18
+ path.each { |k| node = node[k] }
19
+ node[key] = mock.result
20
+ end
21
+
22
+ self
23
+ end
24
+
25
+ def deep_sync_scoped(path: [])
26
+ each_with_index do |item, index|
27
+ # deeeep
28
+ if item.is_a?(Array) || item.is_a?(Hash) # item.respond_to?(:deep_sync_scoped) not works in ruby 2.1.2
29
+ item.deep_sync_scoped(path: path + [index])
30
+ next
31
+ end
32
+
33
+ item.sync.tap do |mock|
34
+ mock.path = path + [index]
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ refine Hash do
41
+ def sync
42
+ res = Fiber.scope { values.collect(&:sync) }
43
+ hash = {}
44
+ res.each_with_index do |mock, index|
45
+ hash[keys[index]] = mock.result
46
+ end
47
+ hash
48
+ end
49
+
50
+ def sync!
51
+ res = Fiber.scope do
52
+ deep_sync_scoped
53
+ end
54
+
55
+ res.each do |mock|
56
+ node = self
57
+ path = mock.path
58
+ key = path.pop
59
+ path.each { |k| node = node[k] }
60
+ node[key] = mock.result
61
+ end
62
+
63
+ self
64
+ end
65
+
66
+ def deep_sync_scoped(path: [])
67
+ each do |key, item|
68
+ # deeeep
69
+ if item.is_a?(Array) || item.is_a?(Hash) # item.respond_to?(:deep_sync_scoped) not works in ruby 2.1.2
70
+ item.deep_sync_scoped(path: path + [key])
71
+ next
72
+ end
73
+ item.sync.tap do |mock|
74
+ mock.path = path + [key]
75
+ end
76
+ end
77
+ end
8
78
  end
9
79
 
10
80
  #<<<
@@ -1,7 +1,8 @@
1
1
  module Fibre
2
2
  class Mock
3
- attr_reader :scope
4
- attr_reader :result
3
+ attr_reader :scope
4
+ attr_reader :result
5
+ attr_accessor :path
5
6
 
6
7
  def initialize(scope)
7
8
  @scope = scope
@@ -7,11 +7,12 @@ module Fibre
7
7
 
8
8
  def scope
9
9
  raise "nested scopes" if Fiber.current[:scope]
10
- Fiber.current[:scope] = self.new(Fiber.current)
11
- res = yield
10
+ scope = self.new(Fiber.current)
11
+ Fiber.current[:scope] = scope
12
+ yield
12
13
  Fiber.current[:scope] = nil
13
14
  Fiber.yield!
14
- res
15
+ scope.mocks
15
16
  end
16
17
 
17
18
  def scope?
@@ -1,3 +1,3 @@
1
1
  module Fibre
2
- VERSION = "0.9.6"
2
+ VERSION = "0.9.7"
3
3
  end
@@ -2,8 +2,10 @@ require "spec_helper"
2
2
 
3
3
  describe Fibre do
4
4
  using EventObject
5
+ using Fibre::Synchrony
5
6
 
6
7
  before { Fibre.pool = nil }
8
+ let(:probe) { lambda {} }
7
9
 
8
10
  it "should create default pool with default size" do
9
11
  expect(Fibre.pool.size).to be(20)
@@ -14,7 +16,6 @@ describe Fibre do
14
16
  expect(Fiber.current.root?).to be true
15
17
  end
16
18
 
17
- let(:probe) { lambda {} }
18
19
 
19
20
  it "should checkout fiber" do
20
21
  expect(probe).to receive(:call)
@@ -60,4 +61,85 @@ describe Fibre do
60
61
  end
61
62
  }.to_not raise_error
62
63
  end
64
+
65
+ describe "in fiber specs" do
66
+
67
+ around do |examples|
68
+ EventMachine.run_block do
69
+ Fibre.pool.checkout(&examples)
70
+ end
71
+ end
72
+
73
+ class FibreTestOperation
74
+ def initialize(number)
75
+ @number = number
76
+ end
77
+
78
+ def sync
79
+ Fiber.sync do |fiber|
80
+ EM.next_tick do
81
+ fiber.resume @number
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ it "should Fiber.sync works well" do
88
+ result = Fiber.sync do |fiber|
89
+ EM.next_tick do
90
+ fiber.resume :success
91
+ end
92
+ end
93
+ expect(result).to be :success
94
+ end
95
+
96
+ it "should method leave raise exception" do
97
+ expect {
98
+ Fiber.sync do |fiber|
99
+ EM.next_tick do
100
+ fiber.leave
101
+ end
102
+ end
103
+ }.to raise_error Fibre::LeaveError
104
+ end
105
+
106
+ it "should sync array (scoping test)" do
107
+ op1 = FibreTestOperation.new(4)
108
+ op2 = FibreTestOperation.new(7)
109
+ res = [op1, op2].sync
110
+ expect(res[0]).to be(4)
111
+ expect(res[1]).to be(7)
112
+ end
113
+
114
+ it "should sync hash" do
115
+ op1 = FibreTestOperation.new(3)
116
+ op2 = FibreTestOperation.new(13)
117
+ op3 = FibreTestOperation.new(5)
118
+ res = {op1: op1, op2: op2, op3: op3}.sync
119
+ expect(res).to include(op1: 3, op2: 13, op3: 5)
120
+ end
121
+
122
+ it "should deep sync and sync! method (two in one)" do
123
+ op1 = FibreTestOperation.new(3)
124
+ op2 = FibreTestOperation.new(13)
125
+ op3 = FibreTestOperation.new(5)
126
+ op4 = FibreTestOperation.new(8)
127
+ op5 = FibreTestOperation.new(9)
128
+ res = {
129
+ ops: [op1, op2],
130
+ op3: op3,
131
+ child: {
132
+ op45: [op4, { op5: op5 }]
133
+ }
134
+ }.sync!
135
+
136
+ expect(res).to include(
137
+ op3: 5,
138
+ ops: match_array([3,13]),
139
+ child: include(
140
+ op45: match_array([8, include(op5: 9)])
141
+ )
142
+ )
143
+ end
144
+ end
63
145
  end
@@ -2,6 +2,7 @@ require 'bundler/setup'
2
2
  Bundler.setup
3
3
 
4
4
  require File.expand_path("./lib/fibre")
5
+ require "eventmachine"
5
6
 
6
7
  RSpec.configure do |config|
7
8
  config.color = true
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fibre
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.6
4
+ version: 0.9.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - che
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-07 00:00:00.000000000 Z
11
+ date: 2014-10-08 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: eventmachine
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.3
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.3
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement