Skip to content

Instantly share code, notes, and snippets.

@digitalextremist
Last active August 29, 2015 14:27
Show Gist options
  • Save digitalextremist/104660e45fa58a03b7cf to your computer and use it in GitHub Desktop.
Save digitalextremist/104660e45fa58a03b7cf to your computer and use it in GitHub Desktop.
Mock Actor::System to simulate the arrival of running actors, for the Unlocker GSoC project.
require 'celluloid/current'
class Mocker
include Celluloid
MAX_INTERVAL = 20
MAX_WORK = 30
MAX_ID = 1000
attr_reader :id
def initialize(id)
@id = id
puts "[#{@id}] Started"
end
def choose_interval(action, max)
interval = choose_number(max)
puts "[#{@id}] #{action.to_s.capitalize}, #{interval} seconds."
interval
end
def choose_number(max)
Random.rand(max)
end
end
class MockActor < Mocker
def initialize
super("Actor##{choose_number(MAX_ID)}".to_sym)
trigger_interval
end
def trigger_interval
after(choose_interval(:triggering, MAX_INTERVAL)) { work }
end
def work
sleep choose_interval(:working, MAX_WORK)
end
end
class MockActorSystem < Mocker
include Celluloid
attr_reader :running
def initialize
@running = []
super(:MockActorSystem)
trigger_interval
end
def trigger_interval
after (choose_interval(:triggering, MAX_INTERVAL)) {
create_actor
}
end
def create_actor
mocking = MockActor.new
puts "[#{@id}] Instantiated #{mocking.id}."
@running << mocking
puts "[#{@id}] Signaling Unlocker"
#de This is what you need to work with:
#de Celluloid.manager.new_actor(mocking)
trigger_interval
end
end
run_length = ARGV[0] || 260
puts "> Giving the mocker system #{run_length} seconds to operate. < < < < < < <"
MockActorSystem.new
sleep run_length.to_i
puts "> Finished mocking."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment