Skip to content

Commit b860326

Browse files
authored
Add sendRequestWithRetry and requestAndResponseWithRetry functions. (#96)
1 parent a18f6ab commit b860326

File tree

7 files changed

+367
-1
lines changed

7 files changed

+367
-1
lines changed

keywords.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ FUNCTIONS
99
begin KEYWORD2
1010
setDebugOutputStream KEYWORD2
1111
clearDebugOutputStream KEYWORD2
12+
setTransactionPins KEYWORD2
1213
newRequest KEYWORD2
1314
newCommand KEYWORD2
1415
sendRequest KEYWORD2
16+
sendRequestWithRetry KEYWORD2
1517
requestAndResponse KEYWORD2
18+
requestAndResponseWithRetry KEYWORD2
1619
deleteResponse KEYWORD2
1720
logDebug KEYWORD2
1821
logDebugf KEYWORD2

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Blues Wireless Notecard
2-
version=1.3.18
2+
version=1.4.0
33
author=Blues Wireless
44
maintainer=Blues Wireless <[email protected]>
55
sentence=An easy to use Notecard Library for Arduino.

src/Notecard.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,23 @@ bool Notecard::sendRequest(J *req)
354354
return NoteRequest(req);
355355
}
356356

357+
/**************************************************************************/
358+
/*!
359+
@brief Sends a request to the Notecard, retrying it on failure until the
360+
provided timeout interval lapses.
361+
@param req
362+
A `J` JSON request object.
363+
@param timeoutSeconds
364+
The timeout interval, in seconds.
365+
@return `True` if the message was successfully sent to the Notecard,
366+
`False` if the message couldn't be sent.
367+
*/
368+
/**************************************************************************/
369+
bool Notecard::sendRequestWithRetry(J *req, uint32_t timeoutSeconds)
370+
{
371+
return NoteRequestWithRetry(req, timeoutSeconds);
372+
}
373+
357374
/**************************************************************************/
358375
/*!
359376
@brief Sends a request to the Notecard and returns the JSON Response.
@@ -369,6 +386,22 @@ J *Notecard::requestAndResponse(J *req)
369386
return NoteRequestResponse(req);
370387
}
371388

389+
/**************************************************************************/
390+
/*!
391+
@brief Sends a request to the Notecard, retrying it on failure until the
392+
provided timeout interval lapses, and returns the JSON response.
393+
@param req
394+
A `J` JSON request object.
395+
@param timeoutSeconds
396+
The timeout interval, in seconds.
397+
@return `J` JSON Object with the response from the Notecard.
398+
*/
399+
/**************************************************************************/
400+
J *Notecard::requestAndResponseWithRetry(J *req, uint32_t timeoutSeconds)
401+
{
402+
return NoteRequestResponseWithRetry(req, timeoutSeconds);
403+
}
404+
372405
/**************************************************************************/
373406
/*!
374407
@brief Deletes a `J` JSON response object from memory.

src/Notecard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ class Notecard
8686
J *newRequest(const char *request);
8787
J *newCommand(const char *request);
8888
bool sendRequest(J *req);
89+
bool sendRequestWithRetry(J *req, uint32_t timeoutSeconds);
8990
J *requestAndResponse(J *req);
91+
J *requestAndResponseWithRetry(J *req, uint32_t timeoutSeconds);
9092
void deleteResponse(J *rsp);
9193
void logDebug(const char *message);
9294
void logDebugf(const char *format, ...);

test/Notecard.test.cpp

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,6 +1773,121 @@ int test_notecard_sendRequest_does_not_modify_note_c_result_value_before_returni
17731773
return result;
17741774
}
17751775

1776+
int test_notecard_sendRequestWithRetry_does_not_modify_j_object_parameter_value_before_passing_to_note_c()
1777+
{
1778+
int result;
1779+
1780+
// Arrange
1781+
////////////
1782+
1783+
Notecard notecard;
1784+
J * EXPECTED_JSON = reinterpret_cast<J *>(malloc(sizeof(J)));
1785+
J * json_cpy;
1786+
assert(nullptr != EXPECTED_JSON);
1787+
1788+
noteRequestWithRetry_Parameters.reset();
1789+
memset(EXPECTED_JSON, 0x55, sizeof(J));
1790+
{
1791+
json_cpy = reinterpret_cast<J *>(malloc(sizeof(J)));
1792+
assert(nullptr != json_cpy);
1793+
memcpy(json_cpy, EXPECTED_JSON, sizeof(J));
1794+
}
1795+
1796+
// Action
1797+
///////////
1798+
1799+
notecard.sendRequestWithRetry(json_cpy, 0);
1800+
1801+
// Assert
1802+
///////////
1803+
1804+
if (!memcmp(EXPECTED_JSON, noteRequestWithRetry_Parameters.req, sizeof(J)))
1805+
{
1806+
result = 0;
1807+
}
1808+
else
1809+
{
1810+
result = static_cast<int>('n' + 'o' + 't' + 'e' + 'c' + 'a' + 'r' + 'd');
1811+
std::cout << "\33[31mFAILED\33[0m] " << __FILE__ << ":" << __LINE__ << std::endl;
1812+
std::cout << "\tnoteRequestWithRetry_Parameters.req != EXPECTED_JSON" << std::endl;
1813+
std::cout << "[";
1814+
}
1815+
1816+
free(json_cpy);
1817+
free(EXPECTED_JSON);
1818+
return result;
1819+
}
1820+
1821+
int test_notecard_sendRequestWithRetry_does_not_modify_timeout_parameter_value_before_passing_to_note_c()
1822+
{
1823+
int result;
1824+
1825+
// Arrange
1826+
////////////
1827+
1828+
Notecard notecard;
1829+
uint32_t EXPECTED_TIMEOUT = 10;
1830+
noteRequestWithRetry_Parameters.reset();
1831+
1832+
// Action
1833+
///////////
1834+
1835+
notecard.sendRequestWithRetry(nullptr, EXPECTED_TIMEOUT);
1836+
1837+
// Assert
1838+
///////////
1839+
1840+
if (EXPECTED_TIMEOUT == noteRequestWithRetry_Parameters.timeoutSeconds)
1841+
{
1842+
result = 0;
1843+
}
1844+
else
1845+
{
1846+
result = static_cast<int>('n' + 'o' + 't' + 'e' + 'c' + 'a' + 'r' + 'd');
1847+
std::cout << "\33[31mFAILED\33[0m] " << __FILE__ << ":" << __LINE__ << std::endl;
1848+
std::cout << "\tnoteRequestWithRetry_Parameters.timeoutSeconds != EXPECTED_TIMEOUT" << std::endl;
1849+
std::cout << "[";
1850+
}
1851+
1852+
return result;
1853+
}
1854+
1855+
int test_notecard_sendRequestWithRetry_does_not_modify_note_c_result_value_before_returning_to_caller()
1856+
{
1857+
int result;
1858+
1859+
// Arrange
1860+
////////////
1861+
1862+
Notecard notecard;
1863+
const bool EXPECTED_RESULT = true;
1864+
1865+
noteRequestWithRetry_Parameters.reset();
1866+
noteRequestWithRetry_Parameters.result = EXPECTED_RESULT;
1867+
1868+
// Action
1869+
///////////
1870+
1871+
const bool ACTUAL_RESULT = notecard.sendRequestWithRetry(nullptr, 0);
1872+
1873+
// Assert
1874+
///////////
1875+
1876+
if (EXPECTED_RESULT == ACTUAL_RESULT)
1877+
{
1878+
result = 0;
1879+
}
1880+
else
1881+
{
1882+
result = static_cast<int>('n' + 'o' + 't' + 'e' + 'c' + 'a' + 'r' + 'd');
1883+
std::cout << "\33[31mFAILED\33[0m] " << __FILE__ << ":" << __LINE__ << std::endl;
1884+
std::cout << "\tnotecard.sendRequestWithRetry(nullptr, 0) == \"" << ACTUAL_RESULT << "\", EXPECTED: \"" << EXPECTED_RESULT << "\"" << std::endl;
1885+
std::cout << "[";
1886+
}
1887+
1888+
return result;
1889+
}
1890+
17761891
int test_notecard_requestAndResponse_does_not_modify_j_object_parameter_value_before_passing_to_note_c()
17771892
{
17781893
int result;
@@ -1862,6 +1977,129 @@ int test_notecard_requestAndResponse_does_not_modify_note_c_result_value_before_
18621977
return result;
18631978
}
18641979

1980+
int test_notecard_requestAndResponseWithRetry_does_not_modify_j_object_parameter_value_before_passing_to_note_c()
1981+
{
1982+
int result;
1983+
1984+
// Arrange
1985+
////////////
1986+
1987+
Notecard notecard;
1988+
J *EXPECTED_JSON = reinterpret_cast<J *>(malloc(sizeof(J)));
1989+
J *json_cpy;
1990+
assert(nullptr != EXPECTED_JSON);
1991+
1992+
noteRequestResponseWithRetry_Parameters.reset();
1993+
memset(EXPECTED_JSON, 0x55, sizeof(J));
1994+
{
1995+
json_cpy = reinterpret_cast<J *>(malloc(sizeof(J)));
1996+
assert(nullptr != json_cpy);
1997+
memcpy(json_cpy, EXPECTED_JSON, sizeof(J));
1998+
}
1999+
2000+
// Action
2001+
///////////
2002+
2003+
notecard.requestAndResponseWithRetry(json_cpy, 0);
2004+
2005+
// Assert
2006+
///////////
2007+
2008+
if (!memcmp(EXPECTED_JSON, noteRequestResponseWithRetry_Parameters.req, sizeof(J)))
2009+
{
2010+
result = 0;
2011+
}
2012+
else
2013+
{
2014+
result = static_cast<int>('n' + 'o' + 't' + 'e' + 'c' + 'a' + 'r' + 'd');
2015+
std::cout << "\33[31mFAILED\33[0m] " << __FILE__ << ":" << __LINE__ << std::endl;
2016+
std::cout << "\tnoteRequestResponseWithRetry_Parameters.req != EXPECTED_JSON" << std::endl;
2017+
std::cout << "[";
2018+
}
2019+
2020+
free(json_cpy);
2021+
free(EXPECTED_JSON);
2022+
return result;
2023+
}
2024+
2025+
int test_notecard_requestAndResponseWithRetry_does_not_modify_timeout_parameter_value_before_passing_to_note_c()
2026+
{
2027+
int result;
2028+
2029+
// Arrange
2030+
////////////
2031+
2032+
Notecard notecard;
2033+
uint32_t EXPECTED_TIMEOUT = 10;
2034+
noteRequestResponseWithRetry_Parameters.reset();
2035+
2036+
// Action
2037+
///////////
2038+
2039+
notecard.requestAndResponseWithRetry(nullptr, EXPECTED_TIMEOUT);
2040+
2041+
// Assert
2042+
///////////
2043+
2044+
if (EXPECTED_TIMEOUT == noteRequestResponseWithRetry_Parameters.timeoutSeconds)
2045+
{
2046+
result = 0;
2047+
}
2048+
else
2049+
{
2050+
result = static_cast<int>('n' + 'o' + 't' + 'e' + 'c' + 'a' + 'r' + 'd');
2051+
std::cout << "\33[31mFAILED\33[0m] " << __FILE__ << ":" << __LINE__ << std::endl;
2052+
std::cout << "\tnoteRequestResponseWithRetry_Parameters.timeoutSeconds != EXPECTED_TIMEOUT" << std::endl;
2053+
std::cout << "[";
2054+
}
2055+
2056+
return result;
2057+
}
2058+
2059+
int test_notecard_requestAndResponseWithRetry_does_not_modify_note_c_result_value_before_returning_to_caller()
2060+
{
2061+
int result;
2062+
2063+
// Arrange
2064+
////////////
2065+
2066+
Notecard notecard;
2067+
J * EXPECTED_JSON = reinterpret_cast<J *>(malloc(sizeof(J)));
2068+
assert(nullptr != EXPECTED_JSON);
2069+
2070+
noteRequestResponseWithRetry_Parameters.reset();
2071+
memset(EXPECTED_JSON, 0x55, sizeof(J));
2072+
{
2073+
noteRequestResponseWithRetry_Parameters.result = reinterpret_cast<J *>(malloc(sizeof(J)));
2074+
assert(nullptr != noteRequestResponseWithRetry_Parameters.result);
2075+
memcpy(noteRequestResponseWithRetry_Parameters.result, EXPECTED_JSON, sizeof(J));
2076+
}
2077+
2078+
// Action
2079+
///////////
2080+
2081+
const J * const ACTUAL_RESULT = notecard.requestAndResponseWithRetry(nullptr, 0);
2082+
2083+
// Assert
2084+
///////////
2085+
2086+
if (!memcmp(EXPECTED_JSON, ACTUAL_RESULT, sizeof(J)))
2087+
{
2088+
result = 0;
2089+
}
2090+
else
2091+
{
2092+
result = static_cast<int>('n' + 'o' + 't' + 'e' + 'c' + 'a' + 'r' + 'd');
2093+
std::cout << "\33[31mFAILED\33[0m] " << __FILE__ << ":" << __LINE__ << std::endl;
2094+
std::cout << "\tnotecard.requestAndResponseWithRetry(nullptr, 0) != EXPECTED_JSON" << std::endl;
2095+
std::cout << "[";
2096+
}
2097+
2098+
free(noteRequestResponseWithRetry_Parameters.result);
2099+
free(EXPECTED_JSON);
2100+
return result;
2101+
}
2102+
18652103
int test_notecard_deleteResponse_does_not_modify_j_object_parameter_pointer_before_passing_to_note_c()
18662104
{
18672105
int result;
@@ -4276,8 +4514,14 @@ int main(void)
42764514
{test_notecard_newRequest_does_not_modify_note_c_result_value_before_returning_to_caller, "test_notecard_newRequest_does_not_modify_note_c_result_value_before_returning_to_caller"},
42774515
{test_notecard_sendRequest_does_not_modify_j_object_parameter_value_before_passing_to_note_c, "test_notecard_sendRequest_does_not_modify_j_object_parameter_value_before_passing_to_note_c"},
42784516
{test_notecard_sendRequest_does_not_modify_note_c_result_value_before_returning_to_caller, "test_notecard_sendRequest_does_not_modify_note_c_result_value_before_returning_to_caller"},
4517+
{test_notecard_sendRequestWithRetry_does_not_modify_j_object_parameter_value_before_passing_to_note_c, "test_notecard_sendRequestWithRetry_does_not_modify_j_object_parameter_value_before_passing_to_note_c"},
4518+
{test_notecard_sendRequestWithRetry_does_not_modify_timeout_parameter_value_before_passing_to_note_c, "test_notecard_sendRequestWithRetry_does_not_modify_timeout_parameter_value_before_passing_to_note_c"},
4519+
{test_notecard_sendRequestWithRetry_does_not_modify_note_c_result_value_before_returning_to_caller, "test_notecard_sendRequestWithRetry_does_not_modify_note_c_result_value_before_returning_to_caller"},
42794520
{test_notecard_requestAndResponse_does_not_modify_j_object_parameter_value_before_passing_to_note_c, "test_notecard_requestAndResponse_does_not_modify_j_object_parameter_value_before_passing_to_note_c"},
42804521
{test_notecard_requestAndResponse_does_not_modify_note_c_result_value_before_returning_to_caller, "test_notecard_requestAndResponse_does_not_modify_note_c_result_value_before_returning_to_caller"},
4522+
{test_notecard_requestAndResponseWithRetry_does_not_modify_j_object_parameter_value_before_passing_to_note_c, "test_notecard_requestAndResponseWithRetry_does_not_modify_j_object_parameter_value_before_passing_to_note_c"},
4523+
{test_notecard_requestAndResponseWithRetry_does_not_modify_timeout_parameter_value_before_passing_to_note_c, "test_notecard_requestAndResponseWithRetry_does_not_modify_timeout_parameter_value_before_passing_to_note_c"},
4524+
{test_notecard_requestAndResponseWithRetry_does_not_modify_note_c_result_value_before_returning_to_caller, "test_notecard_requestAndResponseWithRetry_does_not_modify_note_c_result_value_before_returning_to_caller"},
42814525
{test_notecard_deleteResponse_does_not_modify_j_object_parameter_pointer_before_passing_to_note_c, "test_notecard_deleteResponse_does_not_modify_j_object_parameter_pointer_before_passing_to_note_c"},
42824526
{test_notecard_logDebug_does_not_modify_string_parameter_value_before_passing_to_note_c, "test_notecard_logDebug_does_not_modify_string_parameter_value_before_passing_to_note_c"},
42834527
{test_notecard_logDebugf_does_not_modify_string_parameter_value_before_passing_to_note_c, "test_notecard_logDebugf_does_not_modify_string_parameter_value_before_passing_to_note_c"},

0 commit comments

Comments
 (0)