RKH
test_rkhfwk_pubsub.c
Go to the documentation of this file.
1 /*
2  * --------------------------------------------------------------------------
3  *
4  * Framework RKH
5  * -------------
6  *
7  * State-machine framework for reactive embedded systems
8  *
9  * Copyright (C) 2010 Leandro Francucci.
10  * All rights reserved. Protected by international copyright laws.
11  *
12  *
13  * RKH is free software: you can redistribute it and/or modify it under the
14  * terms of the GNU General Public License as published by the Recycle Software
15  * Foundation, either version 3 of the License, or (at your option) any
16  * later version.
17  *
18  * RKH is distributed in the hope that it will be useful, but WITHOUT ANY
19  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
21  * more details.
22  *
23  * You should have received a copy of the GNU General Public License along
24  * with RKH, see copying.txt file.
25  *
26  * Contact information:
27  * RKH site: http://vortexmakes.com/que-es/
28  * RKH GitHub: https://github.com/vortexmakes/RKH
29  * RKH Sourceforge: https://sourceforge.net/projects/rkh-reactivesys/
30  * e-mail: lf@vortexmakes.com
31  * ---------------------------------------------------------------------------
32  */
33 
46 /* -------------------------- Development history -------------------------- */
47 /*
48  * 2018.08.06 LeFr v3.1.00 Initial version
49  */
50 
51 /* -------------------------------- Authors -------------------------------- */
52 /*
53  * LeFr Leandro Francucci lf@vortexmakes.com
54  */
55 
56 /* --------------------------------- Notes --------------------------------- */
57 /* ----------------------------- Include files ----------------------------- */
58 #include "unity.h"
59 #include "rkhfwk_pubsub.h"
60 #include "rkhfwk_pubsubSpy.h"
61 #include "Mock_rkhfwk_rdygrp.h"
62 #include "Mock_rkhsma.h"
63 #include "Mock_rkhassert.h"
64 #include "Mock_rkhport.h"
65 #include "Mock_rkhfwk_dynevt.h"
66 
67 /* ----------------------------- Local macros ------------------------------ */
68 /* ------------------------------- Constants ------------------------------- */
69 /* ---------------------------- Local data types --------------------------- */
70 typedef struct PubArg PubArg;
71 struct PubArg
72 {
73  RdyCbArg base;
74  RKH_EVT_T *event;
75  const void *sender;
76 };
77 
78 /* ---------------------------- Global variables --------------------------- */
79 int GlobalExpectCount;
80 int GlobalVerifyOrder;
81 char *GlobalOrderError;
82 RKH_SMA_T *rkh_sptbl[RKH_CFG_FWK_MAX_SMA]; /* Just for compiling */
83 static RKH_EVT_T event;
84 
85 /* ---------------------------- Local variables ---------------------------- */
86 RKH_SMA_CREATE(RKH_SMA_T, ao, 0, HCAL, NULL, NULL, NULL);
87 RKH_SMA_DEF_PTR(ao);
88 RKH_SMA_CREATE(RKH_SMA_T, aoSender, 1, HCAL, NULL, NULL, NULL);
89 RKH_SMA_DEF_PTR(aoSender);
90 
91 /* ----------------------- Local function prototypes ----------------------- */
92 /* ---------------------------- Local functions ---------------------------- */
93 static void
94 MockAssertCallback(const char* const file, int line, int cmock_num_calls)
95 {
96  TEST_PASS();
97 }
98 
99 /* ---------------------------- Global functions --------------------------- */
100 void
101 setUp(void)
102 {
103  Mock_rkhfwk_rdygrp_Init();
104  Mock_rkhsma_Init();
105  Mock_rkhassert_Init();
106  Mock_rkhport_Init();
107 }
108 
109 void
110 tearDown(void)
111 {
112  Mock_rkhfwk_rdygrp_Verify();
113  Mock_rkhsma_Verify();
114  Mock_rkhassert_Verify();
115  Mock_rkhport_Verify();
116  Mock_rkhfwk_rdygrp_Destroy();
117  Mock_rkhsma_Destroy();
118  Mock_rkhassert_Destroy();
119  Mock_rkhport_Destroy();
120 }
121 
128 void
129 test_AfterInitAllChannelsAreAvailable(void)
130 {
131  rui8_t nCh;
132 
133  for (nCh = 0; nCh < RKH_CFG_FWK_MAX_SUBS_CHANNELS; ++nCh)
134  {
135  rkh_rdygrp_init_Expect(0);
136  rkh_rdygrp_init_IgnoreArg_me();
137  }
138 
139  rkh_pubsub_init();
140 }
141 
142 void
143 test_SubscribeOneActiveObject(void)
144 {
145  rkh_rdygrp_init_Ignore();
146  rkh_enter_critical_Expect();
147  rkh_rdygrp_setReady_Expect(0, RKH_GET_PRIO(ao));
148  rkh_rdygrp_setReady_IgnoreArg_me();
149  rkh_exit_critical_Expect();
150 
151  rkh_pubsub_init();
152  rkh_pubsub_subscribe(0, ao);
153 }
154 
155 void
156 test_UnsubscribeOneActiveObject(void)
157 {
158  rkh_rdygrp_init_Ignore();
159  rkh_enter_critical_Expect();
160  rkh_rdygrp_setUnready_Expect(0, RKH_GET_PRIO(ao));
161  rkh_rdygrp_setUnready_IgnoreArg_me();
162  rkh_exit_critical_Expect();
163 
164  rkh_pubsub_init();
165  rkh_pubsub_unsubscribe(0, ao);
166 }
167 
168 void
169 test_UnsubscribeAllActiveObjects(void)
170 {
171  rui8_t nCh;
172 
173  rkh_rdygrp_init_Ignore();
174  rkh_pubsub_init();
175 
176  for (nCh = 0; nCh < RKH_CFG_FWK_MAX_SUBS_CHANNELS; ++nCh)
177  {
178  rkh_enter_critical_Expect();
179  rkh_rdygrp_setReady_Expect(0, RKH_GET_PRIO(ao));
180  rkh_exit_critical_Expect();
181  rkh_rdygrp_setReady_IgnoreArg_me();
182  rkh_pubsub_subscribe(nCh, ao);
183  }
184 
185  rkh_enter_critical_Expect();
186  for (nCh = 0; nCh < RKH_CFG_FWK_MAX_SUBS_CHANNELS; ++nCh)
187  {
188  rkh_rdygrp_setUnready_Expect(0, RKH_GET_PRIO(ao));
189  rkh_rdygrp_setUnready_IgnoreArg_me();
190  }
191  rkh_exit_critical_Expect();
192 
194 }
195 
196 void
197 test_PublishOneActiveObject(void)
198 {
199  int me;
200  RKH_EVT_T evt;
201  rui8_t nRdyAo;
202 
203  rkh_rdygrp_init_Ignore();
204  rkh_enter_critical_Expect();
205  rkh_rdygrp_traverse_ExpectAndReturn(0, 0, 0, 1);
206  rkh_exit_critical_Expect();
207  rkh_fwk_gc_Expect(&evt, &me);
208  rkh_rdygrp_traverse_IgnoreArg_me();
209  rkh_rdygrp_traverse_IgnoreArg_rdyCb();
210  rkh_rdygrp_traverse_IgnoreArg_rdyCbArg();
211 
212  rkh_pubsub_init();
213  nRdyAo = rkh_pubsub_publish(0, &evt, &me);
214  TEST_ASSERT_EQUAL(1, nRdyAo);
215 }
216 
217 void
218 test_InvokePublishCallbackOnPublish(void)
219 {
220  rui8_t nRdyAo;
221  PubArg publishArg;
222 
223  rkh_sptbl[RKH_GET_PRIO(ao)] = ao;
224  rkh_sptbl[RKH_GET_PRIO(aoSender)] = aoSender;
225  publishArg.base.aoRdyPrio = RKH_GET_PRIO(ao); /* subscriber */
226  publishArg.event = &event;
227  publishArg.sender = aoSender;
228  rkh_sma_post_fifo_Expect(ao, &event, aoSender);
229 
230  publish((RdyCbArg *)&publishArg);
231 }
232 
233 void
234 test_IgnoreSelfPublishing(void)
235 {
236  rui8_t nRdyAo;
237  PubArg publishArg;
238 
239  rkh_sptbl[RKH_GET_PRIO(ao)] = ao;
240  publishArg.base.aoRdyPrio = RKH_GET_PRIO(ao); /* subscriber */
241  publishArg.event = &event;
242  publishArg.sender = ao;
243 
244  publish((RdyCbArg *)&publishArg);
245 }
246 
247 void
248 test_Fails_SubscribeWithWrongArgs(void)
249 {
250  rui8_t prio = RKH_CFG_FWK_MAX_SMA;
251 
252  rkh_assert_Expect("rkhfwk_pubsub", 0);
253  rkh_assert_IgnoreArg_file();
254  rkh_assert_IgnoreArg_line();
255  rkh_assert_StubWithCallback(MockAssertCallback);
257 
258  rkh_assert_Expect("rkhfwk_pubsub", 0);
259  rkh_assert_IgnoreArg_line();
260  rkh_assert_StubWithCallback(MockAssertCallback);
261  rkh_pubsub_subscribe(0, NULL);
262 }
263 
264 void
265 test_Fails_UnsubscribeWithWrongArgs(void)
266 {
267  rui8_t prio = RKH_CFG_FWK_MAX_SMA;
268 
269  rkh_assert_Expect("rkhfwk_pubsub", 0);
270  rkh_assert_IgnoreArg_file();
271  rkh_assert_IgnoreArg_line();
272  rkh_assert_StubWithCallback(MockAssertCallback);
274 
275  rkh_assert_Expect("rkhfwk_pubsub", 0);
276  rkh_assert_IgnoreArg_line();
277  rkh_assert_StubWithCallback(MockAssertCallback);
278  rkh_pubsub_unsubscribe(0, NULL);
279 }
280 
286 /* ------------------------------ End of file ------------------------------ */
@ HCAL
Definition: rkhsm.h:1611
#define RKH_CFG_FWK_MAX_SMA
Specify the maximum number of state machine applications (SMA) to be used by the application (can be ...
Definition: rkhcfg.h:88
#define RKH_CFG_FWK_MAX_SUBS_CHANNELS
Specify the maximum number of channels (topics) to which an active object wants to subscribe (can be ...
Definition: rkhcfg.h:201
RKH_SMA_T * rkh_sptbl[RKH_CFG_FWK_MAX_SMA]
Priority arranged table of registered SMA.
Specifies the publish-subscribe interface.
rui8_t rkh_pubsub_publish(rui8_t channel, RKH_EVT_T *event, const void *const sender)
void rkh_pubsub_subscribe(rui8_t channel, const RKH_SMA_T *ao)
Adds an active object to the notification list of an event channel. Each subscription registers a cli...
void rkh_pubsub_unsubscribeAll(const RKH_SMA_T *ao)
Unsubscribes an active object from all topics.
void rkh_pubsub_unsubscribe(rui8_t channel, const RKH_SMA_T *ao)
Unsubscribes an active object from topic.
void rkh_pubsub_init(void)
Encapsulates the initialization of publish-subscribe module.
#define RKH_SMA_DEF_PTR(me_)
Declare a opaque pointer pointing to an previously created active object.
Definition: rkhsma.h:390
#define RKH_GET_PRIO(_ao)
Retrieves the priority number of an registered active object (SMA).
Definition: rkhsma.h:713
#define RKH_SMA_CREATE(type, name, prio, ppty, initialState, initialAction, initialEvt)
Declare and allocate a SMA (active object) derived from RKH_SMA_T. Also, initializes and assigns a st...
Definition: rkhsma.h:348
Represents events without parameters.
Definition: rkhevt.h:170
Describes the SMA (active object in UML).
Definition: rkhsma.h:772