|
1 | 1 | require "core/spec_helper" |
2 | | -require "active_support/cache" |
3 | 2 |
|
4 | 3 | describe ZendeskAPI::Middleware::Request::EtagCache do |
5 | 4 | it "caches" do |
|
21 | 20 | end |
22 | 21 |
|
23 | 22 | context "instrumentation" do |
24 | | - let(:instrumentation) { double("Instrumentation") } |
25 | | - let(:cache) { ActiveSupport::Cache::MemoryStore.new } |
26 | | - let(:status) { nil } |
| 23 | + let(:instrumenter) { TestInstrumenter.new } |
| 24 | + let(:cache) { ZendeskAPI::LRUCache.new(5) } |
27 | 25 | let(:middleware) do |
28 | 26 | ZendeskAPI::Middleware::Request::EtagCache.new( |
29 | 27 | ->(env) { Faraday::Response.new(env) }, |
30 | 28 | cache: cache, |
31 | | - instrumentation: instrumentation |
| 29 | + instrumentation: instrumenter |
32 | 30 | ) |
33 | 31 | end |
34 | 32 | let(:env) do |
35 | 33 | { |
36 | 34 | url: URI("https://example.zendesk.com/api/v2/blergh"), |
37 | 35 | method: :get, |
38 | 36 | request_headers: {}, |
39 | | - response_headers: {"Etag" => "x", :x_rate_limit_remaining => 10}, |
40 | | - status: status, |
| 37 | + response_headers: {"Etag" => "x"}, |
| 38 | + status: nil, |
41 | 39 | body: {"x" => 1}, |
42 | 40 | response_body: {"x" => 1} |
43 | 41 | } |
44 | 42 | end |
45 | | - let(:no_instrumentation_middleware) do |
46 | | - ZendeskAPI::Middleware::Request::EtagCache.new( |
47 | | - ->(env) { Faraday::Response.new(env) }, |
48 | | - cache: cache, |
49 | | - instrumentation: nil |
50 | | - ) |
51 | | - end |
52 | | - before do |
53 | | - allow(instrumentation).to receive(:instrument) |
54 | | - end |
55 | 43 |
|
56 | | - it "emits cache_miss on first request" do |
57 | | - expect(instrumentation).to receive(:instrument).with( |
58 | | - "zendesk.cache_miss", |
59 | | - hash_including(endpoint: "/api/v2/blergh", status: 200) |
60 | | - ) |
| 44 | + it "instruments cache miss on first request" do |
61 | 45 | env[:status] = 200 |
62 | | - middleware.call(env).on_complete { |_e| 1 } |
63 | | - end |
| 46 | + middleware.call(env).on_complete { |_e| } |
64 | 47 |
|
65 | | - it "don't care on no instrumentation" do |
66 | | - env[:status] = 200 |
67 | | - no_instrumentation_middleware.call(env).on_complete { |_e| 1 } |
| 48 | + cache_events = instrumenter.find_events("zendesk.cache_miss") |
| 49 | + expect(cache_events.size).to eq(1) |
| 50 | + |
| 51 | + event = cache_events.first[:payload] |
| 52 | + expect(event[:endpoint]).to eq("/api/v2/blergh") |
| 53 | + expect(event[:status]).to eq(200) |
68 | 54 | end |
69 | 55 |
|
70 | | - it "emits cache_hit on 304 response" do |
| 56 | + it "instruments cache hit on 304 response" do |
71 | 57 | cache.write(middleware.cache_key(env), env) |
72 | | - expect(instrumentation).to receive(:instrument).with( |
73 | | - "zendesk.cache_hit", |
74 | | - hash_including(endpoint: "/api/v2/blergh", status: 304) |
75 | | - ) |
76 | 58 | env[:status] = 304 |
77 | | - middleware.call(env).on_complete { |_e| 1 } |
| 59 | + middleware.call(env).on_complete { |_e| } |
| 60 | + |
| 61 | + cache_events = instrumenter.find_events("zendesk.cache_hit") |
| 62 | + expect(cache_events.size).to eq(1) |
| 63 | + |
| 64 | + event = cache_events.first[:payload] |
| 65 | + expect(event[:endpoint]).to eq("/api/v2/blergh") |
| 66 | + expect(event[:status]).to eq(304) |
| 67 | + end |
| 68 | + |
| 69 | + it "does not crash when instrumentation is nil" do |
| 70 | + no_instrumentation_middleware = ZendeskAPI::Middleware::Request::EtagCache.new( |
| 71 | + ->(env) { Faraday::Response.new(env) }, |
| 72 | + cache: cache, |
| 73 | + instrumentation: nil |
| 74 | + ) |
| 75 | + |
| 76 | + env[:status] = 200 |
| 77 | + expect { no_instrumentation_middleware.call(env).on_complete { |_e| } }.not_to raise_error |
78 | 78 | end |
79 | 79 | end |
80 | 80 | end |
0 commit comments