From 6fade20c59095fd2748b4c6ba08edae9d5786f0e Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Wed, 27 Mar 2013 16:09:24 +0900 Subject: [PATCH] Make GC threshold user specifiable. --- lib/http/cookie_jar/hash_store.rb | 9 +++++---- lib/http/cookie_jar/mozilla_store.rb | 5 ++--- test/test_http_cookie_jar.rb | 13 +++++++++---- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/lib/http/cookie_jar/hash_store.rb b/lib/http/cookie_jar/hash_store.rb index 3cadecf..54528a7 100644 --- a/lib/http/cookie_jar/hash_store.rb +++ b/lib/http/cookie_jar/hash_store.rb @@ -9,11 +9,12 @@ end # :startdoc: class HTTP::CookieJar + # A store class that uses a hash of hashes. class HashStore < AbstractStore - GC_THRESHOLD = HTTP::Cookie::MAX_COOKIES_TOTAL / 20 - def default_options - {} + { + :gc_threshold => HTTP::Cookie::MAX_COOKIES_TOTAL / 20 + } end def initialize(options = nil) @@ -41,7 +42,7 @@ class HTTP::CookieJar def add(cookie) path_cookies = ((@jar[cookie.domain_name.hostname] ||= {})[cookie.path] ||= {}) path_cookies[cookie.name] = cookie - cleanup if (@gc_index += 1) >= GC_THRESHOLD + cleanup if (@gc_index += 1) >= @gc_threshold self end diff --git a/lib/http/cookie_jar/mozilla_store.rb b/lib/http/cookie_jar/mozilla_store.rb index cae60f2..d5e67d2 100644 --- a/lib/http/cookie_jar/mozilla_store.rb +++ b/lib/http/cookie_jar/mozilla_store.rb @@ -5,10 +5,9 @@ class HTTP::CookieJar class MozillaStore < AbstractStore SCHEMA_VERSION = 5 - GC_THRESHOLD = HTTP::Cookie::MAX_COOKIES_TOTAL / 20 - def default_options { + :gc_threshold => HTTP::Cookie::MAX_COOKIES_TOTAL / 20, :app_id => 0, :in_browser_element => false, } @@ -170,7 +169,7 @@ class HTTP::CookieJar :isSecure => cookie.secure? ? 1 : 0, :isHttpOnly => cookie.httponly? ? 1 : 0, }) - cleanup if (@gc_index += 1) >= GC_THRESHOLD + cleanup if (@gc_index += 1) >= @gc_threshold self end diff --git a/test/test_http_cookie_jar.rb b/test/test_http_cookie_jar.rb index 5f68f3d..f621b92 100644 --- a/test/test_http_cookie_jar.rb +++ b/test/test_http_cookie_jar.rb @@ -589,16 +589,21 @@ class TestHTTPCookieJar < Test::Unit::TestCase end def test_max_cookies_hashstore + gc_threshold = 150 h_test_max_cookies( - HTTP::CookieJar.new, - HTTP::Cookie::MAX_COOKIES_TOTAL + HTTP::CookieJar::HashStore::GC_THRESHOLD) + HTTP::CookieJar.new(:hash, + :gc_threshold => gc_threshold), + HTTP::Cookie::MAX_COOKIES_TOTAL + gc_threshold) end def test_max_cookies_mozillastore + gc_threshold = 150 Dir.mktmpdir { |dir| h_test_max_cookies( - HTTP::CookieJar.new(:mozilla, :filename => File.join(dir, "cookies.sqlite")), - HTTP::Cookie::MAX_COOKIES_TOTAL + HTTP::CookieJar::MozillaStore::GC_THRESHOLD) + HTTP::CookieJar.new(:mozilla, + :gc_threshold => gc_threshold, + :filename => File.join(dir, "cookies.sqlite")), + HTTP::Cookie::MAX_COOKIES_TOTAL + gc_threshold) } end end