2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe ApplicationRequest do
|
2015-02-04 12:10:54 +08:00
|
|
|
before do
|
2020-05-18 17:22:39 +08:00
|
|
|
ApplicationRequest.enable
|
2022-02-23 00:45:25 +08:00
|
|
|
CachedCounting.reset
|
|
|
|
CachedCounting.enable
|
2015-02-04 12:10:54 +08:00
|
|
|
end
|
|
|
|
|
2017-07-24 10:41:37 +08:00
|
|
|
after do
|
2020-05-18 17:22:39 +08:00
|
|
|
ApplicationRequest.disable
|
2022-02-23 00:45:25 +08:00
|
|
|
CachedCounting.disable
|
2017-07-24 10:41:37 +08:00
|
|
|
end
|
|
|
|
|
2022-02-23 00:45:25 +08:00
|
|
|
def inc(key)
|
|
|
|
ApplicationRequest.increment!(key)
|
2015-02-04 12:10:54 +08:00
|
|
|
end
|
|
|
|
|
2022-02-23 00:45:25 +08:00
|
|
|
it "can log app requests" do
|
|
|
|
freeze_time
|
|
|
|
d1 = Time.now.utc.to_date
|
2017-10-25 10:22:50 +08:00
|
|
|
|
2022-02-23 00:45:25 +08:00
|
|
|
4.times { inc("http_2xx") }
|
2015-02-04 12:10:54 +08:00
|
|
|
|
2022-02-23 00:45:25 +08:00
|
|
|
inc("http_background")
|
2017-10-25 10:19:43 +08:00
|
|
|
|
2022-02-23 00:45:25 +08:00
|
|
|
freeze_time 1.day.from_now
|
|
|
|
d2 = Time.now.utc.to_date
|
2017-10-25 10:19:43 +08:00
|
|
|
|
2022-02-23 00:45:25 +08:00
|
|
|
inc("page_view_crawler")
|
|
|
|
inc("http_2xx")
|
2015-02-06 11:39:04 +08:00
|
|
|
|
2022-02-23 00:45:25 +08:00
|
|
|
CachedCounting.flush
|
2015-02-04 12:10:54 +08:00
|
|
|
|
2022-02-23 00:45:25 +08:00
|
|
|
expect(ApplicationRequest.find_by(date: d1, req_type: "http_2xx").count).to eq(4)
|
|
|
|
expect(ApplicationRequest.find_by(date: d1, req_type: "http_background").count).to eq(1)
|
2015-02-04 12:10:54 +08:00
|
|
|
|
2022-02-23 00:45:25 +08:00
|
|
|
expect(ApplicationRequest.find_by(date: d2, req_type: "page_view_crawler").count).to eq(1)
|
|
|
|
expect(ApplicationRequest.find_by(date: d2, req_type: "http_2xx").count).to eq(1)
|
2015-02-04 12:10:54 +08:00
|
|
|
end
|
|
|
|
end
|