Merge "ari: Implement 'debug all' and request/response logging"
[asterisk/asterisk.git] / tests / test_cel.c
index 247781b..c9ceaf9 100644 (file)
 
 #include "asterisk.h"
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
-
 #include <math.h>
 #include "asterisk/module.h"
 #include "asterisk/test.h"
 #include "asterisk/cel.h"
 #include "asterisk/channel.h"
+#include "asterisk/format_cache.h"
 #include "asterisk/linkedlists.h"
 #include "asterisk/chanvars.h"
 #include "asterisk/utils.h"
 #include "asterisk/causes.h"
 #include "asterisk/time.h"
 #include "asterisk/bridge.h"
-#include "asterisk/bridge_features.h"
+#include "asterisk/bridge_basic.h"
+#include "asterisk/pickup.h"
 #include "asterisk/stasis_channels.h"
 #include "asterisk/stasis_bridges.h"
 #include "asterisk/json.h"
@@ -55,6 +55,8 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
 #define CHANNEL_TECH_NAME "CELTestChannel"
 
+#define TEST_BACKEND_NAME "CEL Test Logging"
+
 /*! \brief A placeholder for Asterisk's 'real' CEL configuration */
 static struct ast_cel_general_config *saved_config;
 
@@ -84,10 +86,17 @@ static struct timespec to_sleep = {1, 0};
 
 static void do_sleep(void)
 {
-       while ((nanosleep(&to_sleep, &to_sleep) == -1) && (errno == EINTR));
+       while ((nanosleep(&to_sleep, &to_sleep) == -1) && (errno == EINTR)) {
+       }
 }
 
-#define APPEND_EVENT(chan, ev_type, userevent, extra, peer) do { \
+#define APPEND_EVENT(chan, ev_type, userevent, extra) do { \
+       if (append_expected_event(chan, ev_type, userevent, extra, NULL)) { \
+               return AST_TEST_FAIL; \
+       } \
+       } while (0)
+
+#define APPEND_EVENT_PEER(chan, ev_type, userevent, extra, peer) do { \
        if (append_expected_event(chan, ev_type, userevent, extra, peer)) { \
                return AST_TEST_FAIL; \
        } \
@@ -105,60 +114,82 @@ static void do_sleep(void)
        } \
        } while (0)
 
-#define CONF_EXIT(channel, bridge) do { \
-       ast_test_validate(test, 0 == ast_bridge_depart(channel)); \
-       CONF_EXIT_EVENT(channel, bridge); \
+#define BRIDGE_EXIT(channel, bridge) do { \
+       ast_test_validate(test, !ast_bridge_depart(channel)); \
+       BRIDGE_EXIT_EVENT(channel, bridge); \
        mid_test_sync(); \
        } while (0)
 
-#define CONF_EXIT_EVENT(channel, bridge) do { \
-       RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
-       extra = ast_json_pack("{s: s}", "bridge_id", bridge->uniqueid); \
-       ast_test_validate(test, extra != NULL); \
-       APPEND_EVENT(channel, AST_CEL_CONF_EXIT, NULL, extra, NULL); \
+#define BRIDGE_EXIT_EVENT(channel, bridge) do { \
+       RAII_VAR(struct ast_str *, peer_str, NULL, ast_free); \
+       peer_str = test_cel_generate_peer_str(channel, bridge); \
+       ast_test_validate(test, peer_str != NULL); \
+       BRIDGE_EXIT_EVENT_PEER(channel, bridge, ast_str_buffer(peer_str)); \
        } while (0)
 
-#define CONF_EXIT_SNAPSHOT(channel, bridge) do { \
+#define BRIDGE_EXIT_EVENT_PEER(channel, bridge, peer) do { \
        RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
-       extra = ast_json_pack("{s: s}", "bridge_id", bridge->uniqueid); \
+       extra = ast_json_pack("{s: s, s: s}", "bridge_id", bridge->uniqueid, "bridge_technology", bridge->technology->name); \
        ast_test_validate(test, extra != NULL); \
-       APPEND_EVENT_SNAPSHOT(channel, AST_CEL_CONF_EXIT, NULL, extra, NULL); \
+       APPEND_EVENT_PEER(channel, AST_CEL_BRIDGE_EXIT, NULL, extra, peer); \
        } while (0)
 
-#define CONF_ENTER_EVENT(channel, bridge) do { \
+#define BRIDGE_EXIT_SNAPSHOT(channel, bridge) do { \
        RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
-       extra = ast_json_pack("{s: s}", "bridge_id", bridge->uniqueid); \
+       RAII_VAR(struct ast_str *, peer_str, NULL, ast_free); \
+       peer_str = test_cel_generate_peer_str_snapshot(channel, bridge); \
+       ast_test_validate(test, peer_str != NULL); \
+       extra = ast_json_pack("{s: s, s: s}", "bridge_id", bridge->uniqueid, "bridge_technology", bridge->technology->name); \
        ast_test_validate(test, extra != NULL); \
-       APPEND_EVENT(channel, AST_CEL_CONF_ENTER, NULL, extra, NULL); \
+       APPEND_EVENT_SNAPSHOT(channel, AST_CEL_BRIDGE_EXIT, NULL, extra, ast_str_buffer(peer_str)); \
+       } while (0)
+
+#define BRIDGE_ENTER(channel, bridge) do { \
+       ast_test_validate(test, !ast_bridge_impart(bridge, channel, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE)); \
+       do_sleep(); \
+       BRIDGE_ENTER_EVENT(channel, bridge); \
+       mid_test_sync(); \
        } while (0)
 
-#define BRIDGE_TO_CONF(first, second, third, bridge) do { \
+#define BRIDGE_ENTER_EVENT(channel, bridge) do { \
+       RAII_VAR(struct ast_str *, peer_str, NULL, ast_free); \
+       peer_str = test_cel_generate_peer_str(channel, bridge); \
+       ast_test_validate(test, peer_str != NULL); \
+       BRIDGE_ENTER_EVENT_PEER(channel, bridge, ast_str_buffer(peer_str)); \
+       } while (0)
+
+#define BRIDGE_ENTER_EVENT_PEER(channel, bridge, peer) do { \
        RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
-       extra = ast_json_pack("{s: s, s: s}", \
-               "channel_name", ast_channel_name(third), \
-               "bridge_id", bridge->uniqueid); \
+       extra = ast_json_pack("{s: s, s: s}", "bridge_id", bridge->uniqueid, "bridge_technology", bridge->technology->name); \
        ast_test_validate(test, extra != NULL); \
-       APPEND_EVENT(first, AST_CEL_BRIDGE_TO_CONF, NULL, extra, ast_channel_name(second)); \
+       APPEND_EVENT_PEER(channel, AST_CEL_BRIDGE_ENTER, NULL, extra, peer); \
        } while (0)
 
 #define BLINDTRANSFER_EVENT(channel, bridge, extension, context) do { \
        RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
-       extra = ast_json_pack("{s: s, s: s, s: s}", \
+       extra = ast_json_pack("{s: s, s: s, s: s, s: s, s: s}", \
                "extension", extension, \
                "context", context, \
-               "bridge_id", bridge->uniqueid); \
+               "bridge_id", bridge->uniqueid, \
+               "transferee_channel_name", "N/A", \
+               "transferee_channel_uniqueid", "N/A"); \
        ast_test_validate(test, extra != NULL); \
-       APPEND_EVENT(channel, AST_CEL_BLINDTRANSFER, NULL, extra, NULL); \
+       APPEND_EVENT(channel, AST_CEL_BLINDTRANSFER, NULL, extra); \
        } while (0)
 
-#define ATTENDEDTRANSFER_BRIDGE(channel1, bridge1, channel2, bridge2) do { \
+#define ATTENDEDTRANSFER_BRIDGE(channel1, bridge1, channel2, bridge2, channel3, channel4) do { \
        RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref); \
-       extra = ast_json_pack("{s: s, s: s, s: s}", \
+       extra = ast_json_pack("{s: s, s: s, s: s, s: s, s: s, s: s, s: s, s: s}", \
                "bridge1_id", bridge1->uniqueid, \
                "channel2_name", ast_channel_name(channel2), \
-               "bridge2_id", bridge2->uniqueid); \
+               "channel2_uniqueid", ast_channel_uniqueid(channel2), \
+               "bridge2_id", bridge2->uniqueid, \
+               "transferee_channel_name", ast_channel_name(channel4), \
+               "transferee_channel_uniqueid", ast_channel_uniqueid(channel4), \
+               "transfer_target_channel_name", ast_channel_name(channel3), \
+               "transfer_target_channel_uniqueid", ast_channel_uniqueid(channel3)); \
        ast_test_validate(test, extra != NULL); \
-       APPEND_EVENT(channel1, AST_CEL_ATTENDEDTRANSFER, NULL, extra, NULL); \
+       APPEND_EVENT(channel1, AST_CEL_ATTENDEDTRANSFER, NULL, extra); \
        } while (0)
 
 /*! \brief Alice's Caller ID */
@@ -173,28 +204,49 @@ static void do_sleep(void)
 /*! \brief David's Caller ID */
 #define DAVID_CALLERID { .id.name.str = "David", .id.name.valid = 1, .id.number.str = "400", .id.number.valid = 1, }
 
+/*! \brief Set ulaw format on channel */
+#define SET_FORMATS(chan) do {\
+       struct ast_format_cap *caps;\
+       caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);\
+       ast_format_cap_append(caps, ast_format_ulaw, 0);\
+       ast_channel_nativeformats_set((chan), caps);\
+       ast_channel_set_writeformat((chan), ast_format_ulaw);\
+       ast_channel_set_rawwriteformat((chan), ast_format_ulaw);\
+       ast_channel_set_readformat((chan), ast_format_ulaw);\
+       ast_channel_set_rawreadformat((chan), ast_format_ulaw);\
+       ao2_ref(caps, -1);\
+} while (0)
+
 /*! \brief Create a \ref test_cel_chan_tech for Alice. */
 #define CREATE_ALICE_CHANNEL(channel_var, caller_id) do { \
-       (channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, (caller_id)->id.number.str, (caller_id)->id.name.str, "100", "100", "default", NULL, 0, CHANNEL_TECH_NAME "/Alice"); \
-       APPEND_EVENT(channel_var, AST_CEL_CHANNEL_START, NULL, NULL, NULL); \
+       (channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, (caller_id)->id.number.str, (caller_id)->id.name.str, "100", "100", "default", NULL, NULL, 0, CHANNEL_TECH_NAME "/Alice"); \
+       SET_FORMATS((channel_var));\
+       APPEND_EVENT(channel_var, AST_CEL_CHANNEL_START, NULL, NULL); \
+       ast_channel_unlock((channel_var)); \
        } while (0)
 
 /*! \brief Create a \ref test_cel_chan_tech for Bob. */
 #define CREATE_BOB_CHANNEL(channel_var, caller_id) do { \
