diff --git a/ruby/lib/jam_ruby/models/user.rb b/ruby/lib/jam_ruby/models/user.rb index cb6ff4b41..c59021903 100644 --- a/ruby/lib/jam_ruby/models/user.rb +++ b/ruby/lib/jam_ruby/models/user.rb @@ -25,6 +25,10 @@ module JamRuby MOD_NO_SHOW = "no_show" + # MIN/MAX AUDIO LATENCY + MINIMUM_AUDIO_LATENCY = 2 + MAXIMUM_AUDIO_LATENCY = 10000 + devise :database_authenticatable, :recoverable, :rememberable acts_as_mappable @@ -166,7 +170,7 @@ module JamRuby validates :musician, :inclusion => {:in => [true, false]} validates :show_whats_next, :inclusion => {:in => [nil, true, false]} validates :mods, json: true - validates_numericality_of :last_jam_audio_latency, greater_than:0, :allow_nil => true + validates_numericality_of :last_jam_audio_latency, greater_than:MINIMUM_AUDIO_LATENCY, less_than:MAXIMUM_AUDIO_LATENCY, :allow_nil => true validates :last_jam_updated_reason, :inclusion => {:in => [nil, JAM_REASON_REGISTRATION, JAM_REASON_NETWORK_TEST, JAM_REASON_FTUE, JAM_REASON_JOIN, JAM_REASON_IMPORT, JAM_REASON_LOGIN] } # custom validators @@ -1377,8 +1381,9 @@ module JamRuby end def update_audio_latency(connection, audio_latency) - if audio_latency > 2 - # updating the connection is best effort + # the backend sometimes gives tiny numbers, and sometimes very large numbers + if audio_latency > MINIMUM_AUDIO_LATENCY && audio_latency < MAXIMUM_AUDIO_LATENCY + # updating the connection is best effort; if it's not there that's OK if connection Connection.where(:id => connection.id).update_all(:last_jam_audio_latency => audio_latency) end diff --git a/ruby/spec/jam_ruby/models/user_spec.rb b/ruby/spec/jam_ruby/models/user_spec.rb index 4fd3d2040..ec8b8440d 100644 --- a/ruby/spec/jam_ruby/models/user_spec.rb +++ b/ruby/spec/jam_ruby/models/user_spec.rb @@ -489,14 +489,14 @@ describe User do describe "audio latency" do it "allow update" do - @user.last_jam_audio_latency = 1 + @user.last_jam_audio_latency = 5 @user.save! end - it "prevent negative or 0" do - @user.last_jam_audio_latency = 0 + it "prevent negative" do + @user.last_jam_audio_latency = -1 @user.save.should be_false - @user.errors[:last_jam_audio_latency].should == ['must be greater than 0'] + @user.errors[:last_jam_audio_latency].should == ['must be greater than 2'] end it "prevent non numerical" do @@ -504,6 +504,42 @@ describe User do @user.save.should be_false @user.errors[:last_jam_audio_latency].should == ['is not a number'] end + + it "prevent 2 (minimum)" do + @user.last_jam_audio_latency = User::MINIMUM_AUDIO_LATENCY + @user.save.should be_false + @user.errors[:last_jam_audio_latency].should == ['must be greater than 2'] + end + + it "prevent 10000 (maximum)" do + @user.last_jam_audio_latency = User::MAXIMUM_AUDIO_LATENCY + @user.save.should be_false + @user.errors[:last_jam_audio_latency].should == ['must be less than 10000'] + end + end + + describe "update_audio_latency" do + + before(:each) do + @user.last_jam_audio_latency.should be_nil + end + + it "ignores low latency" do + @user.update_audio_latency(nil, User::MINIMUM_AUDIO_LATENCY) + @user.last_jam_audio_latency.should be_nil + end + + it "ignores high latency" do + @user.last_jam_audio_latency.should be_nil + @user.update_audio_latency(nil, User::MAXIMUM_AUDIO_LATENCY) + @user.last_jam_audio_latency.should be_nil + end + + it "accepts normal latency" do + @user.last_jam_audio_latency.should be_nil + @user.update_audio_latency(nil, User::MINIMUM_AUDIO_LATENCY + 1) + @user.last_jam_audio_latency.should == User::MINIMUM_AUDIO_LATENCY + 1 + end end describe "html_sanitize" do