-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes in internal logic of sending packets #124
Changes from all commits
5ec1c2f
4205109
0704c72
adce766
f5aa1b3
76166d0
1d73bb7
0c5a9e3
34adc86
2a3cc9a
70b181a
68fcb16
4ba8c53
b710790
f8586dc
c59d39b
af1ca02
86798bf
e1de841
9a792bc
ebcfb92
fe0a071
32f0291
dd91637
a4bf796
bf6f94d
092e7ff
f0a1808
06669de
9092419
858da68
71e2a40
1f6a138
e03f4e5
605a83c
685ed2b
3504b2d
a82053c
245ef09
ddbb4b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,10 +93,18 @@ | |
#define MQTT_OVERFLOW_OFFSET ( 3 ) | ||
|
||
/** | ||
* @brief Subtract this value from max value of global entry time | ||
* for the timer overflow test. | ||
* @brief The number of times the "getTime()" function is called | ||
* within a single iteration of the #MQTT_ProcessLoop. | ||
* | ||
* This constant is used for the timer overflow test which checks | ||
* that the API can support normal behavior even if the timer | ||
* overflows. | ||
* | ||
* @note Currently, there are 5 calls within a single iteration. | ||
* This can change when the implementation changes which would be | ||
* caught through unit test failure. | ||
*/ | ||
#define MQTT_TIMER_CALLS_PER_ITERATION ( 4 ) | ||
#define MQTT_TIMER_CALLS_PER_ITERATION ( 5 ) | ||
|
||
/** | ||
* @brief Timeout for the timer overflow test. | ||
|
@@ -300,6 +308,19 @@ static int32_t transportSendFailure( NetworkContext_t * pNetworkContext, | |
return -1; | ||
} | ||
|
||
/** | ||
* @brief Mocked transport send that always returns 0 bytes sent. | ||
*/ | ||
static int32_t transportSendNoBytes( NetworkContext_t * pNetworkContext, | ||
const void * pBuffer, | ||
size_t bytesToWrite ) | ||
{ | ||
( void ) pNetworkContext; | ||
( void ) pBuffer; | ||
( void ) bytesToWrite; | ||
return 0; | ||
} | ||
|
||
/** | ||
* @brief Mocked transport send that succeeds then fails. | ||
*/ | ||
|
@@ -1327,6 +1348,47 @@ void test_MQTT_Publish( void ) | |
TEST_ASSERT_EQUAL_INT( MQTTSuccess, status ); | ||
} | ||
|
||
/** | ||
* @brief Test that verifies that the MQTT_Publish API detects a timeout | ||
* and returns failure when the transport send function is unable to send any data | ||
* over the network. | ||
*/ | ||
void test_MQTT_Publish_Send_Timeout( void ) | ||
{ | ||
MQTTContext_t mqttContext; | ||
MQTTPublishInfo_t publishInfo; | ||
TransportInterface_t transport; | ||
MQTTFixedBuffer_t networkBuffer; | ||
MQTTStatus_t status; | ||
size_t headerSize; | ||
|
||
setupNetworkBuffer( &networkBuffer ); | ||
setupTransportInterface( &transport ); | ||
|
||
/* Set the transport send function to the mock that always returns zero | ||
* bytes for the test. */ | ||
transport.send = transportSendNoBytes; | ||
|
||
/* Initialize the MQTT context. */ | ||
MQTT_Init( &mqttContext, &transport, getTime, eventCallback, &networkBuffer ); | ||
|
||
/* Setup for making sure that the test results in calling sendPacket function | ||
* where calls to transport send function are made (repeatedly to send packet | ||
* over the network).*/ | ||
memset( &publishInfo, 0, sizeof( MQTTPublishInfo_t ) ); | ||
headerSize = 1; | ||
publishInfo.pPayload = "Test"; | ||
publishInfo.payloadLength = 4; | ||
MQTT_GetPublishPacketSize_IgnoreAndReturn( MQTTSuccess ); | ||
MQTT_SerializePublishHeader_ExpectAnyArgsAndReturn( MQTTSuccess ); | ||
MQTT_SerializePublishHeader_ReturnThruPtr_pHeaderSize( &headerSize ); | ||
|
||
/* Call the API function under test and expect that it detects a timeout in sending | ||
* MQTT packet over the network. */ | ||
status = MQTT_Publish( &mqttContext, &publishInfo, 0 ); | ||
TEST_ASSERT_EQUAL_INT( MQTTSendFailed, status ); | ||
} | ||
|
||
/* ========================================================================== */ | ||
|
||
/** | ||
|
@@ -1949,17 +2011,27 @@ void test_MQTT_ProcessLoop_Timer_Overflow( void ) | |
MQTTPublishState_t publishState = MQTTPubAckSend; | ||
MQTTPublishState_t ackState = MQTTPublishDone; | ||
uint8_t i = 0; | ||
uint8_t numIterations = ( MQTT_TIMER_OVERFLOW_TIMEOUT_MS / MQTT_TIMER_CALLS_PER_ITERATION ) + 1; | ||
|
||
/* Calculate the number of iterations that the loop within the MQTT_ProcessLoop call | ||
* will be executed for the time duration value in the test. | ||
* The number of iterations is ceiling( Time Duration / Number of timer calls per iteration ) . */ | ||
uint8_t numIterations = ( MQTT_TIMER_OVERFLOW_TIMEOUT_MS + MQTT_TIMER_CALLS_PER_ITERATION - 1 ) / | ||
MQTT_TIMER_CALLS_PER_ITERATION; | ||
|
||
uint32_t expectedFinalTime; | ||
|
||
setupTransportInterface( &transport ); | ||
setupNetworkBuffer( &networkBuffer ); | ||
|
||
networkBuffer.size = 1000; | ||
incomingPacket.type = MQTT_PACKET_TYPE_PUBLISH; | ||
incomingPacket.remainingLength = MQTT_SAMPLE_REMAINING_LENGTH; | ||
|
||
globalEntryTime = UINT32_MAX - MQTT_OVERFLOW_OFFSET; | ||
expectedFinalTime = MQTT_TIMER_CALLS_PER_ITERATION * numIterations - MQTT_OVERFLOW_OFFSET; | ||
|
||
/* Calculate the expected time counter value after the MQTT_ProcessLoop API call. | ||
* Note: The "+ 1" is for the call to getTime() function before the loop iterations. */ | ||
expectedFinalTime = globalEntryTime + ( numIterations * MQTT_TIMER_CALLS_PER_ITERATION ) + 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Why this change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is correcting the calculation of the expected time counter after the call to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the correction? This value is the same as before isn't it? Also, basing Ultimately it doesn't matter, since the value is the same as it was before, but that's why this change looked weird to me There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is the main motivation from a resiliency and readability perspective. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was saying it's less resilient since before, accidentally setting |
||
|
||
mqttStatus = MQTT_Init( &context, &transport, getTime, eventCallback, &networkBuffer ); | ||
TEST_ASSERT_EQUAL( MQTTSuccess, mqttStatus ); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you going to make the same change in the other places where
sendPacket
is called, or will that be a separate PR?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Separate PR to avoid scope screep in this PR.