Repository: qpid-proton Updated Branches: refs/heads/master eb12513c5 -> b143db26f
PROTON-1438: added pn_event_condition convenience function A convenience function to make it easier to write generic error handling functions for events. /* * If the event context object has a condition and the condition is set * return it, otherwise return NULL. * If the event context object has remote and local conditions, * try the remote condition first, then the local. */ PN_EXTERN struct pn_condition_t *pn_event_condition(pn_event_t *event); Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/d48bf9b1 Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/d48bf9b1 Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/d48bf9b1 Branch: refs/heads/master Commit: d48bf9b1228cc8ad82b3a0afc5b6402ab9db37ec Parents: eb12513 Author: Alan Conway <[email protected]> Authored: Wed Mar 15 12:17:01 2017 -0400 Committer: Alan Conway <[email protected]> Committed: Sun Mar 19 14:40:48 2017 -0400 ---------------------------------------------------------------------- proton-c/include/proton/event.h | 8 ++++++++ proton-c/src/core/engine.c | 34 ++++++++++++++++++++++++++++++++++ proton-c/src/tests/test_tools.h | 19 +++++++++++++++++++ 3 files changed, 61 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/d48bf9b1/proton-c/include/proton/event.h ---------------------------------------------------------------------- diff --git a/proton-c/include/proton/event.h b/proton-c/include/proton/event.h index 6f93cd4..d11bdfc 100644 --- a/proton-c/include/proton/event.h +++ b/proton-c/include/proton/event.h @@ -524,6 +524,14 @@ PN_EXTERN pn_transport_t *pn_event_transport(pn_event_t *event); PN_EXTERN pn_record_t *pn_event_attachments(pn_event_t *event); /** + * If the event context object has a condition and the condition is set + * return it, otherwise return NULL. + * If the event context object has remote and local conditions, + * try the remote condition first, then the local. + */ +PN_EXTERN struct pn_condition_t *pn_event_condition(pn_event_t *event); + +/** * **Experimental** - A batch of events that must be handled in sequence. * Call pn_event_batch_next() in a loop until it returns NULL to extract * the events. http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/d48bf9b1/proton-c/src/core/engine.c ---------------------------------------------------------------------- diff --git a/proton-c/src/core/engine.c b/proton-c/src/core/engine.c index 8c2aeb0..efddc93 100644 --- a/proton-c/src/core/engine.c +++ b/proton-c/src/core/engine.c @@ -2275,3 +2275,37 @@ int pn_condition_copy(pn_condition_t *dest, pn_condition_t *src) { } return err; } + + +static pn_condition_t *cond_set(pn_condition_t *cond) { + return cond && pn_condition_is_set(cond) ? cond : NULL; +} + +static pn_condition_t *cond2_set(pn_condition_t *cond1, pn_condition_t *cond2) { + pn_condition_t *cond = cond_set(cond1); + if (!cond) cond = cond_set(cond2); + return cond; +} + +pn_condition_t *pn_event_condition(pn_event_t *e) { + void *ctx = pn_event_context(e); + switch (pn_class_id(pn_event_class(e))) { + case CID_pn_connection: { + pn_connection_t *c = (pn_connection_t*)ctx; + return cond2_set(pn_connection_remote_condition(c), pn_connection_condition(c)); + } + case CID_pn_session: { + pn_session_t *s = (pn_session_t*)ctx; + return cond2_set(pn_session_remote_condition(s), pn_session_condition(s)); + } + case CID_pn_link: { + pn_link_t *l = (pn_link_t*)ctx; + return cond2_set(pn_link_remote_condition(l), pn_link_condition(l)); + } + case CID_pn_transport: + return cond_set(pn_transport_condition((pn_transport_t*)ctx)); + + default: + return NULL; + } +} http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/d48bf9b1/proton-c/src/tests/test_tools.h ---------------------------------------------------------------------- diff --git a/proton-c/src/tests/test_tools.h b/proton-c/src/tests/test_tools.h index 9fe679c..f70a327 100644 --- a/proton-c/src/tests/test_tools.h +++ b/proton-c/src/tests/test_tools.h @@ -21,6 +21,7 @@ */ #include <proton/type_compat.h> +#include <proton/condition.h> #include <proton/event.h> #include <errno.h> @@ -123,6 +124,24 @@ static inline bool test_etype_equal_(test_t *t, int want, int got, const char *f #define TEST_ETYPE_EQUAL(TEST, WANT, GOT) \ test_etype_equal_((TEST), (WANT), (GOT), __FILE__, __LINE__) +static inline pn_event_t *test_event_type_(test_t *t, pn_event_type_t want, pn_event_t *got, const char *file, int line) { + test_check_(t, want == pn_event_type(got), NULL, file, line, "want %s got %s", + pn_event_type_name(want), + pn_event_type_name(pn_event_type(got))); + if (want != pn_event_type(got)) { + pn_condition_t *cond = pn_event_condition(got); + if (cond && pn_condition_is_set(cond)) { + test_errorf_(t, NULL, NULL, file, line, "condition: %s:%s", + pn_condition_get_name(cond), pn_condition_get_description(cond)); + } + return NULL; + } + return got; +} + +#define TEST_EVENT_TYPE(TEST, WANT, GOT) \ + test_event_type_((TEST), (WANT), (GOT), __FILE__, __LINE__) + /* T is name of a test_t variable, EXPR is the test expression (which should update T) FAILED is incremented if the test has errors */ --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
