spec/caching.spec in public-suffix-list-0.1.5 vs spec/caching.spec in public-suffix-list-0.1.6

- old
+ new

@@ -1,7 +1,8 @@ require 'tmpdir' require 'public_suffix_list' +require 'timecop' describe PublicSuffixList do FILE = File.join(Dir.tmpdir, "test.dat.cache") @@ -13,23 +14,47 @@ File.exist?(FILE).should be false public_suffix_list = PublicSuffixList.new(:effective_tld_names_url => "spec/test.dat", :cache_dir => Dir.tmpdir, :cache_expiry_period => 10) public_suffix_list.cache_file.cache?.should be true public_suffix_list.cache_file.exist?.should be true created_at = public_suffix_list.cache_file.data[:created_at] - tag = public_suffix_list.cache_file.data[:tag] + tag = public_suffix_list.cache_file.data[:tag] public_suffix_list = PublicSuffixList.new(:effective_tld_names_url => "spec/test.dat", :cache_dir => Dir.tmpdir, :cache_expiry_period => 10) public_suffix_list.cache_file.cache?.should be true public_suffix_list.cache_file.exist?.should be true public_suffix_list.cache_file.data[:created_at].should == created_at public_suffix_list.cache_file.data[:tag].should == tag - public_suffix_list.cache_file.data[:created_at] = Time.now - 100 - public_suffix_list.cache_file.data[:tag] = "1234567890" - public_suffix_list.cache_file.dump_data + end + + it "should fetch a new data file when the cache expiry has passed" do + File.exist?(FILE).should be false public_suffix_list = PublicSuffixList.new(:effective_tld_names_url => "spec/test.dat", :cache_dir => Dir.tmpdir, :cache_expiry_period => 10) public_suffix_list.cache_file.cache?.should be true public_suffix_list.cache_file.exist?.should be true - public_suffix_list.cache_file.data[:created_at].should_not == created_at - public_suffix_list.cache_file.data[:tag].should_not == tag + created_at = public_suffix_list.cache_file.data[:created_at] + tag = public_suffix_list.cache_file.data[:tag] + Timecop.travel(Time.now + 15) do + public_suffix_list = PublicSuffixList.new(:effective_tld_names_url => "spec/test.dat", :cache_dir => Dir.tmpdir, :cache_expiry_period => 10) + public_suffix_list.cache_file.cache?.should be true + public_suffix_list.cache_file.exist?.should be true + public_suffix_list.cache_file.data[:created_at].should_not == created_at + public_suffix_list.cache_file.data[:tag].should_not == tag + end + end + + it "should not fetch a new data file when the cache expiry has not passed" do + File.exist?(FILE).should be false + public_suffix_list = PublicSuffixList.new(:effective_tld_names_url => "spec/test.dat", :cache_dir => Dir.tmpdir, :cache_expiry_period => 10) + public_suffix_list.cache_file.cache?.should be true + public_suffix_list.cache_file.exist?.should be true + created_at = public_suffix_list.cache_file.data[:created_at] + tag = public_suffix_list.cache_file.data[:tag] + Timecop.travel(Time.now + 5) do + public_suffix_list = PublicSuffixList.new(:effective_tld_names_url => "spec/test.dat", :cache_dir => Dir.tmpdir, :cache_expiry_period => 10) + public_suffix_list.cache_file.cache?.should be true + public_suffix_list.cache_file.exist?.should be true + public_suffix_list.cache_file.data[:created_at].should == created_at + public_suffix_list.cache_file.data[:tag].should == tag + end end it "should allow 0 or nil to specify an infinite cache expiry period" do File.exist?(FILE).should be false public_suffix_list = PublicSuffixList.new(:effective_tld_names_url => "spec/test.dat", :cache_dir => Dir.tmpdir, :cache_expiry_period => 10)