Skip to content
This repository has been archived by the owner on Oct 18, 2019. It is now read-only.

Commit

Permalink
Test and fix the PlayersController. #1 #14
Browse files Browse the repository at this point in the history
  • Loading branch information
smashwilson committed Mar 4, 2012
1 parent fb644a8 commit 5b49557
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
3 changes: 2 additions & 1 deletion app/controllers/players_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class PlayersController < ApplicationController
before_filter :create_player_client
attr_accessor :client

def show
render :json => @status
Expand All @@ -19,7 +20,7 @@ def update
private

def create_player_client
@player = Client.new
@player = @client || Client.new
@player.ok?

@status = @player.status
Expand Down
8 changes: 6 additions & 2 deletions app/models/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,32 @@
class Client
include Mpg123Player
include Configurable
include ActiveSupport::BufferedLogger::Severity

attr_accessor :asynchronous
attr_reader :error

def initialize
configure
@asynchronous = false
end

# Issue a command to the server and wait for the remote process to handle it, within the currently configured
# +command_timeout+.
def command string, parameter = nil
cmd = PlayerCommand.create!(:action => string, :parameter => parameter)
return if @asynchronous

Rails.logger.silence(WARN) do
wait_time = 0
until wait_time >= @command_timeout
wait_time += sleep(@command_poll)
return true unless PlayerCommand.exist?(cmd.id)
return unless PlayerCommand.exists?(cmd.id)
end
end

# Timed out waiting for command acceptance
false
@error = "Timed out submitting command #{string}."
end

# Status
Expand Down
28 changes: 28 additions & 0 deletions test/functional/players_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
require 'test_helper'
require 'active_support/json'

require 'mpg123player/common'

class PlayersControllerTest < ActionController::TestCase

class TestClient < Client
def initialize
super
@status = Mpg123Player::Status.stopped
@asynchronous = true
end

def ok?
true
end
end

test "issue commands through the client" do
@controller.client = TestClient.new

post :update, { :command => 'volume', :parameter => '70' }

assert_response :success
assert @response.body.blank?

@c = PlayerCommand.flush_queue[0]
assert_equal 'volume', @c.action
assert_equal '70', @c.parameter
end

end

0 comments on commit 5b49557

Please sign in to comment.