Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c8b7fbf70b77e60e611305c205a6e130e1f6b438 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*******************************************************************************
 * Copyright (c) 2011 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * CONTRIBUTORS:
 * 		Thomas Schuetz (initial contribution)
 *
 *******************************************************************************/

#include "TestEtMessageService.h"
#include "etUnit.h"
#include "etMessageService.h"


/* mocking for MessageDispatcher */

static etInt16 receivedEventIDs[2] = {0,0};
static etInt16 receivedEventIDCounter = 0;

void MessageReceiver1(const etMessage* msg){
	receivedEventIDs[receivedEventIDCounter] = msg->evtID;
	receivedEventIDCounter++;
}

void MessageReceiver2(const etMessage* msg){
	receivedEventIDs[receivedEventIDCounter] = msg->evtID;
	receivedEventIDCounter++;
}

/* dummy message dispatcher */
void DummyMessageDispatcher(const etMessage* msg){
	switch(msg->address){
		case 11:
			MessageReceiver1(msg);
			break;
		case 22:
			MessageReceiver2(msg);
			break;
		default:
			break;
	}
}




void TestEtMessageService_init(void){

	etMessageService msgService;
	uint16 max = 6;
	uint16 blockSize = 32;

	uint8 msgBuffer[max*blockSize];

	etMessageService_init(&msgService, msgBuffer, max, blockSize, DummyMessageDispatcher);

	EXPECT_EQUAL_PTR("msgService.messagePool.first", msgBuffer, msgService.messagePool.first);
	EXPECT_EQUAL_PTR("msgService.messagePool in between", &msgBuffer[3*blockSize], msgService.messagePool.first->next->next->next);
	EXPECT_EQUAL_PTR("msgService.messagePool.last(1)", &msgBuffer[5*blockSize], msgService.messagePool.first->next->next->next->next->next);
	EXPECT_EQUAL_PTR("msgService.messagePool.last(2)", &msgBuffer[5*blockSize], msgService.messagePool.last);
	EXPECT_EQUAL_PTR("msgService.messagePool.last.next", 0, msgService.messagePool.last->next);

}

void TestEtMessageService_GetPushPopReturn(void){

	etMessageService msgService;
	uint16 max = 6;
	uint16 blockSize = 32;
	uint8 msgBuffer[max*blockSize];

	etMessageService_init(&msgService, msgBuffer, max, blockSize, DummyMessageDispatcher);

	// get messages from pool
	etMessage* msg1 = etMessageService_getMessageBuffer(&msgService, sizeof(etMessage));
	etMessage* msg2 = etMessageService_getMessageBuffer(&msgService, sizeof(etMessage));

	EXPECT_EQUAL_INT16("msgService.messagePool.size", 4, msgService.messagePool.size);

	// define content
	msg1->address = 11;
	msg1->evtID = 111;
	msg2->address = 22;
	msg2->evtID = 222;

	// push messages to queue
	etMessageService_pushMessage(&msgService, msg2);
	etMessageService_pushMessage(&msgService, msg1);

	EXPECT_EQUAL_INT16("msgService.messageQueue.size", 2, msgService.messageQueue.size);

	// pop messages from queue
	etMessage* rcvMsg1 = etMessageService_popMessage(&msgService);
	etMessage* rcvMsg2 = etMessageService_popMessage(&msgService);

	EXPECT_EQUAL_INT16("msgService.messageQueue.size",0, msgService.messageQueue.size);


	EXPECT_EQUAL_INT16("msgService.popMessage", 22, rcvMsg1->address);
	EXPECT_EQUAL_INT16("msgService.popMessage", 222, rcvMsg1->evtID);
	EXPECT_EQUAL_INT16("msgService.popMessage", 11, rcvMsg2->address);
	EXPECT_EQUAL_INT16("msgService.popMessage", 111, rcvMsg2->evtID);

	etMessageService_returnMessageBuffer(&msgService, rcvMsg1);
	etMessageService_returnMessageBuffer(&msgService, rcvMsg2);

	EXPECT_EQUAL_INT16("msgService.messagePool.size", 6, msgService.messagePool.size);

}

void TestEtMessageService_GetReturn(void){

	etMessageService msgService;
	uint16 max = 2;
	uint16 blockSize = 32;
	uint8 msgBuffer[max*blockSize];

	etMessageService_init(&msgService, msgBuffer, max, blockSize, DummyMessageDispatcher);

	// get one message too much from pool
	etMessage* msg1 = etMessageService_getMessageBuffer(&msgService, sizeof(etMessage));
	etMessage* msg2 = etMessageService_getMessageBuffer(&msgService, sizeof(etMessage));
	etMessage* msg3 = etMessageService_getMessageBuffer(&msgService, sizeof(etMessage));
	EXPECT_TRUE("msgService getMessageBuffer", msg1!=NULL);
	EXPECT_TRUE("msgService getMessageBuffer", msg2!=NULL);
	EXPECT_EQUAL_PTR("msgService getMessageBuffer", msg3, NULL);

	// return messages
	etMessageService_returnMessageBuffer(&msgService, msg1);
	etMessageService_returnMessageBuffer(&msgService, msg2);
	EXPECT_EQUAL_INT16("msgService.messagePool.size", 2, msgService.messagePool.size);

	// get message bigger than blocksize
	etMessage* msg4 = etMessageService_getMessageBuffer(&msgService, 33);
	EXPECT_EQUAL_PTR("msgService getMessageBuffer", msg4, NULL);

}

void TestEtMessageService_execute(void){
	etMessageService msgService;
	uint16 max = 6;
	uint16 blockSize = 32;
	uint8 msgBuffer[max*blockSize];

	etMessageService_init(&msgService, msgBuffer, max, blockSize, DummyMessageDispatcher);

	// get messages from pool
	etMessage* msg1 = etMessageService_getMessageBuffer(&msgService, sizeof(etMessage));
	etMessage* msg2 = etMessageService_getMessageBuffer(&msgService, sizeof(etMessage));

	// define content
	msg1->address = 11;
	msg1->evtID = 111;
	msg2->address = 22;
	msg2->evtID = 222;

	// push messages to queue
	etMessageService_pushMessage(&msgService, msg1);
	etMessageService_pushMessage(&msgService, msg2);

	/* make sure that receivedEventIDCounter==0 and receivedEventIDs[n]==0*/
	etMessageService_execute(&msgService);
	EXPECT_EQUAL_INT16("deliverAllMessages msg1", msg1->evtID, receivedEventIDs[0]);
	EXPECT_EQUAL_INT16("deliverAllMessages msg2", msg2->evtID, receivedEventIDs[1]);
	EXPECT_EQUAL_INT16("deliverAllMessages receivedEventIDCounter", 2, receivedEventIDCounter);
}

void TestEtMessageService_runSuite(void){
	etUnit_openTestSuite("TestEtMessageService");
	ADD_TESTCASE(TestEtMessageService_init);
	ADD_TESTCASE(TestEtMessageService_GetPushPopReturn);
	ADD_TESTCASE(TestEtMessageService_GetReturn);
	ADD_TESTCASE(TestEtMessageService_execute);
	etUnit_closeTestSuite();
}

Back to the top