-       (channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, (caller_id)->id.number.str, (caller_id)->id.name.str, "200", "200", "default", NULL, 0, CHANNEL_TECH_NAME "/Bob"); \
-       APPEND_EVENT(channel_var, AST_CEL_CHANNEL_START, NULL, NULL, NULL); \
+       (channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, (caller_id)->id.number.str, (caller_id)->id.name.str, "200", "200", "default", NULL, NULL, 0, CHANNEL_TECH_NAME "/Bob"); \
+       SET_FORMATS((channel_var));\
+       APPEND_EVENT(channel_var, AST_CEL_CHANNEL_START, NULL, NULL); \
+       ast_channel_unlock((channel_var)); \
        } while (0)
 
 /*! \brief Create a \ref test_cel_chan_tech for Charlie. */
 #define CREATE_CHARLIE_CHANNEL(channel_var, caller_id) do { \
-       (channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, (caller_id)->id.number.str, (caller_id)->id.name.str, "300", "300", "default", NULL, 0, CHANNEL_TECH_NAME "/Charlie"); \
-       APPEND_EVENT(channel_var, AST_CEL_CHANNEL_START, NULL, NULL, NULL); \
+       (channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, (caller_id)->id.number.str, (caller_id)->id.name.str, "300", "300", "default", NULL, NULL, 0, CHANNEL_TECH_NAME "/Charlie"); \
+       SET_FORMATS((channel_var));\
+       APPEND_EVENT(channel_var, AST_CEL_CHANNEL_START, NULL, NULL); \
+       ast_channel_unlock((channel_var)); \
        } while (0)
 
 /*! \brief Create a \ref test_cel_chan_tech for David. */
 #define CREATE_DAVID_CHANNEL(channel_var, caller_id) do { \
-       (channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, (caller_id)->id.number.str, (caller_id)->id.name.str, "400", "400", "default", NULL, 0, CHANNEL_TECH_NAME "/David"); \
-       APPEND_EVENT(channel_var, AST_CEL_CHANNEL_START, NULL, NULL, NULL); \
+       (channel_var) = ast_channel_alloc(0, AST_STATE_DOWN, (caller_id)->id.number.str, (caller_id)->id.name.str, "400", "400", "default", NULL, NULL, 0, CHANNEL_TECH_NAME "/David"); \
+       SET_FORMATS((channel_var));\
+       APPEND_EVENT(channel_var, AST_CEL_CHANNEL_START, NULL, NULL); \
+       ast_channel_unlock((channel_var)); \
        } while (0)
 
 /*! \brief Emulate a channel entering into an application */
@@ -214,7 +266,7 @@ static void do_sleep(void)
 
 #define ANSWER_NO_APP(chan) do { \
        ast_setstate(chan, AST_STATE_UP); \
-       APPEND_EVENT(chan, AST_CEL_ANSWER, NULL, NULL, NULL); \
+       APPEND_EVENT(chan, AST_CEL_ANSWER, NULL, NULL); \
        } while (0)
 
 /*! \brief Hang up a test channel safely */
@@ -223,8 +275,7 @@ static void do_sleep(void)
        ao2_ref(channel, +1); \
        ast_hangup((channel)); \
        HANGUP_EVENT(channel, cause, dialstatus); \
-       APPEND_EVENT(channel, AST_CEL_CHANNEL_END, NULL, NULL, NULL); \
-       stasis_topic_wait(ast_channel_topic_all_cached()); \
+       APPEND_EVENT(channel, AST_CEL_CHANNEL_END, NULL, NULL); \
        ao2_cleanup(stasis_cache_get(ast_channel_cache(), \
                ast_channel_snapshot_type(), ast_channel_uniqueid(channel))); \
        ao2_cleanup(channel); \
@@ -238,7 +289,7 @@ static void do_sleep(void)
                "hangupsource", "", \
                "dialstatus", dialstatus); \
        ast_test_validate(test, extra != NULL); \
-       APPEND_EVENT(channel, AST_CEL_HANGUP, NULL, extra, NULL); \
+       APPEND_EVENT(channel, AST_CEL_HANGUP, NULL, extra); \
        } while (0)
 
 static void mid_test_sync(void);
@@ -247,15 +298,82 @@ static int append_expected_event(
        struct ast_channel *chan,
        enum ast_cel_event_type type,
        const char *userdefevname,
-       struct ast_json *extra, const char *peer);
+       struct ast_json *extra,
+       const char *peer);
 
 static int append_expected_event_snapshot(
        struct ast_channel_snapshot *snapshot,
        enum ast_cel_event_type type,
        const char *userdefevname,
-       struct ast_json *extra, const char *peer);
+       struct ast_json *extra,
+       const char *peer);
 
+#ifdef RACEY_TESTS
 static int append_dummy_event(void);
+#endif
+
+static struct ast_str *__test_cel_generate_peer_str(struct ast_channel_snapshot *chan, struct ast_bridge_snapshot *bridge)
+{
+       struct ast_str *peer_str = ast_str_create(32);
+       struct ao2_iterator i;
+       char *current_chan = NULL;
+
+       if (!peer_str) {
+               return NULL;
+       }
+
+       for (i = ao2_iterator_init(bridge->channels, 0);
+               (current_chan = ao2_iterator_next(&i));
+               ao2_cleanup(current_chan)) {
+               RAII_VAR(struct ast_channel_snapshot *, current_snapshot,
+                       NULL,
+                       ao2_cleanup);
+
+               /* Don't add the channel for which this message is being generated */
+               if (!strcmp(current_chan, chan->uniqueid)) {
+                       continue;
+               }
+
+               current_snapshot = ast_channel_snapshot_get_latest(current_chan);
+               if (!current_snapshot) {
+                       continue;
+               }
+
+               ast_str_append(&peer_str, 0, "%s,", current_snapshot->name);
+       }
+       ao2_iterator_destroy(&i);
+
+       /* Rip off the trailing comma */
+       ast_str_truncate(peer_str, -1);
+
+       return peer_str;
+}
+
+static struct ast_str *test_cel_generate_peer_str_snapshot(struct ast_channel_snapshot *chan, struct ast_bridge *bridge)
+{
+       RAII_VAR(struct ast_bridge_snapshot *, snapshot,
+               ast_bridge_snapshot_get_latest(bridge->uniqueid),
+               ao2_cleanup);
+
+       if (!snapshot) {
+               return NULL;
+       }
+
+       return __test_cel_generate_peer_str(chan, snapshot);
+}
+
+static struct ast_str *test_cel_generate_peer_str(struct ast_channel *chan, struct ast_bridge *bridge)
+{
+       RAII_VAR(struct ast_channel_snapshot *, snapshot,
+               ast_channel_snapshot_get_latest(ast_channel_uniqueid(chan)),
+               ao2_cleanup);
+
+       if (!snapshot) {
+               return NULL;
+       }
+
+       return test_cel_generate_peer_str_snapshot(snapshot, bridge);
+}
 
 static void safe_channel_release(struct ast_channel *chan)
 {
@@ -265,6 +383,14 @@ static void safe_channel_release(struct ast_channel *chan)
        ast_channel_release(chan);
 }
 
