Skip to content

Instantly share code, notes, and snippets.

@tcrayford
Created November 10, 2012 15:52
Show Gist options
  • Save tcrayford/4051456 to your computer and use it in GitHub Desktop.
Save tcrayford/4051456 to your computer and use it in GitHub Desktop.
require 'thread'
class Actor
class ActorDied < RuntimeError; end
def self.queue
@queue ||= Queue.new
end
def self.push(x)
queue.push(x)
end
def self.die
raise ActorDied
end
def self.run(&block)
Thread.new do
begin
while true do
self.instance_eval(&block)
end
rescue ActorDied => _
break
rescue Exception => e
# understand an asynchronous exception. understand it.
puts e
end
end
end
end
module Actors
def self.actor(clazz, &block)
clazz.run(&block)
end
def self.const_missing(const_name)
self.const_set(const_name, Class.new(Actor))
end
end
describe Actors do
it "actors can push messages onto an queue" do
module Actors
actor Shower do
Soap.push(1)
die
end
actor Soap do
die
end
end
Actors::Soap.queue.pop.should == 1
end
it "dies as soon as it is told to" do
module Actors
actor Shower do
Soap.push(1)
die
end
actor Soap do
die
end
end
Actors::Soap.queue.length.should == 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment