feedtools_ram_cache 1.0.2 → 1.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,5 @@
1
+ v1.1. Offline mode for easy testing.
2
+
1
3
  v1.0.2. Minor improvements in implementation and documentation.
2
4
 
3
5
  v1.0.1. Fixed typo in dependency list.
@@ -43,6 +43,22 @@ clean slate for testing.
43
43
 
44
44
  bc. FeedTools::RamFeedCache.clear
45
45
 
46
+ h3. Offline Testing Mode
47
+
48
+ One-line summary: use the following in your unit tests.
49
+
50
+ bc. FeedTools::RamFeedCache.offline_mode = true
51
+
52
+ The RAM-backed cache can be used for testing as follows. Test data is generated
53
+ by clearing the cache, opening the feed to be tested, and saving the cache's
54
+ state (perhaps by serializing to a YAML file). Testing code sets the cache state
55
+ to the saved state, and opens the feed again.
56
+
57
+ This strategy has the following issues: the code to be tested may clear the
58
+ cache (my code does), and FeedTools will not use the cached data to see if it's
59
+ stale. While the cache is in offline mode, it ignores clearing requests, and it
60
+ pretends its items are fresh.
61
+
46
62
  h2. Naming Convention
47
63
 
48
64
  The naming scheme for this gem is confusing, but it is consistent with the
@@ -2,22 +2,22 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{feedtools_ram_cache}
5
- s.version = "1.0.2"
5
+ s.version = "1.1"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Victor Costan"]
9
- s.date = %q{2009-09-21}
9
+ s.date = %q{2009-10-03}
10
10
  s.description = %q{RAM-based cache for FeedTools.}
11
11
  s.email = %q{victor@zergling.net}
12
12
  s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README.textile", "lib/feed_tools_ram_cache.rb"]
13
- s.files = ["CHANGELOG", "LICENSE", "Manifest", "README.textile", "Rakefile", "lib/feed_tools_ram_cache.rb", "test/cache_test.rb", "test/fixtures/unit.yml", "test/integration_test.rb", "feedtools_ram_cache.gemspec"]
13
+ s.files = ["CHANGELOG", "LICENSE", "Manifest", "README.textile", "Rakefile", "lib/feed_tools_ram_cache.rb", "test/cache_test.rb", "test/fixtures/unit.yml", "test/integration_test.rb", "feedtools_ram_cache.gemspec", "test/offline_test.rb"]
14
14
  s.homepage = %q{http://github.com/costan/feed_tools_ram_cache}
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Feedtools_ram_cache", "--main", "README.textile"]
16
16
  s.require_paths = ["lib"]
17
17
  s.rubyforge_project = %q{zerglings}
18
18
  s.rubygems_version = %q{1.3.5}
19
19
  s.summary = %q{RAM-based cache for FeedTools.}
20
- s.test_files = ["test/cache_test.rb", "test/integration_test.rb"]
20
+ s.test_files = ["test/cache_test.rb", "test/integration_test.rb", "test/offline_test.rb"]
21
21
 
22
22
  if s.respond_to? :specification_version then
23
23
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -37,8 +37,7 @@ class RamFeedCache
37
37
 
38
38
  # Removes all the cache items.
39
39
  def self.clear
40
- @by_id, @by_href = nil, nil
41
- initialize_cache
40
+ # NOTE: Actual implementation supplied in offline_mode=.
42
41
  end
43
42
 
44
43
  # The number of entries in the cache.
@@ -81,6 +80,46 @@ class RamFeedCache
81
80
  @fields[:id].nil? ? true : false
82
81
  end
83
82
 
83
+ # Enters or exits offline testing mode.
84
+ #
85
+ # In offline testing mode, cache clearing requests are ignored, and items
86
+ # pretend that they are fresh. Warning: this means items won't pretend like
87
+ # dumb ActiveRecord objects, and may fail unit tests that treat them as such.
88
+ def self.offline_mode=(offline)
89
+ if offline # Pretend items are live to stay offline.
90
+ undef :last_retrieved, :time_to_live
91
+ def last_retrieved
92
+ Time.now
93
+ end
94
+ def time_to_live
95
+ 86400 # A day since last_retrieved was read, should avoid re-fetching.
96
+ end
97
+
98
+ class <<self
99
+ undef :clear
100
+ def clear
101
+ initialize_cache
102
+ end
103
+ end
104
+ else # Going online is OK, don't lie about the cache state.
105
+ undef :last_retrieved, :time_to_live
106
+ def last_retrieved
107
+ @fields[:last_retrieved]
108
+ end
109
+ def time_to_live
110
+ @fields[:time_to_live]
111
+ end
112
+ class <<self
113
+ undef :clear
114
+ def clear
115
+ @by_id, @by_href = nil, nil
116
+ initialize_cache
117
+ end
118
+ end
119
+ end
120
+ end
121
+ self.offline_mode = false
122
+
84
123
  # Called by FeedTools to initialize the cache.
85
124
  def self.initialize_cache
86
125
  # NOTE: the FeedTools documentation says this will be called once. In fact,
@@ -0,0 +1,27 @@
1
+ # Author:: Victor Costan
2
+ # Copyright:: Copyright (C) 2009 Zergling.Net
3
+ # License:: MIT
4
+
5
+ require 'feed_tools_ram_cache'
6
+ require 'test/unit'
7
+
8
+
9
+ class OfflineTest < Test::Unit::TestCase
10
+ def setup
11
+ FeedTools.configurations[:feed_cache] = FeedTools::RamFeedCache
12
+ fixtures_path = File.join File.dirname(__FILE__), 'fixtures'
13
+ FeedTools::RamFeedCache.state =
14
+ YAML.load File.read(File.join(fixtures_path, 'expired_cache.yml'))
15
+ @url = "http://events.mit.edu/rss/"
16
+ end
17
+
18
+ def test_offline_and_online
19
+ FeedTools::RamFeedCache.offline_mode = true
20
+ feed = FeedTools::Feed.open @url
21
+ assert !feed.live?, "Offline mode resulted in live feed"
22
+
23
+ FeedTools::RamFeedCache.offline_mode = false
24
+ feed = FeedTools::Feed.open @url
25
+ assert feed.live?, "Online mode read expired feed"
26
+ end
27
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: feedtools_ram_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: "1.1"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Costan
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-21 00:00:00 -04:00
12
+ date: 2009-10-03 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -100,3 +100,4 @@ summary: RAM-based cache for FeedTools.
100
100
  test_files:
101
101
  - test/cache_test.rb
102
102
  - test/integration_test.rb
103
+ - test/offline_test.rb