+static void safe_bridge_destroy(struct ast_bridge *bridge)
+{
+       if (!bridge) {
+               return;
+       }
+       ast_bridge_destroy(bridge, 0);
+}
+
 AST_TEST_DEFINE(test_cel_channel_creation)
 {
        RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
@@ -302,7 +428,7 @@ AST_TEST_DEFINE(test_cel_unanswered_inbound_call)
                info->description =
                        "Test CEL records for a call that is\n"
                        "inbound to Asterisk, executes some dialplan, but\n"
-                       "is never answered.\n";
+                       "is never answered.";
                return AST_TEST_NOT_RUN;
        case TEST_EXECUTE:
                break;
@@ -333,7 +459,7 @@ AST_TEST_DEFINE(test_cel_unanswered_outbound_call)
                info->summary = "Test outbound unanswered calls";
                info->description =
                        "Test CEL records for a call that is\n"
-                       "outbound to Asterisk but is never answered.\n";
+                       "outbound to Asterisk but is never answered.";
                return AST_TEST_NOT_RUN;
        case TEST_EXECUTE:
                break;
@@ -362,7 +488,7 @@ AST_TEST_DEFINE(test_cel_single_party)
                info->summary = "Test CEL for a single party";
                info->description =
                        "Test CEL records for a call that is\n"
-                       "answered, but only involves a single channel\n";
+                       "answered, but only involves a single channel";
                return AST_TEST_NOT_RUN;
        case TEST_EXECUTE:
                break;
@@ -380,7 +506,7 @@ AST_TEST_DEFINE(test_cel_single_party)
 AST_TEST_DEFINE(test_cel_single_bridge)
 {
        RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
-       RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
+       RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
 
        struct ast_party_caller caller = ALICE_CALLERID;
 
@@ -391,7 +517,7 @@ AST_TEST_DEFINE(test_cel_single_bridge)
                info->summary = "Test CEL for a single party entering/leaving a bridge";
                info->description =
                        "Test CEL records for a call that is\n"
-                       "answered, enters a bridge, and leaves it.\n";
+                       "answered, enters a bridge, and leaves it.";
                return AST_TEST_NOT_RUN;
        case TEST_EXECUTE:
                break;
@@ -405,11 +531,11 @@ AST_TEST_DEFINE(test_cel_single_bridge)
        EMULATE_APP_DATA(chan, 2, "Bridge", "");
 
        do_sleep();
-       ast_bridge_impart(bridge, chan, NULL, NULL, 0);
+       BRIDGE_ENTER(chan, bridge);
 
        do_sleep();
 
-       ast_bridge_depart(chan);
+       BRIDGE_EXIT(chan, bridge);
 
        HANGUP_CHANNEL(chan, AST_CAUSE_NORMAL, "");
 
@@ -419,7 +545,7 @@ AST_TEST_DEFINE(test_cel_single_bridge)
 AST_TEST_DEFINE(test_cel_single_bridge_continue)
 {
        RAII_VAR(struct ast_channel *, chan, NULL, safe_channel_release);
-       RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
+       RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
        struct ast_party_caller caller = ALICE_CALLERID;
 
        switch (cmd) {
@@ -429,7 +555,7 @@ AST_TEST_DEFINE(test_cel_single_bridge_continue)
                info->summary = "Test CEL for a single party entering/leaving a bridge";
                info->description =
                        "Test CEL records for a call that is\n"
-                       "answered, enters a bridge, and leaves it.\n";
+                       "answered, enters a bridge, and leaves it.";
                return AST_TEST_NOT_RUN;
        case TEST_EXECUTE:
                break;
@@ -443,11 +569,11 @@ AST_TEST_DEFINE(test_cel_single_bridge_continue)
        EMULATE_APP_DATA(chan, 2, "Bridge", "");
 
        do_sleep();
-       ast_bridge_impart(bridge, chan, NULL, NULL, 0);
+       BRIDGE_ENTER(chan, bridge);
 
        do_sleep();
 
-       ast_bridge_depart(chan);
+       BRIDGE_EXIT(chan, bridge);
 
        EMULATE_APP_DATA(chan, 3, "Wait", "");
 
@@ -461,7 +587,7 @@ AST_TEST_DEFINE(test_cel_single_twoparty_bridge_a)
 {
        RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
        RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
-       RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
+       RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
        struct ast_party_caller caller_alice = ALICE_CALLERID;
        struct ast_party_caller caller_bob = BOB_CALLERID;
 
@@ -473,7 +599,7 @@ AST_TEST_DEFINE(test_cel_single_twoparty_bridge_a)
                info->description =
                        "Test CEL records for a call that is\n"
                        "answered, enters a bridge, and leaves it. In this scenario, the\n"
-                       "Party A should answer the bridge first.\n";
+                       "Party A should answer the bridge first.";
                return AST_TEST_NOT_RUN;
        case TEST_EXECUTE:
                break;
@@ -488,19 +614,16 @@ AST_TEST_DEFINE(test_cel_single_twoparty_bridge_a)
        ANSWER_CHANNEL(chan_alice);
        EMULATE_APP_DATA(chan_alice, 2, "Bridge", "");
 
-       ast_bridge_impart(bridge, chan_alice, NULL, NULL, 0);
+       BRIDGE_ENTER(chan_alice, bridge);
        do_sleep();
 
        ANSWER_CHANNEL(chan_bob);
        EMULATE_APP_DATA(chan_bob, 2, "Bridge", "");
 
-       ast_bridge_impart(bridge, chan_bob, NULL, NULL, 0);
-       do_sleep();
-       APPEND_EVENT(chan_alice, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_bob));
+       BRIDGE_ENTER(chan_bob, bridge);
 
-       ast_bridge_depart(chan_alice);
-       ast_bridge_depart(chan_bob);
-       APPEND_EVENT(chan_alice, AST_CEL_BRIDGE_END, NULL, NULL, ast_channel_name(chan_bob));
+       BRIDGE_EXIT(chan_alice, bridge);
+       BRIDGE_EXIT(chan_bob, bridge);
 
        HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
        HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
@@ -512,7 +635,7 @@ AST_TEST_DEFINE(test_cel_single_twoparty_bridge_b)
 {
        RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
        RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
-       RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
+       RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
        struct ast_party_caller caller_alice = ALICE_CALLERID;
        struct ast_party_caller caller_bob = BOB_CALLERID;
 
@@ -524,7 +647,7 @@ AST_TEST_DEFINE(test_cel_single_twoparty_bridge_b)
                info->description =
                        "Test CEL records for a call that is\n"
                        "answered, enters a bridge, and leaves it. In this scenario, the\n"
-                       "Party B should answer the bridge first.\n";
+                       "Party B should answer the bridge first.";
                return AST_TEST_NOT_RUN;
        case TEST_EXECUTE:
                break;
@@ -543,16 +666,12 @@ AST_TEST_DEFINE(test_cel_single_twoparty_bridge_b)
        EMULATE_APP_DATA(chan_bob, 2, "Bridge", "");
        do_sleep();
 
-       ast_bridge_impart(bridge, chan_bob, NULL, NULL, 0);
-       do_sleep();
+       BRIDGE_ENTER(chan_bob, bridge);
 
-       ast_bridge_impart(bridge, chan_alice, NULL, NULL, 0);
-       do_sleep();
-       APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_alice));
+       BRIDGE_ENTER(chan_alice, bridge);
 
-       ast_bridge_depart(chan_alice);
-       ast_bridge_depart(chan_bob);
-       APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_END, NULL, NULL, ast_channel_name(chan_alice));
+       BRIDGE_EXIT(chan_alice, bridge);
+       BRIDGE_EXIT(chan_bob, bridge);
 
        HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
        HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
@@ -563,13 +682,13 @@ AST_TEST_DEFINE(test_cel_single_twoparty_bridge_b)
 /* XXX Validation needs to be reworked on a per-channel basis before
  * test_cel_single_multiparty_bridge and test_cel_dial_answer_multiparty
  * can operate properly. */
-#if 0
+#ifdef RACEY_TESTS
 AST_TEST_DEFINE(test_cel_single_multiparty_bridge)
 {
        RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
        RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
        RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
-       RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
+       RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
        struct ast_party_caller caller_alice = ALICE_CALLERID;
        struct ast_party_caller caller_bob = BOB_CALLERID;
        struct ast_party_caller caller_charlie = CHARLIE_CALLERID;
@@ -582,7 +701,7 @@ AST_TEST_DEFINE(test_cel_single_multiparty_bridge)
                info->description =
                        "Test CEL records for a call that is\n"
                        "answered, enters a bridge, and leaves it. A total of three\n"
-                       "parties perform this action.\n";
+                       "parties perform this action.";
                return AST_TEST_NOT_RUN;
        case TEST_EXECUTE:
                break;
@@ -599,26 +718,22 @@ AST_TEST_DEFINE(test_cel_single_multiparty_bridge)
 
        do_sleep();
 
-       ast_bridge_impart(bridge, chan_alice, NULL, NULL, 0);
+       BRIDGE_ENTER(chan_alice, bridge);
 
        ANSWER_CHANNEL(chan_bob);
        EMULATE_APP_DATA(chan_bob, 2, "Bridge", "");
        do_sleep();
 
-       ast_bridge_impart(bridge, chan_bob, NULL, NULL, 0);
-       do_sleep();
-       APPEND_EVENT(chan_alice, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_bob));
+       BRIDGE_ENTER(chan_bob, bridge);
 
        ANSWER_CHANNEL(chan_charlie);
        EMULATE_APP_DATA(chan_charlie, 2, "Bridge", "");
        do_sleep();
-       ast_bridge_impart(bridge, chan_charlie, NULL, NULL, 0);
-       do_sleep();
-       BRIDGE_TO_CONF(chan_alice, chan_bob, chan_charlie, bridge);
+       BRIDGE_ENTER(chan_charlie, bridge);
 
-       CONF_EXIT(chan_alice, bridge);
-       CONF_EXIT(chan_bob, bridge);
-       CONF_EXIT(chan_charlie, bridge);
+       BRIDGE_EXIT(chan_alice, bridge);
+       BRIDGE_EXIT(chan_bob, bridge);
+       BRIDGE_EXIT(chan_charlie, bridge);
 
        HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
        HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
@@ -639,7 +754,9 @@ AST_TEST_DEFINE(test_cel_single_multiparty_bridge)
        START_DIALED_FULL(caller, callee, "200", "Bob")
 
 #define START_DIALED_FULL(caller, callee, number, name) do { \
-       callee = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, number, NULL, NULL, ast_channel_linkedid(caller), 0, CHANNEL_TECH_NAME "/" name); \
+       callee = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, number, NULL, NULL, NULL, caller, 0, CHANNEL_TECH_NAME "/" name); \
+       SET_FORMATS(callee);\
+       ast_channel_unlock(callee); \
        if (append_expected_event(callee, AST_CEL_CHANNEL_START, NULL, NULL, NULL)) { \
                return AST_TEST_FAIL; \
        } \
@@ -661,7 +778,7 @@ AST_TEST_DEFINE(test_cel_dial_unanswered)
                info->summary = "Test CEL for a dial that isn't answered";
                info->description =
                        "Test CEL records for a channel that\n"
-                       "performs a dial operation that isn't answered\n";
+                       "performs a dial operation that isn't answered";
                return AST_TEST_NOT_RUN;
        case TEST_EXECUTE:
                break;
@@ -682,6 +799,40 @@ AST_TEST_DEFINE(test_cel_dial_unanswered)
        return AST_TEST_PASS;
 }
 
