Skip to content

Commit

Permalink
Improve test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
dodecaphonic committed Mar 12, 2014
1 parent b261ca7 commit 116b044
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 10 deletions.
10 changes: 5 additions & 5 deletions lib/balladina/control_socket_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ module Balladina
class ControlSocketListener
include Celluloid

def initialize(secretary, control_socket)
@secretary = secretary
def initialize(coordinator, control_socket)
@coordinator = coordinator
@control_socket = control_socket

async.listen
end

attr_reader :secretary, :control_socket
private :secretary, :control_socket
attr_reader :coordinator, :control_socket
private :coordinator, :control_socket

def listen
loop do
secretary.async.on_message next_message
coordinator.async.on_message next_message
end
ensure
terminate
Expand Down
12 changes: 7 additions & 5 deletions lib/balladina/track_coordinator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ class TrackCoordinator

finalizer :remove_track_from_board

def initialize(control_socket, track, board)
@track = track
@board = board
@listener = ControlSocketListener.new_link(Actor.current, control_socket)
def initialize(control_socket, track, board, options = {})
@track = track
@board = board
@listener = options.fetch(:creates_socket_listeners) {
ControlSocketListener
}.new_link(Actor.current, control_socket)
@control_socket = control_socket

subscribe "peers_ready", :notify_peers
Expand All @@ -22,7 +24,6 @@ def initialize(control_socket, track, board)
private :track, :board, :control_socket

def on_message(message)
info message
case message["command"]
when "start_recording", "stop_recording"
board.async.public_send message["command"]
Expand All @@ -42,6 +43,7 @@ def control_recording(msg)
track.async.public_send msg
end

private
def remove_track_from_board
board.async.remove_track track
end
Expand Down
40 changes: 40 additions & 0 deletions spec/balladina/chunk_writer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require_relative "../spec_helper"

describe Balladina::ChunkWriter, actor_system: :global do
let(:target_dir) { Dir.tmpdir + "/balladina_tests" }

before do
@track = StubTrack.new
@chunk_writer = Balladina::ChunkWriter.new(@track, target_dir)
end

after do
@chunk_writer.terminate
@track.terminate

FileUtils.rm_rf target_dir
end

describe "writing chunks" do
it "writes to disk and notifies the Track" do
received_at = Time.now.to_i
@chunk_writer.on_chunk received_at, "IMPORTANT AUDIO"
expect(File.exist?(target_dir + "/#{received_at}.wav")).to be_true
expect(@track.chunks).to have(1).item
end
end

class StubTrack
include Celluloid

def initialize
@chunks = []
end

attr_reader :chunks

def on_chunk(chunk_path)
chunks << chunk_path
end
end
end
71 changes: 71 additions & 0 deletions spec/balladina/track_coordinator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
require_relative "../spec_helper"

describe Balladina::TrackCoordinator, actor_system: :global do
let(:socket) { double("socket") }
let(:track) { double("track") }
let(:board) { double("board") }

before do
@coordinator = Balladina::TrackCoordinator.new(socket, track, board, creates_socket_listeners: StubListener)
end

after do
@coordinator.terminate
end

describe "processing messages from the control socket" do
before do
board.should_receive(:async).at_least(1).times.and_return board
end

it "tells the board to start recording" do
board.should_receive(:start_recording)
@coordinator.on_message "command" => "start_recording"
end

it "tells the board to stop recording" do
board.should_receive(:stop_recording)
@coordinator.on_message "command" => "stop_recording"
end

it "tells the board to promote another track to leader" do
board.should_receive(:promote_leader).with "track-1"
@coordinator.on_message "command" => "promote_leader", "data" => "track-1"
end
end

describe "processing messages from the pub/sub channels" do
it "notifies peers of who's ready or online" do
socket.should_receive(:<<).twice
@coordinator.notify_peers "peers_ready", ["track-1"]
@coordinator.notify_peers "peers_online", ["track-1"]
end

it "controls its track's recording" do
socket.should_receive(:<<).twice
track.should_receive(:async).twice.and_return track
track.should_receive(:start_recording)
track.should_receive(:stop_recording)

@coordinator.control_recording "start_recording"
@coordinator.control_recording "stop_recording"
end
end

describe "removing its Track from the board if it is terminated" do
before do
socket.should_receive(:<<).and_raise "hell"
board.should_receive(:async).and_return board
board.should_receive(:remove_track).with track
end

xit do
@coordinator.control_recording "start_recording"
end
end

class StubListener
include Celluloid
def initialize(*); end
end
end

0 comments on commit 116b044

Please sign in to comment.