em-synchrony 1.0.4 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +28 -0
- data/Gemfile +5 -5
- data/LICENSE +20 -0
- data/README.md +26 -0
- data/em-synchrony.gemspec +1 -3
- data/lib/active_record/connection_adapters/em_mysql2_adapter.rb +6 -1
- data/lib/em-synchrony.rb +13 -0
- data/lib/em-synchrony/activerecord_4_2.rb +61 -0
- data/lib/em-synchrony/em-multi.rb +2 -2
- data/lib/em-synchrony/fiber_iterator.rb +1 -1
- data/lib/em-synchrony/kernel.rb +27 -0
- data/spec/activerecord_spec.rb +19 -15
- data/spec/amqp_spec.rb +2 -2
- data/spec/defer_spec.rb +1 -1
- data/spec/em-mongo_spec.rb +6 -6
- data/spec/fiber_iterator_spec.rb +13 -0
- data/spec/helper/all.rb +2 -0
- data/spec/hiredis_spec.rb +5 -2
- data/spec/kernel_override_spec.rb +78 -0
- data/spec/multi_spec.rb +17 -1
- data/spec/redis_spec.rb +3 -3
- data/spec/remcached_spec.rb +3 -3
- data/spec/synchrony_spec.rb +15 -6
- data/spec/system_spec.rb +3 -5
- data/spec/widgets.sql +9 -0
- metadata +13 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: da875d80b703df6b9a5edcb8af8a005347e53a4a
|
4
|
+
data.tar.gz: 978abcb2c127704ef62362e97aac92fe71804f1c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b5b91b1d7b354a8ada8805d96eb16ff9ca5e66d1d0c76d259b27d0f1fc051fecb02c22856e7a5fbfa58fe04223923d2380db5ea543ebe253d4f59d1460fa9789
|
7
|
+
data.tar.gz: 62c7e7ffa573dd30654b92f0642a61905e2b071d8e5f312eaf0b52321754fcacfd9b59ef669b5fecfb56b16f43165c12b81d684c0d2b32a13c0d05242f42ccfd
|
data/.travis.yml
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
language: ruby
|
2
|
+
|
3
|
+
rvm:
|
4
|
+
- 1.9.3
|
5
|
+
|
6
|
+
sudo: false
|
7
|
+
|
8
|
+
services:
|
9
|
+
- mysql
|
10
|
+
- rabbitmq
|
11
|
+
- mongodb
|
12
|
+
- memcached
|
13
|
+
- redis-server
|
14
|
+
|
15
|
+
before_script:
|
16
|
+
- mysql < spec/widgets.sql
|
17
|
+
|
18
|
+
matrix:
|
19
|
+
include:
|
20
|
+
- env: "activerecord=3.2.21"
|
21
|
+
script: "bundle exec rspec spec/activerecord_spec.rb"
|
22
|
+
- env: "activerecord=4.0.9"
|
23
|
+
script: "bundle exec rspec spec/activerecord_spec.rb"
|
24
|
+
- env: "activerecord=4.2.0"
|
25
|
+
script: "bundle exec rspec spec/activerecord_spec.rb"
|
26
|
+
allow_failures:
|
27
|
+
- env: "activerecord=3.2.21"
|
28
|
+
- env: "activerecord=4.2.0"
|
data/Gemfile
CHANGED
@@ -1,19 +1,19 @@
|
|
1
|
-
source
|
1
|
+
source 'https://rubygems.org'
|
2
2
|
|
3
3
|
gem 'eventmachine', :git => 'git://github.com/eventmachine/eventmachine.git'
|
4
4
|
|
5
5
|
group :development do
|
6
6
|
gem 'rake'
|
7
|
-
gem 'rspec'
|
7
|
+
gem 'rspec', '~> 3.2'
|
8
8
|
gem 'em-http-request', :git => 'git://github.com/igrigorik/em-http-request'
|
9
9
|
gem 'remcached'
|
10
10
|
# gem 'em-mongo', :git => 'https://github.com/bcg/em-mongo.git'
|
11
|
-
gem 'activerecord', '
|
11
|
+
gem 'activerecord', "= #{ENV['activerecord'] || '4.1.8'}"
|
12
12
|
gem 'em-mongo'
|
13
13
|
gem 'bson_ext'
|
14
|
-
gem 'mysql2'
|
14
|
+
gem 'mysql2', '~> 0.3.18'
|
15
15
|
gem 'em-redis', '~> 0.3.0'
|
16
16
|
gem 'em-hiredis'
|
17
17
|
gem 'mongo'
|
18
|
-
gem 'amqp'
|
18
|
+
gem 'amqp', '= 1.0.0'
|
19
19
|
end
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Ilya Grigorik
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be included
|
12
|
+
in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
17
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
18
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
19
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
20
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/em-synchrony.png)](http://rubygems.org/gems/em-synchrony)
|
4
4
|
[![Analytics](https://ga-beacon.appspot.com/UA-71196-10/em-synchrony/readme)](https://github.com/igrigorik/ga-beacon)
|
5
|
+
[![Build Status](https://travis-ci.org/igrigorik/em-synchrony.svg?branch=master)](https://travis-ci.org/igrigorik/em-synchrony)
|
5
6
|
|
6
7
|
Collection of convenience classes and primitives to help untangle evented code, plus a number of patched EM clients to make them Fiber aware. To learn more, please see: [Untangling Evented Code with Ruby Fibers](http://www.igvita.com/2010/03/22/untangling-evented-code-with-ruby-fibers).
|
7
8
|
|
@@ -172,6 +173,31 @@ ActiveRecord::Base.establish_connection(
|
|
172
173
|
result = Widget.all.to_a
|
173
174
|
```
|
174
175
|
|
176
|
+
## Hooks
|
177
|
+
|
178
|
+
em-synchrony already provides fiber-aware calls for sleep, system and defer. When mixing fiber-aware code with other gems, these might use non-fiber-aware versions which result in unexpected behavior: calling `sleep` would pause the whole reactor instead of
|
179
|
+
a single fiber. For that reason, hooks into the Kernel are provided to override the default behavior to e.g. add logging or redirect these calls their fiber-aware versions.
|
180
|
+
|
181
|
+
```ruby
|
182
|
+
# Adding logging but still executes the actual sleep
|
183
|
+
require "em-synchrony"
|
184
|
+
log = Logger.new(STDOUT)
|
185
|
+
EM::Synchrony.on_sleep do |*args|
|
186
|
+
log.warn "Kernel.sleep called by:"
|
187
|
+
caller.each { |line| log.warn line }
|
188
|
+
sleep(*args) # Calls the actual sleep
|
189
|
+
end
|
190
|
+
```
|
191
|
+
|
192
|
+
```ruby
|
193
|
+
# Redirects to EM::Synchrony.sleep
|
194
|
+
require "em-synchrony"
|
195
|
+
log = Logger.new(STDOUT)
|
196
|
+
EM::Synchrony.on_sleep do |*args|
|
197
|
+
EM::Synchrony.sleep(*args)
|
198
|
+
end
|
199
|
+
```
|
200
|
+
|
175
201
|
# License
|
176
202
|
|
177
203
|
The MIT License - Copyright (c) 2011 Ilya Grigorik
|
data/em-synchrony.gemspec
CHANGED
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
|
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "em-synchrony"
|
6
|
-
s.version = "1.0.
|
6
|
+
s.version = "1.0.5"
|
7
7
|
s.platform = Gem::Platform::RUBY
|
8
8
|
s.authors = ["Ilya Grigorik"]
|
9
9
|
s.email = ["ilya@igvita.com"]
|
@@ -12,8 +12,6 @@ Gem::Specification.new do |s|
|
|
12
12
|
s.summary = %q{Fiber aware EventMachine libraries}
|
13
13
|
s.description = s.summary
|
14
14
|
|
15
|
-
s.rubyforge_project = "em-synchrony"
|
16
|
-
|
17
15
|
s.add_runtime_dependency("eventmachine", ">= 1.0.0.beta.1")
|
18
16
|
|
19
17
|
s.files = `git ls-files`.split("\n")
|
@@ -41,7 +41,12 @@ module ActiveRecord
|
|
41
41
|
include EM::Synchrony::ActiveRecord::Client
|
42
42
|
end
|
43
43
|
|
44
|
-
|
44
|
+
if ::ActiveRecord.version >= Gem::Version.new('4.2')
|
45
|
+
require 'em-synchrony/activerecord_4_2'
|
46
|
+
include EM::Synchrony::ActiveRecord::Adapter_4_2
|
47
|
+
else
|
48
|
+
include EM::Synchrony::ActiveRecord::Adapter
|
49
|
+
end
|
45
50
|
end
|
46
51
|
end
|
47
52
|
end
|
data/lib/em-synchrony.rb
CHANGED
@@ -15,6 +15,7 @@ require "em-synchrony/tcpsocket"
|
|
15
15
|
require "em-synchrony/connection_pool"
|
16
16
|
require "em-synchrony/keyboard"
|
17
17
|
require "em-synchrony/iterator" if EventMachine::VERSION > '0.12.10'
|
18
|
+
require "em-synchrony/kernel"
|
18
19
|
|
19
20
|
module EventMachine
|
20
21
|
|
@@ -130,6 +131,15 @@ module EventMachine
|
|
130
131
|
Fiber.yield
|
131
132
|
end
|
132
133
|
|
134
|
+
# Overrides behavior of kernel.sleep
|
135
|
+
# Allows to add custom behavior, e.g. logging or redirection to
|
136
|
+
# EM::Synchrony.sleep .
|
137
|
+
# Calling 'sleep' in this function calls the actual kernel method.
|
138
|
+
#
|
139
|
+
def self.on_sleep(&blk)
|
140
|
+
Kernel.em_synchrony_sleep_hook = blk
|
141
|
+
end
|
142
|
+
|
133
143
|
# Routes to EM::Synchrony::Keyboard
|
134
144
|
#
|
135
145
|
def self.gets
|
@@ -145,3 +155,6 @@ module EventMachine
|
|
145
155
|
end
|
146
156
|
end
|
147
157
|
end
|
158
|
+
|
159
|
+
# Alias for {EventMachine::Synchrony}
|
160
|
+
EM::S = EventMachine::Synchrony
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
module EM::Synchrony
|
4
|
+
module ActiveRecord
|
5
|
+
module Adapter_4_2
|
6
|
+
def configure_connection
|
7
|
+
nil
|
8
|
+
end
|
9
|
+
|
10
|
+
def transaction(*args)
|
11
|
+
@connection.execute(false) do |conn|
|
12
|
+
super
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def reset_transaction #:nodoc:
|
17
|
+
@transaction_manager = TransactionManager.new(self)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class TransactionManager < ::ActiveRecord::ConnectionAdapters::TransactionManager
|
22
|
+
def initialize(*args)
|
23
|
+
super
|
24
|
+
@stack = Hash.new { |h, k| h[k] = [] }
|
25
|
+
end
|
26
|
+
|
27
|
+
def current_transaction #:nodoc:
|
28
|
+
_current_stack.last || NULL_TRANSACTION
|
29
|
+
end
|
30
|
+
|
31
|
+
def open_transactions
|
32
|
+
_current_stack.size
|
33
|
+
end
|
34
|
+
|
35
|
+
def begin_transaction(options = {}) #:nodoc:
|
36
|
+
transaction =
|
37
|
+
if _current_stack.empty?
|
38
|
+
::ActiveRecord::ConnectionAdapters::RealTransaction.new(@connection, options)
|
39
|
+
else
|
40
|
+
::ActiveRecord::ConnectionAdapters::SavepointTransaction.new(@connection, "active_record_#{Fiber.current.object_id}_#{open_transactions}", options)
|
41
|
+
end
|
42
|
+
_current_stack.push(transaction)
|
43
|
+
transaction
|
44
|
+
end
|
45
|
+
|
46
|
+
def commit_transaction #:nodoc:
|
47
|
+
_current_stack.pop.commit
|
48
|
+
end
|
49
|
+
|
50
|
+
def rollback_transaction #:nodoc:
|
51
|
+
_current_stack.pop.rollback
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def _current_stack
|
57
|
+
@stack[Fiber.current.object_id]
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -13,11 +13,11 @@ module EventMachine
|
|
13
13
|
def add(name, conn)
|
14
14
|
raise 'Duplicate Multi key' if @requests.key? name
|
15
15
|
|
16
|
+
@requests[name] = conn
|
17
|
+
|
16
18
|
fiber = Fiber.current
|
17
19
|
conn.callback { @responses[:callback][name] = conn; check_progress(fiber) }
|
18
20
|
conn.errback { @responses[:errback][name] = conn; check_progress(fiber) }
|
19
|
-
|
20
|
-
@requests[name] = conn
|
21
21
|
end
|
22
22
|
|
23
23
|
def finished?
|
@@ -7,7 +7,7 @@ module EventMachine
|
|
7
7
|
# and auto-advance the iterator after each call
|
8
8
|
def each(foreach=nil, after=nil, &blk)
|
9
9
|
fe = Proc.new do |obj, iter|
|
10
|
-
Fiber.new { (foreach || blk).call(obj); iter.next }.resume
|
10
|
+
Fiber.new { (foreach || blk).call(obj, iter); iter.next }.resume
|
11
11
|
end
|
12
12
|
|
13
13
|
super(fe, after)
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'em-synchrony'
|
3
|
+
|
4
|
+
# Monkey-patch
|
5
|
+
module Kernel
|
6
|
+
alias_method :orig_sleep, :sleep
|
7
|
+
|
8
|
+
class << self
|
9
|
+
attr_accessor :em_synchrony_sleep_hook
|
10
|
+
end
|
11
|
+
|
12
|
+
# Monkey-patch
|
13
|
+
def sleep(sleep_time)
|
14
|
+
if Kernel.em_synchrony_sleep_hook &&
|
15
|
+
EM.reactor_thread? &&
|
16
|
+
!Thread.current[:em_synchrony_sleep_hook_called]
|
17
|
+
begin
|
18
|
+
Thread.current[:em_synchrony_sleep_hook_called] = true
|
19
|
+
Kernel.em_synchrony_sleep_hook.call(sleep_time)
|
20
|
+
ensure
|
21
|
+
Thread.current[:em_synchrony_sleep_hook_called] = false
|
22
|
+
end
|
23
|
+
else
|
24
|
+
orig_sleep(sleep_time)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/spec/activerecord_spec.rb
CHANGED
@@ -2,28 +2,31 @@ require "spec/helper/all"
|
|
2
2
|
require "em-synchrony/activerecord"
|
3
3
|
require "em-synchrony/fiber_iterator"
|
4
4
|
|
5
|
-
#
|
6
|
-
# use widgets;
|
7
|
-
# create table widgets (
|
8
|
-
# id INT NOT NULL AUTO_INCREMENT,
|
9
|
-
# title varchar(255),
|
10
|
-
# PRIMARY KEY (`id`)
|
11
|
-
# );
|
5
|
+
# mysql < spec/widgets.sql
|
12
6
|
|
13
7
|
class Widget < ActiveRecord::Base; end;
|
14
8
|
|
15
9
|
describe "Fiberized ActiveRecord driver for mysql2" do
|
16
10
|
DELAY = 0.25
|
17
11
|
QUERY = "SELECT sleep(#{DELAY})"
|
12
|
+
LOGGER = Logger.new(STDOUT).tap do |logger|
|
13
|
+
logger.formatter = proc do |_severity, datetime, _progname, msg|
|
14
|
+
"[#{datetime.strftime('%Y-%m-%d %H:%M:%S')} ##{Fiber.current.object_id}] -- : #{msg}\n"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
before(:all) do
|
19
|
+
ActiveRecord::Base.logger = LOGGER if ENV['LOGGER']
|
20
|
+
end
|
18
21
|
|
19
22
|
def establish_connection
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
23
|
+
ActiveRecord::Base.establish_connection(
|
24
|
+
:adapter => 'em_mysql2',
|
25
|
+
:database => 'widgets',
|
26
|
+
:username => 'root',
|
27
|
+
:pool => 10
|
28
|
+
)
|
29
|
+
Widget.delete_all
|
27
30
|
end
|
28
31
|
|
29
32
|
it "should establish AR connection" do
|
@@ -95,6 +98,7 @@ describe "Fiberized ActiveRecord driver for mysql2" do
|
|
95
98
|
widget.update_attributes title: "hello"
|
96
99
|
end
|
97
100
|
ActiveRecord::Base.transaction do
|
101
|
+
widget.update_attributes(title: 'hey')
|
98
102
|
raise ActiveRecord::Rollback
|
99
103
|
end
|
100
104
|
end
|
@@ -106,4 +110,4 @@ describe "Fiberized ActiveRecord driver for mysql2" do
|
|
106
110
|
end
|
107
111
|
end
|
108
112
|
|
109
|
-
end
|
113
|
+
end
|
data/spec/amqp_spec.rb
CHANGED
@@ -5,7 +5,7 @@ describe EM::Synchrony::AMQP do
|
|
5
5
|
it "should yield until connection is ready" do
|
6
6
|
EM.synchrony do
|
7
7
|
connection = EM::Synchrony::AMQP.connect
|
8
|
-
connection.connected?.should
|
8
|
+
connection.connected?.should eq(true)
|
9
9
|
EM.stop
|
10
10
|
end
|
11
11
|
end
|
@@ -14,7 +14,7 @@ describe EM::Synchrony::AMQP do
|
|
14
14
|
EM.synchrony do
|
15
15
|
connection = EM::Synchrony::AMQP.connect
|
16
16
|
connection.disconnect
|
17
|
-
connection.connected?.should
|
17
|
+
connection.connected?.should eq(false)
|
18
18
|
EM.stop
|
19
19
|
end
|
20
20
|
end
|
data/spec/defer_spec.rb
CHANGED
data/spec/em-mongo_spec.rb
CHANGED
@@ -4,10 +4,10 @@ describe EM::Mongo do
|
|
4
4
|
it "should yield until connection is ready" do
|
5
5
|
EventMachine.synchrony do
|
6
6
|
connection = EM::Mongo::Connection.new
|
7
|
-
connection.connected?.should
|
7
|
+
connection.connected?.should eq(true)
|
8
8
|
|
9
9
|
db = connection.db('db')
|
10
|
-
db.is_a?(EventMachine::Mongo::Database).should
|
10
|
+
db.is_a?(EventMachine::Mongo::Database).should eq(true)
|
11
11
|
|
12
12
|
EventMachine.stop
|
13
13
|
end
|
@@ -45,7 +45,7 @@ describe EM::Mongo do
|
|
45
45
|
obj2.size.should == 1
|
46
46
|
|
47
47
|
obj3 = collection.first
|
48
|
-
obj3.is_a?(Hash).should
|
48
|
+
obj3.is_a?(Hash).should eq(true)
|
49
49
|
|
50
50
|
EventMachine.stop
|
51
51
|
end
|
@@ -114,7 +114,7 @@ describe EM::Mongo do
|
|
114
114
|
obj2.size.should == 1
|
115
115
|
end
|
116
116
|
collection.afirst.callback do |obj3|
|
117
|
-
obj3.is_a?(Hash).should
|
117
|
+
obj3.is_a?(Hash).should eq(true)
|
118
118
|
obj3['hello'].should == 'world'
|
119
119
|
EM.next_tick{ EventMachine.stop }
|
120
120
|
end
|
@@ -181,7 +181,7 @@ describe EM::Mongo do
|
|
181
181
|
obj2.size.should == 1
|
182
182
|
end
|
183
183
|
collection.afirst do |obj3|
|
184
|
-
obj3.is_a?(Hash).should
|
184
|
+
obj3.is_a?(Hash).should eq(true)
|
185
185
|
obj3['hello'].should == 'world'
|
186
186
|
EM.next_tick{ EventMachine.stop }
|
187
187
|
end
|
@@ -233,7 +233,7 @@ describe EM::Mongo do
|
|
233
233
|
|
234
234
|
context "authentication" do
|
235
235
|
# these specs only get asserted if you run mongod with the --auth flag
|
236
|
-
it "successfully authenticates" do
|
236
|
+
it "successfully authenticates", ci_skip: true do
|
237
237
|
# For this spec you will need to add this user to the database
|
238
238
|
#
|
239
239
|
# From the Mongo shell:
|
data/spec/fiber_iterator_spec.rb
CHANGED
@@ -18,6 +18,19 @@ describe EventMachine::Synchrony::FiberIterator do
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
+
it "works even with nested arrays and iterator" do
|
22
|
+
EM.synchrony do
|
23
|
+
results = []
|
24
|
+
list = [[1, 2], [3, 4]]
|
25
|
+
|
26
|
+
EM::Synchrony::FiberIterator.new(list, 2).each { |sublist, iter| results.push(sublist) }
|
27
|
+
|
28
|
+
expect(results).to eq(list)
|
29
|
+
|
30
|
+
EM.stop
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
21
34
|
#
|
22
35
|
# it "should sum values within the iterator" do
|
23
36
|
# EM.synchrony do
|
data/spec/helper/all.rb
CHANGED
data/spec/hiredis_spec.rb
CHANGED
@@ -2,10 +2,13 @@ require "spec/helper/all"
|
|
2
2
|
|
3
3
|
describe EM::Hiredis do
|
4
4
|
|
5
|
-
it "should
|
5
|
+
it "should connect on demand" do
|
6
6
|
EventMachine.synchrony do
|
7
7
|
connection = EM::Hiredis::Client.connect
|
8
|
-
connection.
|
8
|
+
connection.should_not be_connected
|
9
|
+
|
10
|
+
connection.ping
|
11
|
+
connection.should be_connected
|
9
12
|
|
10
13
|
EventMachine.stop
|
11
14
|
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'spec/helper/all'
|
3
|
+
|
4
|
+
describe EventMachine::Synchrony do
|
5
|
+
before do
|
6
|
+
EM::Synchrony.on_sleep
|
7
|
+
end
|
8
|
+
after do
|
9
|
+
EM::Synchrony.on_sleep
|
10
|
+
end
|
11
|
+
describe '#sleep' do
|
12
|
+
context 'outside synchrony' do
|
13
|
+
it 'does not call hook' do
|
14
|
+
EM::Synchrony.on_sleep { fail 'should not happen' }
|
15
|
+
expect { sleep(0.01) }.not_to raise_error
|
16
|
+
end
|
17
|
+
context 'with synchrony in another thread'do
|
18
|
+
before do
|
19
|
+
@thread = Thread.new do
|
20
|
+
EM.run do
|
21
|
+
sleep(0.5)
|
22
|
+
EM.stop
|
23
|
+
end
|
24
|
+
end
|
25
|
+
sleep(0.1)
|
26
|
+
end
|
27
|
+
after do
|
28
|
+
@thread.join
|
29
|
+
end
|
30
|
+
it 'does not call hook' do
|
31
|
+
EM::Synchrony.on_sleep { fail 'should not happen' }
|
32
|
+
expect { sleep(0.01) }.not_to raise_error
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'within synchrony' do
|
38
|
+
around do |example|
|
39
|
+
EM.synchrony do
|
40
|
+
example.run
|
41
|
+
EM.next_tick { EM.stop }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
context 'with no hook defined' do
|
45
|
+
it 'calls Kernel.sleep' do
|
46
|
+
expect(self).to receive(:sleep)
|
47
|
+
sleep(1)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
context 'with hook defined' do
|
51
|
+
it 'executes the hook' do
|
52
|
+
called = 0
|
53
|
+
EM::Synchrony.on_sleep { called += 1 }
|
54
|
+
(1..10).each do |count|
|
55
|
+
sleep(1)
|
56
|
+
expect(called).to be count
|
57
|
+
end
|
58
|
+
end
|
59
|
+
it 'propagates exceptions' do
|
60
|
+
msg = 'expected exception'
|
61
|
+
EM::Synchrony.on_sleep { fail msg }
|
62
|
+
expect { sleep(1) }.to raise_error(RuntimeError, msg)
|
63
|
+
end
|
64
|
+
context "when calling 'sleep' in the hook" do
|
65
|
+
it 'calls the original sleep' do
|
66
|
+
sleep_time = 1.213234123412341454134512345
|
67
|
+
expect(self).to receive(:orig_sleep).with(sleep_time)
|
68
|
+
EM::Synchrony.on_sleep do |*args|
|
69
|
+
sleep(*args)
|
70
|
+
end
|
71
|
+
sleep(sleep_time)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
data/spec/multi_spec.rb
CHANGED
@@ -9,5 +9,21 @@ describe EM::Synchrony do
|
|
9
9
|
m.add :df1, EM::DefaultDeferrable.new
|
10
10
|
end.should raise_error("Duplicate Multi key")
|
11
11
|
end
|
12
|
+
|
13
|
+
context "when defferable succeeded before adding" do
|
14
|
+
it "does not succeed twice" do
|
15
|
+
multi = EM::Synchrony::Multi.new
|
16
|
+
multi.should_receive(:succeed).once
|
17
|
+
|
18
|
+
slow = EM::DefaultDeferrable.new
|
19
|
+
multi.add :slow, slow
|
20
|
+
|
21
|
+
quick = EM::DefaultDeferrable.new
|
22
|
+
quick.succeed
|
23
|
+
multi.add :quick, quick
|
24
|
+
|
25
|
+
slow.succeed
|
26
|
+
end
|
27
|
+
end
|
12
28
|
end
|
13
|
-
end
|
29
|
+
end
|
data/spec/redis_spec.rb
CHANGED
@@ -5,7 +5,7 @@ describe EM::Protocols::Redis do
|
|
5
5
|
it "should yield until connection is ready" do
|
6
6
|
EventMachine.synchrony do
|
7
7
|
connection = EM::Protocols::Redis.connect
|
8
|
-
connection.connected.should
|
8
|
+
connection.connected.should eq(true)
|
9
9
|
|
10
10
|
EventMachine.stop
|
11
11
|
end
|
@@ -90,7 +90,7 @@ describe EM::Protocols::Redis do
|
|
90
90
|
end
|
91
91
|
end
|
92
92
|
|
93
|
-
it "should execute sync add and auth" do
|
93
|
+
it "should execute sync add and auth", ci_skip: true do
|
94
94
|
EventMachine.synchrony do
|
95
95
|
redis = EM::Protocols::Redis.connect
|
96
96
|
redis.auth('abc')
|
@@ -102,4 +102,4 @@ describe EM::Protocols::Redis do
|
|
102
102
|
EM.stop
|
103
103
|
end
|
104
104
|
end
|
105
|
-
end
|
105
|
+
end
|
data/spec/remcached_spec.rb
CHANGED
@@ -6,7 +6,7 @@ describe Memcached do
|
|
6
6
|
it "should yield until connection is ready" do
|
7
7
|
EventMachine.synchrony do
|
8
8
|
Memcached.connect %w(localhost)
|
9
|
-
Memcached.usable?.should
|
9
|
+
Memcached.usable?.should eq(true)
|
10
10
|
EventMachine.stop
|
11
11
|
end
|
12
12
|
end
|
@@ -28,9 +28,9 @@ describe Memcached do
|
|
28
28
|
end
|
29
29
|
|
30
30
|
it "should fire multi memcached requests" do
|
31
|
-
|
32
|
-
pending "patch mult-get"
|
31
|
+
skip "patch mult-get"
|
33
32
|
|
33
|
+
EventMachine.synchrony do
|
34
34
|
Memcached.connect %w(localhost)
|
35
35
|
Memcached.multi_get([{:key => 'foo'},{:key => 'bar'}, {:key => 'test'}]) do |res|
|
36
36
|
# TODO
|
data/spec/synchrony_spec.rb
CHANGED
@@ -19,14 +19,23 @@ describe EM::Synchrony do
|
|
19
19
|
|
20
20
|
describe "#next_tick" do
|
21
21
|
it "should wrap next_tick into a Fiber context" do
|
22
|
-
|
23
|
-
|
22
|
+
EM.synchrony {
|
23
|
+
begin
|
24
|
+
fired = false
|
25
|
+
f = Fiber.current
|
26
|
+
|
27
|
+
EM::Synchrony.next_tick do
|
28
|
+
fired = true
|
29
|
+
Fiber.current.should_not eq(f)
|
30
|
+
end
|
24
31
|
|
25
|
-
|
26
|
-
|
27
|
-
|
32
|
+
EM::Synchrony.interrupt
|
33
|
+
|
34
|
+
fired.should eq(true)
|
35
|
+
ensure
|
36
|
+
EM.stop
|
28
37
|
end
|
29
|
-
}
|
38
|
+
}
|
30
39
|
end
|
31
40
|
end
|
32
41
|
|
data/spec/system_spec.rb
CHANGED
data/spec/widgets.sql
ADDED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: em-synchrony
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ilya Grigorik
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-04-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: eventmachine
|
@@ -33,7 +33,9 @@ extra_rdoc_files: []
|
|
33
33
|
files:
|
34
34
|
- ".gitignore"
|
35
35
|
- ".rspec"
|
36
|
+
- ".travis.yml"
|
36
37
|
- Gemfile
|
38
|
+
- LICENSE
|
37
39
|
- README.md
|
38
40
|
- Rakefile
|
39
41
|
- em-synchrony.gemspec
|
@@ -48,6 +50,7 @@ files:
|
|
48
50
|
- lib/active_record/connection_adapters/em_mysql2_adapter.rb
|
49
51
|
- lib/em-synchrony.rb
|
50
52
|
- lib/em-synchrony/activerecord.rb
|
53
|
+
- lib/em-synchrony/activerecord_4_2.rb
|
51
54
|
- lib/em-synchrony/amqp.rb
|
52
55
|
- lib/em-synchrony/connection_pool.rb
|
53
56
|
- lib/em-synchrony/core_ext.rb
|
@@ -61,6 +64,7 @@ files:
|
|
61
64
|
- lib/em-synchrony/em-remcached.rb
|
62
65
|
- lib/em-synchrony/fiber_iterator.rb
|
63
66
|
- lib/em-synchrony/iterator.rb
|
67
|
+
- lib/em-synchrony/kernel.rb
|
64
68
|
- lib/em-synchrony/keyboard.rb
|
65
69
|
- lib/em-synchrony/mechanize.rb
|
66
70
|
- lib/em-synchrony/mongo.rb
|
@@ -83,6 +87,7 @@ files:
|
|
83
87
|
- spec/http_spec.rb
|
84
88
|
- spec/inlinesync_spec.rb
|
85
89
|
- spec/iterator_spec.rb
|
90
|
+
- spec/kernel_override_spec.rb
|
86
91
|
- spec/keyboard_spec.rb
|
87
92
|
- spec/memcache_spec.rb
|
88
93
|
- spec/mongo_spec.rb
|
@@ -95,6 +100,7 @@ files:
|
|
95
100
|
- spec/tcpsocket_spec.rb
|
96
101
|
- spec/thread_spec.rb
|
97
102
|
- spec/timer_spec.rb
|
103
|
+
- spec/widgets.sql
|
98
104
|
homepage: http://github.com/igrigorik/em-synchrony
|
99
105
|
licenses:
|
100
106
|
- MIT
|
@@ -114,8 +120,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
114
120
|
- !ruby/object:Gem::Version
|
115
121
|
version: '0'
|
116
122
|
requirements: []
|
117
|
-
rubyforge_project:
|
118
|
-
rubygems_version: 2.4.
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 2.4.5.1
|
119
125
|
signing_key:
|
120
126
|
specification_version: 4
|
121
127
|
summary: Fiber aware EventMachine libraries
|
@@ -135,6 +141,7 @@ test_files:
|
|
135
141
|
- spec/http_spec.rb
|
136
142
|
- spec/inlinesync_spec.rb
|
137
143
|
- spec/iterator_spec.rb
|
144
|
+
- spec/kernel_override_spec.rb
|
138
145
|
- spec/keyboard_spec.rb
|
139
146
|
- spec/memcache_spec.rb
|
140
147
|
- spec/mongo_spec.rb
|
@@ -147,3 +154,5 @@ test_files:
|
|
147
154
|
- spec/tcpsocket_spec.rb
|
148
155
|
- spec/thread_spec.rb
|
149
156
|
- spec/timer_spec.rb
|
157
|
+
- spec/widgets.sql
|
158
|
+
has_rdoc:
|