+AST_TEST_DEFINE(test_cel_dial_unanswered_filter)
+{
+       RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
+       RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
+       struct ast_party_caller caller = ALICE_CALLERID;
+
+       switch (cmd) {
+       case TEST_INIT:
+               info->name = __func__;
+               info->category = TEST_CATEGORY;
+               info->summary = "Test CEL for a dial that isn't answered";
+               info->description =
+                       "Test CEL records for a channel that\n"
+                       "performs a dial operation that isn't answered";
+               return AST_TEST_NOT_RUN;
+       case TEST_EXECUTE:
+               break;
+       }
+
+       CREATE_ALICE_CHANNEL(chan_caller, &caller);
+
+       EMULATE_DIAL(chan_caller, CHANNEL_TECH_NAME "/Bob");
+
+       START_DIALED(chan_caller, chan_callee);
+
+       ast_channel_state_set(chan_caller, AST_STATE_RINGING);
+       ast_channel_publish_dial(chan_caller, chan_callee, NULL, "NOT A VALID DIAL STATUS");
+       ast_channel_publish_dial(chan_caller, chan_callee, NULL, "NOANSWER");
+
+       HANGUP_CHANNEL(chan_caller, AST_CAUSE_NO_ANSWER, "NOANSWER");
+       HANGUP_CHANNEL(chan_callee, AST_CAUSE_NO_ANSWER, "");
+
+       return AST_TEST_PASS;
+}
 
 AST_TEST_DEFINE(test_cel_dial_busy)
 {
@@ -696,7 +847,7 @@ AST_TEST_DEFINE(test_cel_dial_busy)
                info->summary = "Test CEL for a dial that results in a busy";
                info->description =
                        "Test CEL records for a channel that\n"
-                       "performs a dial operation to an endpoint that's busy\n";
+                       "performs a dial operation to an endpoint that's busy";
                return AST_TEST_NOT_RUN;
        case TEST_EXECUTE:
                break;
@@ -730,7 +881,7 @@ AST_TEST_DEFINE(test_cel_dial_congestion)
                info->summary = "Test CEL for a dial that results in congestion";
                info->description =
                        "Test CEL records for a channel that\n"
-                       "performs a dial operation to an endpoint that's congested\n";
+                       "performs a dial operation to an endpoint that's congested";
                return AST_TEST_NOT_RUN;
        case TEST_EXECUTE:
                break;
@@ -764,7 +915,7 @@ AST_TEST_DEFINE(test_cel_dial_unavailable)
                info->summary = "Test CEL for a dial that results in unavailable";
                info->description =
                        "Test CEL records for a channel that\n"
-                       "performs a dial operation to an endpoint that's unavailable\n";
+                       "performs a dial operation to an endpoint that's unavailable";
                return AST_TEST_NOT_RUN;
        case TEST_EXECUTE:
                break;
@@ -799,7 +950,7 @@ AST_TEST_DEFINE(test_cel_dial_caller_cancel)
                info->description =
                        "Test CEL records for a channel that\n"
                        "performs a dial operation to an endpoint but then decides\n"
-                       "to hang up, cancelling the dial\n";
+                       "to hang up, cancelling the dial";
                return AST_TEST_NOT_RUN;
        case TEST_EXECUTE:
                break;
@@ -836,7 +987,7 @@ AST_TEST_DEFINE(test_cel_dial_parallel_failed)
                info->description =
                        "This tests dialing three parties: Bob, Charlie, David. Charlie\n"
                        "returns BUSY; David returns CONGESTION; Bob fails to answer and\n"
-                       "Alice hangs up. Three records are created for Alice as a result.\n";
+                       "Alice hangs up. Three records are created for Alice as a result.";
                return AST_TEST_NOT_RUN;
        case TEST_EXECUTE:
                break;
@@ -889,7 +1040,7 @@ AST_TEST_DEFINE(test_cel_dial_answer_no_bridge)
                        "a dial, then bounce both channels to different priorities and\n"
                        "never have them enter a bridge together. Ew. This makes sure that\n"
                        "when we answer, we get a CEL, it gets ended at that point, and\n"
-                       "that it gets finalized appropriately.\n";
+                       "that it gets finalized appropriately.";
                return AST_TEST_NOT_RUN;
        case TEST_EXECUTE:
                break;
@@ -921,7 +1072,7 @@ AST_TEST_DEFINE(test_cel_dial_answer_twoparty_bridge_a)
 {
        RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
        RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
-       RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
+       RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
        struct ast_party_caller caller = ALICE_CALLERID;
 
        switch (cmd) {
@@ -930,7 +1081,7 @@ AST_TEST_DEFINE(test_cel_dial_answer_twoparty_bridge_a)
                info->category = TEST_CATEGORY;
                info->summary = "Test dialing, answering, and going into a 2-party bridge";
                info->description =
-                       "The most 'basic' of scenarios\n";
+                       "The most 'basic' of scenarios";
                return AST_TEST_NOT_RUN;
        case TEST_EXECUTE:
                break;
@@ -952,15 +1103,11 @@ AST_TEST_DEFINE(test_cel_dial_answer_twoparty_bridge_a)
 
        do_sleep();
 
-       ast_bridge_impart(bridge, chan_caller, NULL, NULL, 0);
-       do_sleep();
-       ast_bridge_impart(bridge, chan_callee, NULL, NULL, 0);
-       do_sleep();
-       APPEND_EVENT(chan_caller, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_callee));
+       BRIDGE_ENTER(chan_caller, bridge);
+       BRIDGE_ENTER(chan_callee, bridge);
 
-       ast_bridge_depart(chan_caller);
-       ast_bridge_depart(chan_callee);
-       APPEND_EVENT(chan_caller, AST_CEL_BRIDGE_END, NULL, NULL, ast_channel_name(chan_callee));
+       BRIDGE_EXIT(chan_caller, bridge);
+       BRIDGE_EXIT(chan_callee, bridge);
 
        HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "ANSWER");
        HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL, "");
@@ -972,7 +1119,7 @@ AST_TEST_DEFINE(test_cel_dial_answer_twoparty_bridge_b)
 {
        RAII_VAR(struct ast_channel *, chan_caller, NULL, safe_channel_release);
        RAII_VAR(struct ast_channel *, chan_callee, NULL, safe_channel_release);
-       RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
+       RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
        struct ast_party_caller caller = ALICE_CALLERID;
 
        switch (cmd) {
@@ -981,7 +1128,7 @@ AST_TEST_DEFINE(test_cel_dial_answer_twoparty_bridge_b)
                info->category = TEST_CATEGORY;
                info->summary = "Test dialing, answering, and going into a 2-party bridge";
                info->description =
-                       "The most 'basic' of scenarios\n";
+                       "The most 'basic' of scenarios";
                return AST_TEST_NOT_RUN;
        case TEST_EXECUTE:
                break;
@@ -1002,15 +1149,11 @@ AST_TEST_DEFINE(test_cel_dial_answer_twoparty_bridge_b)
        ANSWER_NO_APP(chan_callee);
 
        do_sleep();
-       ast_bridge_impart(bridge, chan_callee, NULL, NULL, 0);
-       do_sleep();
-       ast_bridge_impart(bridge, chan_caller, NULL, NULL, 0);
-       do_sleep();
-       APPEND_EVENT(chan_callee, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_caller));
+       BRIDGE_ENTER(chan_callee, bridge);
+       BRIDGE_ENTER(chan_caller, bridge);
 
-       ast_bridge_depart(chan_caller);
-       ast_bridge_depart(chan_callee);
-       APPEND_EVENT(chan_callee, AST_CEL_BRIDGE_END, NULL, NULL, ast_channel_name(chan_caller));
+       BRIDGE_EXIT(chan_caller, bridge);
+       BRIDGE_EXIT(chan_callee, bridge);
 
        HANGUP_CHANNEL(chan_caller, AST_CAUSE_NORMAL, "ANSWER");
        HANGUP_CHANNEL(chan_callee, AST_CAUSE_NORMAL, "");
@@ -1018,14 +1161,14 @@ AST_TEST_DEFINE(test_cel_dial_answer_twoparty_bridge_b)
        return AST_TEST_PASS;
 }
 
-#if 0
+#ifdef RACEY_TESTS
 AST_TEST_DEFINE(test_cel_dial_answer_multiparty)
 {
        RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
        RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
        RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
        RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
-       RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
+       RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
        struct ast_party_caller alice_caller = ALICE_CALLERID;
        struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
 
@@ -1035,7 +1178,7 @@ AST_TEST_DEFINE(test_cel_dial_answer_multiparty)
                info->category = TEST_CATEGORY;
                info->summary = "Test dialing, answering, and going into a multi-party bridge";
                info->description =
-                       "A little tricky to get to do, but possible with some redirects.\n";
+                       "A little tricky to get to do, but possible with some redirects.";
                return AST_TEST_NOT_RUN;
        case TEST_EXECUTE:
                break;
@@ -1076,24 +1219,15 @@ AST_TEST_DEFINE(test_cel_dial_answer_multiparty)
        do_sleep();
 
        do_sleep();
-       ast_test_validate(test, 0 == ast_bridge_impart(bridge, chan_charlie, NULL, NULL, 0));
-       do_sleep();
-       ast_test_validate(test, 0 == ast_bridge_impart(bridge, chan_david, NULL, NULL, 0));
-       do_sleep();
-       APPEND_EVENT(chan_charlie, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_david));
-
-       ast_test_validate(test, 0 == ast_bridge_impart(bridge, chan_bob, NULL, NULL, 0));
-       do_sleep();
-       BRIDGE_TO_CONF(chan_charlie, chan_david, chan_bob, bridge);
+       BRIDGE_ENTER(chan_charlie, bridge);
+       BRIDGE_ENTER(chan_david, bridge);
+       BRIDGE_ENTER(chan_bob, bridge);
+       BRIDGE_ENTER(chan_alice, bridge);
 
-       ast_test_validate(test, 0 == ast_bridge_impart(bridge, chan_alice, NULL, NULL, 0));
-       do_sleep();
-       CONF_ENTER_EVENT(chan_alice, bridge);
-
-       CONF_EXIT(chan_alice, bridge);
-       CONF_EXIT(chan_bob, bridge);
-       CONF_EXIT(chan_charlie, bridge);
-       CONF_EXIT(chan_david, bridge);
+       BRIDGE_EXIT(chan_alice, bridge);
+       BRIDGE_EXIT(chan_bob, bridge);
+       BRIDGE_EXIT(chan_charlie, bridge);
+       BRIDGE_EXIT(chan_david, bridge);
 
        HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "ANSWER");
        HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
@@ -1108,10 +1242,10 @@ AST_TEST_DEFINE(test_cel_blind_transfer)
 {
        RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
        RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
-       RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
+       RAII_VAR(struct ast_bridge *, bridge, NULL, safe_bridge_destroy);
+       RAII_VAR(struct ast_blind_transfer_message *, transfer_msg, NULL, ao2_cleanup);
        struct ast_party_caller alice_caller = ALICE_CALLERID;
        struct ast_party_caller bob_caller = BOB_CALLERID;
-       struct ast_bridge_channel_pair pair;
 
        switch (cmd) {
        case TEST_INIT:
@@ -1120,7 +1254,7 @@ AST_TEST_DEFINE(test_cel_blind_transfer)
                info->summary = "Test blind transfers to an extension";
                info->description =
                        "This test creates two channels, bridges them, and then"
-                       " blind transfers the bridge to an extension.\n";
+                       " blind transfers the bridge to an extension.";
                return AST_TEST_NOT_RUN;
        case TEST_EXECUTE:
                break;
@@ -1134,23 +1268,30 @@ AST_TEST_DEFINE(test_cel_blind_transfer)
        ANSWER_NO_APP(chan_alice);
        ANSWER_NO_APP(chan_bob);
 
-       ast_test_validate(test, 0 == ast_bridge_impart(bridge, chan_bob, NULL, NULL, 0));
-       do_sleep();
-
-       ast_test_validate(test, 0 == ast_bridge_impart(bridge, chan_alice, NULL, NULL, 0));
-       do_sleep();
-       APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_alice));
+       BRIDGE_ENTER(chan_bob, bridge);
+       BRIDGE_ENTER(chan_alice, bridge);
 
