require 'spec_helper' require 'fileutils' # these tests avoid the use of ActiveRecord and FactoryGirl to do blackbox, non test-instrumented tests describe IcecastSourceCheck do let(:check) { IcecastSourceCheck.new } describe "integration" do it "be OK with no mounts" do IcecastMount.count().should == 0 check.should_not_receive(:handle_notifications) check.run end it "find no mounts if source_hanged timestamp is nil and listeners = 1/sourced = false" do mount = FactoryGirl.create(:iceast_mount_with_music_session, sourced: false, listeners: 1) check.should_not_receive(:handle_notifications) check.run end it "find no mounts if source_changed timestamp is nil and listeners = 0/sourced = true" do mount = FactoryGirl.create(:iceast_mount_with_music_session, sourced: true, listeners: 1) check.should_not_receive(:handle_notifications) check.run end it "find no mounts if source_changed timestamp is very recent and listeners = 1/sourced = false" do mount = FactoryGirl.create(:iceast_mount_with_music_session, sourced_needs_changing_at: Time.now, sourced: false, listeners: 1) check.should_not_receive(:handle_notifications) check.run end it "find no mounts if source_changed timestamp is very recent and listeners = 0/sourced = true" do mount = FactoryGirl.create(:iceast_mount_with_music_session, sourced_needs_changing_at: Time.now, sourced: true, listeners: 0) check.should_not_receive(:handle_notifications) check.run end it "sends notify_source_down_requested when old source_changed timestamp, and sourced = true and listeners = 0" do mount = FactoryGirl.create(:iceast_mount_with_music_session, sourced_needs_changing_at: 2.days.ago, sourced:true, listeners: 0) check.stub(:handle_notifications) do |mount| mount.should_receive(:notify_source_down_requested).once mount.should_not_receive(:notify_source_up_requested) mount.should_not_receive(:notify_source_up) mount.should_not_receive(:notify_source_down) check.unstub!(:handle_notifications) check.handle_notifications(mount) end check.run end it "does not send notify_source_down_requested when old source_changed timestamp, and sourced = true and listeners = 1" do mount = FactoryGirl.create(:iceast_mount_with_music_session, sourced_needs_changing_at: 2.days.ago, sourced:true, listeners: 1) check.stub(:handle_notifications) do |mount| mount.should_not_receive(:notify_source_down_requested) mount.should_not_receive(:notify_source_up_requested) mount.should_not_receive(:notify_source_up) mount.should_not_receive(:notify_source_down) check.unstub!(:handle_notifications) check.handle_notifications(mount) end check.run end it "sends notify_source_up_requested when old source_changed timestamp, and sourced = false and listeners = 1" do mount = FactoryGirl.create(:iceast_mount_with_music_session, sourced_needs_changing_at: 2.days.ago, sourced:false, listeners: 1) check.stub(:handle_notifications) do |mount| mount.should_not_receive(:notify_source_down_requested) mount.should_receive(:notify_source_up_requested).once mount.should_not_receive(:notify_source_up) mount.should_not_receive(:notify_source_down) check.unstub!(:handle_notifications) check.handle_notifications(mount) end check.run end it "does not send notify_source_up_requested when old source_changed timestamp, and sourced = false and listeners = 0" do mount = FactoryGirl.create(:iceast_mount_with_music_session, sourced_needs_changing_at: 2.days.ago, sourced:false, listeners: 0) check.stub(:handle_notifications) do |mount| mount.should_not_receive(:notify_source_down_requested) mount.should_not_receive(:notify_source_up_requested) mount.should_not_receive(:notify_source_up) mount.should_not_receive(:notify_source_down) check.unstub!(:handle_notifications) check.handle_notifications(mount) end check.run end it "resets source_changed_at when a notification is sent out" do mount = FactoryGirl.create(:iceast_mount_with_music_session, sourced_needs_changing_at: 2.days.ago, sourced:false, listeners: 1) check.stub(:handle_notifications) do |mount| mount.should_not_receive(:notify_source_down_requested) mount.should_receive(:notify_source_up_requested).once mount.should_not_receive(:notify_source_up) mount.should_not_receive(:notify_source_down) check.unstub!(:handle_notifications) check.handle_notifications(mount) end check.run mount.reload (mount.sourced_needs_changing_at.to_i - Time.now.to_i).abs.should < 10 # less than 5 seconds -- just a little slop for a very slow build server end end end