123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505 |
- /*
- * Asterisk -- An open source telephony toolkit.
- *
- * Copyright (C) 2013, Digium, Inc.
- *
- * David M. Lee, II <dlee@digium.com>
- *
- * See http://www.asterisk.org for more information about
- * the Asterisk project. Please do not directly contact
- * any of the maintainers of this project for assistance;
- * the project provides a web site, mailing lists and IRC
- * channels for your use.
- *
- * This program is free software, distributed under the terms of
- * the GNU General Public License Version 2. See the LICENSE file
- * at the top of the source tree.
- */
- /*!
- * \file \brief Test Stasis message bus.
- *
- * \author\verbatim David M. Lee, II <dlee@digium.com> \endverbatim
- *
- * \ingroup tests
- */
- /*** MODULEINFO
- <depend>TEST_FRAMEWORK</depend>
- <support_level>core</support_level>
- ***/
- #include "asterisk.h"
- ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
- #include "asterisk/astobj2.h"
- #include "asterisk/module.h"
- #include "asterisk/stasis.h"
- #include "asterisk/stasis_message_router.h"
- #include "asterisk/test.h"
- #define test_category "/stasis/core/"
- static struct ast_event *fake_event(struct stasis_message *message)
- {
- return ast_event_new(AST_EVENT_CUSTOM,
- AST_EVENT_IE_DESCRIPTION, AST_EVENT_IE_PLTYPE_STR, "Dummy", AST_EVENT_IE_END);
- }
- static struct ast_json *fake_json(struct stasis_message *message, const struct stasis_message_sanitizer *sanitize)
- {
- const char *text = stasis_message_data(message);
- return ast_json_string_create(text);
- }
- static struct ast_manager_event_blob *fake_ami(struct stasis_message *message)
- {
- RAII_VAR(struct ast_manager_event_blob *, res, NULL, ao2_cleanup);
- const char *text = stasis_message_data(message);
- res = ast_manager_event_blob_create(EVENT_FLAG_TEST, "FakeMI",
- "Message: %s\r\n", text);
- if (res == NULL) {
- return NULL;
- }
- ao2_ref(res, +1);
- return res;
- }
- static struct stasis_message_vtable fake_vtable = {
- .to_json = fake_json,
- .to_ami = fake_ami
- };
- AST_TEST_DEFINE(message_type)
- {
- RAII_VAR(struct stasis_message_type *, uut, NULL, ao2_cleanup);
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = test_category;
- info->summary = "Test basic message_type functions";
- info->description = "Test basic message_type functions";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- ast_test_validate(test, stasis_message_type_create(NULL, NULL, NULL) == STASIS_MESSAGE_TYPE_ERROR);
- ast_test_validate(test, stasis_message_type_create("SomeMessage", NULL, &uut) == STASIS_MESSAGE_TYPE_SUCCESS);
- ast_test_validate(test, 0 == strcmp(stasis_message_type_name(uut), "SomeMessage"));
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(message)
- {
- RAII_VAR(struct stasis_message_type *, type, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, uut1, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, uut2, NULL, ao2_cleanup);
- RAII_VAR(char *, data, NULL, ao2_cleanup);
- char *expected = "SomeData";
- struct timeval expected_timestamp;
- struct timeval time_diff;
- struct ast_eid foreign_eid;
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = test_category;
- info->summary = "Test basic message functions";
- info->description = "Test basic message functions";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- memset(&foreign_eid, 0xFF, sizeof(foreign_eid));
- ast_test_validate(test, stasis_message_type_create("SomeMessage", NULL, &type) == STASIS_MESSAGE_TYPE_SUCCESS);
- ast_test_validate(test, NULL == stasis_message_create_full(NULL, NULL, NULL));
- ast_test_validate(test, NULL == stasis_message_create_full(type, NULL, NULL));
- data = ao2_alloc(strlen(expected) + 1, NULL);
- strcpy(data, expected);/* Safe */
- expected_timestamp = ast_tvnow();
- uut1 = stasis_message_create_full(type, data, &foreign_eid);
- uut2 = stasis_message_create_full(type, data, NULL);
- ast_test_validate(test, NULL != uut1);
- ast_test_validate(test, NULL != uut2);
- ast_test_validate(test, type == stasis_message_type(uut1));
- ast_test_validate(test, type == stasis_message_type(uut2));
- ast_test_validate(test, 0 == strcmp(expected, stasis_message_data(uut1)));
- ast_test_validate(test, 0 == strcmp(expected, stasis_message_data(uut2)));
- ast_test_validate(test, NULL != stasis_message_eid(uut1));
- ast_test_validate(test, NULL == stasis_message_eid(uut2));
- ast_test_validate(test, !ast_eid_cmp(&foreign_eid, stasis_message_eid(uut1)));
- ast_test_validate(test, 3 == ao2_ref(data, 0)); /* uut1 and uut2 have ref to data */
- time_diff = ast_tvsub(*stasis_message_timestamp(uut1), expected_timestamp);
- /* 10ms is certainly long enough for the two calls to complete */
- ast_test_validate(test, time_diff.tv_sec == 0);
- ast_test_validate(test, time_diff.tv_usec < 10000);
- ao2_ref(uut1, -1);
- uut1 = NULL;
- ast_test_validate(test, 2 == ao2_ref(data, 0)); /* uut1 unreffed data */
- ao2_ref(uut2, -1);
- uut2 = NULL;
- ast_test_validate(test, 1 == ao2_ref(data, 0)); /* uut2 unreffed data */
- return AST_TEST_PASS;
- }
- struct consumer {
- ast_cond_t out;
- struct stasis_message **messages_rxed;
- size_t messages_rxed_len;
- int ignore_subscriptions;
- int complete;
- };
- static void consumer_dtor(void *obj)
- {
- struct consumer *consumer = obj;
- ast_cond_destroy(&consumer->out);
- while (consumer->messages_rxed_len > 0) {
- ao2_cleanup(consumer->messages_rxed[--consumer->messages_rxed_len]);
- }
- ast_free(consumer->messages_rxed);
- consumer->messages_rxed = NULL;
- }
- static struct consumer *consumer_create(int ignore_subscriptions)
- {
- struct consumer *consumer;
- consumer = ao2_alloc(sizeof(*consumer), consumer_dtor);
- if (!consumer) {
- return NULL;
- }
- consumer->ignore_subscriptions = ignore_subscriptions;
- consumer->messages_rxed = ast_malloc(sizeof(*consumer->messages_rxed));
- if (!consumer->messages_rxed) {
- ao2_cleanup(consumer);
- return NULL;
- }
- ast_cond_init(&consumer->out, NULL);
- return consumer;
- }
- static void consumer_exec(void *data, struct stasis_subscription *sub, struct stasis_message *message)
- {
- struct consumer *consumer = data;
- RAII_VAR(struct consumer *, consumer_needs_cleanup, NULL, ao2_cleanup);
- SCOPED_AO2LOCK(lock, consumer);
- if (!consumer->ignore_subscriptions || stasis_message_type(message) != stasis_subscription_change_type()) {
- ++consumer->messages_rxed_len;
- consumer->messages_rxed = ast_realloc(consumer->messages_rxed, sizeof(*consumer->messages_rxed) * consumer->messages_rxed_len);
- ast_assert(consumer->messages_rxed != NULL);
- consumer->messages_rxed[consumer->messages_rxed_len - 1] = message;
- ao2_ref(message, +1);
- }
- if (stasis_subscription_final_message(sub, message)) {
- consumer->complete = 1;
- consumer_needs_cleanup = consumer;
- }
- ast_cond_signal(&consumer->out);
- }
- static void consumer_exec_sync(void *data, struct stasis_subscription *sub, struct stasis_message *message)
- {
- struct consumer *consumer = data;
- RAII_VAR(struct consumer *, consumer_needs_cleanup, NULL, ao2_cleanup);
- SCOPED_AO2LOCK(lock, consumer);
- if (!consumer->ignore_subscriptions || stasis_message_type(message) != stasis_subscription_change_type()) {
- ++consumer->messages_rxed_len;
- consumer->messages_rxed = ast_realloc(consumer->messages_rxed, sizeof(*consumer->messages_rxed) * consumer->messages_rxed_len);
- ast_assert(consumer->messages_rxed != NULL);
- consumer->messages_rxed[consumer->messages_rxed_len - 1] = message;
- ao2_ref(message, +1);
- }
- if (stasis_subscription_final_message(sub, message)) {
- consumer->complete = 1;
- consumer_needs_cleanup = consumer;
- }
- }
- static int consumer_wait_for(struct consumer *consumer, size_t expected_len)
- {
- struct timeval start = ast_tvnow();
- struct timespec end = {
- .tv_sec = start.tv_sec + 30,
- .tv_nsec = start.tv_usec * 1000
- };
- SCOPED_AO2LOCK(lock, consumer);
- while (consumer->messages_rxed_len < expected_len) {
- int r = ast_cond_timedwait(&consumer->out, ao2_object_get_lockaddr(consumer), &end);
- if (r == ETIMEDOUT) {
- break;
- }
- ast_assert(r == 0); /* Not expecting any othet types of errors */
- }
- return consumer->messages_rxed_len;
- }
- static int consumer_wait_for_completion(struct consumer *consumer)
- {
- struct timeval start = ast_tvnow();
- struct timespec end = {
- .tv_sec = start.tv_sec + 3,
- .tv_nsec = start.tv_usec * 1000
- };
- SCOPED_AO2LOCK(lock, consumer);
- while (!consumer->complete) {
- int r = ast_cond_timedwait(&consumer->out, ao2_object_get_lockaddr(consumer), &end);
- if (r == ETIMEDOUT) {
- break;
- }
- ast_assert(r == 0); /* Not expecting any othet types of errors */
- }
- return consumer->complete;
- }
- static int consumer_should_stay(struct consumer *consumer, size_t expected_len)
- {
- struct timeval start = ast_tvnow();
- struct timeval diff = {
- .tv_sec = 0,
- .tv_usec = 100000 /* wait for 100ms */
- };
- struct timeval end_tv = ast_tvadd(start, diff);
- struct timespec end = {
- .tv_sec = end_tv.tv_sec,
- .tv_nsec = end_tv.tv_usec * 1000
- };
- SCOPED_AO2LOCK(lock, consumer);
- while (consumer->messages_rxed_len == expected_len) {
- int r = ast_cond_timedwait(&consumer->out, ao2_object_get_lockaddr(consumer), &end);
- if (r == ETIMEDOUT) {
- break;
- }
- ast_assert(r == 0); /* Not expecting any othet types of errors */
- }
- return consumer->messages_rxed_len;
- }
- AST_TEST_DEFINE(subscription_messages)
- {
- RAII_VAR(struct stasis_topic *, topic, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_subscription *, uut, NULL, stasis_unsubscribe);
- RAII_VAR(char *, test_data, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message_type *, test_message_type, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message, NULL, ao2_cleanup);
- RAII_VAR(struct consumer *, consumer, NULL, ao2_cleanup);
- RAII_VAR(char *, expected_uniqueid, NULL, ast_free);
- int complete;
- struct stasis_subscription_change *change;
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = test_category;
- info->summary = "Test subscribe/unsubscribe messages";
- info->description = "Test subscribe/unsubscribe messages";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- topic = stasis_topic_create("TestTopic");
- ast_test_validate(test, NULL != topic);
- consumer = consumer_create(0);
- ast_test_validate(test, NULL != consumer);
- uut = stasis_subscribe(topic, consumer_exec, consumer);
- ast_test_validate(test, NULL != uut);
- ao2_ref(consumer, +1);
- expected_uniqueid = ast_strdup(stasis_subscription_uniqueid(uut));
- uut = stasis_unsubscribe(uut);
- complete = consumer_wait_for_completion(consumer);
- ast_test_validate(test, 1 == complete);
- ast_test_validate(test, 2 == consumer->messages_rxed_len);
- ast_test_validate(test, stasis_subscription_change_type() == stasis_message_type(consumer->messages_rxed[0]));
- ast_test_validate(test, stasis_subscription_change_type() == stasis_message_type(consumer->messages_rxed[1]));
- change = stasis_message_data(consumer->messages_rxed[0]);
- ast_test_validate(test, topic == change->topic);
- ast_test_validate(test, 0 == strcmp("Subscribe", change->description));
- ast_test_validate(test, 0 == strcmp(expected_uniqueid, change->uniqueid));
- change = stasis_message_data(consumer->messages_rxed[1]);
- ast_test_validate(test, topic == change->topic);
- ast_test_validate(test, 0 == strcmp("Unsubscribe", change->description));
- ast_test_validate(test, 0 == strcmp(expected_uniqueid, change->uniqueid));
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(subscription_pool_messages)
- {
- RAII_VAR(struct stasis_topic *, topic, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_subscription *, uut, NULL, stasis_unsubscribe);
- RAII_VAR(char *, test_data, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message_type *, test_message_type, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message, NULL, ao2_cleanup);
- RAII_VAR(struct consumer *, consumer, NULL, ao2_cleanup);
- RAII_VAR(char *, expected_uniqueid, NULL, ast_free);
- int complete;
- struct stasis_subscription_change *change;
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = test_category;
- info->summary = "Test subscribe/unsubscribe messages using a threadpool subscription";
- info->description = "Test subscribe/unsubscribe messages using a threadpool subscription";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- topic = stasis_topic_create("TestTopic");
- ast_test_validate(test, NULL != topic);
- consumer = consumer_create(0);
- ast_test_validate(test, NULL != consumer);
- uut = stasis_subscribe_pool(topic, consumer_exec, consumer);
- ast_test_validate(test, NULL != uut);
- ao2_ref(consumer, +1);
- expected_uniqueid = ast_strdup(stasis_subscription_uniqueid(uut));
- uut = stasis_unsubscribe(uut);
- complete = consumer_wait_for_completion(consumer);
- ast_test_validate(test, 1 == complete);
- ast_test_validate(test, 2 == consumer->messages_rxed_len);
- ast_test_validate(test, stasis_subscription_change_type() == stasis_message_type(consumer->messages_rxed[0]));
- ast_test_validate(test, stasis_subscription_change_type() == stasis_message_type(consumer->messages_rxed[1]));
- change = stasis_message_data(consumer->messages_rxed[0]);
- ast_test_validate(test, topic == change->topic);
- ast_test_validate(test, 0 == strcmp("Subscribe", change->description));
- ast_test_validate(test, 0 == strcmp(expected_uniqueid, change->uniqueid));
- change = stasis_message_data(consumer->messages_rxed[1]);
- ast_test_validate(test, topic == change->topic);
- ast_test_validate(test, 0 == strcmp("Unsubscribe", change->description));
- ast_test_validate(test, 0 == strcmp(expected_uniqueid, change->uniqueid));
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(publish)
- {
- RAII_VAR(struct stasis_topic *, topic, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_subscription *, uut, NULL, stasis_unsubscribe);
- RAII_VAR(char *, test_data, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message_type *, test_message_type, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message, NULL, ao2_cleanup);
- RAII_VAR(struct consumer *, consumer, NULL, ao2_cleanup);
- int actual_len;
- const char *actual;
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = test_category;
- info->summary = "Test publishing";
- info->description = "Test publishing";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- topic = stasis_topic_create("TestTopic");
- ast_test_validate(test, NULL != topic);
- consumer = consumer_create(1);
- ast_test_validate(test, NULL != consumer);
- uut = stasis_subscribe(topic, consumer_exec, consumer);
- ast_test_validate(test, NULL != uut);
- ao2_ref(consumer, +1);
- test_data = ao2_alloc(1, NULL);
- ast_test_validate(test, NULL != test_data);
- ast_test_validate(test, stasis_message_type_create("TestMessage", NULL, &test_message_type) == STASIS_MESSAGE_TYPE_SUCCESS);
- test_message = stasis_message_create(test_message_type, test_data);
- stasis_publish(topic, test_message);
- actual_len = consumer_wait_for(consumer, 1);
- ast_test_validate(test, 1 == actual_len);
- actual = stasis_message_data(consumer->messages_rxed[0]);
- ast_test_validate(test, test_data == actual);
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(publish_sync)
- {
- RAII_VAR(struct stasis_topic *, topic, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_subscription *, uut, NULL, stasis_unsubscribe);
- RAII_VAR(char *, test_data, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message_type *, test_message_type, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message, NULL, ao2_cleanup);
- RAII_VAR(struct consumer *, consumer, NULL, ao2_cleanup);
- int actual_len;
- const char *actual;
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = test_category;
- info->summary = "Test synchronous publishing";
- info->description = "Test synchronous publishing";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- topic = stasis_topic_create("TestTopic");
- ast_test_validate(test, NULL != topic);
- consumer = consumer_create(1);
- ast_test_validate(test, NULL != consumer);
- uut = stasis_subscribe(topic, consumer_exec_sync, consumer);
- ast_test_validate(test, NULL != uut);
- ao2_ref(consumer, +1);
- test_data = ao2_alloc(1, NULL);
- ast_test_validate(test, NULL != test_data);
- ast_test_validate(test, stasis_message_type_create("TestMessage", NULL, &test_message_type) == STASIS_MESSAGE_TYPE_SUCCESS);
- test_message = stasis_message_create(test_message_type, test_data);
- stasis_publish_sync(uut, test_message);
- actual_len = consumer->messages_rxed_len;
- ast_test_validate(test, 1 == actual_len);
- actual = stasis_message_data(consumer->messages_rxed[0]);
- ast_test_validate(test, test_data == actual);
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(publish_pool)
- {
- RAII_VAR(struct stasis_topic *, topic, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_subscription *, uut, NULL, stasis_unsubscribe);
- RAII_VAR(char *, test_data, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message_type *, test_message_type, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message, NULL, ao2_cleanup);
- RAII_VAR(struct consumer *, consumer, NULL, ao2_cleanup);
- int actual_len;
- const char *actual;
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = test_category;
- info->summary = "Test publishing with a threadpool";
- info->description = "Test publishing to a subscriber whose\n"
- "subscription dictates messages are received through a\n"
- "threadpool.";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- topic = stasis_topic_create("TestTopic");
- ast_test_validate(test, NULL != topic);
- consumer = consumer_create(1);
- ast_test_validate(test, NULL != consumer);
- uut = stasis_subscribe_pool(topic, consumer_exec, consumer);
- ast_test_validate(test, NULL != uut);
- ao2_ref(consumer, +1);
- test_data = ao2_alloc(1, NULL);
- ast_test_validate(test, NULL != test_data);
- ast_test_validate(test, stasis_message_type_create("TestMessage", NULL, &test_message_type) == STASIS_MESSAGE_TYPE_SUCCESS);
- test_message = stasis_message_create(test_message_type, test_data);
- stasis_publish(topic, test_message);
- actual_len = consumer_wait_for(consumer, 1);
- ast_test_validate(test, 1 == actual_len);
- actual = stasis_message_data(consumer->messages_rxed[0]);
- ast_test_validate(test, test_data == actual);
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(unsubscribe_stops_messages)
- {
- RAII_VAR(struct stasis_topic *, topic, NULL, ao2_cleanup);
- RAII_VAR(struct consumer *, consumer, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_subscription *, uut, NULL, stasis_unsubscribe);
- RAII_VAR(char *, test_data, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message_type *, test_message_type, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message, NULL, ao2_cleanup);
- int actual_len;
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = test_category;
- info->summary = "Test simple subscriptions";
- info->description = "Test simple subscriptions";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- topic = stasis_topic_create("TestTopic");
- ast_test_validate(test, NULL != topic);
- consumer = consumer_create(1);
- ast_test_validate(test, NULL != consumer);
- uut = stasis_subscribe(topic, consumer_exec, consumer);
- ast_test_validate(test, NULL != uut);
- ao2_ref(consumer, +1);
- uut = stasis_unsubscribe(uut);
- test_data = ao2_alloc(1, NULL);
- ast_test_validate(test, NULL != test_data);
- ast_test_validate(test, stasis_message_type_create("TestMessage", NULL, &test_message_type) == STASIS_MESSAGE_TYPE_SUCCESS);
- test_message = stasis_message_create(test_message_type, test_data);
- stasis_publish(topic, test_message);
- actual_len = consumer_should_stay(consumer, 0);
- ast_test_validate(test, 0 == actual_len);
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(forward)
- {
- RAII_VAR(struct stasis_topic *, parent_topic, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_topic *, topic, NULL, ao2_cleanup);
- RAII_VAR(struct consumer *, parent_consumer, NULL, ao2_cleanup);
- RAII_VAR(struct consumer *, consumer, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_forward *, forward_sub, NULL, stasis_forward_cancel);
- RAII_VAR(struct stasis_subscription *, parent_sub, NULL, stasis_unsubscribe);
- RAII_VAR(struct stasis_subscription *, sub, NULL, stasis_unsubscribe);
- RAII_VAR(char *, test_data, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message_type *, test_message_type, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message, NULL, ao2_cleanup);
- int actual_len;
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = test_category;
- info->summary = "Test sending events to a parent topic";
- info->description = "Test sending events to a parent topic.\n"
- "This test creates three topics (one parent, two children)\n"
- "and publishes a message to one child, and verifies it's\n"
- "only seen by that child and the parent";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- parent_topic = stasis_topic_create("ParentTestTopic");
- ast_test_validate(test, NULL != parent_topic);
- topic = stasis_topic_create("TestTopic");
- ast_test_validate(test, NULL != topic);
- forward_sub = stasis_forward_all(topic, parent_topic);
- ast_test_validate(test, NULL != forward_sub);
- parent_consumer = consumer_create(1);
- ast_test_validate(test, NULL != parent_consumer);
- consumer = consumer_create(1);
- ast_test_validate(test, NULL != consumer);
- parent_sub = stasis_subscribe(parent_topic, consumer_exec, parent_consumer);
- ast_test_validate(test, NULL != parent_sub);
- ao2_ref(parent_consumer, +1);
- sub = stasis_subscribe(topic, consumer_exec, consumer);
- ast_test_validate(test, NULL != sub);
- ao2_ref(consumer, +1);
- test_data = ao2_alloc(1, NULL);
- ast_test_validate(test, NULL != test_data);
- ast_test_validate(test, stasis_message_type_create("TestMessage", NULL, &test_message_type) == STASIS_MESSAGE_TYPE_SUCCESS);
- test_message = stasis_message_create(test_message_type, test_data);
- stasis_publish(topic, test_message);
- actual_len = consumer_wait_for(consumer, 1);
- ast_test_validate(test, 1 == actual_len);
- actual_len = consumer_wait_for(parent_consumer, 1);
- ast_test_validate(test, 1 == actual_len);
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(interleaving)
- {
- RAII_VAR(struct stasis_topic *, parent_topic, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_topic *, topic1, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_topic *, topic2, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message_type *, test_message_type, NULL, ao2_cleanup);
- RAII_VAR(char *, test_data, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message1, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message2, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message3, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_forward *, forward_sub1, NULL, stasis_forward_cancel);
- RAII_VAR(struct stasis_forward *, forward_sub2, NULL, stasis_forward_cancel);
- RAII_VAR(struct stasis_subscription *, sub, NULL, stasis_unsubscribe);
- RAII_VAR(struct consumer *, consumer, NULL, ao2_cleanup);
- int actual_len;
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = test_category;
- info->summary = "Test sending interleaved events to a parent topic";
- info->description = "Test sending events to a parent topic.\n"
- "This test creates three topics (one parent, two children)\n"
- "and publishes messages alternately between the children.\n"
- "It verifies that the messages are received in the expected\n"
- "order.";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- ast_test_validate(test, stasis_message_type_create("test", NULL, &test_message_type) == STASIS_MESSAGE_TYPE_SUCCESS);
- ast_test_validate(test, NULL != test_message_type);
- test_data = ao2_alloc(1, NULL);
- ast_test_validate(test, NULL != test_data);
- test_message1 = stasis_message_create(test_message_type, test_data);
- ast_test_validate(test, NULL != test_message1);
- test_message2 = stasis_message_create(test_message_type, test_data);
- ast_test_validate(test, NULL != test_message2);
- test_message3 = stasis_message_create(test_message_type, test_data);
- ast_test_validate(test, NULL != test_message3);
- parent_topic = stasis_topic_create("ParentTestTopic");
- ast_test_validate(test, NULL != parent_topic);
- topic1 = stasis_topic_create("Topic1");
- ast_test_validate(test, NULL != topic1);
- topic2 = stasis_topic_create("Topic2");
- ast_test_validate(test, NULL != topic2);
- forward_sub1 = stasis_forward_all(topic1, parent_topic);
- ast_test_validate(test, NULL != forward_sub1);
- forward_sub2 = stasis_forward_all(topic2, parent_topic);
- ast_test_validate(test, NULL != forward_sub2);
- consumer = consumer_create(1);
- ast_test_validate(test, NULL != consumer);
- sub = stasis_subscribe(parent_topic, consumer_exec, consumer);
- ast_test_validate(test, NULL != sub);
- ao2_ref(consumer, +1);
- stasis_publish(topic1, test_message1);
- stasis_publish(topic2, test_message2);
- stasis_publish(topic1, test_message3);
- actual_len = consumer_wait_for(consumer, 3);
- ast_test_validate(test, 3 == actual_len);
- ast_test_validate(test, test_message1 == consumer->messages_rxed[0]);
- ast_test_validate(test, test_message2 == consumer->messages_rxed[1]);
- ast_test_validate(test, test_message3 == consumer->messages_rxed[2]);
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(subscription_interleaving)
- {
- RAII_VAR(struct stasis_topic *, parent_topic, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_topic *, topic1, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_topic *, topic2, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message_type *, test_message_type, NULL, ao2_cleanup);
- RAII_VAR(char *, test_data, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message1, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message2, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message3, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_forward *, forward_sub1, NULL, stasis_forward_cancel);
- RAII_VAR(struct stasis_forward *, forward_sub2, NULL, stasis_forward_cancel);
- RAII_VAR(struct stasis_subscription *, sub1, NULL, stasis_unsubscribe);
- RAII_VAR(struct stasis_subscription *, sub2, NULL, stasis_unsubscribe);
- RAII_VAR(struct consumer *, consumer1, NULL, ao2_cleanup);
- RAII_VAR(struct consumer *, consumer2, NULL, ao2_cleanup);
- int actual_len;
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = test_category;
- info->summary = "Test sending interleaved events to a parent topic with different subscribers";
- info->description = "Test sending events to a parent topic.\n"
- "This test creates three topics (one parent, two children)\n"
- "and publishes messages alternately between the children.\n"
- "It verifies that the messages are received in the expected\n"
- "order, for different subscription types: one with a dedicated\n"
- "thread, the other on the Stasis threadpool.";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- ast_test_validate(test, stasis_message_type_create("test", NULL, &test_message_type) == STASIS_MESSAGE_TYPE_SUCCESS);
- ast_test_validate(test, NULL != test_message_type);
- test_data = ao2_alloc(1, NULL);
- ast_test_validate(test, NULL != test_data);
- test_message1 = stasis_message_create(test_message_type, test_data);
- ast_test_validate(test, NULL != test_message1);
- test_message2 = stasis_message_create(test_message_type, test_data);
- ast_test_validate(test, NULL != test_message2);
- test_message3 = stasis_message_create(test_message_type, test_data);
- ast_test_validate(test, NULL != test_message3);
- parent_topic = stasis_topic_create("ParentTestTopic");
- ast_test_validate(test, NULL != parent_topic);
- topic1 = stasis_topic_create("Topic1");
- ast_test_validate(test, NULL != topic1);
- topic2 = stasis_topic_create("Topic2");
- ast_test_validate(test, NULL != topic2);
- forward_sub1 = stasis_forward_all(topic1, parent_topic);
- ast_test_validate(test, NULL != forward_sub1);
- forward_sub2 = stasis_forward_all(topic2, parent_topic);
- ast_test_validate(test, NULL != forward_sub2);
- consumer1 = consumer_create(1);
- ast_test_validate(test, NULL != consumer1);
- consumer2 = consumer_create(1);
- ast_test_validate(test, NULL != consumer2);
- sub1 = stasis_subscribe(parent_topic, consumer_exec, consumer1);
- ast_test_validate(test, NULL != sub1);
- ao2_ref(consumer1, +1);
- sub2 = stasis_subscribe_pool(parent_topic, consumer_exec, consumer2);
- ast_test_validate(test, NULL != sub2);
- ao2_ref(consumer2, +1);
- stasis_publish(topic1, test_message1);
- stasis_publish(topic2, test_message2);
- stasis_publish(topic1, test_message3);
- actual_len = consumer_wait_for(consumer1, 3);
- ast_test_validate(test, 3 == actual_len);
- actual_len = consumer_wait_for(consumer2, 3);
- ast_test_validate(test, 3 == actual_len);
- ast_test_validate(test, test_message1 == consumer1->messages_rxed[0]);
- ast_test_validate(test, test_message2 == consumer1->messages_rxed[1]);
- ast_test_validate(test, test_message3 == consumer1->messages_rxed[2]);
- ast_test_validate(test, test_message1 == consumer2->messages_rxed[0]);
- ast_test_validate(test, test_message2 == consumer2->messages_rxed[1]);
- ast_test_validate(test, test_message3 == consumer2->messages_rxed[2]);
- return AST_TEST_PASS;
- }
- struct cache_test_data {
- char *id;
- char *value;
- };
- static void cache_test_data_dtor(void *obj)
- {
- struct cache_test_data *data = obj;
- ast_free(data->id);
- ast_free(data->value);
- }
- static struct stasis_message *cache_test_message_create_full(struct stasis_message_type *type, const char *name, const char *value, struct ast_eid *eid)
- {
- RAII_VAR(struct cache_test_data *, data, NULL, ao2_cleanup);
- data = ao2_alloc(sizeof(*data), cache_test_data_dtor);
- if (data == NULL) {
- return NULL;
- }
- ast_assert(name != NULL);
- ast_assert(value != NULL);
- data->id = ast_strdup(name);
- data->value = ast_strdup(value);
- if (!data->id || !data->value) {
- return NULL;
- }
- return stasis_message_create_full(type, data, eid);
- }
- static struct stasis_message *cache_test_message_create(struct stasis_message_type *type, const char *name, const char *value)
- {
- return cache_test_message_create_full(type, name, value, &ast_eid_default);
- }
- static const char *cache_test_data_id(struct stasis_message *message)
- {
- struct cache_test_data *cachable = stasis_message_data(message);
- if (0 != strcmp("Cacheable", stasis_message_type_name(stasis_message_type(message)))) {
- return NULL;
- }
- return cachable->id;
- }
- static struct stasis_message *cache_test_aggregate_calc_fn(struct stasis_cache_entry *entry, struct stasis_message *new_snapshot)
- {
- struct stasis_message *aggregate_snapshot;
- struct stasis_message *snapshot;
- struct stasis_message_type *type = NULL;
- struct cache_test_data *test_data = NULL;
- int idx;
- int accumulated = 0;
- char aggregate_str[30];
- /* Accumulate the aggregate value. */
- snapshot = stasis_cache_entry_get_local(entry);
- if (snapshot) {
- type = stasis_message_type(snapshot);
- test_data = stasis_message_data(snapshot);
- accumulated += atoi(test_data->value);
- }
- for (idx = 0; ; ++idx) {
- snapshot = stasis_cache_entry_get_remote(entry, idx);
- if (!snapshot) {
- break;
- }
- type = stasis_message_type(snapshot);
- test_data = stasis_message_data(snapshot);
- accumulated += atoi(test_data->value);
- }
- if (!test_data) {
- /* There are no test entries cached. Delete the aggregate. */
- return NULL;
- }
- snapshot = stasis_cache_entry_get_aggregate(entry);
- if (snapshot) {
- type = stasis_message_type(snapshot);
- test_data = stasis_message_data(snapshot);
- if (accumulated == atoi(test_data->value)) {
- /* Aggregate test entry did not change. */
- return ao2_bump(snapshot);
- }
- }
- snprintf(aggregate_str, sizeof(aggregate_str), "%d", accumulated);
- aggregate_snapshot = cache_test_message_create_full(type, test_data->id, aggregate_str, NULL);
- if (!aggregate_snapshot) {
- /* Bummer. We have to keep the old aggregate snapshot. */
- ast_log(LOG_ERROR, "Could not create aggregate snapshot.\n");
- return ao2_bump(snapshot);
- }
- return aggregate_snapshot;
- }
- static void cache_test_aggregate_publish_fn(struct stasis_topic *topic, struct stasis_message *aggregate)
- {
- stasis_publish(topic, aggregate);
- }
- static int check_cache_aggregate(struct stasis_cache *cache, struct stasis_message_type *cache_type, const char *id, const char *value)
- {
- RAII_VAR(struct stasis_message *, aggregate, NULL, ao2_cleanup);
- struct cache_test_data *test_data;
- aggregate = stasis_cache_get_by_eid(cache, cache_type, id, NULL);
- if (!aggregate) {
- /* No aggregate, return true if given no value. */
- return !value;
- }
- /* Return true if the given value matches the aggregate value. */
- test_data = stasis_message_data(aggregate);
- return value && !strcmp(value, test_data->value);
- }
- AST_TEST_DEFINE(cache_filter)
- {
- RAII_VAR(struct stasis_message_type *, non_cache_type, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_topic *, topic, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_caching_topic *, caching_topic, NULL, stasis_caching_unsubscribe);
- RAII_VAR(struct consumer *, consumer, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_subscription *, sub, NULL, stasis_unsubscribe);
- RAII_VAR(struct stasis_message *, test_message, NULL, ao2_cleanup);
- int actual_len;
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = test_category;
- info->summary = "Test caching topics only forward cache_update messages.";
- info->description = "Test caching topics only forward cache_update messages.";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- ast_test_validate(test, stasis_message_type_create("NonCacheable", NULL, &non_cache_type) == STASIS_MESSAGE_TYPE_SUCCESS);
- ast_test_validate(test, NULL != non_cache_type);
- topic = stasis_topic_create("SomeTopic");
- ast_test_validate(test, NULL != topic);
- cache = stasis_cache_create(cache_test_data_id);
- ast_test_validate(test, NULL != cache);
- caching_topic = stasis_caching_topic_create(topic, cache);
- ast_test_validate(test, NULL != caching_topic);
- consumer = consumer_create(1);
- ast_test_validate(test, NULL != consumer);
- sub = stasis_subscribe(stasis_caching_get_topic(caching_topic), consumer_exec, consumer);
- ast_test_validate(test, NULL != sub);
- ao2_ref(consumer, +1);
- test_message = cache_test_message_create(non_cache_type, "1", "1");
- ast_test_validate(test, NULL != test_message);
- stasis_publish(topic, test_message);
- actual_len = consumer_should_stay(consumer, 0);
- ast_test_validate(test, 0 == actual_len);
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(cache)
- {
- RAII_VAR(struct stasis_message_type *, cache_type, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_topic *, topic, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_caching_topic *, caching_topic, NULL, stasis_caching_unsubscribe);
- RAII_VAR(struct consumer *, consumer, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_subscription *, sub, NULL, stasis_unsubscribe);
- RAII_VAR(struct stasis_message *, test_message1_1, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message2_1, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message2_2, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message1_clear, NULL, ao2_cleanup);
- int actual_len;
- struct stasis_cache_update *actual_update;
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = test_category;
- info->summary = "Test passing messages through cache topic unscathed.";
- info->description = "Test passing messages through cache topic unscathed.";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- ast_test_validate(test, stasis_message_type_create("Cacheable", NULL, &cache_type) == STASIS_MESSAGE_TYPE_SUCCESS);
- ast_test_validate(test, NULL != cache_type);
- topic = stasis_topic_create("SomeTopic");
- ast_test_validate(test, NULL != topic);
- cache = stasis_cache_create(cache_test_data_id);
- ast_test_validate(test, NULL != cache);
- caching_topic = stasis_caching_topic_create(topic, cache);
- ast_test_validate(test, NULL != caching_topic);
- consumer = consumer_create(1);
- ast_test_validate(test, NULL != consumer);
- sub = stasis_subscribe(stasis_caching_get_topic(caching_topic), consumer_exec, consumer);
- ast_test_validate(test, NULL != sub);
- ao2_ref(consumer, +1);
- test_message1_1 = cache_test_message_create(cache_type, "1", "1");
- ast_test_validate(test, NULL != test_message1_1);
- test_message2_1 = cache_test_message_create(cache_type, "2", "1");
- ast_test_validate(test, NULL != test_message2_1);
- /* Post a couple of snapshots */
- stasis_publish(topic, test_message1_1);
- stasis_publish(topic, test_message2_1);
- actual_len = consumer_wait_for(consumer, 2);
- ast_test_validate(test, 2 == actual_len);
- /* Check for new snapshot messages */
- ast_test_validate(test, stasis_cache_update_type() == stasis_message_type(consumer->messages_rxed[0]));
- actual_update = stasis_message_data(consumer->messages_rxed[0]);
- ast_test_validate(test, NULL == actual_update->old_snapshot);
- ast_test_validate(test, test_message1_1 == actual_update->new_snapshot);
- ast_test_validate(test, test_message1_1 == stasis_cache_get(cache, cache_type, "1"));
- /* stasis_cache_get returned a ref, so unref test_message1_1 */
- ao2_ref(test_message1_1, -1);
- ast_test_validate(test, stasis_cache_update_type() == stasis_message_type(consumer->messages_rxed[1]));
- actual_update = stasis_message_data(consumer->messages_rxed[1]);
- ast_test_validate(test, NULL == actual_update->old_snapshot);
- ast_test_validate(test, test_message2_1 == actual_update->new_snapshot);
- ast_test_validate(test, test_message2_1 == stasis_cache_get(cache, cache_type, "2"));
- /* stasis_cache_get returned a ref, so unref test_message2_1 */
- ao2_ref(test_message2_1, -1);
- /* Update snapshot 2 */
- test_message2_2 = cache_test_message_create(cache_type, "2", "2");
- ast_test_validate(test, NULL != test_message2_2);
- stasis_publish(topic, test_message2_2);
- actual_len = consumer_wait_for(consumer, 3);
- ast_test_validate(test, 3 == actual_len);
- actual_update = stasis_message_data(consumer->messages_rxed[2]);
- ast_test_validate(test, test_message2_1 == actual_update->old_snapshot);
- ast_test_validate(test, test_message2_2 == actual_update->new_snapshot);
- ast_test_validate(test, test_message2_2 == stasis_cache_get(cache, cache_type, "2"));
- /* stasis_cache_get returned a ref, so unref test_message2_2 */
- ao2_ref(test_message2_2, -1);
- /* Clear snapshot 1 */
- test_message1_clear = stasis_cache_clear_create(test_message1_1);
- ast_test_validate(test, NULL != test_message1_clear);
- stasis_publish(topic, test_message1_clear);
- actual_len = consumer_wait_for(consumer, 4);
- ast_test_validate(test, 4 == actual_len);
- actual_update = stasis_message_data(consumer->messages_rxed[3]);
- ast_test_validate(test, test_message1_1 == actual_update->old_snapshot);
- ast_test_validate(test, NULL == actual_update->new_snapshot);
- ast_test_validate(test, NULL == stasis_cache_get(cache, cache_type, "1"));
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(cache_dump)
- {
- RAII_VAR(struct stasis_message_type *, cache_type, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_topic *, topic, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_caching_topic *, caching_topic, NULL, stasis_caching_unsubscribe);
- RAII_VAR(struct consumer *, consumer, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_subscription *, sub, NULL, stasis_unsubscribe);
- RAII_VAR(struct stasis_message *, test_message1_1, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message2_1, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message2_2, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message1_clear, NULL, ao2_cleanup);
- RAII_VAR(struct ao2_container *, cache_dump, NULL, ao2_cleanup);
- int actual_len;
- struct ao2_iterator i;
- void *obj;
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = test_category;
- info->summary = "Test cache dump routines.";
- info->description = "Test cache dump routines.";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- ast_test_validate(test, stasis_message_type_create("Cacheable", NULL, &cache_type) == STASIS_MESSAGE_TYPE_SUCCESS);
- ast_test_validate(test, NULL != cache_type);
- topic = stasis_topic_create("SomeTopic");
- ast_test_validate(test, NULL != topic);
- cache = stasis_cache_create(cache_test_data_id);
- ast_test_validate(test, NULL != cache);
- caching_topic = stasis_caching_topic_create(topic, cache);
- ast_test_validate(test, NULL != caching_topic);
- consumer = consumer_create(1);
- ast_test_validate(test, NULL != consumer);
- sub = stasis_subscribe(stasis_caching_get_topic(caching_topic), consumer_exec, consumer);
- ast_test_validate(test, NULL != sub);
- ao2_ref(consumer, +1);
- test_message1_1 = cache_test_message_create(cache_type, "1", "1");
- ast_test_validate(test, NULL != test_message1_1);
- test_message2_1 = cache_test_message_create(cache_type, "2", "1");
- ast_test_validate(test, NULL != test_message2_1);
- /* Post a couple of snapshots */
- stasis_publish(topic, test_message1_1);
- stasis_publish(topic, test_message2_1);
- actual_len = consumer_wait_for(consumer, 2);
- ast_test_validate(test, 2 == actual_len);
- /* Check the cache */
- ao2_cleanup(cache_dump);
- cache_dump = stasis_cache_dump(cache, NULL);
- ast_test_validate(test, NULL != cache_dump);
- ast_test_validate(test, 2 == ao2_container_count(cache_dump));
- i = ao2_iterator_init(cache_dump, 0);
- while ((obj = ao2_iterator_next(&i))) {
- RAII_VAR(struct stasis_message *, actual_cache_entry, obj, ao2_cleanup);
- ast_test_validate(test, actual_cache_entry == test_message1_1 || actual_cache_entry == test_message2_1);
- }
- ao2_iterator_destroy(&i);
- /* Update snapshot 2 */
- test_message2_2 = cache_test_message_create(cache_type, "2", "2");
- ast_test_validate(test, NULL != test_message2_2);
- stasis_publish(topic, test_message2_2);
- actual_len = consumer_wait_for(consumer, 3);
- ast_test_validate(test, 3 == actual_len);
- /* Check the cache */
- ao2_cleanup(cache_dump);
- cache_dump = stasis_cache_dump(cache, NULL);
- ast_test_validate(test, NULL != cache_dump);
- ast_test_validate(test, 2 == ao2_container_count(cache_dump));
- i = ao2_iterator_init(cache_dump, 0);
- while ((obj = ao2_iterator_next(&i))) {
- RAII_VAR(struct stasis_message *, actual_cache_entry, obj, ao2_cleanup);
- ast_test_validate(test, actual_cache_entry == test_message1_1 || actual_cache_entry == test_message2_2);
- }
- ao2_iterator_destroy(&i);
- /* Clear snapshot 1 */
- test_message1_clear = stasis_cache_clear_create(test_message1_1);
- ast_test_validate(test, NULL != test_message1_clear);
- stasis_publish(topic, test_message1_clear);
- actual_len = consumer_wait_for(consumer, 4);
- ast_test_validate(test, 4 == actual_len);
- /* Check the cache */
- ao2_cleanup(cache_dump);
- cache_dump = stasis_cache_dump(cache, NULL);
- ast_test_validate(test, NULL != cache_dump);
- ast_test_validate(test, 1 == ao2_container_count(cache_dump));
- i = ao2_iterator_init(cache_dump, 0);
- while ((obj = ao2_iterator_next(&i))) {
- RAII_VAR(struct stasis_message *, actual_cache_entry, obj, ao2_cleanup);
- ast_test_validate(test, actual_cache_entry == test_message2_2);
- }
- ao2_iterator_destroy(&i);
- /* Dump the cache to ensure that it has no subscription change items in it since those aren't cached */
- ao2_cleanup(cache_dump);
- cache_dump = stasis_cache_dump(cache, stasis_subscription_change_type());
- ast_test_validate(test, 0 == ao2_container_count(cache_dump));
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(cache_eid_aggregate)
- {
- RAII_VAR(struct stasis_message_type *, cache_type, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_topic *, topic, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_caching_topic *, caching_topic, NULL, stasis_caching_unsubscribe);
- RAII_VAR(struct consumer *, cache_consumer, NULL, ao2_cleanup);
- RAII_VAR(struct consumer *, topic_consumer, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_subscription *, topic_sub, NULL, stasis_unsubscribe);
- RAII_VAR(struct stasis_subscription *, cache_sub, NULL, stasis_unsubscribe);
- RAII_VAR(struct stasis_message *, test_message1_1, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message2_1, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message2_2, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message2_3, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message2_4, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message1_clear, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message2_clear, NULL, ao2_cleanup);
- RAII_VAR(struct ao2_container *, cache_dump, NULL, ao2_cleanup);
- int actual_len;
- struct ao2_iterator i;
- void *obj;
- struct ast_eid foreign_eid1;
- struct ast_eid foreign_eid2;
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = test_category;
- info->summary = "Test cache eid and aggregate support.";
- info->description = "Test cache eid and aggregate support.";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- memset(&foreign_eid1, 0xAA, sizeof(foreign_eid1));
- memset(&foreign_eid2, 0xBB, sizeof(foreign_eid2));
- ast_test_validate(test, stasis_message_type_create("Cacheable", NULL, &cache_type) == STASIS_MESSAGE_TYPE_SUCCESS);
- ast_test_validate(test, NULL != cache_type);
- topic = stasis_topic_create("SomeTopic");
- ast_test_validate(test, NULL != topic);
- /* To consume events published to the topic. */
- topic_consumer = consumer_create(1);
- ast_test_validate(test, NULL != topic_consumer);
- topic_sub = stasis_subscribe(topic, consumer_exec, topic_consumer);
- ast_test_validate(test, NULL != topic_sub);
- ao2_ref(topic_consumer, +1);
- cache = stasis_cache_create_full(cache_test_data_id,
- cache_test_aggregate_calc_fn, cache_test_aggregate_publish_fn);
- ast_test_validate(test, NULL != cache);
- caching_topic = stasis_caching_topic_create(topic, cache);
- ast_test_validate(test, NULL != caching_topic);
- /* To consume update events published to the caching_topic. */
- cache_consumer = consumer_create(1);
- ast_test_validate(test, NULL != cache_consumer);
- cache_sub = stasis_subscribe(stasis_caching_get_topic(caching_topic), consumer_exec, cache_consumer);
- ast_test_validate(test, NULL != cache_sub);
- ao2_ref(cache_consumer, +1);
- /* Create test messages. */
- test_message1_1 = cache_test_message_create_full(cache_type, "1", "1", &ast_eid_default);
- ast_test_validate(test, NULL != test_message1_1);
- test_message2_1 = cache_test_message_create_full(cache_type, "2", "1", &ast_eid_default);
- ast_test_validate(test, NULL != test_message2_1);
- test_message2_2 = cache_test_message_create_full(cache_type, "2", "2", &foreign_eid1);
- ast_test_validate(test, NULL != test_message2_2);
- test_message2_3 = cache_test_message_create_full(cache_type, "2", "3", &foreign_eid2);
- ast_test_validate(test, NULL != test_message2_3);
- test_message2_4 = cache_test_message_create_full(cache_type, "2", "4", &foreign_eid2);
- ast_test_validate(test, NULL != test_message2_4);
- /* Post some snapshots */
- stasis_publish(topic, test_message1_1);
- ast_test_validate(test, check_cache_aggregate(cache, cache_type, "1", "1"));
- stasis_publish(topic, test_message2_1);
- ast_test_validate(test, check_cache_aggregate(cache, cache_type, "2", "1"));
- stasis_publish(topic, test_message2_2);
- ast_test_validate(test, check_cache_aggregate(cache, cache_type, "2", "3"));
- actual_len = consumer_wait_for(cache_consumer, 6);
- ast_test_validate(test, 6 == actual_len);
- actual_len = consumer_wait_for(topic_consumer, 6);
- ast_test_validate(test, 6 == actual_len);
- /* Check the cache */
- ao2_cleanup(cache_dump);
- cache_dump = stasis_cache_dump_all(cache, NULL);
- ast_test_validate(test, NULL != cache_dump);
- ast_test_validate(test, 3 == ao2_container_count(cache_dump));
- i = ao2_iterator_init(cache_dump, 0);
- while ((obj = ao2_iterator_next(&i))) {
- RAII_VAR(struct stasis_message *, actual_cache_entry, obj, ao2_cleanup);
- ast_test_validate(test,
- actual_cache_entry == test_message1_1
- || actual_cache_entry == test_message2_1
- || actual_cache_entry == test_message2_2);
- }
- ao2_iterator_destroy(&i);
- /* Check the local cached items */
- ao2_cleanup(cache_dump);
- cache_dump = stasis_cache_dump_by_eid(cache, NULL, &ast_eid_default);
- ast_test_validate(test, NULL != cache_dump);
- ast_test_validate(test, 2 == ao2_container_count(cache_dump));
- i = ao2_iterator_init(cache_dump, 0);
- while ((obj = ao2_iterator_next(&i))) {
- RAII_VAR(struct stasis_message *, actual_cache_entry, obj, ao2_cleanup);
- ast_test_validate(test,
- actual_cache_entry == test_message1_1
- || actual_cache_entry == test_message2_1);
- }
- ao2_iterator_destroy(&i);
- /* Post snapshot 2 from another eid. */
- stasis_publish(topic, test_message2_3);
- ast_test_validate(test, check_cache_aggregate(cache, cache_type, "2", "6"));
- actual_len = consumer_wait_for(cache_consumer, 8);
- ast_test_validate(test, 8 == actual_len);
- actual_len = consumer_wait_for(topic_consumer, 8);
- ast_test_validate(test, 8 == actual_len);
- /* Check the cache */
- ao2_cleanup(cache_dump);
- cache_dump = stasis_cache_dump_all(cache, NULL);
- ast_test_validate(test, NULL != cache_dump);
- ast_test_validate(test, 4 == ao2_container_count(cache_dump));
- i = ao2_iterator_init(cache_dump, 0);
- while ((obj = ao2_iterator_next(&i))) {
- RAII_VAR(struct stasis_message *, actual_cache_entry, obj, ao2_cleanup);
- ast_test_validate(test,
- actual_cache_entry == test_message1_1
- || actual_cache_entry == test_message2_1
- || actual_cache_entry == test_message2_2
- || actual_cache_entry == test_message2_3);
- }
- ao2_iterator_destroy(&i);
- /* Check the remote cached items */
- ao2_cleanup(cache_dump);
- cache_dump = stasis_cache_dump_by_eid(cache, NULL, &foreign_eid1);
- ast_test_validate(test, NULL != cache_dump);
- ast_test_validate(test, 1 == ao2_container_count(cache_dump));
- i = ao2_iterator_init(cache_dump, 0);
- while ((obj = ao2_iterator_next(&i))) {
- RAII_VAR(struct stasis_message *, actual_cache_entry, obj, ao2_cleanup);
- ast_test_validate(test, actual_cache_entry == test_message2_2);
- }
- ao2_iterator_destroy(&i);
- /* Post snapshot 2 from a repeated eid. */
- stasis_publish(topic, test_message2_4);
- ast_test_validate(test, check_cache_aggregate(cache, cache_type, "2", "7"));
- actual_len = consumer_wait_for(cache_consumer, 10);
- ast_test_validate(test, 10 == actual_len);
- actual_len = consumer_wait_for(topic_consumer, 10);
- ast_test_validate(test, 10 == actual_len);
- /* Check the cache */
- ao2_cleanup(cache_dump);
- cache_dump = stasis_cache_dump_all(cache, NULL);
- ast_test_validate(test, NULL != cache_dump);
- ast_test_validate(test, 4 == ao2_container_count(cache_dump));
- i = ao2_iterator_init(cache_dump, 0);
- while ((obj = ao2_iterator_next(&i))) {
- RAII_VAR(struct stasis_message *, actual_cache_entry, obj, ao2_cleanup);
- ast_test_validate(test,
- actual_cache_entry == test_message1_1
- || actual_cache_entry == test_message2_1
- || actual_cache_entry == test_message2_2
- || actual_cache_entry == test_message2_4);
- }
- ao2_iterator_destroy(&i);
- /* Check all snapshot 2 cache entries. */
- ao2_cleanup(cache_dump);
- cache_dump = stasis_cache_get_all(cache, cache_type, "2");
- ast_test_validate(test, NULL != cache_dump);
- ast_test_validate(test, 3 == ao2_container_count(cache_dump));
- i = ao2_iterator_init(cache_dump, 0);
- while ((obj = ao2_iterator_next(&i))) {
- RAII_VAR(struct stasis_message *, actual_cache_entry, obj, ao2_cleanup);
- ast_test_validate(test,
- actual_cache_entry == test_message2_1
- || actual_cache_entry == test_message2_2
- || actual_cache_entry == test_message2_4);
- }
- ao2_iterator_destroy(&i);
- /* Clear snapshot 1 */
- test_message1_clear = stasis_cache_clear_create(test_message1_1);
- ast_test_validate(test, NULL != test_message1_clear);
- stasis_publish(topic, test_message1_clear);
- ast_test_validate(test, check_cache_aggregate(cache, cache_type, "1", NULL));
- actual_len = consumer_wait_for(cache_consumer, 12);
- ast_test_validate(test, 12 == actual_len);
- actual_len = consumer_wait_for(topic_consumer, 11);
- ast_test_validate(test, 11 == actual_len);
- /* Check the cache */
- ao2_cleanup(cache_dump);
- cache_dump = stasis_cache_dump_all(cache, NULL);
- ast_test_validate(test, NULL != cache_dump);
- ast_test_validate(test, 3 == ao2_container_count(cache_dump));
- i = ao2_iterator_init(cache_dump, 0);
- while ((obj = ao2_iterator_next(&i))) {
- RAII_VAR(struct stasis_message *, actual_cache_entry, obj, ao2_cleanup);
- ast_test_validate(test,
- actual_cache_entry == test_message2_1
- || actual_cache_entry == test_message2_2
- || actual_cache_entry == test_message2_4);
- }
- ao2_iterator_destroy(&i);
- /* Clear snapshot 2 from a remote eid */
- test_message2_clear = stasis_cache_clear_create(test_message2_2);
- ast_test_validate(test, NULL != test_message2_clear);
- stasis_publish(topic, test_message2_clear);
- ast_test_validate(test, check_cache_aggregate(cache, cache_type, "2", "5"));
- actual_len = consumer_wait_for(cache_consumer, 14);
- ast_test_validate(test, 14 == actual_len);
- actual_len = consumer_wait_for(topic_consumer, 13);
- ast_test_validate(test, 13 == actual_len);
- /* Check the cache */
- ao2_cleanup(cache_dump);
- cache_dump = stasis_cache_dump_all(cache, NULL);
- ast_test_validate(test, NULL != cache_dump);
- ast_test_validate(test, 2 == ao2_container_count(cache_dump));
- i = ao2_iterator_init(cache_dump, 0);
- while ((obj = ao2_iterator_next(&i))) {
- RAII_VAR(struct stasis_message *, actual_cache_entry, obj, ao2_cleanup);
- ast_test_validate(test,
- actual_cache_entry == test_message2_1
- || actual_cache_entry == test_message2_4);
- }
- ao2_iterator_destroy(&i);
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(router)
- {
- RAII_VAR(struct stasis_topic *, topic, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message_router *, uut, NULL, stasis_message_router_unsubscribe_and_join);
- RAII_VAR(char *, test_data, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message_type *, test_message_type1, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message_type *, test_message_type2, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message_type *, test_message_type3, NULL, ao2_cleanup);
- RAII_VAR(struct consumer *, consumer1, NULL, ao2_cleanup);
- RAII_VAR(struct consumer *, consumer2, NULL, ao2_cleanup);
- RAII_VAR(struct consumer *, consumer3, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message1, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message2, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message3, NULL, ao2_cleanup);
- int actual_len, ret;
- struct stasis_message *actual;
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = test_category;
- info->summary = "Test simple message routing";
- info->description = "Test simple message routing";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- topic = stasis_topic_create("TestTopic");
- ast_test_validate(test, NULL != topic);
- consumer1 = consumer_create(1);
- ast_test_validate(test, NULL != consumer1);
- consumer2 = consumer_create(1);
- ast_test_validate(test, NULL != consumer2);
- consumer3 = consumer_create(1);
- ast_test_validate(test, NULL != consumer3);
- ast_test_validate(test, stasis_message_type_create("TestMessage1", NULL, &test_message_type1) == STASIS_MESSAGE_TYPE_SUCCESS);
- ast_test_validate(test, NULL != test_message_type1);
- ast_test_validate(test, stasis_message_type_create("TestMessage2", NULL, &test_message_type2) == STASIS_MESSAGE_TYPE_SUCCESS);
- ast_test_validate(test, NULL != test_message_type2);
- ast_test_validate(test, stasis_message_type_create("TestMessage3", NULL, &test_message_type3) == STASIS_MESSAGE_TYPE_SUCCESS);
- ast_test_validate(test, NULL != test_message_type3);
- uut = stasis_message_router_create(topic);
- ast_test_validate(test, NULL != uut);
- ret = stasis_message_router_add(
- uut, test_message_type1, consumer_exec, consumer1);
- ast_test_validate(test, 0 == ret);
- ao2_ref(consumer1, +1);
- ret = stasis_message_router_add(
- uut, test_message_type2, consumer_exec, consumer2);
- ast_test_validate(test, 0 == ret);
- ao2_ref(consumer2, +1);
- ret = stasis_message_router_set_default(uut, consumer_exec, consumer3);
- ast_test_validate(test, 0 == ret);
- ao2_ref(consumer3, +1);
- test_data = ao2_alloc(1, NULL);
- ast_test_validate(test, NULL != test_data);
- test_message1 = stasis_message_create(test_message_type1, test_data);
- ast_test_validate(test, NULL != test_message1);
- test_message2 = stasis_message_create(test_message_type2, test_data);
- ast_test_validate(test, NULL != test_message2);
- test_message3 = stasis_message_create(test_message_type3, test_data);
- ast_test_validate(test, NULL != test_message3);
- stasis_publish(topic, test_message1);
- stasis_publish(topic, test_message2);
- stasis_publish(topic, test_message3);
- actual_len = consumer_wait_for(consumer1, 1);
- ast_test_validate(test, 1 == actual_len);
- actual_len = consumer_wait_for(consumer2, 1);
- ast_test_validate(test, 1 == actual_len);
- actual_len = consumer_wait_for(consumer3, 1);
- ast_test_validate(test, 1 == actual_len);
- actual = consumer1->messages_rxed[0];
- ast_test_validate(test, test_message1 == actual);
- actual = consumer2->messages_rxed[0];
- ast_test_validate(test, test_message2 == actual);
- actual = consumer3->messages_rxed[0];
- ast_test_validate(test, test_message3 == actual);
- /* consumer1 and consumer2 do not get the final message. */
- ao2_cleanup(consumer1);
- ao2_cleanup(consumer2);
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(router_pool)
- {
- RAII_VAR(struct stasis_topic *, topic, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message_router *, uut, NULL, stasis_message_router_unsubscribe_and_join);
- RAII_VAR(char *, test_data, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message_type *, test_message_type1, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message_type *, test_message_type2, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message_type *, test_message_type3, NULL, ao2_cleanup);
- RAII_VAR(struct consumer *, consumer1, NULL, ao2_cleanup);
- RAII_VAR(struct consumer *, consumer2, NULL, ao2_cleanup);
- RAII_VAR(struct consumer *, consumer3, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message1, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message2, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message3, NULL, ao2_cleanup);
- int actual_len, ret;
- struct stasis_message *actual;
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = test_category;
- info->summary = "Test message routing via threadpool";
- info->description = "Test simple message routing when\n"
- "the subscriptions dictate usage of the Stasis\n"
- "threadpool.";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- topic = stasis_topic_create("TestTopic");
- ast_test_validate(test, NULL != topic);
- consumer1 = consumer_create(1);
- ast_test_validate(test, NULL != consumer1);
- consumer2 = consumer_create(1);
- ast_test_validate(test, NULL != consumer2);
- consumer3 = consumer_create(1);
- ast_test_validate(test, NULL != consumer3);
- ast_test_validate(test, stasis_message_type_create("TestMessage1", NULL, &test_message_type1) == STASIS_MESSAGE_TYPE_SUCCESS);
- ast_test_validate(test, NULL != test_message_type1);
- ast_test_validate(test, stasis_message_type_create("TestMessage2", NULL, &test_message_type2) == STASIS_MESSAGE_TYPE_SUCCESS);
- ast_test_validate(test, NULL != test_message_type2);
- ast_test_validate(test, stasis_message_type_create("TestMessage3", NULL, &test_message_type3) == STASIS_MESSAGE_TYPE_SUCCESS);
- ast_test_validate(test, NULL != test_message_type3);
- uut = stasis_message_router_create_pool(topic);
- ast_test_validate(test, NULL != uut);
- ret = stasis_message_router_add(
- uut, test_message_type1, consumer_exec, consumer1);
- ast_test_validate(test, 0 == ret);
- ao2_ref(consumer1, +1);
- ret = stasis_message_router_add(
- uut, test_message_type2, consumer_exec, consumer2);
- ast_test_validate(test, 0 == ret);
- ao2_ref(consumer2, +1);
- ret = stasis_message_router_set_default(uut, consumer_exec, consumer3);
- ast_test_validate(test, 0 == ret);
- ao2_ref(consumer3, +1);
- test_data = ao2_alloc(1, NULL);
- ast_test_validate(test, NULL != test_data);
- test_message1 = stasis_message_create(test_message_type1, test_data);
- ast_test_validate(test, NULL != test_message1);
- test_message2 = stasis_message_create(test_message_type2, test_data);
- ast_test_validate(test, NULL != test_message2);
- test_message3 = stasis_message_create(test_message_type3, test_data);
- ast_test_validate(test, NULL != test_message3);
- stasis_publish(topic, test_message1);
- stasis_publish(topic, test_message2);
- stasis_publish(topic, test_message3);
- actual_len = consumer_wait_for(consumer1, 1);
- ast_test_validate(test, 1 == actual_len);
- actual_len = consumer_wait_for(consumer2, 1);
- ast_test_validate(test, 1 == actual_len);
- actual_len = consumer_wait_for(consumer3, 1);
- ast_test_validate(test, 1 == actual_len);
- actual = consumer1->messages_rxed[0];
- ast_test_validate(test, test_message1 == actual);
- actual = consumer2->messages_rxed[0];
- ast_test_validate(test, test_message2 == actual);
- actual = consumer3->messages_rxed[0];
- ast_test_validate(test, test_message3 == actual);
- /* consumer1 and consumer2 do not get the final message. */
- ao2_cleanup(consumer1);
- ao2_cleanup(consumer2);
- return AST_TEST_PASS;
- }
- static const char *cache_simple(struct stasis_message *message)
- {
- const char *type_name =
- stasis_message_type_name(stasis_message_type(message));
- if (!ast_begins_with(type_name, "Cache")) {
- return NULL;
- }
- return "cached";
- }
- AST_TEST_DEFINE(router_cache_updates)
- {
- RAII_VAR(struct stasis_topic *, topic, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_caching_topic *, caching_topic, NULL, stasis_caching_unsubscribe_and_join);
- RAII_VAR(struct stasis_message_type *, test_message_type1, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message_type *, test_message_type2, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message_type *, test_message_type3, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message_router *, uut, NULL, stasis_message_router_unsubscribe_and_join);
- RAII_VAR(char *, test_data, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message1, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message2, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, test_message3, NULL, ao2_cleanup);
- RAII_VAR(struct consumer *, consumer1, NULL, ao2_cleanup);
- RAII_VAR(struct consumer *, consumer2, NULL, ao2_cleanup);
- RAII_VAR(struct consumer *, consumer3, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, message1, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, message2, NULL, ao2_cleanup);
- struct stasis_cache_update *update;
- int actual_len, ret;
- struct stasis_message *actual;
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = test_category;
- info->summary = "Test special handling cache_update messages";
- info->description = "Test special handling cache_update messages";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- topic = stasis_topic_create("TestTopic");
- ast_test_validate(test, NULL != topic);
- cache = stasis_cache_create(cache_simple);
- ast_test_validate(test, NULL != cache);
- caching_topic = stasis_caching_topic_create(topic, cache);
- ast_test_validate(test, NULL != caching_topic);
- consumer1 = consumer_create(1);
- ast_test_validate(test, NULL != consumer1);
- consumer2 = consumer_create(1);
- ast_test_validate(test, NULL != consumer2);
- consumer3 = consumer_create(1);
- ast_test_validate(test, NULL != consumer3);
- ast_test_validate(test, stasis_message_type_create("Cache1", NULL, &test_message_type1) == STASIS_MESSAGE_TYPE_SUCCESS);
- ast_test_validate(test, NULL != test_message_type1);
- ast_test_validate(test, stasis_message_type_create("Cache2", NULL, &test_message_type2) == STASIS_MESSAGE_TYPE_SUCCESS);
- ast_test_validate(test, NULL != test_message_type2);
- ast_test_validate(test, stasis_message_type_create("NonCache", NULL, &test_message_type3) == STASIS_MESSAGE_TYPE_SUCCESS);
- ast_test_validate(test, NULL != test_message_type3);
- uut = stasis_message_router_create(
- stasis_caching_get_topic(caching_topic));
- ast_test_validate(test, NULL != uut);
- ret = stasis_message_router_add_cache_update(
- uut, test_message_type1, consumer_exec, consumer1);
- ast_test_validate(test, 0 == ret);
- ao2_ref(consumer1, +1);
- ret = stasis_message_router_add(
- uut, stasis_cache_update_type(), consumer_exec, consumer2);
- ast_test_validate(test, 0 == ret);
- ao2_ref(consumer2, +1);
- ret = stasis_message_router_set_default(uut, consumer_exec, consumer3);
- ast_test_validate(test, 0 == ret);
- ao2_ref(consumer3, +1);
- test_data = ao2_alloc(1, NULL);
- ast_test_validate(test, NULL != test_data);
- test_message1 = stasis_message_create(test_message_type1, test_data);
- ast_test_validate(test, NULL != test_message1);
- test_message2 = stasis_message_create(test_message_type2, test_data);
- ast_test_validate(test, NULL != test_message2);
- test_message3 = stasis_message_create(test_message_type3, test_data);
- ast_test_validate(test, NULL != test_message3);
- stasis_publish(topic, test_message1);
- stasis_publish(topic, test_message2);
- stasis_publish(topic, test_message3);
- actual_len = consumer_wait_for(consumer1, 1);
- ast_test_validate(test, 1 == actual_len);
- actual_len = consumer_wait_for(consumer2, 1);
- ast_test_validate(test, 1 == actual_len);
- /* Uncacheable message should not be passed through */
- actual_len = consumer_should_stay(consumer3, 0);
- ast_test_validate(test, 0 == actual_len);
- actual = consumer1->messages_rxed[0];
- ast_test_validate(test, stasis_cache_update_type() == stasis_message_type(actual));
- update = stasis_message_data(actual);
- ast_test_validate(test, test_message_type1 == update->type);
- ast_test_validate(test, test_message1 == update->new_snapshot);
- actual = consumer2->messages_rxed[0];
- ast_test_validate(test, stasis_cache_update_type() == stasis_message_type(actual));
- update = stasis_message_data(actual);
- ast_test_validate(test, test_message_type2 == update->type);
- ast_test_validate(test, test_message2 == update->new_snapshot);
- /* consumer1 and consumer2 do not get the final message. */
- ao2_cleanup(consumer1);
- ao2_cleanup(consumer2);
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(no_to_json)
- {
- RAII_VAR(struct stasis_message_type *, type, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, uut, NULL, ao2_cleanup);
- RAII_VAR(char *, data, NULL, ao2_cleanup);
- RAII_VAR(struct ast_json *, actual, NULL, ast_json_unref);
- char *expected = "SomeData";
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = test_category;
- info->summary = "Test message to_json function";
- info->description = "Test message to_json function";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- /* Test NULL */
- actual = stasis_message_to_json(NULL, NULL);
- ast_test_validate(test, NULL == actual);
- /* Test message with NULL to_json function */
- ast_test_validate(test, stasis_message_type_create("SomeMessage", NULL, &type) == STASIS_MESSAGE_TYPE_SUCCESS);
- data = ao2_alloc(strlen(expected) + 1, NULL);
- strcpy(data, expected);
- uut = stasis_message_create(type, data);
- ast_test_validate(test, NULL != uut);
- actual = stasis_message_to_json(uut, NULL);
- ast_test_validate(test, NULL == actual);
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(to_json)
- {
- RAII_VAR(struct stasis_message_type *, type, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, uut, NULL, ao2_cleanup);
- RAII_VAR(char *, data, NULL, ao2_cleanup);
- RAII_VAR(struct ast_json *, actual, NULL, ast_json_unref);
- const char *expected_text = "SomeData";
- RAII_VAR(struct ast_json *, expected, NULL, ast_json_unref);
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = test_category;
- info->summary = "Test message to_json function when NULL";
- info->description = "Test message to_json function when NULL";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- ast_test_validate(test, stasis_message_type_create("SomeMessage", &fake_vtable, &type) == STASIS_MESSAGE_TYPE_SUCCESS);
- data = ao2_alloc(strlen(expected_text) + 1, NULL);
- strcpy(data, expected_text);
- uut = stasis_message_create(type, data);
- ast_test_validate(test, NULL != uut);
- expected = ast_json_string_create(expected_text);
- actual = stasis_message_to_json(uut, NULL);
- ast_test_validate(test, ast_json_equal(expected, actual));
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(no_to_ami)
- {
- RAII_VAR(struct stasis_message_type *, type, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, uut, NULL, ao2_cleanup);
- RAII_VAR(char *, data, NULL, ao2_cleanup);
- RAII_VAR(struct ast_manager_event_blob *, actual, NULL, ao2_cleanup);
- char *expected = "SomeData";
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = test_category;
- info->summary = "Test message to_ami function when NULL";
- info->description = "Test message to_ami function when NULL";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- /* Test NULL */
- actual = stasis_message_to_ami(NULL);
- ast_test_validate(test, NULL == actual);
- /* Test message with NULL to_ami function */
- ast_test_validate(test, stasis_message_type_create("SomeMessage", NULL, &type) == STASIS_MESSAGE_TYPE_SUCCESS);
- data = ao2_alloc(strlen(expected) + 1, NULL);
- strcpy(data, expected);
- uut = stasis_message_create(type, data);
- ast_test_validate(test, NULL != uut);
- actual = stasis_message_to_ami(uut);
- ast_test_validate(test, NULL == actual);
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(to_ami)
- {
- RAII_VAR(struct stasis_message_type *, type, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, uut, NULL, ao2_cleanup);
- RAII_VAR(char *, data, NULL, ao2_cleanup);
- RAII_VAR(struct ast_manager_event_blob *, actual, NULL, ao2_cleanup);
- const char *expected_text = "SomeData";
- const char *expected = "Message: SomeData\r\n";
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = test_category;
- info->summary = "Test message to_ami function";
- info->description = "Test message to_ami function";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- ast_test_validate(test, stasis_message_type_create("SomeMessage", &fake_vtable, &type) == STASIS_MESSAGE_TYPE_SUCCESS);
- data = ao2_alloc(strlen(expected_text) + 1, NULL);
- strcpy(data, expected_text);
- uut = stasis_message_create(type, data);
- ast_test_validate(test, NULL != uut);
- actual = stasis_message_to_ami(uut);
- ast_test_validate(test, strcmp(expected, actual->extra_fields) == 0);
- return AST_TEST_PASS;
- }
- static void noop(void *data, struct stasis_subscription *sub,
- struct stasis_message *message)
- {
- /* no-op */
- }
- AST_TEST_DEFINE(dtor_order)
- {
- RAII_VAR(struct stasis_topic *, topic, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_subscription *, sub, NULL, stasis_unsubscribe);
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = test_category;
- info->summary = "Test that destruction order doesn't bomb stuff";
- info->description = "Test that destruction order doesn't bomb stuff";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- topic = stasis_topic_create("test-topic");
- ast_test_validate(test, NULL != topic);
- sub = stasis_subscribe(topic, noop, NULL);
- ast_test_validate(test, NULL != sub);
- /* With any luck, this won't completely blow everything up */
- ao2_cleanup(topic);
- stasis_unsubscribe(sub);
- /* These refs were cleaned up manually */
- topic = NULL;
- sub = NULL;
- return AST_TEST_PASS;
- }
- static const char *noop_get_id(struct stasis_message *message)
- {
- return NULL;
- }
- AST_TEST_DEFINE(caching_dtor_order)
- {
- RAII_VAR(struct stasis_topic *, topic, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_caching_topic *, caching_topic, NULL,
- stasis_caching_unsubscribe);
- RAII_VAR(struct stasis_subscription *, sub, NULL, stasis_unsubscribe);
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = test_category;
- info->summary = "Test that destruction order doesn't bomb stuff";
- info->description = "Test that destruction order doesn't bomb stuff";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- cache = stasis_cache_create(noop_get_id);
- ast_test_validate(test, NULL != cache);
- topic = stasis_topic_create("test-topic");
- ast_test_validate(test, NULL != topic);
- caching_topic = stasis_caching_topic_create(topic, cache);
- ast_test_validate(test, NULL != caching_topic);
- sub = stasis_subscribe(stasis_caching_get_topic(caching_topic), noop,
- NULL);
- ast_test_validate(test, NULL != sub);
- /* With any luck, this won't completely blow everything up */
- ao2_cleanup(cache);
- ao2_cleanup(topic);
- stasis_caching_unsubscribe(caching_topic);
- stasis_unsubscribe(sub);
- /* These refs were cleaned up manually */
- cache = NULL;
- topic = NULL;
- caching_topic = NULL;
- sub = NULL;
- return AST_TEST_PASS;
- }
- struct test_message_types {
- struct stasis_message_type *none;
- struct stasis_message_type *ami;
- struct stasis_message_type *json;
- struct stasis_message_type *event;
- struct stasis_message_type *amievent;
- struct stasis_message_type *type1;
- struct stasis_message_type *type2;
- struct stasis_message_type *type3;
- struct stasis_message_type *change;
- };
- static void destroy_message_types(void *obj)
- {
- struct test_message_types *types = obj;
- ao2_cleanup(types->none);
- ao2_cleanup(types->ami);
- ao2_cleanup(types->json);
- ao2_cleanup(types->event);
- ao2_cleanup(types->amievent);
- ao2_cleanup(types->type1);
- ao2_cleanup(types->type2);
- ao2_cleanup(types->type3);
- /* N.B. Don't cleanup types->change! */
- }
- static struct test_message_types *create_message_types(struct ast_test *test)
- {
- struct stasis_message_vtable vtable = { 0 };
- struct test_message_types *types;
- enum ast_test_result_state __attribute__ ((unused)) rc;
- types = ao2_alloc(sizeof(*types), destroy_message_types);
- if (!types) {
- return NULL;
- }
- ast_test_validate_cleanup(test,
- stasis_message_type_create("TestMessageNONE", &vtable, &types->none) == STASIS_MESSAGE_TYPE_SUCCESS,
- rc, cleanup);
- vtable.to_ami = fake_ami;
- ast_test_validate_cleanup(test,
- stasis_message_type_create("TestMessageAMI", &vtable, &types->ami) == STASIS_MESSAGE_TYPE_SUCCESS,
- rc, cleanup);
- vtable.to_ami = NULL;
- vtable.to_json = fake_json;
- ast_test_validate_cleanup(test,
- stasis_message_type_create("TestMessageJSON", &vtable, &types->json) == STASIS_MESSAGE_TYPE_SUCCESS,
- rc, cleanup);
- vtable.to_ami = NULL;
- vtable.to_json = NULL;
- vtable.to_event = fake_event;
- ast_test_validate_cleanup(test,
- stasis_message_type_create("TestMessageEVENT", &vtable, &types->event) == STASIS_MESSAGE_TYPE_SUCCESS,
- rc, cleanup);
- vtable.to_ami = fake_ami;
- ast_test_validate_cleanup(test,
- stasis_message_type_create("TestMessageAMIEVENT", &vtable, &types->amievent) == STASIS_MESSAGE_TYPE_SUCCESS,
- rc, cleanup);
- ast_test_validate_cleanup(test,
- stasis_message_type_create("TestMessageType1", NULL, &types->type1) == STASIS_MESSAGE_TYPE_SUCCESS,
- rc, cleanup);
- ast_test_validate_cleanup(test,
- stasis_message_type_create("TestMessageType2", NULL, &types->type2) == STASIS_MESSAGE_TYPE_SUCCESS,
- rc, cleanup);
- ast_test_validate_cleanup(test,
- stasis_message_type_create("TestMessageType3", NULL, &types->type3) == STASIS_MESSAGE_TYPE_SUCCESS,
- rc, cleanup);
- types->change = stasis_subscription_change_type();
- return types;
- cleanup:
- ao2_cleanup(types);
- return NULL;
- }
- struct cts {
- struct consumer *consumer;
- struct stasis_topic *topic;
- struct stasis_subscription *sub;
- };
- static void destroy_cts(void *obj)
- {
- struct cts *c = obj;
- stasis_unsubscribe(c->sub);
- ao2_cleanup(c->topic);
- ao2_cleanup(c->consumer);
- }
- static struct cts *create_cts(struct ast_test *test)
- {
- struct cts *cts = ao2_alloc(sizeof(*cts), destroy_cts);
- enum ast_test_result_state __attribute__ ((unused)) rc;
- ast_test_validate_cleanup(test, cts, rc, cleanup);
- cts->topic = stasis_topic_create("TestTopic");
- ast_test_validate_cleanup(test, NULL != cts->topic, rc, cleanup);
- cts->consumer = consumer_create(0);
- ast_test_validate_cleanup(test, NULL != cts->consumer, rc, cleanup);
- ao2_ref(cts->consumer, +1);
- cts->sub = stasis_subscribe(cts->topic, consumer_exec, cts->consumer);
- ast_test_validate_cleanup(test, NULL != cts->sub, rc, cleanup);
- return cts;
- cleanup:
- ao2_cleanup(cts);
- return NULL;
- }
- static int is_msg(struct stasis_message *msg, struct stasis_message_type *mtype, const char *data)
- {
- struct stasis_subscription_change *msg_data = stasis_message_data(msg);
- if (stasis_message_type(msg) != mtype) {
- return 0;
- }
- if (data) {
- return (strcmp(data, msg_data->description) == 0);
- }
- return 1;
- }
- static void dump_consumer(struct ast_test *test, struct cts *cts)
- {
- int i;
- struct stasis_subscription_change *data;
- ast_test_status_update(test, "Messages received: %ld Final? %s\n", cts->consumer->messages_rxed_len,
- cts->consumer->complete ? "yes" : "no");
- for (i = 0; i < cts->consumer->messages_rxed_len; i++) {
- data = stasis_message_data(cts->consumer->messages_rxed[i]);
- ast_test_status_update(test, "Message type received: %s %s\n",
- stasis_message_type_name(stasis_message_type(cts->consumer->messages_rxed[i])),
- data && data->description ? data->description : "no data");
- }
- }
- static int send_msg(struct ast_test *test, struct cts *cts, struct stasis_message_type *msg_type,
- const char *data)
- {
- struct stasis_message *msg;
- struct stasis_subscription_change *test_data =
- ao2_alloc(sizeof(*test_data) + (data ? strlen(data) : strlen("no data")) + 1, NULL);
- if (!test_data) {
- return 0;
- }
- strcpy(test_data->description, S_OR(data, "no data")); /* Safe */
- msg = stasis_message_create(msg_type, test_data);
- ao2_ref(test_data, -1);
- if (!msg) {
- ast_test_status_update(test, "Unable to create %s message\n",
- stasis_message_type_name(msg_type));
- return 0;
- }
- stasis_publish(cts->topic, msg);
- ao2_ref(msg, -1);
- return 1;
- }
- AST_TEST_DEFINE(type_filters)
- {
- RAII_VAR(struct cts *, cts, NULL, ao2_cleanup);
- RAII_VAR(struct test_message_types *, types, NULL, ao2_cleanup);
- int ix = 0;
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = test_category "filtering/";
- info->summary = "Test message filtering by type";
- info->description = "Test message filtering by type";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- types = create_message_types(test);
- ast_test_validate(test, NULL != types);
- cts = create_cts(test);
- ast_test_validate(test, NULL != cts);
- ast_test_validate(test, stasis_subscription_accept_message_type(cts->sub, types->type1) == 0);
- ast_test_validate(test, stasis_subscription_accept_message_type(cts->sub, types->type2) == 0);
- ast_test_validate(test, stasis_subscription_accept_message_type(cts->sub, types->change) == 0);
- ast_test_validate(test, stasis_subscription_set_filter(cts->sub, STASIS_SUBSCRIPTION_FILTER_SELECTIVE) == 0);
- /* We should get these */
- ast_test_validate(test, send_msg(test, cts, types->type1, "Pass"));
- ast_test_validate(test, send_msg(test, cts, types->type2, "Pass"));
- /* ... but not this one */
- ast_test_validate(test, send_msg(test, cts, types->type3, "FAIL"));
- /* Wait for change(subscribe) and "Pass" messages */
- consumer_wait_for(cts->consumer, 3);
- /* Remove type 1 */
- ast_test_validate(test, stasis_subscription_decline_message_type(cts->sub, types->type1) == 0);
- /* We should now NOT get this one */
- ast_test_validate(test, send_msg(test, cts, types->type1, "FAIL"));
- /* We should get this one (again) */
- ast_test_validate(test, send_msg(test, cts, types->type2, "Pass2"));
- /* We still should NOT get this one */
- ast_test_validate(test, send_msg(test, cts, types->type3, "FAIL"));
- /* We should now have a second type2 */
- consumer_wait_for(cts->consumer, 4);
- stasis_unsubscribe(cts->sub);
- cts->sub = NULL;
- consumer_wait_for_completion(cts->consumer);
- dump_consumer(test, cts);
- ast_test_validate(test, 1 == cts->consumer->complete);
- ast_test_validate(test, 5 == cts->consumer->messages_rxed_len);
- ast_test_validate(test, is_msg(cts->consumer->messages_rxed[ix++], types->change, "Subscribe"));
- ast_test_validate(test, is_msg(cts->consumer->messages_rxed[ix++], types->type1, "Pass"));
- ast_test_validate(test, is_msg(cts->consumer->messages_rxed[ix++], types->type2, "Pass"));
- ast_test_validate(test, is_msg(cts->consumer->messages_rxed[ix++], types->type2, "Pass2"));
- ast_test_validate(test, is_msg(cts->consumer->messages_rxed[ix++], types->change, "Unsubscribe"));
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(formatter_filters)
- {
- RAII_VAR(struct cts *, cts, NULL, ao2_cleanup);
- RAII_VAR(struct test_message_types *, types, NULL, ao2_cleanup) ;
- int ix = 0;
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = test_category "filtering/";
- info->summary = "Test message filtering by formatter";
- info->description = "Test message filtering by formatter";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- types = create_message_types(test);
- ast_test_validate(test, NULL != types);
- cts = create_cts(test);
- ast_test_validate(test, NULL != cts);
- stasis_subscription_accept_formatters(cts->sub,
- STASIS_SUBSCRIPTION_FORMATTER_AMI | STASIS_SUBSCRIPTION_FORMATTER_JSON);
- /* We should get these */
- ast_test_validate(test, send_msg(test, cts, types->ami, "Pass"));
- ast_test_validate(test, send_msg(test, cts, types->json, "Pass"));
- ast_test_validate(test, send_msg(test, cts, types->amievent, "Pass"));
- /* ... but not these */
- ast_test_validate(test, send_msg(test, cts, types->none, "FAIL"));
- ast_test_validate(test, send_msg(test, cts, types->event, "FAIL"));
- ast_test_validate(test, send_msg(test, cts, types->type1, "FAIL"));
- /* Wait for change(subscribe) and the "Pass" messages */
- consumer_wait_for(cts->consumer, 4);
- /* Change the subscription to accept only event formatters */
- stasis_subscription_accept_formatters(cts->sub, STASIS_SUBSCRIPTION_FORMATTER_EVENT);
- /* We should NOT get these now */
- ast_test_validate(test, send_msg(test, cts, types->ami, "FAIL"));
- ast_test_validate(test, send_msg(test, cts, types->json, "FAIL"));
- /* ... but we should still get this one */
- ast_test_validate(test, send_msg(test, cts, types->amievent, "Pass2"));
- /* ... and this one should be new */
- ast_test_validate(test, send_msg(test, cts, types->event, "Pass"));
- /* We should now have a second amievent */
- consumer_wait_for(cts->consumer, 6);
- stasis_unsubscribe(cts->sub);
- cts->sub = NULL;
- consumer_wait_for_completion(cts->consumer);
- dump_consumer(test, cts);
- ast_test_validate(test, 1 == cts->consumer->complete);
- ast_test_validate(test, 7 == cts->consumer->messages_rxed_len);
- ast_test_validate(test, is_msg(cts->consumer->messages_rxed[ix++], types->change, "Subscribe"));
- ast_test_validate(test, is_msg(cts->consumer->messages_rxed[ix++], types->ami, "Pass"));
- ast_test_validate(test, is_msg(cts->consumer->messages_rxed[ix++], types->json, "Pass"));
- ast_test_validate(test, is_msg(cts->consumer->messages_rxed[ix++], types->amievent, "Pass"));
- ast_test_validate(test, is_msg(cts->consumer->messages_rxed[ix++], types->amievent, "Pass2"));
- ast_test_validate(test, is_msg(cts->consumer->messages_rxed[ix++], types->event, "Pass"));
- ast_test_validate(test, is_msg(cts->consumer->messages_rxed[ix++], types->change, "Unsubscribe"));
- return AST_TEST_PASS;
- }
- AST_TEST_DEFINE(combo_filters)
- {
- RAII_VAR(struct cts *, cts, NULL, ao2_cleanup);
- RAII_VAR(struct test_message_types *, types, NULL, ao2_cleanup);
- int ix = 0;
- switch (cmd) {
- case TEST_INIT:
- info->name = __func__;
- info->category = test_category "filtering/";
- info->summary = "Test message filtering by type and formatter";
- info->description = "Test message filtering by type and formatter";
- return AST_TEST_NOT_RUN;
- case TEST_EXECUTE:
- break;
- }
- types = create_message_types(test);
- ast_test_validate(test, NULL != types);
- cts = create_cts(test);
- ast_test_validate(test, NULL != cts);
- ast_test_validate(test, stasis_subscription_accept_message_type(cts->sub, types->type1) == 0);
- ast_test_validate(test, stasis_subscription_accept_message_type(cts->sub, types->type2) == 0);
- ast_test_validate(test, stasis_subscription_accept_message_type(cts->sub, types->change) == 0);
- ast_test_validate(test, stasis_subscription_set_filter(cts->sub, STASIS_SUBSCRIPTION_FILTER_SELECTIVE) == 0);
- stasis_subscription_accept_formatters(cts->sub,
- STASIS_SUBSCRIPTION_FORMATTER_AMI | STASIS_SUBSCRIPTION_FORMATTER_JSON);
- /* We should get these */
- ast_test_validate(test, send_msg(test, cts, types->type1, "Pass"));
- ast_test_validate(test, send_msg(test, cts, types->type2, "Pass"));
- ast_test_validate(test, send_msg(test, cts, types->ami, "Pass"));
- ast_test_validate(test, send_msg(test, cts, types->amievent, "Pass"));
- ast_test_validate(test, send_msg(test, cts, types->json, "Pass"));
- /* ... but not these */
- ast_test_validate(test, send_msg(test, cts, types->type3, "FAIL"));
- ast_test_validate(test, send_msg(test, cts, types->event, "FAIL"));
- /* Wait for change(subscribe) and the "Pass" messages */
- consumer_wait_for(cts->consumer, 6);
- stasis_unsubscribe(cts->sub);
- cts->sub = NULL;
- consumer_wait_for_completion(cts->consumer);
- dump_consumer(test, cts);
- ast_test_validate(test, 1 == cts->consumer->complete);
- ast_test_validate(test, 7 == cts->consumer->messages_rxed_len);
- ast_test_validate(test, is_msg(cts->consumer->messages_rxed[ix++], types->change, "Subscribe"));
- ast_test_validate(test, is_msg(cts->consumer->messages_rxed[ix++], types->type1, "Pass"));
- ast_test_validate(test, is_msg(cts->consumer->messages_rxed[ix++], types->type2, "Pass"));
- ast_test_validate(test, is_msg(cts->consumer->messages_rxed[ix++], types->ami, "Pass"));
- ast_test_validate(test, is_msg(cts->consumer->messages_rxed[ix++], types->amievent, "Pass"));
- ast_test_validate(test, is_msg(cts->consumer->messages_rxed[ix++], types->json, "Pass"));
- ast_test_validate(test, is_msg(cts->consumer->messages_rxed[ix++], types->change, "Unsubscribe"));
- return AST_TEST_PASS;
- }
- static int unload_module(void)
- {
- AST_TEST_UNREGISTER(message_type);
- AST_TEST_UNREGISTER(message);
- AST_TEST_UNREGISTER(subscription_messages);
- AST_TEST_UNREGISTER(subscription_pool_messages);
- AST_TEST_UNREGISTER(publish);
- AST_TEST_UNREGISTER(publish_sync);
- AST_TEST_UNREGISTER(publish_pool);
- AST_TEST_UNREGISTER(unsubscribe_stops_messages);
- AST_TEST_UNREGISTER(forward);
- AST_TEST_UNREGISTER(cache_filter);
- AST_TEST_UNREGISTER(cache);
- AST_TEST_UNREGISTER(cache_dump);
- AST_TEST_UNREGISTER(cache_eid_aggregate);
- AST_TEST_UNREGISTER(router);
- AST_TEST_UNREGISTER(router_pool);
- AST_TEST_UNREGISTER(router_cache_updates);
- AST_TEST_UNREGISTER(interleaving);
- AST_TEST_UNREGISTER(subscription_interleaving);
- AST_TEST_UNREGISTER(no_to_json);
- AST_TEST_UNREGISTER(to_json);
- AST_TEST_UNREGISTER(no_to_ami);
- AST_TEST_UNREGISTER(to_ami);
- AST_TEST_UNREGISTER(dtor_order);
- AST_TEST_UNREGISTER(caching_dtor_order);
- AST_TEST_UNREGISTER(type_filters);
- AST_TEST_UNREGISTER(formatter_filters);
- AST_TEST_UNREGISTER(combo_filters);
- return 0;
- }
- static int load_module(void)
- {
- AST_TEST_REGISTER(message_type);
- AST_TEST_REGISTER(message);
- AST_TEST_REGISTER(subscription_messages);
- AST_TEST_REGISTER(subscription_pool_messages);
- AST_TEST_REGISTER(publish);
- AST_TEST_REGISTER(publish_sync);
- AST_TEST_REGISTER(publish_pool);
- AST_TEST_REGISTER(unsubscribe_stops_messages);
- AST_TEST_REGISTER(forward);
- AST_TEST_REGISTER(cache_filter);
- AST_TEST_REGISTER(cache);
- AST_TEST_REGISTER(cache_dump);
- AST_TEST_REGISTER(cache_eid_aggregate);
- AST_TEST_REGISTER(router);
- AST_TEST_REGISTER(router_pool);
- AST_TEST_REGISTER(router_cache_updates);
- AST_TEST_REGISTER(interleaving);
- AST_TEST_REGISTER(subscription_interleaving);
- AST_TEST_REGISTER(no_to_json);
- AST_TEST_REGISTER(to_json);
- AST_TEST_REGISTER(no_to_ami);
- AST_TEST_REGISTER(to_ami);
- AST_TEST_REGISTER(dtor_order);
- AST_TEST_REGISTER(caching_dtor_order);
- AST_TEST_REGISTER(type_filters);
- AST_TEST_REGISTER(formatter_filters);
- AST_TEST_REGISTER(combo_filters);
- return AST_MODULE_LOAD_SUCCESS;
- }
- AST_MODULE_INFO(ASTERISK_GPL_KEY, 0, "Stasis testing",
- .support_level = AST_MODULE_SUPPORT_CORE,
- .load = load_module,
- .unload = unload_module
- );
|