Added notion of synchronization between error handler

this is convenient to integrate two components using their own ERROR_HANDLER (not sharing the same object)
This commit is contained in:
Jocelyn Fiat
2012-02-14 12:57:01 +01:00
parent e52cd6b352
commit dd6b74326e
3 changed files with 287 additions and 6 deletions

View File

@@ -21,7 +21,7 @@ feature {NONE} -- Initialization
make make
-- Initialize `Current'. -- Initialize `Current'.
do do
create {ARRAYED_LIST [ERROR]} errors.make (3) create {ARRAYED_LIST [like errors.item]} errors.make (3)
create error_added_actions create error_added_actions
end end
@@ -60,12 +60,139 @@ feature -- Events
error_added_actions: ACTION_SEQUENCE [TUPLE [ERROR]] error_added_actions: ACTION_SEQUENCE [TUPLE [ERROR]]
-- Actions triggered when a new error is added -- Actions triggered when a new error is added
feature -- Synchronization
add_synchronized_handler (h: ERROR_HANDLER)
--| the same handler can be added more than once
--| it will be synchronized only once
require
no_cycle: not has_synchronized_handler_in_cycle (h)
local
lst: like synchronized_handlers
do
lst := synchronized_handlers
if lst = Void then
create {ARRAYED_LIST [like synchronized_handlers.item]} lst.make (0)
lst.compare_references
synchronized_handlers := lst
end
if lst.has (h) then
check attached h.synchronized_handlers as h_lst and then h_lst.has (Current) end
else
lst.extend (h)
h.add_synchronized_handler (Current)
end
end
has_synchronized_handler_in_cycle (h: ERROR_HANDLER): BOOLEAN
do
Result := False
end
feature {ERROR_HANDLER} -- Synchronization implementation
synchronized_handlers: detachable LIST [ERROR_HANDLER]
-- Synchronized handlers
synchronize_error_from (e: ERROR; h_lst: LIST [ERROR_HANDLER])
-- Called by error_handler during synchronization process
-- if `synchronized_handlers' is Void, this means Current is synchronizing
-- this is to prevent infinite cycle iteration
require
not h_lst.has (Current)
do
h_lst.extend (Current)
if attached synchronized_handlers as lst then
synchronized_handlers := Void
add_error (e)
across
lst as c
loop
if not h_lst.has (c.item) then
c.item.synchronize_error_from (e, h_lst)
end
end
synchronized_handlers := lst
else
-- In synchronization
end
end
synchronize_reset_from (h_lst: LIST [ERROR_HANDLER])
-- Called by error_handler during synchronization process
-- if `synchronized_handlers' is Void, this means Current is synchronizing
-- this is to prevent infinite cycle iteration
require
not h_lst.has (Current)
do
h_lst.extend (Current)
if attached synchronized_handlers as lst then
synchronized_handlers := Void
reset
across
lst as c
loop
if not h_lst.has (c.item) then
c.item.synchronize_reset_from (h_lst)
end
end
synchronized_handlers := lst
else
-- In synchronization
end
end
remove_synchronized_handler (h: ERROR_HANDLER)
do
if attached synchronized_handlers as lst then
lst.prune_all (h)
if lst.is_empty then
synchronized_handlers := Void
end
end
end
feature {NONE} -- Event: implementation feature {NONE} -- Event: implementation
on_error_added (e: ERROR) on_error_added (e: ERROR)
-- Error `e' was just added -- Error `e' was just added
local
sync_list: LINKED_LIST [ERROR_HANDLER]
do do
error_added_actions.call ([e]) error_added_actions.call ([e])
if attached synchronized_handlers as lst then
synchronized_handlers := Void
create sync_list.make
sync_list.extend (Current)
across
lst as c
loop
if not sync_list.has (c.item) then
c.item.synchronize_error_from (e, sync_list)
end
end
synchronized_handlers := lst
end
end
on_reset
local
sync_list: LINKED_LIST [ERROR_HANDLER]
do
if attached synchronized_handlers as lst then
synchronized_handlers := Void
create sync_list.make
sync_list.extend (Current)
across
lst as c
loop
if not sync_list.has (c.item) then
c.item.synchronize_reset_from (sync_list)
end
end
synchronized_handlers := lst
end
end end
feature -- Basic operation feature -- Basic operation
@@ -138,18 +265,30 @@ feature -- Element changes
-- Concatenate into a single error if any -- Concatenate into a single error if any
do do
if count > 1 and then attached as_single_error as e then if count > 1 and then attached as_single_error as e then
wipe_out reset
errors.force (e) add_error (e)
end end
end end
reset, wipe_out reset
do do
if errors.count > 0 then
errors.wipe_out errors.wipe_out
on_reset
end
end
destroy
do
error_added_actions.wipe_out
if attached synchronized_handlers as lst then
lst.item.remove_synchronized_handler (Current)
end
synchronized_handlers := Void
end end
note note
copyright: "Copyright (c) 1984-2011, Eiffel Software and others" copyright: "2011-2012, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)" license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
source: "[ source: "[
Eiffel Software Eiffel Software

View File

@@ -0,0 +1,124 @@
note
description: "[
Eiffel tests that can be executed by testing tool.
]"
author: "EiffelStudio test wizard"
date: "$Date$"
revision: "$Revision$"
testing: "type/manual"
class
TEST_ERROR
inherit
EQA_TEST_SET
feature -- Test routines
test_error
note
testing: "error"
local
h: ERROR_HANDLER
cl: CELL [INTEGER]
do
create h.make
h.add_custom_error (123, "abc", "abc error occurred")
h.add_custom_error (456, "abc", "abc error occurred")
assert ("has 2 errors", h.count = 2)
h.reset
assert ("reset then no error", h.count = 0)
-- error_added_actions
create cl.put (0)
h.error_added_actions.extend (agent (i_e: ERROR; i_cl: CELL [INTEGER]) do i_cl.replace (i_cl.item + 1) end(?, cl))
h.add_custom_error (123, "abc", "abc error occurred")
h.add_custom_error (456, "abc", "abc error occurred")
assert ("has 2 errors, same as counted", h.count = 2 and h.count = cl.item)
end
test_sync_2
note
testing: "error"
local
h1, h2: ERROR_HANDLER
-- cl: CELL [INTEGER]
do
create h1.make
create h2.make
h1.add_synchronized_handler (h2)
h1.add_custom_error (123, "abc", "abc error occurred")
h1.add_custom_error (456, "abc", "abc error occurred")
assert ("has 2 errors", h1.count = 2 and h2.count = h1.count)
h1.reset
assert ("reset then no error", h1.count = 0 and h2.count = h1.count)
end
test_sync_3
note
testing: "error"
local
h1, h2, h3: ERROR_HANDLER
-- cl: CELL [INTEGER]
do
create h1.make
create h2.make
create h3.make
h1.add_synchronized_handler (h2)
h2.add_synchronized_handler (h3)
h1.add_custom_error (123, "abc", "abc error occurred")
h1.add_custom_error (456, "abc", "abc error occurred")
assert ("has 2 errors", h1.count = 2 and h2.count = h1.count and h3.count = h2.count)
h1.reset
assert ("reset then no error", h1.count = 0 and h2.count = h1.count and h3.count = h2.count)
end
test_sync_3_with_cycle
note
testing: "error"
local
h1, h2, h3: ERROR_HANDLER
-- cl: CELL [INTEGER]
do
create h1.make
create h2.make
create h3.make
h1.add_synchronized_handler (h2)
h2.add_synchronized_handler (h3)
h3.add_synchronized_handler (h1)
h1.add_custom_error (123, "abc", "abc error occurred")
h1.add_custom_error (456, "abc", "abc error occurred")
assert ("has 2 errors", h1.count = 2 and h2.count = h1.count and h3.count = h2.count)
h1.reset
assert ("reset then no error", h1.count = 0 and h2.count = h1.count and h3.count = h2.count)
end
note
copyright: "Copyright (c) 1984-2011, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
source: "[
Eiffel Software
5949 Hollister Ave., Goleta, CA 93117 USA
Telephone 805-685-1006, Fax 805-685-6869
Website http://www.eiffel.com
Customer support http://support.eiffel.com
]"
end

View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<system xmlns="http://www.eiffel.com/developers/xml/configuration-1-9-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.eiffel.com/developers/xml/configuration-1-9-0 http://www.eiffel.com/developers/xml/configuration-1-9-0.xsd" name="error_tests" uuid="54F59BB2-AD49-42C7-ABAA-B60765F4F926">
<target name="error_tests">
<root class="ANY" feature="default_create"/>
<file_rule>
<exclude>/.git$</exclude>
<exclude>/EIFGENs$</exclude>
<exclude>/.svn$</exclude>
</file_rule>
<option warning="true" full_class_checking="true" is_attached_by_default="true" void_safety="all" syntax="transitional">
<assertions precondition="true"/>
</option>
<library name="base" location="$ISE_LIBRARY\library\base\base-safe.ecf"/>
<library name="error" location="..\error-safe.ecf" readonly="false"/>
<library name="testing" location="$ISE_LIBRARY\library\testing\testing-safe.ecf"/>
<tests name="tests" location=".\"/>
</target>
</system>