summaryrefslogtreecommitdiff
path: root/FreeRTOS-Plus/Test/FreeRTOS-Plus-TCP/Integration/Full-TCP-Suite/Test_Code/Test_Cases/include/test_utils.h
blob: a62c131207270d1b5cf97e4cf37ac10ef1497e43 (plain)
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
/*
 * FreeRTOS V202111.00
 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * http://aws.amazon.com/freertos
 * http://www.FreeRTOS.org
 */

#ifndef _TEST_UTILS_H_
#define _TEST_UTILS_H_

/**
 * @brief      Retry expression or statement with exponential backoff
 *
 * @param      xCommand            The expression or statement that should be
 *                                 retried
 * @param      xSuccessStatus      The success status where a xCommand need not
 *                                 be retried any more
 * @param      ulStartingPeriodMs  The initial delay period in milliseconds
 * @param      lRetries            The number of times to retry.  xCommand will
 *                                 be tried once, and then retried n times.
 *
 * @code
 *  int a = 0;
 *  RETRY_EXPONENTIAL( printf( "foo\n" ), 4, 150, 8 );
 *  RETRY_EXPONENTIAL( a = printf( "bar\n" ), 0, 250, 8 );
 *  RETRY_EXPONENTIAL( a = printf( "bar\n" ), 0, 250, 8 );
 *  RETRY_EXPONENTIAL( a = connect_to_server(), CONNECTION_SUCCESS, 250, 8 );
 *  RETRY_EXPONENTIAL( a++, 10, 250, 8 );
 * @endcode
 *
 * @return     None
 */
#define RETRY_EXPONENTIAL(                                               \
        xCommand, xSuccessStatus, ulStartingPeriodMs, lRetries )         \
    {                                                                    \
        int32_t lRetried = 0;                                            \
        uint32_t ulPeriodMs = ulStartingPeriodMs;                        \
        int32_t lStatus;                                                 \
        for( ; lRetried <= lRetries; lRetried++ ) {                      \
            if( lRetried ) {                                             \
                configPRINTF( ( "retrying \"%s\", %d of %d, in %d ms\n", \
                                # xCommand, lRetried,                    \
                                lRetries, ulPeriodMs ) );                \
                vTaskDelay( pdMS_TO_TICKS( ulPeriodMs ) );               \
                ulPeriodMs *= 2;                                         \
            }                                                            \
            lStatus = xCommand;                                          \
            if( xSuccessStatus == lStatus ) {                            \
                break;                                                   \
            }                                                            \
            configPRINTF( ( "expected %d, got %d\n",                     \
                            xSuccessStatus, lStatus ) );                 \
        }                                                                \
    }

/**
 * @brief      Returns the file name at the end of a windows path
 *
 * @param      full_path  The full path
 *
 * @return     file name
 */
#define WIN_FILENAME( full_path ) \
    ( strrchr( full_path, '\\' ) ? strrchr( full_path, '\\' ) + 1 : full_path )

/**
 * @brief      Returns the file name at the end of a linux path
 *
 * @param      full_path  The full path
 *
 * @return     file name
 */
#define NIX_FILENAME( full_path ) \
    ( strrchr( full_path, '/' ) ? strrchr( full_path, '/' ) + 1 : full_path )

/**
 * The name of the current file, stripped of the path
 */
#define __FILENAME__    WIN_FILENAME( NIX_FILENAME( __FILE__ ) )


#endif /* end of include guard: _AWS_TEST_UTILS_H_ */