spec/lib/tabs/metrics/value_spec.rb in tabs-0.9.1 vs spec/lib/tabs/metrics/value_spec.rb in tabs-1.0.0

- old
+ new

@@ -38,10 +38,12 @@ Timecop.return end def create_span(time_unit) metric.record(5) + Timecop.freeze(now + 1.send(time_unit)) + metric.record(25) Timecop.freeze(now + 3.send(time_unit)) metric.record(10) Timecop.freeze(now + 6.send(time_unit)) metric.record(15) metric.record(20) @@ -76,10 +78,12 @@ end it "returns the expected results for a weekly metric" do create_span(:weeks) stats = metric.stats(now..(now + 7.weeks), :week) + second_week_stats = stats.detect{|s| s["timestamp"] == (now + 1.week).beginning_of_week } + expect(second_week_stats["count"]).to eq(1) expect(stats).to include({ "timestamp" => (now + 3.weeks).beginning_of_week, "count"=>1, "min"=>10, "max"=>10, "sum"=>10, "avg"=>10}) expect(stats).to include({ "timestamp" => (now + 6.weeks).beginning_of_week, "count"=>2, "min"=>15, "max"=>20, "sum"=>35, "avg"=>17.5}) end it "returns the expected results for a monthly metric" do @@ -119,6 +123,38 @@ end end end + describe ".drop_by_resolution!" do + before do + Timecop.freeze(now) + 2.times { metric.record(rand(30)) } + metric.drop_by_resolution!(:minute) + end + + it "deletes all metrics for a resolution" do + stats = metric.stats((now - 1.minute)..(now + 1.minute), :minute) + expect(stats.sum).to eq(0) + end + end + + describe "expiration of value metrics" do + let(:expires_setting){ 6.hours } + let(:now){ Time.utc(2050, 1, 1, 0, 0) } + + before do + Tabs::Config.set_expirations({ minute: expires_setting }) + end + + after do + Tabs::Config.reset_expirations + end + + it "sets an expiration when recording a value" do + metric.record(17, now) + redis_expire_date = Time.now + Tabs::Storage.ttl(metric.storage_key(:minute, now)) + expire_date = now + expires_setting + Tabs::Resolutions::Minute.to_seconds + expect(redis_expire_date).to be_within(2.seconds).of(expire_date) + end + end end