-       pair.bridge = bridge;
-       pair.channel = chan_alice;
-       ast_bridge_publish_blind_transfer(1, AST_BRIDGE_TRANSFER_SUCCESS,
-               &pair, "transfer_context", "transfer_extension");
+       ast_bridge_lock(bridge);
+       transfer_msg = ast_blind_transfer_message_create(1, chan_alice,
+                       "transfer_extension", "transfer_context");
+       if (!transfer_msg) {
+               ast_bridge_unlock(bridge);
+               ast_test_status_update(test, "Failed to create transfer Stasis message\n");
+               return AST_TEST_FAIL;
+       }
+       transfer_msg->bridge = ast_bridge_snapshot_create(bridge);
+       if (!transfer_msg->bridge) {
+               ast_bridge_unlock(bridge);
+               ast_test_status_update(test, "Failed to create bridge snapshot\n");
+               return AST_TEST_FAIL;
+       }
+       ast_bridge_unlock(bridge);
+       transfer_msg->result = AST_BRIDGE_TRANSFER_SUCCESS;
+       ast_bridge_publish_blind_transfer(transfer_msg);
        BLINDTRANSFER_EVENT(chan_alice, bridge, "transfer_extension", "transfer_context");
 
-       APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_END, NULL, NULL, ast_channel_name(chan_alice));
-       ast_test_validate(test, 0 == ast_bridge_depart(chan_alice));
-
-       ast_test_validate(test, 0 == ast_bridge_depart(chan_bob));
+       BRIDGE_EXIT(chan_alice, bridge);
+       BRIDGE_EXIT(chan_bob, bridge);
 
        HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
        do_sleep();
@@ -1159,14 +1300,18 @@ AST_TEST_DEFINE(test_cel_blind_transfer)
        return AST_TEST_PASS;
 }
 
+/* XXX Validation needs to take into account the BRIDGE_EXIT for Alice and the
+ * ATTENDEDTRANSFER message are not guaranteed to be ordered
+ */
+#ifdef RACEY_TESTS
 AST_TEST_DEFINE(test_cel_attended_transfer_bridges_swap)
 {
        RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
        RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
        RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
        RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
-       RAII_VAR(struct ast_bridge *, bridge1, NULL, ao2_cleanup);
-       RAII_VAR(struct ast_bridge *, bridge2, NULL, ao2_cleanup);
+       RAII_VAR(struct ast_bridge *, bridge1, NULL, safe_bridge_destroy);
+       RAII_VAR(struct ast_bridge *, bridge2, NULL, safe_bridge_destroy);
        struct ast_party_caller alice_caller = ALICE_CALLERID;
        struct ast_party_caller bob_caller = BOB_CALLERID;
        struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
@@ -1180,7 +1325,7 @@ AST_TEST_DEFINE(test_cel_attended_transfer_bridges_swap)
                info->description =
                        "This test creates four channels, places each pair in"
                        " a bridge, and then attended transfers the bridges"
-                       " together.\n";
+                       " together.";
                return AST_TEST_NOT_RUN;
        case TEST_EXECUTE:
                break;
@@ -1194,12 +1339,8 @@ AST_TEST_DEFINE(test_cel_attended_transfer_bridges_swap)
        ANSWER_NO_APP(chan_alice);
        ANSWER_NO_APP(chan_bob);
 
-       ast_test_validate(test, 0 == ast_bridge_impart(bridge1, chan_bob, NULL, NULL, 0));
-       do_sleep();
-
-       ast_test_validate(test, 0 == ast_bridge_impart(bridge1, chan_alice, NULL, NULL, 0));
-       do_sleep();
-       APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_alice));
+       BRIDGE_ENTER(chan_bob, bridge1);
+       BRIDGE_ENTER(chan_alice, bridge1);
 
        /* Create second set of bridged parties */
        bridge2 = ast_bridge_basic_new();
@@ -1210,26 +1351,27 @@ AST_TEST_DEFINE(test_cel_attended_transfer_bridges_swap)
        ANSWER_NO_APP(chan_david);
        ANSWER_NO_APP(chan_charlie);
 
-       ast_test_validate(test, 0 == ast_bridge_impart(bridge2, chan_charlie, NULL, NULL, 0));
-       do_sleep();
+       BRIDGE_ENTER(chan_charlie, bridge2);
 
-       ast_test_validate(test, 0 == ast_bridge_impart(bridge2, chan_david, NULL, NULL, 0));
+       BRIDGE_ENTER(chan_david, bridge2);
+       BRIDGE_EXIT_EVENT(chan_bob, bridge1);
        do_sleep();
-       APPEND_EVENT(chan_charlie, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_david));
 
        /* Perform attended transfer */
-       APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_END, NULL, NULL, ast_channel_name(chan_alice));
-
-       ast_bridge_transfer_attended(chan_alice, chan_david);
+       if (ast_bridge_transfer_attended(chan_alice, chan_david)) {
+               ast_test_status_update(test, "Attended transfer failed!\n");
+               return AST_TEST_FAIL;
+       }
        do_sleep();
-       BRIDGE_TO_CONF(chan_charlie, chan_david, chan_bob, bridge2);
-       CONF_EXIT_EVENT(chan_david, bridge2);
+       BRIDGE_ENTER_EVENT_PEER(chan_bob, bridge2, "CELTestChannel/David,CELTestChannel/Charlie");
 
-       ATTENDEDTRANSFER_BRIDGE(chan_alice, bridge1, chan_david, bridge2);
+       BRIDGE_EXIT_EVENT(chan_david, bridge2);
+       BRIDGE_EXIT_EVENT(chan_alice, bridge1);
+       ATTENDEDTRANSFER_BRIDGE(chan_alice, bridge1, chan_david, bridge2, chan_charlie, chan_bob);
 
        do_sleep();
-       CONF_EXIT(chan_bob, bridge2);
-       CONF_EXIT(chan_charlie, bridge2);
+       BRIDGE_EXIT(chan_bob, bridge2);
+       BRIDGE_EXIT(chan_charlie, bridge2);
 
        HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
        do_sleep();
@@ -1241,6 +1383,7 @@ AST_TEST_DEFINE(test_cel_attended_transfer_bridges_swap)
 
        return AST_TEST_PASS;
 }
+#endif
 
 AST_TEST_DEFINE(test_cel_attended_transfer_bridges_merge)
 {
@@ -1248,8 +1391,8 @@ AST_TEST_DEFINE(test_cel_attended_transfer_bridges_merge)
        RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
        RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
        RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
-       RAII_VAR(struct ast_bridge *, bridge1, NULL, ao2_cleanup);
-       RAII_VAR(struct ast_bridge *, bridge2, NULL, ao2_cleanup);
+       RAII_VAR(struct ast_bridge *, bridge1, NULL, safe_bridge_destroy);
+       RAII_VAR(struct ast_bridge *, bridge2, NULL, safe_bridge_destroy);
        struct ast_party_caller alice_caller = ALICE_CALLERID;
        struct ast_party_caller bob_caller = BOB_CALLERID;
        struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
@@ -1264,14 +1407,15 @@ AST_TEST_DEFINE(test_cel_attended_transfer_bridges_merge)
                info->description =
                        "This test creates four channels, places each pair"
                        " in a bridge, and then attended transfers the bridges"
-                       " together causing a bridge merge.\n";
+                       " together causing a bridge merge.";
                return AST_TEST_NOT_RUN;
        case TEST_EXECUTE:
                break;
        }
        /* Create first set of bridged parties */
        bridge1 = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_MULTIMIX,
-               AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART);
+               AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART,
+               "test_cel", "test_cel_atxfer_bridges_merge_1", NULL);
        ast_test_validate(test, bridge1 != NULL);
 
        CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
@@ -1279,16 +1423,13 @@ AST_TEST_DEFINE(test_cel_attended_transfer_bridges_merge)
        ANSWER_NO_APP(chan_alice);
        ANSWER_NO_APP(chan_bob);
 
-       ast_test_validate(test, 0 == ast_bridge_impart(bridge1, chan_bob, NULL, NULL, 0));
-       do_sleep();
-
-       ast_test_validate(test, 0 == ast_bridge_impart(bridge1, chan_alice, NULL, NULL, 0));
-       do_sleep();
-       APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_alice));
+       BRIDGE_ENTER(chan_bob, bridge1);
+       BRIDGE_ENTER(chan_alice, bridge1);
 
        /* Create second set of bridged parties */
        bridge2 = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_MULTIMIX,
-               AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART);
+               AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART,
+               "test_cel", "test_cel_atxfer_bridges_merge_2", NULL);
        ast_test_validate(test, bridge2 != NULL);
 
        CREATE_DAVID_CHANNEL(chan_david, &david_caller);
@@ -1296,26 +1437,26 @@ AST_TEST_DEFINE(test_cel_attended_transfer_bridges_merge)
        ANSWER_NO_APP(chan_david);
        ANSWER_NO_APP(chan_charlie);
 
-       ast_test_validate(test, 0 == ast_bridge_impart(bridge2, chan_charlie, NULL, NULL, 0));
-       do_sleep();
+       BRIDGE_ENTER(chan_charlie, bridge2);
 
-       ast_test_validate(test, 0 == ast_bridge_impart(bridge2, chan_david, NULL, NULL, 0));
-       do_sleep();
-       APPEND_EVENT(chan_charlie, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_david));
+       BRIDGE_ENTER(chan_david, bridge2);
 
        /* Perform attended transfer */
-       APPEND_EVENT(chan_charlie, AST_CEL_BRIDGE_END, NULL, NULL, ast_channel_name(chan_david));
-
-       ast_bridge_transfer_attended(chan_alice, chan_david);
+       if (ast_bridge_transfer_attended(chan_alice, chan_david)) {
+               ast_test_status_update(test, "Attended transfer failed!\n");
+               return AST_TEST_FAIL;
+       }
        do_sleep();
-       BRIDGE_TO_CONF(chan_bob, chan_alice, chan_charlie, bridge1);
-       CONF_EXIT_EVENT(chan_alice, bridge1);
+       BRIDGE_EXIT_EVENT_PEER(chan_charlie, bridge2, "CELTestChannel/David");
+       BRIDGE_ENTER_EVENT_PEER(chan_charlie, bridge1, "CELTestChannel/Bob,CELTestChannel/Alice");
+       BRIDGE_EXIT_EVENT(chan_david, bridge2);
+       BRIDGE_EXIT_EVENT(chan_alice, bridge1);
 
