Skip to content

Commit

Permalink
add tests for the api cache code
Browse files Browse the repository at this point in the history
  • Loading branch information
syphax-bouazzouni committed Apr 14, 2024
1 parent 6741932 commit 8f42d4c
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ gemspec

gem 'rake'
gem 'pry'
gem 'test-unit'
gem 'test-unit'
gem 'webmock', require: false
14 changes: 14 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ GEM
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
bigdecimal (3.1.7)
coderay (1.1.3)
concurrent-ruby (1.1.10)
crack (1.0.0)
bigdecimal
rexml
excon (0.95.0)
faraday (2.0.1)
faraday-net_http (~> 2.0)
Expand All @@ -32,6 +38,7 @@ GEM
faraday-multipart (1.0.4)
multipart-post (~> 2)
faraday-net_http (2.1.0)
hashdiff (1.1.0)
i18n (1.12.0)
concurrent-ruby (~> 1.0)
lz4-ruby (0.3.3)
Expand All @@ -44,13 +51,19 @@ GEM
pry (0.14.1)
coderay (~> 1.1)
method_source (~> 1.0)
public_suffix (5.0.5)
rake (13.0.6)
rexml (3.2.6)
ruby2_keywords (0.0.5)
spawnling (2.1.5)
test-unit (3.5.7)
power_assert
tzinfo (2.0.5)
concurrent-ruby (~> 1.0)
webmock (3.23.0)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)

PLATFORMS
x86_64-darwin-21
Expand All @@ -62,6 +75,7 @@ DEPENDENCIES
pry
rake
test-unit
webmock

BUNDLED WITH
2.4.21
3 changes: 2 additions & 1 deletion config/config.test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
# be set via ENV variable UT_APIKEY
abort('UT_APIKEY env variable is not set. Canceling tests') unless ENV.include?('UT_APIKEY')
abort('UT_APIKEY env variable is set to an empty value. Canceling tests') unless ENV['UT_APIKEY'].size > 5

$API_CLIENT_INVALIDATE_CACHE = false
$DEBUG_API_CLIENT = false
LinkedData::Client.config do |config|
config.rest_url = 'https://data.bioontology.org'
config.apikey = ENV['UT_APIKEY']
Expand Down
141 changes: 141 additions & 0 deletions test/middleware/test_cache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
require_relative '../test_case'
require 'faraday'
require 'active_support'
require 'active_support/cache'
require_relative '../../lib/ontologies_api_client/middleware/faraday-object-cache'
require 'pry'
require 'benchmark'
require 'webmock'

class FaradayObjectCacheTest < LinkedData::Client::TestCase
def setup
WebMock.disable!
apikey = LinkedData::Client.settings.apikey
@url = "#{LinkedData::Client.settings.rest_url}/ontologies/SNOMEDCT?apikey=#{apikey}"

@cache_store = ActiveSupport::Cache::MemoryStore.new
@app = Faraday.new(url: @url) do |faraday|
faraday.use Faraday::ObjectCache, store: @cache_store
faraday.adapter :excon
end
end

def test_cache_hit_for_get_request
body1, body2 = nil
# First request should not hit the cache
time1 = Benchmark.realtime do
response1 = @app.get
assert_equal 200, response1.status
assert uncached?(response1)

body1 = JSON.parse(response1.body)
end

time2 = Benchmark.realtime do
# Second request should hit the cache
response2 = @app.get
assert_equal 304, response2.status
assert cached?(response2)
body2 = response2.parsed_body.to_hash.stringify_keys
end

assert time2 < time1

body2.each do |k,v|
k = "@id" if k.eql?('id')
k = "@type" if k.eql?('type')

next if k.eql?('context') || k.eql?('links')

assert_equal v, body1[k]
end
end


def test_cache_invalidation
# Make a request and cache the response
response1 = @app.get
assert_equal 200, response1.status

response1 = @app.get
assert_equal 304, response1.status
assert cached?(response1)

# Invalidate the cache
response2 = @app.get do |req|
req.headers['invalidate_cache'] = true
end
assert_equal 200, response2.status
assert uncached?(response2)
end

def test_cache_expiration
WebMock.enable!
WebMock.stub_request(:get, @url)
.to_return(headers: { 'Cache-Control': "max-age=1" , 'Last-Modified': Time.now.httpdate}, body: {result: 'hello'}.to_json)


# Make a request and cache the response with a short expiry time
response1 = @app.get
assert_equal 200, response1.status

# Wait for the cache to expire
sleep 2

response2 = @app.get

assert_equal 200, response2.status
assert uncached?(response2)

sleep 2

WebMock.stub_request(:get, @url)
.to_return(headers: { 'Cache-Control': "max-age=100" , 'Last-Modified': Time.now.httpdate}, body: {result: 'hello'}.to_json)
@app.get


# Wait for the cache to expire
sleep 2

response2 = @app.get

assert cached?(response2)

WebMock.disable!
end

def test_cache_last_modified
WebMock.enable!
# Make a request with Last-Modified header
WebMock.stub_request(:get, @url)
.to_return(headers: { 'Cache-Control': "max-age=1", 'Last-Modified': 3.days.ago.to_time.httpdate}, body: {result: 'hello'}.to_json, status: 304)

@app.get

response2 = @app.get
assert cached?(response2)

sleep 1

WebMock.stub_request(:get, @url)
.to_return(headers: { 'Cache-Control': "max-age=10", 'Last-Modified': 1.days.ago.to_time.httpdate}, body: {result: 'hello'}.to_json, status: 304)

response2 = @app.get
assert refreshed?(response2)
WebMock.disable!
end


private
def cached?(response)
response.env.response_headers['x-rack-cache'].eql?('hit')
end

def uncached?(response)
response.env.response_headers['x-rack-cache'].eql?('miss')
end

def refreshed?(response)
response.env.response_headers['x-rack-cache'].eql?('fresh')
end
end

0 comments on commit 8f42d4c

Please sign in to comment.