Commit 336f3209ae1c9b975aada4ab3dd676287709f4b8
1 parent
5a1eee95
Exists in
caching-rails4
Testing environment caching timestamp
Showing
2 changed files
with
228 additions
and
1 deletions
Show diff stats
app/helpers/application_helper.rb
| @@ -1241,7 +1241,7 @@ module ApplicationHelper | @@ -1241,7 +1241,7 @@ module ApplicationHelper | ||
| 1241 | end | 1241 | end |
| 1242 | 1242 | ||
| 1243 | def cache_timeout(key, timeout, &block) | 1243 | def cache_timeout(key, timeout, &block) |
| 1244 | - cache(key, { :expires_in => timeout }, &block) | 1244 | + cache(key, { :expires_in => timeout, :skip_digest => true }, &block) |
| 1245 | end | 1245 | end |
| 1246 | 1246 | ||
| 1247 | def is_cache_expired?(key) | 1247 | def is_cache_expired?(key) |
| @@ -0,0 +1,227 @@ | @@ -0,0 +1,227 @@ | ||
| 1 | +require 'test_helper' | ||
| 2 | + | ||
| 3 | +# Controllers to be tested | ||
| 4 | +class BlockCachingTestController < HomeController | ||
| 5 | +end | ||
| 6 | + | ||
| 7 | +class ArticleCachingTestController < ProfileController | ||
| 8 | +end | ||
| 9 | + | ||
| 10 | +# Test case | ||
| 11 | +class BlockCacheTest < ActionController::TestCase | ||
| 12 | + | ||
| 13 | + CACHE_DIR = 'cache_test' | ||
| 14 | + FILE_STORE_PATH = Rails.root.join('tmp/test', CACHE_DIR) | ||
| 15 | + UP_DIR = ".." | ||
| 16 | + | ||
| 17 | + include NoosferoTestHelper | ||
| 18 | + | ||
| 19 | + fixtures :environments | ||
| 20 | + | ||
| 21 | + #tests CachingTestController | ||
| 22 | + | ||
| 23 | + def setup | ||
| 24 | + super | ||
| 25 | + # Enable caching in this test | ||
| 26 | + enable_cache | ||
| 27 | + end | ||
| 28 | + | ||
| 29 | + def teardown | ||
| 30 | + #disable_cache | ||
| 31 | + end | ||
| 32 | + | ||
| 33 | + def create_blog_with_articles(community) | ||
| 34 | + blog = fast_create(Blog, :name => 'Blog', :profile_id => @person.id) | ||
| 35 | + @article1 = fast_create(TinyMceArticle, :name => "First Post", :profile_id => @person.id, :parent_id => blog.id, :body => '<p> Wasserstoffbombe </p>') | ||
| 36 | + fast_create(TinyMceArticle, :name => "A Post", :profile_id => @person.id, :parent_id => blog.id, :body => '<p>Lorem ipsum dolor sit amet</p> <p>Second paragraph</p>') | ||
| 37 | + block = ArticleBlock.new | ||
| 38 | + block.article = blog | ||
| 39 | + @person.boxes << Box.new | ||
| 40 | + @person.boxes.first.blocks << block | ||
| 41 | + return block | ||
| 42 | + end | ||
| 43 | + | ||
| 44 | + # Event item CSS selector | ||
| 45 | + ev = '.event-plugin_event-block ul.events li.event[itemscope]' + | ||
| 46 | + '[itemtype="http://data-vocabulary.org/Event"] ' | ||
| 47 | + | ||
| 48 | + | ||
| 49 | + should 'update event block cache' do | ||
| 50 | + environment = Environment.default | ||
| 51 | + environment.enable_plugin('EventPlugin') | ||
| 52 | + | ||
| 53 | + environment.locales.delete_if {|key, value| key != "en" } | ||
| 54 | + | ||
| 55 | + box = Box.create!(:owner => environment) | ||
| 56 | + | ||
| 57 | + EventPlugin::EventBlock.create!(:box => box) | ||
| 58 | + | ||
| 59 | + person = fast_create(Person, :environment_id => environment.id) | ||
| 60 | + | ||
| 61 | + | ||
| 62 | + @controller = BlockCachingTestController.new | ||
| 63 | + | ||
| 64 | + event1 = Event.create!(:name=>'Event 1', :profile =>person) | ||
| 65 | + event1.slug = 'event1a' | ||
| 66 | + event1.start_date = DateTime.now | ||
| 67 | + event1.end_date = DateTime.now + 3.day | ||
| 68 | + event1.save! | ||
| 69 | + | ||
| 70 | + get :index | ||
| 71 | + | ||
| 72 | + # Check the cache store and save the entry | ||
| 73 | + cache_entry = verify_cache_entries | ||
| 74 | + | ||
| 75 | + assert_select ev + 'time.duration[itemprop="endDate"]', /4 days/ | ||
| 76 | + | ||
| 77 | + # Change the event | ||
| 78 | + event1.reload | ||
| 79 | + event1.start_date = DateTime.now | ||
| 80 | + event1.end_date = DateTime.now + 5.day | ||
| 81 | + event1.save! | ||
| 82 | + | ||
| 83 | + get :index | ||
| 84 | + | ||
| 85 | + assert_select ev + 'time.duration[itemprop="endDate"]', /6 days/ | ||
| 86 | + | ||
| 87 | + # Verify if entry have changed | ||
| 88 | + assert verify_cache_entries != cache_entry | ||
| 89 | + | ||
| 90 | + end | ||
| 91 | + | ||
| 92 | + should 'timestamp change' do | ||
| 93 | + puts "Initializing test..." | ||
| 94 | + @controller = BlockCachingTestController.new | ||
| 95 | + get :index | ||
| 96 | + puts "portal_community: #{@controller.environment.portal_community}" | ||
| 97 | + c = Community.create!(:name => 'community test', :environment => @controller.environment) | ||
| 98 | + a1 = TextileArticle.create!(:name => "Article 1", | ||
| 99 | + :profile => c, | ||
| 100 | + :abstract => "This is the article1 lead.", | ||
| 101 | + :body => "This is the article1 body.", | ||
| 102 | + :highlighted => true) | ||
| 103 | + puts "A1-updated_at: #{a1.updated_at}" | ||
| 104 | + puts "A1-to_i: #{a1.updated_at.to_i}" | ||
| 105 | + puts "A1-to_time: #{a1.updated_at.to_time}" | ||
| 106 | + puts "A1-cache_key: #{a1.cache_key}" | ||
| 107 | + puts "E1-cache_key: #{@controller.environment.cache_key}" | ||
| 108 | + puts "PE1-cache_key: #{a1.environment.cache_key}" | ||
| 109 | + a1.touch | ||
| 110 | + puts "A2-updated_at: #{a1.updated_at}" | ||
| 111 | + puts "A2-to_i: #{a1.updated_at.to_i}" | ||
| 112 | + puts "A2-to_time: #{a1.updated_at.to_time}" | ||
| 113 | + puts "A2-cache_key: #{a1.cache_key}" | ||
| 114 | + puts "E2-cache_key: #{@controller.environment.cache_key}" | ||
| 115 | + puts "PE2-cache_key: #{a1.environment.cache_key}" | ||
| 116 | + get :index | ||
| 117 | + puts "A3-updated_at: #{a1.updated_at}" | ||
| 118 | + puts "A3-to_i: #{a1.updated_at.to_i}" | ||
| 119 | + puts "A3-to_time: #{a1.updated_at.to_time}" | ||
| 120 | + puts "A3-cache_key: #{a1.cache_key}" | ||
| 121 | + puts "E3-cache_key: #{@controller.environment.cache_key}" | ||
| 122 | + puts "PE3-cache_key: #{a1.environment.cache_key}" | ||
| 123 | + end | ||
| 124 | + | ||
| 125 | + should 'update article block cache' do | ||
| 126 | + puts "Initializing test..." | ||
| 127 | + @controller = BlockCachingTestController.new | ||
| 128 | + Noosfero.stubs(:locales).returns({"en"=>"English"}) | ||
| 129 | + get :index # creates and set the environment on controller | ||
| 130 | + | ||
| 131 | + c = Community.create!(:name => 'community test', :environment => @controller.environment) | ||
| 132 | + @controller.environment.portal_community = c | ||
| 133 | + @controller.environment.enable('use_portal_community') | ||
| 134 | + puts "Test.beforeSave Environment..." | ||
| 135 | + @controller.environment.save! | ||
| 136 | + puts "Test.environment Saved!" | ||
| 137 | + | ||
| 138 | + puts "Test.index 0" | ||
| 139 | + get :index | ||
| 140 | + puts "Test.index 1" | ||
| 141 | + get :index | ||
| 142 | + puts "Test.index 2" | ||
| 143 | + get :index | ||
| 144 | + puts "Test.index 3" | ||
| 145 | + get :index | ||
| 146 | + | ||
| 147 | + puts "Test.creating Article1..." | ||
| 148 | + a1 = TextileArticle.create!(:name => "Article 1", | ||
| 149 | + :profile => c, | ||
| 150 | + :abstract => "This is the article1 lead.", | ||
| 151 | + :body => "This is the article1 body.", | ||
| 152 | + :highlighted => true) | ||
| 153 | + | ||
| 154 | + assert a1.environment == c.environment | ||
| 155 | + assert c.environment == @controller.environment | ||
| 156 | + assert @controller.environment == a1.environment | ||
| 157 | + | ||
| 158 | + puts "Article1 created" | ||
| 159 | + | ||
| 160 | + puts "Test.creating Article2..." | ||
| 161 | + a2 = TextileArticle.create!(:name => "Article 2", | ||
| 162 | + :profile => c, | ||
| 163 | + :body => "This is the article2 body.", | ||
| 164 | + :highlighted => true) | ||
| 165 | + puts "Article2 created" | ||
| 166 | + | ||
| 167 | + assert a2.environment == c.environment | ||
| 168 | + assert c.environment == @controller.environment | ||
| 169 | + assert @controller.environment == a2.environment | ||
| 170 | + assert a1.environment == a2.environment | ||
| 171 | + | ||
| 172 | + puts "Before INDEX 1" | ||
| 173 | + get :index | ||
| 174 | + | ||
| 175 | + #assert_match(/This is the article1 lead/, @response.body) | ||
| 176 | + | ||
| 177 | + # Check the cache store and save the entry | ||
| 178 | + #assert verify_cache_entries != UP_DIR, "Cache entry should have been created" | ||
| 179 | + | ||
| 180 | + a1.abstract = "Changed article1 lead" | ||
| 181 | + puts "Changed Article1. Before Save!" | ||
| 182 | + a1.save! | ||
| 183 | + puts "After Save Article1" | ||
| 184 | + | ||
| 185 | + # The cache should have been expired! | ||
| 186 | + #assert verify_cache_entries == UP_DIR, "Cache should have been expired" | ||
| 187 | + puts "Before INDEX 2" | ||
| 188 | + get :index | ||
| 189 | + | ||
| 190 | + #assert_match(/Changed article1 lead/, @response.body) | ||
| 191 | + | ||
| 192 | + # The cache should have been recreated | ||
| 193 | + #assert verify_cache_entries != UP_DIR, "Cache entry should have been created" | ||
| 194 | + | ||
| 195 | + end | ||
| 196 | + | ||
| 197 | + def verify_cache_entries | ||
| 198 | + entries = Dir.entries(FILE_STORE_PATH) | ||
| 199 | + # The cache entry created | ||
| 200 | + entries.sort.last.to_s | ||
| 201 | + end | ||
| 202 | + | ||
| 203 | + def enable_cache | ||
| 204 | + # Remove and create store | ||
| 205 | + FileUtils.rm_rf(File.dirname(FILE_STORE_PATH)) | ||
| 206 | + FileUtils.mkdir_p(FILE_STORE_PATH) | ||
| 207 | + | ||
| 208 | + ActionController::Base.perform_caching = true | ||
| 209 | + #Backup | ||
| 210 | + @page_cache_directory = ActionController::Base.page_cache_directory | ||
| 211 | + @cache_store = ActionController::Base.cache_store | ||
| 212 | + #Setup | ||
| 213 | + ActionController::Base.page_cache_directory = FILE_STORE_PATH | ||
| 214 | + ActionController::Base.cache_store = :file_store, FILE_STORE_PATH | ||
| 215 | + end | ||
| 216 | + | ||
| 217 | + def disable_cache | ||
| 218 | + # Remove cache store | ||
| 219 | + FileUtils.rm_rf(File.dirname(FILE_STORE_PATH)) | ||
| 220 | + | ||
| 221 | + #Restore | ||
| 222 | + ActionController::Base.perform_caching = false | ||
| 223 | + ActionController::Base.page_cache_directory = @page_cache_directory | ||
| 224 | + ActionController::Base.cache_store = @cache_store | ||
| 225 | + end | ||
| 226 | + | ||
| 227 | +end | ||
| 0 | \ No newline at end of file | 228 | \ No newline at end of file |