-       ATTENDEDTRANSFER_BRIDGE(chan_alice, bridge1, chan_david, bridge2);
+       ATTENDEDTRANSFER_BRIDGE(chan_alice, bridge1, chan_david, bridge2, chan_charlie, chan_bob);
 
        do_sleep();
-       CONF_EXIT(chan_bob, bridge1);
-       CONF_EXIT(chan_charlie, bridge1);
+       BRIDGE_EXIT(chan_bob, bridge1);
+       BRIDGE_EXIT(chan_charlie, bridge1);
 
        HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
        do_sleep();
@@ -1328,14 +1469,18 @@ AST_TEST_DEFINE(test_cel_attended_transfer_bridges_merge)
        return AST_TEST_PASS;
 }
 
+/* XXX Validation needs to take into account the BRIDGE_EXIT for David and the
+ * ATTENDEDTRANSFER message are not guaranteed to be ordered
+ */
+#ifdef RACEY_TESTS
 AST_TEST_DEFINE(test_cel_attended_transfer_bridges_link)
 {
        RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
        RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
        RAII_VAR(struct ast_channel *, chan_charlie, NULL, safe_channel_release);
        RAII_VAR(struct ast_channel *, chan_david, NULL, safe_channel_release);
-       RAII_VAR(struct ast_bridge *, bridge1, NULL, ao2_cleanup);
-       RAII_VAR(struct ast_bridge *, bridge2, NULL, ao2_cleanup);
+       RAII_VAR(struct ast_bridge *, bridge1, NULL, safe_bridge_destroy);
+       RAII_VAR(struct ast_bridge *, bridge2, NULL, safe_bridge_destroy);
        struct ast_party_caller alice_caller = ALICE_CALLERID;
        struct ast_party_caller bob_caller = BOB_CALLERID;
        struct ast_party_caller charlie_caller = CHARLIE_CALLERID;
@@ -1350,7 +1495,7 @@ AST_TEST_DEFINE(test_cel_attended_transfer_bridges_link)
                info->description =
                        "This test creates four channels, places each pair"
                        " in a bridge, and then attended transfers the bridges"
-                       " together causing a bridge link.\n";
+                       " together causing a bridge link.";
                return AST_TEST_NOT_RUN;
        case TEST_EXECUTE:
                break;
@@ -1359,7 +1504,8 @@ AST_TEST_DEFINE(test_cel_attended_transfer_bridges_link)
        bridge1 = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_MULTIMIX,
                AST_BRIDGE_FLAG_MERGE_INHIBIT_TO | AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM
                | AST_BRIDGE_FLAG_SWAP_INHIBIT_TO | AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM
-               | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART);
+               | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART,
+               "test_cel", "test_cel_atxfer_bridges_link_1", NULL);
        ast_test_validate(test, bridge1 != NULL);
 
        CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
@@ -1367,18 +1513,15 @@ AST_TEST_DEFINE(test_cel_attended_transfer_bridges_link)
        ANSWER_NO_APP(chan_alice);
        ANSWER_NO_APP(chan_bob);
 
-       ast_test_validate(test, 0 == ast_bridge_impart(bridge1, chan_bob, NULL, NULL, 0));
-       do_sleep();
-
-       ast_test_validate(test, 0 == ast_bridge_impart(bridge1, chan_alice, NULL, NULL, 0));
-       do_sleep();
-       APPEND_EVENT(chan_bob, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_alice));
+       BRIDGE_ENTER(chan_bob, bridge1);
+       BRIDGE_ENTER(chan_alice, bridge1);
 
        /* Create second set of bridged parties */
        bridge2 = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_MULTIMIX,
                AST_BRIDGE_FLAG_MERGE_INHIBIT_TO | AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM
                | AST_BRIDGE_FLAG_SWAP_INHIBIT_TO | AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM
-               | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART);
+               | AST_BRIDGE_FLAG_TRANSFER_PROHIBITED | AST_BRIDGE_FLAG_SMART,
+               "test_cel", "test_cel_atxfer_bridges_link_2", NULL);
        ast_test_validate(test, bridge2 != NULL);
 
        CREATE_DAVID_CHANNEL(chan_david, &david_caller);
@@ -1386,45 +1529,22 @@ AST_TEST_DEFINE(test_cel_attended_transfer_bridges_link)
        ANSWER_NO_APP(chan_david);
        ANSWER_NO_APP(chan_charlie);
 
-       ast_test_validate(test, 0 == ast_bridge_impart(bridge2, chan_charlie, NULL, NULL, 0));
-       do_sleep();
-
-       ast_test_validate(test, 0 == ast_bridge_impart(bridge2, chan_david, NULL, NULL, 0));
-       do_sleep();
-       APPEND_EVENT(chan_charlie, AST_CEL_BRIDGE_START, NULL, NULL, ast_channel_name(chan_david));
+       BRIDGE_ENTER(chan_charlie, bridge2);
+       BRIDGE_ENTER(chan_david, bridge2);
 
        /* Perform attended transfer */
-
-       /* The following events can not be matched directly since nothing is known
-        * about the linking local channel.
-        * local channel ;1 and ;2 creation and ;2 answer */
-       APPEND_DUMMY_EVENT();
-       APPEND_DUMMY_EVENT();
-       APPEND_DUMMY_EVENT();
-
-       ATTENDEDTRANSFER_BRIDGE(chan_alice, bridge1, chan_david, bridge2);
-
-       /* The two BRIDGE_TO_CONFs and CONF_EXITs are all racing to be first */
-
-       /* BRIDGE_TO_CONF with primary charlie, peer david, and trigger channel ;2 */
-       APPEND_DUMMY_EVENT();
+       ATTENDEDTRANSFER_BRIDGE(chan_alice, bridge1, chan_david, bridge2, chan_charlie, chan_bob);
 
        ast_bridge_transfer_attended(chan_alice, chan_david);
        do_sleep();
 
-       /* BRIDGE_TO_CONF with primary bob, peer alice, and trigger channel ;1 */
-       APPEND_DUMMY_EVENT();
-
-       /* CONF_EXIT alice and david */
-       APPEND_DUMMY_EVENT();
+       /* BRIDGE_EXIT alice and david */
        APPEND_DUMMY_EVENT();
-
-       /* ANSWER ;1 */
        APPEND_DUMMY_EVENT();
 
        do_sleep();
-       CONF_EXIT(chan_bob, bridge1);
-       CONF_EXIT(chan_charlie, bridge2);
+       BRIDGE_EXIT(chan_bob, bridge1);
+       BRIDGE_EXIT(chan_charlie, bridge2);
 
        HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
        do_sleep();
@@ -1433,9 +1553,11 @@ AST_TEST_DEFINE(test_cel_attended_transfer_bridges_link)
        HANGUP_CHANNEL(chan_david, AST_CAUSE_NORMAL, "");
        do_sleep();
        HANGUP_CHANNEL(chan_charlie, AST_CAUSE_NORMAL, "");
+       do_sleep();
 
        return AST_TEST_PASS;
 }
+#endif
 
 AST_TEST_DEFINE(test_cel_dial_pickup)
 {
@@ -1453,7 +1575,7 @@ AST_TEST_DEFINE(test_cel_dial_pickup)
                info->description =
                        "Test CEL records for a call that is\n"
                        "inbound to Asterisk, executes some dialplan, and\n"
-                       "is picked up.\n";
+                       "is picked up.";
                return AST_TEST_NOT_RUN;
        case TEST_EXECUTE:
                break;
@@ -1470,9 +1592,15 @@ AST_TEST_DEFINE(test_cel_dial_pickup)
        CREATE_CHARLIE_CHANNEL(chan_charlie, &charlie_caller);
 
        {
+               RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref);
                SCOPED_CHANNELLOCK(lock, chan_callee);
-               APPEND_EVENT(chan_callee, AST_CEL_PICKUP, NULL, NULL, ast_channel_name(chan_charlie));
-               ast_test_validate(test, 0 == ast_do_pickup(chan_charlie, chan_callee));
+
+               extra = ast_json_pack("{s: s, s: s}", "pickup_channel", ast_channel_name(chan_charlie),
+                       "pickup_channel_uniqueid", ast_channel_uniqueid(chan_charlie));
+               ast_test_validate(test, extra != NULL);
+
+               APPEND_EVENT(chan_callee, AST_CEL_PICKUP, NULL, extra);
+               ast_test_validate(test, !ast_do_pickup(chan_charlie, chan_callee));
        }
 
        /* Hang up the masqueraded zombie */
@@ -1497,6 +1625,7 @@ AST_TEST_DEFINE(test_cel_local_optimize)
        RAII_VAR(struct ast_channel_snapshot *, bob_snapshot, NULL, ao2_cleanup);
        RAII_VAR(struct stasis_message *, local_opt_begin, NULL, ao2_cleanup);
        RAII_VAR(struct stasis_message *, local_opt_end, NULL, ao2_cleanup);
+       RAII_VAR(struct ast_json *, extra, NULL, ast_json_unref);
 
        switch (cmd) {
        case TEST_INIT:
@@ -1506,7 +1635,7 @@ AST_TEST_DEFINE(test_cel_local_optimize)
                info->description =
                        "Test CEL records for two local channels being optimized\n"
                        "out by sending a messages indicating local optimization\n"
-                       "begin and end\n";
+                       "begin and end";
                return AST_TEST_NOT_RUN;
        case TEST_EXECUTE:
                break;
@@ -1518,10 +1647,14 @@ AST_TEST_DEFINE(test_cel_local_optimize)
        CREATE_ALICE_CHANNEL(chan_alice, &alice_caller);
        CREATE_BOB_CHANNEL(chan_bob, &bob_caller);
 
+       ast_channel_lock(chan_alice);
        alice_snapshot = ast_channel_snapshot_create(chan_alice);
+       ast_channel_unlock(chan_alice);
        ast_test_validate(test, alice_snapshot != NULL);
 
+       ast_channel_lock(chan_bob);
        bob_snapshot = ast_channel_snapshot_create(chan_bob);
+       ast_channel_unlock(chan_bob);
        ast_test_validate(test, bob_snapshot != NULL);
 
        ast_multi_channel_blob_add_channel(mc_blob, "1", alice_snapshot);
@@ -1535,7 +1668,12 @@ AST_TEST_DEFINE(test_cel_local_optimize)
 
        stasis_publish(ast_channel_topic(chan_alice), local_opt_begin);
        stasis_publish(ast_channel_topic(chan_alice), local_opt_end);
-       APPEND_EVENT_SNAPSHOT(alice_snapshot, AST_CEL_LOCAL_OPTIMIZE, NULL, NULL, bob_snapshot->name);
+
+       extra = ast_json_pack("{s: s, s: s}", "local_two", bob_snapshot->name,
+               "local_two_uniqueid", bob_snapshot->uniqueid);
+       ast_test_validate(test, extra != NULL);
+
+       APPEND_EVENT_SNAPSHOT(alice_snapshot, AST_CEL_LOCAL_OPTIMIZE, NULL, extra, NULL);
 
        HANGUP_CHANNEL(chan_alice, AST_CAUSE_NORMAL, "");
        HANGUP_CHANNEL(chan_bob, AST_CAUSE_NORMAL, "");
@@ -1543,9 +1681,6 @@ AST_TEST_DEFINE(test_cel_local_optimize)
        return AST_TEST_PASS;
 }
 
