From da3c060c89dafbd2a04bb3813f3466f753223ac6 Mon Sep 17 00:00:00 2001 From: Alexey Verkhovsky Date: Sun, 16 Jan 2005 23:51:59 +0000 Subject: [PATCH] Non-mutating operations should not be logged. --- app/models/wiki_service.rb | 27 +++++++++++++++++++++------ test/unit/wiki_service_test.rb | 30 ++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/app/models/wiki_service.rb b/app/models/wiki_service.rb index 757afa6e..96b3afb9 100755 --- a/app/models/wiki_service.rb +++ b/app/models/wiki_service.rb @@ -103,6 +103,10 @@ class WikiService include AbstractWikiService include Madeleine::Automatic::Interceptor + + # These methods do not change the state of persistent objects, and + # should not be ogged by Madeleine + automatic_read_only :authenticate, :read_page, :setup?, :webs @@storage_path = './storage/' @@ -120,8 +124,15 @@ class WikiService end def instance - @system ||= MadeleineServer.new(self).system + @madeleine ||= MadeleineServer.new(self) + @system = @madeleine.system + return @system end + + def snapshot + @madeleine.snapshot + end + end def initialize @@ -157,14 +168,14 @@ class MadeleineServer start_snapshot_thread end - def system - @server.system - end - def command_log_present? not Dir[storage_path + '/*.command_log'].empty? end + def snapshot + @server.take_snapshot + end + def start_snapshot_thread Thread.new(@server) { hours_since_last_snapshot = 0 @@ -176,7 +187,7 @@ class MadeleineServer if command_log_present? or hours_since_last_snapshot >= 24 ActionController::Base.logger.info "[#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}] " + 'Taking a Madeleine snapshot' - @server.take_snapshot + snapshot hours_since_last_snapshot = 0 end sleep(1.hour) @@ -190,5 +201,9 @@ class MadeleineServer end } end + + def system + @server.system + end end diff --git a/test/unit/wiki_service_test.rb b/test/unit/wiki_service_test.rb index 641443dd..1c3f7695 100755 --- a/test/unit/wiki_service_test.rb +++ b/test/unit/wiki_service_test.rb @@ -14,6 +14,7 @@ class WikiServiceTest < Test::Unit::TestCase FileUtils.rm(Dir[RAILS_ROOT + 'storage/test/*.zip']) FileUtils.rm(Dir[RAILS_ROOT + 'storage/test/*.pdf']) @@cleaned_storage = true + WikiService.instance.setup('pswd', 'Wiki', 'wiki') end def setup @@ -30,4 +31,33 @@ class WikiServiceTest < Test::Unit::TestCase Time.now, 'DavidHeinemeierHansson' assert_equal "Electric shocks, I love 'em", @s.read_page('instiki', 'FirstPage').content end + + def test_read_only_operations + @s.write_page 'instiki', 'TestReadOnlyOperations', 'Read only operations dont change the' + + 'state of any object, and therefore should not be logged by Madeleine!', + Time.now, 'AlexeyVerkhovsky' + + assert_doesnt_change_state :authenticate, 'pswd' + assert_doesnt_change_state :read_page, 'instiki', 'TestReadOnlyOperations' + assert_doesnt_change_state :setup? + assert_doesnt_change_state :webs + + @s.write_page 'instiki', 'FirstPage', "Electric shocks, I love 'em", + Time.now, 'DavidHeinemeierHansson' + assert_equal "Electric shocks, I love 'em", @s.read_page('instiki', 'FirstPage').content + end + + + def assert_doesnt_change_state(method, *args) + WikiService.snapshot + last_snapshot_before = File.read(Dir[RAILS_ROOT + 'storage/test/*.snapshot'].last) + + @s.send(method, *args) + + command_logs = Dir[RAILS_ROOT + 'storage/test/*.command_log'] + assert command_logs.empty?, "Calls to #{method} should not be logged" + last_snapshot_after = File.read(Dir[RAILS_ROOT + 'storage/test/*.snapshot'].last) + assert last_snapshot_before == last_snapshot_after, + 'Calls to #{method} should not change the state of any persisted object' + end end