Skip to content
Browse files

Holy whitespace, Batman

  • Loading branch information...
1 parent 1b3825f commit 9568e2f62a6ed977708fd041b70f81b6641c943a @tarcieri tarcieri committed
View
2 .document
@@ -1,5 +1,5 @@
lib/**/*.rb
bin/*
--
+-
features/**/*.feature
LICENSE.txt
View
4 .rspec
@@ -1,4 +1,4 @@
---color
---format documentation
+--color
+--format documentation
--backtrace
--default_path spec
View
4 benchmarks/actor.rb
@@ -26,10 +26,10 @@ def example_method; end
ips.report("spawn") { ExampleActor.new.terminate }
ips.report("calls") { example_actor.example_method }
ips.report("async calls") { example_actor.example_method! }
-
+
ips.report("messages") do |n|
latch_in << n
for i in 0..n; mailbox << :message; end
latch_out.pop
end
-end
+end
View
6 examples/pool.rb
@@ -14,7 +14,7 @@
class Rehasher
include Celluloid
-
+
def rehash(string, rounds)
raise ArgumentError, "hurr" unless rounds > 1
penultimate = (rounds - 1).times.inject(string) { |s| Digest::SHA512.digest(s) }
@@ -30,10 +30,10 @@ def rehash(string, rounds)
elsif RUBY_ENGINE == "ruby"
puts "Sorry, this Ruby interpreter has a GIL, so this will only use one core"
end
-
+
futures = %w(i am the very model of a modern major general).map do |word|
pool.future(:rehash, word, 1_000_000)
end
p futures.map(&:value)
-end
+end
View
10 lib/celluloid.rb
@@ -132,7 +132,7 @@ def supervise(*args, &block)
def supervise_as(name, *args, &block)
Supervisor.supervise_as(name, self, *args, &block)
end
-
+
# Create a new pool of workers. Accepts the following options:
#
# * size: how many workers to create. Default is worker per CPU core
@@ -141,7 +141,7 @@ def supervise_as(name, *args, &block)
def pool(options = {})
PoolManager.new(self, options)
end
-
+
# Run an actor in the foreground
def run(*args, &block)
new(*args, &block).join
@@ -310,12 +310,12 @@ def defer(&block)
# Celluloid::Future, which uses a thread from InternalPool to run the block
Future.new(&block).value
end
-
+
# Handle async calls within an actor itself
def async(meth, *args, &block)
Actor.async Thread.current[:actor].mailbox, meth, *args, &block
end
-
+
# Handle calls to future within an actor itself
def future(meth, *args, &block)
Actor.future Thread.current[:actor].mailbox, meth, *args, &block
@@ -327,7 +327,7 @@ def method_missing(meth, *args, &block)
if meth.to_s.match(/!$/)
unbanged_meth = meth.to_s.sub(/!$/, '')
args.unshift unbanged_meth
-
+
call = AsyncCall.new(nil, :__send__, args, block)
begin
Thread.current[:actor].mailbox << call
View
6 lib/celluloid/actor.rb
@@ -29,14 +29,14 @@ def current
raise NotActorError, "not in actor scope" unless actor
actor.proxy
end
-
+
# Obtain the name of the current actor
def name
actor = Thread.current[:actor]
raise NotActorError, "not in actor scope" unless actor
actor.name
end
-
+
# Invoke a method on the given actor via its mailbox
def call(mailbox, meth, *args, &block)
call = SyncCall.new(Thread.mailbox, meth, args, block)
@@ -108,7 +108,7 @@ def initialize(subject)
Thread.current[:mailbox] = @mailbox
run
end
-
+
@proxy = ActorProxy.new(self)
end
View
6 lib/celluloid/actor_proxy.rb
@@ -50,7 +50,7 @@ def inspect
rescue DeadActorError
"#<Celluloid::Actor(#{@klass}) dead>"
end
-
+
# Make an asynchronous call to an actor, for those who don't like the
# predicate syntax. TIMTOWTDI!
def async(method_name, *args, &block)
@@ -73,7 +73,7 @@ def terminate!
raise DeadActorError, "actor already terminated" unless alive?
@mailbox.system_event TerminationRequest.new
end
-
+
# Forcibly kill a given actor
def kill
@thread.kill
@@ -82,7 +82,7 @@ def kill
rescue DeadActorError
end
end
-
+
# Wait for an actor to terminate
def join
@thread.join
View
12 lib/celluloid/events.rb
@@ -1,26 +1,26 @@
module Celluloid
# Exceptional system events which need to be processed out of band
class SystemEvent < Exception; end
-
+
# An actor has exited for the given reason
class ExitEvent < SystemEvent
attr_reader :actor, :reason
-
+
def initialize(actor, reason = nil)
@actor, @reason = actor, reason
super reason.to_s
end
end
-
+
# Name an actor at the time it's registered
class NamingRequest < SystemEvent
attr_reader :name
-
+
def initialize(name)
@name = name
end
end
-
+
# Request for an actor to terminate
class TerminationRequest < SystemEvent; end
-end
+end
View
20 lib/celluloid/pool_manager.rb
@@ -1,25 +1,25 @@
module Celluloid
# Manages a fixed-size pool of workers
- # Delegates work (i.e. methods) and supervises workers
+ # Delegates work (i.e. methods) and supervises workers
class PoolManager
include Celluloid
trap_exit :crash_handler
-
+
def initialize(worker_class, options = {})
@size = options[:size]
raise ArgumentError, "minimum pool size is 2" if @size && @size < 2
-
+
@size ||= [Celluloid.cores, 2].max
@args = options[:args] ? Array(options[:args]) : []
-
+
@worker_class = worker_class
@idle = @size.times.map { worker_class.new_link(*@args) }
end
-
+
# Execute the given method within a worker
def execute(method, *args, &block)
worker = provision_worker
-
+
begin
worker._send_ method, *args, &block
rescue => ex
@@ -28,7 +28,7 @@ def execute(method, *args, &block)
@idle << worker if worker.alive?
end
end
-
+
# Provision a new worker
def provision_worker
while @idle.empty?
@@ -45,11 +45,11 @@ def crash_handler(actor, reason)
return unless reason # don't restart workers that exit cleanly
@idle << @worker_class.new_link(*@args)
end
-
+
def respond_to?(method)
super || (@worker_class ? @worker_class.instance_methods.include?(method.to_sym) : false)
end
-
+
def method_missing(method, *args, &block)
if respond_to?(method)
execute method, *args, &block
@@ -58,4 +58,4 @@ def method_missing(method, *args, &block)
end
end
end
-end
+end
View
2 lib/celluloid/registry.rb
@@ -16,7 +16,7 @@ def []=(name, actor)
@@registry_lock.synchronize do
@@registry[name.to_sym] = actor
end
-
+
actor.mailbox.system_event NamingRequest.new(name.to_sym)
end
View
10 lib/celluloid/thread_handle.rb
@@ -6,7 +6,7 @@ class ThreadHandle
def initialize
@mutex = Mutex.new
@join = ConditionVariable.new
-
+
@thread = InternalPool.get do
begin
yield
@@ -18,21 +18,21 @@ def initialize
end
end
end
-
+
# Is the thread running?
def alive?
@mutex.synchronize { @thread.alive? if @thread }
end
-
+
# Forcibly kill the thread
def kill
!!@mutex.synchronize { @thread.kill if @thread }
end
-
+
# Join to a running thread, blocking until it terminates
def join
@mutex.synchronize { @join.wait(@mutex) if @thread }
nil
end
end
-end
+end
View
8 spec/celluloid/future_spec.rb
@@ -5,16 +5,16 @@
future = Celluloid::Future.new { 40 + 2 }
future.value.should == 42
end
-
+
it "passes arguments to future blocks" do
future = Celluloid::Future.new(40) { |n| n + 2 }
future.value.should == 42
end
-
+
it "reraises exceptions that occur when the value is retrieved" do
class ExampleError < StandardError; end
-
+
future = Celluloid::Future.new { raise ExampleError, "oh noes crash!" }
expect { future.value }.to raise_exception(ExampleError)
end
-end
+end
View
16 spec/celluloid/pool_spec.rb
@@ -3,10 +3,10 @@
describe "Celluloid.pool" do
before do
class ExampleError < StandardError; end
-
+
class MyWorker
include Celluloid
-
+
def process(queue = nil)
if queue
queue << :done
@@ -14,27 +14,27 @@ def process(queue = nil)
:done
end
end
-
+
def crash
raise ExampleError, "zomgcrash"
end
end
end
-
+
subject { MyWorker.pool }
-
+
it "processes work units synchronously" do
subject.process.should == :done
end
-
+
it "processes work units asynchronously" do
queue = Queue.new
subject.process!(queue)
queue.pop.should == :done
end
-
+
it "handles crashes" do
expect { subject.crash }.to raise_error(ExampleError)
subject.process.should == :done
end
-end
+end
View
2 spec/celluloid/registry_spec.rb
@@ -24,7 +24,7 @@ def sing_for(person)
Celluloid::Actor[:marilyn] = Marilyn.new
Celluloid::Actor.registered.should include :marilyn
end
-
+
it "knows its name once registered" do
Celluloid::Actor[:marilyn] = Marilyn.new
Celluloid::Actor[:marilyn].name.should == :marilyn
View
36 spec/celluloid/supervisor_spec.rb
@@ -3,15 +3,15 @@
describe Celluloid::Supervisor do
before do
class SubordinateDead < StandardError; end
-
+
class Subordinate
include Celluloid
attr_reader :state
-
+
def initialize(state)
@state = state
end
-
+
def crack_the_whip
case @state
when :idle
@@ -21,74 +21,74 @@ def crack_the_whip
end
end
end
-
+
it "restarts actors when they die" do
supervisor = Celluloid::Supervisor.supervise(Subordinate, :idle)
subordinate = supervisor.actor
subordinate.state.should == :idle
-
+
subordinate.crack_the_whip
subordinate.state.should == :working
-
+
expect do
subordinate.crack_the_whip
end.to raise_exception(SubordinateDead)
sleep 0.1 # hax to prevent race :(
subordinate.should_not be_alive
-
+
new_subordinate = supervisor.actor
new_subordinate.should_not == subordinate
new_subordinate.state.should == :idle
end
-
+
it "registers actors and reregisters them when they die" do
supervisor = Celluloid::Supervisor.supervise_as(:subordinate, Subordinate, :idle)
subordinate = Celluloid::Actor[:subordinate]
subordinate.state.should == :idle
-
+
subordinate.crack_the_whip
subordinate.state.should == :working
-
+
expect do
subordinate.crack_the_whip
end.to raise_exception(SubordinateDead)
sleep 0.1 # hax to prevent race :(
subordinate.should_not be_alive
-
+
new_subordinate = Celluloid::Actor[:subordinate]
new_subordinate.should_not == subordinate
new_subordinate.state.should == :idle
end
-
+
it "creates supervisors via Actor.supervise" do
supervisor = Subordinate.supervise(:working)
subordinate = supervisor.actor
subordinate.state.should == :working
-
+
expect do
subordinate.crack_the_whip
end.to raise_exception(SubordinateDead)
sleep 0.1 # hax to prevent race :(
subordinate.should_not be_alive
-
+
new_subordinate = supervisor.actor
new_subordinate.should_not == subordinate
new_subordinate.state.should == :working
end
-
+
it "creates supervisors and registers actors via Actor.supervise_as" do
supervisor = Subordinate.supervise_as(:subordinate, :working)
subordinate = Celluloid::Actor[:subordinate]
subordinate.state.should == :working
-
+
expect do
subordinate.crack_the_whip
end.to raise_exception(SubordinateDead)
sleep 0.1 # hax to prevent race :(
subordinate.should_not be_alive
-
+
new_subordinate = supervisor.actor
new_subordinate.should_not == subordinate
new_subordinate.state.should == :working
end
-end
+end
View
8 spec/celluloid/thread_handle_spec.rb
@@ -5,14 +5,14 @@
queue = Queue.new
handle = Celluloid::ThreadHandle.new { queue.pop }
handle.should be_alive
-
+
queue << :die
-
+
sleep 0.01 # hax
handle.should_not be_alive
end
-
+
it "joins to thread handles" do
Celluloid::ThreadHandle.new { sleep 0.01 }.join
end
-end
+end
View
10 spec/support/actor_examples.rb
@@ -62,11 +62,11 @@ def method_missing(method_name, *args, &block)
def respond_to?(method_name)
super || delegates?(method_name)
end
-
+
def call_private
zomg_private!
end
-
+
def zomg_private
@private_called = true
end
@@ -200,7 +200,7 @@ def to_s
actor.change_name! "Charlie Sheen"
actor.greet.should == "Hi, I'm Charlie Sheen"
end
-
+
it "handles asynchronous calls via #async" do
actor = actor_class.new "Troy McClure"
actor.async :change_name, "Charlie Sheen"
@@ -212,7 +212,7 @@ def to_s
actor.change_name_async "Charlie Sheen"
actor.greet.should == "Hi, I'm Charlie Sheen"
end
-
+
it "allows an actor to call private methods asynchronously with a bang" do
actor = actor_class.new "Troy McClure"
actor.call_private
@@ -272,7 +272,7 @@ def to_s
actor.join
actor.should_not be_alive
end
-
+
it "kills" do
actor = actor_class.new "Woody Harrelson"
actor.should be_alive

0 comments on commit 9568e2f

Please sign in to comment.
Something went wrong with that request. Please try again.