-/*! Subscription for CEL events */
-static struct ast_event_sub *event_sub = NULL;
-
 /*! Container for astobj2 duplicated ast_events */
 static struct ao2_container *cel_received_events = NULL;
 
@@ -1604,6 +1739,7 @@ static int append_event(struct ast_event *ev)
        return 0;
 }
 
+#ifdef RACEY_TESTS
 static int append_dummy_event(void)
 {
        RAII_VAR(struct ast_event *, ev, NULL, ast_free);
@@ -1616,12 +1752,14 @@ static int append_dummy_event(void)
 
        return append_event(ev);
 }
+#endif
 
 static int append_expected_event_snapshot(
        struct ast_channel_snapshot *snapshot,
        enum ast_cel_event_type type,
        const char *userdefevname,
-       struct ast_json *extra, const char *peer)
+       struct ast_json *extra,
+       const char *peer)
 {
        RAII_VAR(struct ast_event *, ev, NULL, ast_free);
        ev = ast_cel_create_event(snapshot, type, userdefevname, extra, peer);
@@ -1636,10 +1774,13 @@ static int append_expected_event(
        struct ast_channel *chan,
        enum ast_cel_event_type type,
        const char *userdefevname,
-       struct ast_json *extra, const char *peer)
+       struct ast_json *extra,
+       const char *peer)
 {
        RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
+       ast_channel_lock(chan);
        snapshot = ast_channel_snapshot_create(chan);
+       ast_channel_unlock(chan);
        if (!snapshot) {
                return -1;
        }
@@ -1647,24 +1788,19 @@ static int append_expected_event(
        return append_expected_event_snapshot(snapshot, type, userdefevname, extra, peer);
 }
 
-static void test_sub(const struct ast_event *event, void *data)
+static void test_sub(struct ast_event *event)
 {
        struct ast_event *event_dup = ao2_dup_event(event);
-       const char *sync_tag;
+       const char *chan_name;
        SCOPED_MUTEX(mid_test_lock, &mid_test_sync_lock);
 
        if (!event_dup) {
                return;
        }
 
-       sync_tag = ast_event_get_ie_str(event, AST_EVENT_IE_SERVICE);
-       if (sync_tag) {
-               if (!strcmp(sync_tag, "SYNC")) {
-                       /* trigger things */
-                       SCOPED_MUTEX(lock, &sync_lock);
-                       ast_cond_signal(&sync_out);
-                       return;
-               }
+       chan_name = ast_event_get_ie_str(event_dup, AST_EVENT_IE_CEL_CHANNAME);
+       if (chan_name && strncmp(chan_name, CHANNEL_TECH_NAME, 14)) {
+               return;
        }
 
        /* save the event for later processing */
@@ -1684,11 +1820,11 @@ static void test_sub(const struct ast_event *event, void *data)
 }
 
 /*!
- * \internal \brief Callback function called before each test executes
+ * \internal
+ * \brief Callback function called before each test executes
  */
 static int test_cel_init_cb(struct ast_test_info *info, struct ast_test *test)
 {
-       ast_assert(event_sub == NULL);
        ast_assert(cel_received_events == NULL);
        ast_assert(cel_expected_events == NULL);
 
@@ -1705,12 +1841,54 @@ static int test_cel_init_cb(struct ast_test_info *info, struct ast_test *test)
        cel_expected_events = ao2_container_alloc(1, NULL, NULL);
 
        /* start the CEL event callback */
-       event_sub = ast_event_subscribe(AST_EVENT_CEL, test_sub, "CEL Test Logging",
-               NULL, AST_EVENT_IE_END);
+       if (ast_cel_backend_register(TEST_BACKEND_NAME, test_sub)) {
+               return -1;
+       }
        return 0;
 }
 
-/*! \brief Check an IE value from two events,  */
+/*!
+ * \brief Check two peer strings for equality
+ *
+ * \retval zero if the peer strings do not match
+ * \retval non-zero if the peer strings match
+ */
+static int test_cel_peer_strings_match(const char *str1, const char *str2)
+{
+       struct ao2_container *intersection = ast_str_container_alloc(11);
+       RAII_VAR(char *, str1_dup, ast_strdup(str1), ast_free);
+       RAII_VAR(char *, str2_dup, ast_strdup(str2), ast_free);
+       char *chan;
+
+       while ((chan = strsep(&str1_dup, ","))) {
+               ast_str_container_add(intersection, chan);
+       }
+
+       while ((chan = strsep(&str2_dup, ","))) {
+               RAII_VAR(char *, ao2_chan, ao2_find(intersection, chan, OBJ_SEARCH_KEY), ao2_cleanup);
+
+               /* item in str2 not in str1 */
+               if (!ao2_chan) {
+                       return 0;
+               }
+
+               ast_str_container_remove(intersection, chan);
+       }
+
+       /* item in str1 not in str2 */
+       if (ao2_container_count(intersection)) {
+               return 0;
+       }
+
+       return 1;
+}
+
+/*!
+ * \brief Check an IE value from two events
+ *
+ * \retval zero if the IEs in the events of the specified type do not match
+ * \retval non-zero if the IEs in the events of the specified type match
+ */
 static int match_ie_val(
        const struct ast_event *event1,
        const struct ast_event *event2,
@@ -1718,6 +1896,15 @@ static int match_ie_val(
 {
        enum ast_event_ie_pltype pltype = ast_event_get_ie_pltype(type);
 
+       /* XXX ignore sec/usec for now */
+       if (type == AST_EVENT_IE_CEL_EVENT_TIME_USEC) {
+               return 1;
+       }
+
+       if (type == AST_EVENT_IE_CEL_EVENT_TIME) {
+               return 1;
+       }
+
        switch (pltype) {
        case AST_EVENT_IE_PLTYPE_UINT:
        {
@@ -1727,32 +1914,28 @@ static int match_ie_val(
        }
        case AST_EVENT_IE_PLTYPE_STR:
        {
-               const char *str;
-               uint32_t hash;
+               const char *str1 = ast_event_get_ie_str(event1, type);
+               const char *str2 = ast_event_get_ie_str(event2, type);
 
-               hash = ast_event_get_ie_str_hash(event2, type);
-               if (hash != ast_event_get_ie_str_hash(event1, type)) {
+               if (!str1 && !str2) {
+                       return 1;
+               } else if (!str1) {
+                       return 0;
+               } else if (!str2) {
                        return 0;
                }
 
-               str = ast_event_get_ie_str(event2, type);
-               if (str) {
-                       const char *e1str, *e2str;
-                       e1str = ast_event_get_ie_str(event1, type);
-                       e2str = str;
-
-                       if (type == AST_EVENT_IE_DEVICE) {
-                               e1str = ast_tech_to_upper(ast_strdupa(e1str));
-                               e2str = ast_tech_to_upper(ast_strdupa(e2str));
-                       }
-
-                       if (!strcmp(e1str, e2str)) {
-                               return 1;
-                       }
+               /* use special matching for CEL PEER field */
+               if (type == AST_EVENT_IE_CEL_PEER) {
+                       return test_cel_peer_strings_match(str1, str2);
                }
 
-               return 0;
+               return !strcmp(str1, str2);
        }
+       case AST_EVENT_IE_PLTYPE_RAW:
+       case AST_EVENT_IE_PLTYPE_BITFLAGS:
+               /* Fall through: just pass on these types */
+               return 1;
        default:
                break;
        }
@@ -1770,13 +1953,8 @@ static int events_are_equal(struct ast_test *test, struct ast_event *received, s
        }
 
        for (res = ast_event_iterator_init(&iterator, received); !res; res = ast_event_iterator_next(&iterator)) {
-               /* XXX ignore sec/usec for now */
-               /* ignore EID */
                int ie_type = ast_event_iterator_get_ie_type(&iterator);
-               if (ie_type != AST_EVENT_IE_CEL_EVENT_TIME_USEC
-                       && ie_type != AST_EVENT_IE_EID
-                       && ie_type != AST_EVENT_IE_CEL_EVENT_TIME
-                       && !match_ie_val(received, expected, ie_type)) {
+               if (!match_ie_val(received, expected, ie_type)) {
                        ast_test_status_update(test, "Failed matching on field %s\n", ast_event_get_ie_type_name(ie_type));
                        return 0;
                }
@@ -1794,7 +1972,7 @@ static int dump_event(struct ast_test *test, struct ast_event *event)
                return 0;
        }
 
-       ast_test_status_update(test, "Event: %s %s\n", ast_event_get_type_name(event),
+       ast_test_status_update(test, "Event: %s\n",
                ast_cel_get_type_name(ast_event_get_ie_uint(event, AST_EVENT_IE_CEL_EVENT_TYPE)));
 
        do {
@@ -1808,9 +1986,6 @@ static int dump_event(struct ast_test *test, struct ast_event *event)
 
                switch (ie_pltype) {
                case AST_EVENT_IE_PLTYPE_UNKNOWN:
-               case AST_EVENT_IE_PLTYPE_EXISTS:
-                       ast_test_status_update(test, "%s\n", ie_type_name);
-                       break;
                case AST_EVENT_IE_PLTYPE_STR:
                        ast_test_status_update(test, "%.30s: %s\n", ie_type_name,
                                        ast_event_iterator_get_ie_str(&i));
@@ -1819,9 +1994,6 @@ static int dump_event(struct ast_test *test, struct ast_event *event)
                        ast_test_status_update(test, "%.30s: %u\n", ie_type_name,
                                        ast_event_iterator_get_ie_uint(&i));
                        break;
-               case AST_EVENT_IE_PLTYPE_BITFLAGS:
-                       ast_test_status_update(test, "%.30s: %u\n", ie_type_name,
-                                       ast_event_iterator_get_ie_bitflags(&i));
                default:
                        break;
                }
@@ -1834,8 +2006,10 @@ static int dump_event(struct ast_test *test, struct ast_event *event)
 
 static int check_events(struct ast_test *test, struct ao2_container *local_expected, struct ao2_container *local_received)
 {
-       struct ao2_iterator expected_it, received_it;
-       struct ast_event *rx_event, *ex_event;
+       struct ao2_iterator received_it;
+       struct ao2_iterator expected_it;
+       RAII_VAR(struct ast_event *, rx_event, NULL, ao2_cleanup);
+       RAII_VAR(struct ast_event *, ex_event, NULL, ao2_cleanup);
        int debug = 0;
 
        if (ao2_container_count(local_expected) != ao2_container_count(local_received)) {
@@ -1846,12 +2020,14 @@ static int check_events(struct ast_test *test, struct ao2_container *local_expec
                debug = 1;
        }
 
-       expected_it = ao2_iterator_init(local_expected, 0);
        received_it = ao2_iterator_init(local_received, 0);
+       expected_it = ao2_iterator_init(local_expected, 0);
        rx_event = ao2_iterator_next(&received_it);
        ex_event = ao2_iterator_next(&expected_it);
        while (rx_event && ex_event) {
                if (!events_are_equal(test, rx_event, ex_event)) {
+                       ao2_iterator_destroy(&received_it);
+                       ao2_iterator_destroy(&expected_it);
                        ast_test_status_update(test, "Received event:\n");
                        dump_event(test, rx_event);
                        ast_test_status_update(test, "Expected event:\n");
@@ -1859,7 +2035,9 @@ static int check_events(struct ast_test *test, struct ao2_container *local_expec
                        return -1;
                }
                if (debug) {
-                       ast_test_status_update(test, "Compared events successfully%s\n", ast_event_get_type(ex_event) == AST_EVENT_CUSTOM ? " (wildcard match)" : "");
+                       ast_test_status_update(test, "Compared events successfully%s\n",
+                               ast_event_get_type(ex_event) == AST_EVENT_CUSTOM
+                                       ? " (wildcard match)" : "");
                        dump_event(test, rx_event);
                }
                ao2_cleanup(rx_event);
@@ -1867,82 +2045,42 @@ static int check_events(struct ast_test *test, struct ao2_container *local_expec
                rx_event = ao2_iterator_next(&received_it);
                ex_event = ao2_iterator_next(&expected_it);
        }
+       ao2_iterator_destroy(&received_it);
+       ao2_iterator_destroy(&expected_it);
 
        if (rx_event) {
                ast_test_status_update(test, "Received event:\n");
                dump_event(test, rx_event);
-               ao2_cleanup(rx_event);
                return -1;
        }
        if (ex_event) {
                ast_test_status_update(test, "Expected event:\n");
                dump_event(test, ex_event);
-               ao2_cleanup(ex_event);
                return -1;
        }
        return 0;
 }
 
-static struct ast_event *create_sync_event(void)
-{
-       struct ast_event *event_dup;
-       RAII_VAR(struct ast_event *, event, ao2_callback(cel_expected_events, 0, NULL, NULL), ao2_cleanup);
-       uint16_t event_len;
-
-       if (!event) {
-               return NULL;
-       }
-
-       event_len = ast_event_get_size(event);
-
-       event_dup = ast_calloc(1, event_len);
-       if (!event_dup) {
-               return NULL;
-       }
-
-       memcpy(event_dup, event, event_len);
-       ast_event_append_ie_str(&event_dup, AST_EVENT_IE_SERVICE, "SYNC");
-
-       return event_dup;
-}
-
 /*!
- * \internal \brief Callback function called after each test executes.
+ * \internal
+ * \brief Callback function called after each test executes.
+ *
+ * \details
  * In addition to cleanup, this function also performs verification
  * that the events received during a test match the events that were
  * expected to have been generated during the test.
  */
 static int cel_verify_and_cleanup_cb(struct ast_test_info *info, struct ast_test *test)
 {
-       struct ast_event *sync;
        RAII_VAR(struct ao2_container *, local_expected, cel_expected_events, ao2_cleanup);
        RAII_VAR(struct ao2_container *, local_received, cel_received_events, ao2_cleanup);
-       ast_assert(event_sub != NULL);
        ast_assert(cel_received_events != NULL);
        ast_assert(cel_expected_events != NULL);
 
        do_sleep();
 
-       /* sync with the event system */
-       sync = create_sync_event();
-       ast_test_validate(test, sync != NULL);
-       if (ast_event_queue(sync)) {
-               ast_event_destroy(sync);
-               ast_test_validate(test, NULL);
-       } else {
-               struct timeval start = ast_tvnow();
-               struct timespec end = {
-                       .tv_sec = start.tv_sec + 15,
-                       .tv_nsec = start.tv_usec * 1000
-               };
-
-               SCOPED_MUTEX(lock, &sync_lock);
-               ast_cond_timedwait(&sync_out, &sync_lock, &end);
-       }
-
        /* stop the CEL event callback and clean up storage structures*/
-       ast_event_unsubscribe(event_sub);
-       event_sub = NULL;
+       ast_cel_backend_unregister(TEST_BACKEND_NAME);
 
        /* cleaned up by RAII_VAR's */
        cel_expected_events = NULL;
@@ -1973,9 +2111,12 @@ static int unload_module(void)
        AST_TEST_UNREGISTER(test_cel_single_bridge_continue);
        AST_TEST_UNREGISTER(test_cel_single_twoparty_bridge_a);
        AST_TEST_UNREGISTER(test_cel_single_twoparty_bridge_b);
-       /*AST_TEST_UNREGISTER(test_cel_single_multiparty_bridge);*/
+#ifdef RACEY_TESTS
+       AST_TEST_UNREGISTER(test_cel_single_multiparty_bridge);
+#endif
 
        AST_TEST_UNREGISTER(test_cel_dial_unanswered);
+       AST_TEST_UNREGISTER(test_cel_dial_unanswered_filter);
        AST_TEST_UNREGISTER(test_cel_dial_congestion);
        AST_TEST_UNREGISTER(test_cel_dial_busy);
        AST_TEST_UNREGISTER(test_cel_dial_unavailable);
@@ -1984,12 +2125,14 @@ static int unload_module(void)
        AST_TEST_UNREGISTER(test_cel_dial_answer_no_bridge);
        AST_TEST_UNREGISTER(test_cel_dial_answer_twoparty_bridge_a);
        AST_TEST_UNREGISTER(test_cel_dial_answer_twoparty_bridge_b);
-       /*AST_TEST_UNREGISTER(test_cel_dial_answer_multiparty);*/
+#ifdef RACEY_TESTS
+       AST_TEST_UNREGISTER(test_cel_dial_answer_multiparty);
+       AST_TEST_UNREGISTER(test_cel_attended_transfer_bridges_swap);
+       AST_TEST_UNREGISTER(test_cel_attended_transfer_bridges_link);
+#endif
 
        AST_TEST_UNREGISTER(test_cel_blind_transfer);
-       AST_TEST_UNREGISTER(test_cel_attended_transfer_bridges_swap);
        AST_TEST_UNREGISTER(test_cel_attended_transfer_bridges_merge);
-       AST_TEST_UNREGISTER(test_cel_attended_transfer_bridges_link);
 
        AST_TEST_UNREGISTER(test_cel_dial_pickup);
 
@@ -2025,11 +2168,8 @@ static int load_module(void)
        cel_test_config->events |= 1<<AST_CEL_CHANNEL_END;
        cel_test_config->events |= 1<<AST_CEL_ANSWER;
        cel_test_config->events |= 1<<AST_CEL_HANGUP;
-       cel_test_config->events |= 1<<AST_CEL_BRIDGE_START;
-       cel_test_config->events |= 1<<AST_CEL_BRIDGE_END;
-       cel_test_config->events |= 1<<AST_CEL_BRIDGE_TO_CONF;
-       cel_test_config->events |= 1<<AST_CEL_CONF_ENTER;
-       cel_test_config->events |= 1<<AST_CEL_CONF_EXIT;
+       cel_test_config->events |= 1<<AST_CEL_BRIDGE_ENTER;
+       cel_test_config->events |= 1<<AST_CEL_BRIDGE_EXIT;
        cel_test_config->events |= 1<<AST_CEL_BLINDTRANSFER;
        cel_test_config->events |= 1<<AST_CEL_ATTENDEDTRANSFER;
        cel_test_config->events |= 1<<AST_CEL_PICKUP;
@@ -2046,9 +2186,12 @@ static int load_module(void)
        AST_TEST_REGISTER(test_cel_single_bridge_continue);
        AST_TEST_REGISTER(test_cel_single_twoparty_bridge_a);
        AST_TEST_REGISTER(test_cel_single_twoparty_bridge_b);
-       /*AST_TEST_REGISTER(test_cel_single_multiparty_bridge);*/
+#ifdef RACEY_TESTS
+       AST_TEST_REGISTER(test_cel_single_multiparty_bridge);
+#endif
 
        AST_TEST_REGISTER(test_cel_dial_unanswered);
+       AST_TEST_REGISTER(test_cel_dial_unanswered_filter);
        AST_TEST_REGISTER(test_cel_dial_congestion);
        AST_TEST_REGISTER(test_cel_dial_busy);
        AST_TEST_REGISTER(test_cel_dial_unavailable);
@@ -2057,12 +2200,14 @@ static int load_module(void)
        AST_TEST_REGISTER(test_cel_dial_answer_no_bridge);
        AST_TEST_REGISTER(test_cel_dial_answer_twoparty_bridge_a);
        AST_TEST_REGISTER(test_cel_dial_answer_twoparty_bridge_b);
-       /*AST_TEST_REGISTER(test_cel_dial_answer_multiparty);*/
+#ifdef RACEY_TESTS
+       AST_TEST_REGISTER(test_cel_dial_answer_multiparty);
+       AST_TEST_REGISTER(test_cel_attended_transfer_bridges_swap);
+       AST_TEST_REGISTER(test_cel_attended_transfer_bridges_link);
+#endif
 
        AST_TEST_REGISTER(test_cel_blind_transfer);
-       AST_TEST_REGISTER(test_cel_attended_transfer_bridges_swap);
        AST_TEST_REGISTER(test_cel_attended_transfer_bridges_merge);
-       AST_TEST_REGISTER(test_cel_attended_transfer_bridges_link);
 
        AST_TEST_REGISTER(test_cel_dial_pickup);