From 805d1d5f5f8922610113227a7e03599e6952eb28 Mon Sep 17 00:00:00 2001 From: Matt Hale Date: Sun, 17 Jan 2016 12:13:17 -0600 Subject: [PATCH 1/6] Fixes VoltTime comparison with nil issue --- lib/volt/helpers/time/volt_time.rb | 2 +- spec/helpers/volt_time_spec.rb | 90 ++++++++++++++++-------------- 2 files changed, 48 insertions(+), 44 deletions(-) diff --git a/lib/volt/helpers/time/volt_time.rb b/lib/volt/helpers/time/volt_time.rb index ac696e09..08dce813 100644 --- a/lib/volt/helpers/time/volt_time.rb +++ b/lib/volt/helpers/time/volt_time.rb @@ -94,7 +94,7 @@ def == (other) if other.is_a?(::Time) @time == other else - @time == other.getutc + @time == other.try(:getutc) end end diff --git a/spec/helpers/volt_time_spec.rb b/spec/helpers/volt_time_spec.rb index 975a56ba..e54a4f5d 100644 --- a/spec/helpers/volt_time_spec.rb +++ b/spec/helpers/volt_time_spec.rb @@ -6,44 +6,44 @@ let(:vt0) { VoltTime.at(0) } let(:vt120) { VoltTime.at(120) } let(:rt0) { Time.at(0) } - + let(:local_beginning) do if rt0.utc_offset >= 0 Time.new(1970, 1, 1) else Time.new(1969, 12, 31) - end - end - + end + end + let(:local_end) do if rt0.utc_offset >= 0 Time.new(1970, 1, 1, 23, 59, 59.999) else Time.new(1969, 12, 31, 23, 59, 59.999) end - end - + end + let(:local_middle) do if rt0.utc_offset >= 0 Time.new(1970, 1, 1, 12, 0, 0) else Time.new(1969, 12, 31, 12, 0, 0) end - end - - + end + + describe "#new" do it "assumes the time provided is utc" do expect(VoltTime.new(:utc, 1970, 1, 1, 0, 0, 0)).to eq(vt0) end - + it "assumes the time provided is local" do expect(VoltTime.new(:local, 1970, 1, 1, 0, 0, 0)).to eq(rt0 - rt0.utc_offset) end - + end - + describe "#+" do it "returns the time plus a second" do expect(vt0 + 1).to eq(VoltTime.at(1)) @@ -53,16 +53,16 @@ expect(vt0 + 1.month).to eq(VoltTime.new(:utc, 1970, 2, 1)) end end - + describe "#-" do it "returns the time minus a second" do expect(vt0 - 1).to eq(VoltTime.at(-1)) end - + it "returns the seconds between two VoltTime object" do expect(VoltTime.at(100) - vt0).to eq(100) end - + it "returns the seconds between a VoltTime and a Time object" do expect(VoltTime.at(100) - vt0).to eq(100) end @@ -71,19 +71,23 @@ expect(vt0 - 1.month).to eq(VoltTime.new(:utc, 1969, 12, 1)) end end - + describe "#==" do it "returns true for two equal times" do expect((vt0 + 1) == VoltTime.at(1)).to eq(true) end + + it "returns false for a VoltTime and nil" do + expect(VoltTime.at(1) == nil).to eq(false) + end end - + describe "#<=>" do it "returns 0 for two equal times" do expect((vt0 + 1) <=> VoltTime.at(1)).to eq(0) end end - + describe "#local_strftime" do it "returns formatted local time" do if RUBY_PLATFORM == 'opal' @@ -96,7 +100,7 @@ describe "#local_beginning_of_day" do it "returns the start of the local day i.e. 00:00:00" do - + if RUBY_PLATFORM == 'opal' expect(vt120.local_beginning_of_day).to eq(VoltTime.from_time(local_beginning)) else @@ -124,11 +128,11 @@ end end end - + describe "#local_seconds_since_midnight" do it "returns the number of seconds since local midnight" do if RUBY_PLATFORM == 'opal' - expect(vt120.local_seconds_since_midnight).to eq(vt120 - VoltTime.from_time(local_beginning)) + expect(vt120.local_seconds_since_midnight).to eq(vt120 - VoltTime.from_time(local_beginning)) else expect { vt120.local_seconds_since_midnight }.to raise_error end @@ -144,7 +148,7 @@ end end end - + describe "#local_all_day" do it "returns a Range for the whole day" do if RUBY_PLATFORM == 'opal' @@ -155,27 +159,27 @@ else expect { vt120.local_all_day }.to raise_error end - end + end end - + describe "#beginning_of_day" do it "returns the start of day i.e. 00:00:00" do expect(vt120.beginning_of_day).to eq(VoltTime.new(:utc, 1970, 01, 01, 0, 0, 0)) end end - + describe "#end_of_day" do it "returns the end of day ie. 23:59:59" do expect(vt120.end_of_day).to eq(VoltTime.new(:utc, 1970, 01, 01, 23, 59, 59.999)) end end - + describe "#seconds_since_midnight" do it "returns the number of seconds since 00:00:00" do expect(vt120.seconds_since_midnight).to eq(120) end end - + describe "#seconds_until_end_of_day" do it "returns the number of seconds to 23:59:59" do expect((vt120.seconds_until_end_of_day)).to eq(86399.999 - 120) @@ -187,55 +191,55 @@ expect(VoltTime.at(30).ago(30)).to eq(VoltTime.new(:utc, 1970, 01, 01, 00, 00, 0)) end end - + describe "#since" do it "returns a VoltTime for an integer number of seconds since the instance VoltTime" do expect(vt0.since(30)).to eq(VoltTime.new(:utc, 1970, 01, 01, 00, 00, 30)) end end - + describe "#middle_of_day" do it "returns a VoltTime for the middle of the day" do expect(vt0.middle_of_day).to eq(VoltTime.new(:utc, 1970, 01, 01, 12, 0, 0)) end end - + describe "#beginning_of_hour" do it "returns a VoltTime for the beginning of the current hour" do expect(VoltTime.at(90).beginning_of_hour).to eq(VoltTime.new(:utc, 1970, 01, 01, 0, 0, 0)) end end - + describe "#end_of_hour" do it "returns a VoltTime for the end of the current hour" do expect(vt0.end_of_hour).to eq(VoltTime.new(:utc, 1970, 01, 01, 00, 59, 59.999)) end end - + describe "#beginning_of_minute" do it "returns a VoltTime for the beginning of the current minute" do expect(VoltTime.at(30).beginning_of_minute).to eq(VoltTime.new(:utc, 1970, 01, 01, 00, 00, 0)) end end - + describe "#end_of_minute" do it "returns a VoltTime for the end of the current minute" do expect(vt0.end_of_minute).to eq(VoltTime.new(:utc, 1970, 01, 01, 00, 00, 59.999)) end end - + describe "#all_day" do it "returns a Range for the whole day" do r = VoltTime.at(100).all_day expect(r.end - r.begin).to eq(86399.999) - end + end end - + describe "#days_in_month" do it "returns the number of days in March" do expect(VoltTime.days_in_month(3)).to eq(31) end - + it "returns the number of days in Feb leap year" do expect(VoltTime.days_in_month(2, 2016)).to eq(29) end @@ -246,30 +250,30 @@ expect(vt0.local_offset).to eq(Time.at(0).utc_offset) end end - + describe "#compare and #compare?" do it "checks if the years are the same" do expect(vt0.compare(VoltTime.at(10000), :year)).to eq(0) expect(vt0.compare?(VoltTime.at(10000), :year)).to be true end - + it "checks if the months are the same" do expect(vt0.compare(VoltTime.new(:utc, 1970, 1, 30), :month)).to eq(0) expect(vt0.compare?(VoltTime.new(:utc, 1970, 1, 30), :month)).to be true end - + it "checks if the day is the same" do expect(vt0.compare(VoltTime.new(:utc, 1971, 1,1), :day)).to eq(-1) expect(vt0.compare?(VoltTime.new(:utc, 1971, 1,1), :day)).to be false end - + it "checks if the minute is the same" do expect(VoltTime.at(61).compare(VoltTime.at(0), :min)).to eq(1) expect(VoltTime.at(61).compare?(VoltTime.at(0), :min)).to be false end - + end - + end From e5419f5ed7e4ac1748b33cdd16965d8b5e22308b Mon Sep 17 00:00:00 2001 From: mike Date: Sun, 28 Feb 2016 22:43:57 -0500 Subject: [PATCH 2/6] remove queries from channel_live_queries when they are no longer associated with a channel --- app/volt/tasks/live_query/live_query.rb | 2 +- app/volt/tasks/query_tasks.rb | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/app/volt/tasks/live_query/live_query.rb b/app/volt/tasks/live_query/live_query.rb index 2b3ef2c9..2b53fdf0 100644 --- a/app/volt/tasks/live_query/live_query.rb +++ b/app/volt/tasks/live_query/live_query.rb @@ -3,7 +3,7 @@ # Tracks a channel and a query on a collection. Alerts # the listener when the data in the query changes. class LiveQuery - attr_reader :current_ids, :collection, :query + attr_reader :current_ids, :collection, :query, :channels def initialize(pool, data_store, collection, query) @pool = pool diff --git a/app/volt/tasks/query_tasks.rb b/app/volt/tasks/query_tasks.rb index 6a9b0d1b..c80706ae 100644 --- a/app/volt/tasks/query_tasks.rb +++ b/app/volt/tasks/query_tasks.rb @@ -45,6 +45,9 @@ def initial_data def remove_listener(collection, query) live_query = @volt_app.live_query_pool.lookup(collection, query) live_query.remove_channel(@channel) + + # If query has no more channels remove it from channel_live_queries + @volt_app.channel_live_queries[@channel].delete(live_query) if live_query.channels.blank? end # Removes a channel from all associated live queries From 97b4abca1852d8d4d735d63bc2e23c4414ed83ac Mon Sep 17 00:00:00 2001 From: mike Date: Sun, 28 Feb 2016 23:26:52 -0500 Subject: [PATCH 3/6] checking if serialize errors are from this delete --- app/volt/tasks/query_tasks.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/volt/tasks/query_tasks.rb b/app/volt/tasks/query_tasks.rb index c80706ae..0be756a8 100644 --- a/app/volt/tasks/query_tasks.rb +++ b/app/volt/tasks/query_tasks.rb @@ -47,7 +47,7 @@ def remove_listener(collection, query) live_query.remove_channel(@channel) # If query has no more channels remove it from channel_live_queries - @volt_app.channel_live_queries[@channel].delete(live_query) if live_query.channels.blank? + # @volt_app.channel_live_queries[@channel].delete(live_query) if live_query.channels.blank? end # Removes a channel from all associated live queries From d97ee8f245fa68ea2d313a8a4d0f10a2f32419d9 Mon Sep 17 00:00:00 2001 From: mike Date: Sun, 28 Feb 2016 23:36:59 -0500 Subject: [PATCH 4/6] can't do delete on the query object. instead loop through the channel live queries looking for ones with no more channels --- app/volt/tasks/query_tasks.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/volt/tasks/query_tasks.rb b/app/volt/tasks/query_tasks.rb index 0be756a8..7247f08d 100644 --- a/app/volt/tasks/query_tasks.rb +++ b/app/volt/tasks/query_tasks.rb @@ -47,7 +47,7 @@ def remove_listener(collection, query) live_query.remove_channel(@channel) # If query has no more channels remove it from channel_live_queries - # @volt_app.channel_live_queries[@channel].delete(live_query) if live_query.channels.blank? + @volt_app.channel_live_queries[@channel].delete_if{|channel_live_query| channel_live_query.channels.blank? } end # Removes a channel from all associated live queries From be023b8239d7cb90f692833436a502ed26912f2f Mon Sep 17 00:00:00 2001 From: mike Date: Mon, 29 Feb 2016 02:21:44 -0500 Subject: [PATCH 5/6] return true when running remove listener to provide a return value that's easily serializable. --- app/volt/tasks/query_tasks.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/volt/tasks/query_tasks.rb b/app/volt/tasks/query_tasks.rb index 7247f08d..81146237 100644 --- a/app/volt/tasks/query_tasks.rb +++ b/app/volt/tasks/query_tasks.rb @@ -47,7 +47,8 @@ def remove_listener(collection, query) live_query.remove_channel(@channel) # If query has no more channels remove it from channel_live_queries - @volt_app.channel_live_queries[@channel].delete_if{|channel_live_query| channel_live_query.channels.blank? } + @volt_app.channel_live_queries[@channel].delete(live_query) if live_query.channels.blank? unless RUBY_PLATFORM == 'opal' + return true end # Removes a channel from all associated live queries From 214ab96e32511c40b0c1f61cbb9f38b10577c7f1 Mon Sep 17 00:00:00 2001 From: mike Date: Mon, 29 Feb 2016 02:25:05 -0500 Subject: [PATCH 6/6] don't need to check for opal from the task --- app/volt/tasks/query_tasks.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/volt/tasks/query_tasks.rb b/app/volt/tasks/query_tasks.rb index 81146237..c356109d 100644 --- a/app/volt/tasks/query_tasks.rb +++ b/app/volt/tasks/query_tasks.rb @@ -47,7 +47,7 @@ def remove_listener(collection, query) live_query.remove_channel(@channel) # If query has no more channels remove it from channel_live_queries - @volt_app.channel_live_queries[@channel].delete(live_query) if live_query.channels.blank? unless RUBY_PLATFORM == 'opal' + @volt_app.channel_live_queries[@channel].delete(live_query) if live_query.channels.blank? return true end