Non-mutating operations should not be logged.

This commit is contained in:
Alexey Verkhovsky 2005-01-16 23:51:59 +00:00
parent 621b79db55
commit da3c060c89
2 changed files with 51 additions and 6 deletions

View file

@ -103,6 +103,10 @@ class WikiService
include AbstractWikiService include AbstractWikiService
include Madeleine::Automatic::Interceptor 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/' @@storage_path = './storage/'
@ -120,8 +124,15 @@ class WikiService
end end
def instance def instance
@system ||= MadeleineServer.new(self).system @madeleine ||= MadeleineServer.new(self)
@system = @madeleine.system
return @system
end end
def snapshot
@madeleine.snapshot
end
end end
def initialize def initialize
@ -157,14 +168,14 @@ class MadeleineServer
start_snapshot_thread start_snapshot_thread
end end
def system
@server.system
end
def command_log_present? def command_log_present?
not Dir[storage_path + '/*.command_log'].empty? not Dir[storage_path + '/*.command_log'].empty?
end end
def snapshot
@server.take_snapshot
end
def start_snapshot_thread def start_snapshot_thread
Thread.new(@server) { Thread.new(@server) {
hours_since_last_snapshot = 0 hours_since_last_snapshot = 0
@ -176,7 +187,7 @@ class MadeleineServer
if command_log_present? or hours_since_last_snapshot >= 24 if command_log_present? or hours_since_last_snapshot >= 24
ActionController::Base.logger.info "[#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}] " + ActionController::Base.logger.info "[#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}] " +
'Taking a Madeleine snapshot' 'Taking a Madeleine snapshot'
@server.take_snapshot snapshot
hours_since_last_snapshot = 0 hours_since_last_snapshot = 0
end end
sleep(1.hour) sleep(1.hour)
@ -190,5 +201,9 @@ class MadeleineServer
end end
} }
end end
def system
@server.system
end
end end

View file

@ -14,6 +14,7 @@ class WikiServiceTest < Test::Unit::TestCase
FileUtils.rm(Dir[RAILS_ROOT + 'storage/test/*.zip']) FileUtils.rm(Dir[RAILS_ROOT + 'storage/test/*.zip'])
FileUtils.rm(Dir[RAILS_ROOT + 'storage/test/*.pdf']) FileUtils.rm(Dir[RAILS_ROOT + 'storage/test/*.pdf'])
@@cleaned_storage = true @@cleaned_storage = true
WikiService.instance.setup('pswd', 'Wiki', 'wiki')
end end
def setup def setup
@ -30,4 +31,33 @@ class WikiServiceTest < Test::Unit::TestCase
Time.now, 'DavidHeinemeierHansson' Time.now, 'DavidHeinemeierHansson'
assert_equal "Electric shocks, I love 'em", @s.read_page('instiki', 'FirstPage').content assert_equal "Electric shocks, I love 'em", @s.read_page('instiki', 'FirstPage').content
end 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 end