Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
test_main.hxx File Reference
#include "nmranet_config.h"
#include <stdio.h>
#include <stdarg.h>
#include <memory>
#include <string>
#include <functional>
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "can_frame.h"
#include "executor/CallableFlow.hxx"
#include "executor/Executor.hxx"
#include "executor/Service.hxx"
#include "os/TempFile.hxx"
#include "os/os.h"
#include "utils/StringPrintf.hxx"

Go to the source code of this file.

Classes

class  ExecutorStartupFix
 Fixes race condition between test teardown and executor startup. More...
 
class  ExecuteOnMainExecutor
 Utility class to help running a "pthread"-like thread in the main executor. More...
 
class  FnExecutable
 Helper class to run a lambda in the main executor. More...
 
class  RunInConstruct
 Runs some code in the constructor. More...
 
class  RunInConstructOnMain
 Runs some code in the constructor on the main executor. More...
 
struct  PendingInvocation< T >
 Structure holding returned objects for an invoke_flow_nowait command. More...
 
class  BlockExecutor
 Utility class to block an executor for a while. More...
 
class  ScopedOverride
 Overrides the value of a variable and restores it to the original value when destructed. More...
 
class  ScopedOverride::HolderBase
 Virtual base class for the destructible holders. More...
 
class  ScopedOverride::Holder< T >
 Type-accurate class that holds the temporary variable with the old value, the pointer to the variable and restores the previous state upon destruction. More...
 

Macros

#define RX(statement)   run_x([&](){ statement; })
 Helper macro to make running certain test commands run on the main executor simpler.
 

Functions

int appl_main (int argc, char *argv[])
 Entry point to application.
 
void log_output (char *buf, int size)
 Prints a line of log to the log output destination.
 
Executor< 1 > g_executor ("ex_thread", 0, 1024)
 Global executor thread for tests.
 
 OVERRIDE_CONST_ (gridconnect_buffer_size, 1)
 Do not buffer gridconnect bytes when we are running in a test.
 
void wait_for_main_executor ()
 Blocks the current thread until the main executor has run out of work.
 
void wait_for_main_timers ()
 Delays the current thread until all asynchronous processing and all pending timers have completed.
 
void run_x (std::function< void()> fn)
 Synchronously runs a function in the main executor.
 
template<class T , typename... Args>
std::unique_ptr< PendingInvocation< T > > invoke_flow_nowait (FlowInterface< Buffer< T > > *flow, Args &&...args)
 Executes a callable flow similar to invoke_flow(...) but does not wait for the result to come back.
 
int nibble_to_int (char n)
 Converts a character containing a hex digit to the value of that digit.
 
std::string hex2str (const char *hex)
 Converts a hex string into the respective byte string.
 
std::string str2hex (const string &s)
 Converts a byte string into the hexadecimal representation, with no separators.
 

Variables

bool mute_log_output = false
 
Executor< 1 > g_executor
 Global executor thread for tests.
 
class ExecutorStartupFix unused_executor_startup_guard_instance
 actual instance.
 
const char HEXCHR [17] = "0123456789abcdef"
 

Detailed Description

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Include this file into your unittest to define the necessary symbols and main function.

Author
Balazs Racz
Date
3 Nov 2013

Definition in file test_main.hxx.

Macro Definition Documentation

◆ RX

#define RX (   statement)    run_x([&](){ statement; })

Helper macro to make running certain test commands run on the main executor simpler.

Definition at line 218 of file test_main.hxx.

Function Documentation

◆ appl_main()

int appl_main ( int  argc,
char *  argv[] 
)

Entry point to application.

Parameters
argcnumber of arguments
argvlist of arguments
Returns
0 upon success.

Definition at line 66 of file test_main.hxx.

◆ g_executor()

Executor< 1 > g_executor ( "ex_thread"  ,
,
1024   
)

Global executor thread for tests.

Todo:
maybe this should have 5 bands?

◆ hex2str()

std::string hex2str ( const char *  hex)

Converts a hex string into the respective byte string.

Parameters
hexa string containing hex digits. Separators between hex bytes are allowed and ignored.
Returns
string containing bytes respective to the hex.

Definition at line 422 of file test_main.hxx.

◆ invoke_flow_nowait()

template<class T , typename... Args>
std::unique_ptr< PendingInvocation< T > > invoke_flow_nowait ( FlowInterface< Buffer< T > > *  flow,
Args &&...  args 
)

Executes a callable flow similar to invoke_flow(...) but does not wait for the result to come back.

Instead, returns a PendingInvocation object, where there is a wait() method to be called.

Definition at line 252 of file test_main.hxx.

◆ log_output()

void log_output ( char *  buf,
int  size 
)

Prints a line of log to the log output destination.

Parameters
bufis the logging buffer. Guaranteed to be available until this function returns.
sizeis now many bytes are there in the logging buffer. There is never a terminating
in the log buffer. There is a terminating zero at buf[size].

Prints a line of log to the log output destination.

This is a definition fo a C++ symbol, and should be included exactly once into one .cxx file; typicall into main.cxx.

Parameters
bufpointer to bytes to log. Does not contain a
.
sizehow may bytes to log.

Definition at line 75 of file test_main.hxx.

◆ nibble_to_int()

int nibble_to_int ( char  n)

Converts a character containing a hex digit to the value of that digit.

Parameters
ncharacter with lower or upper case hex digit.
Returns
int value of that digit.

Definition at line 401 of file test_main.hxx.

◆ run_x()

void run_x ( std::function< void()>  fn)

Synchronously runs a function in the main executor.

Definition at line 190 of file test_main.hxx.

◆ str2hex()

std::string str2hex ( const string &  s)

Converts a byte string into the hexadecimal representation, with no separators.

Parameters
sArbitrary byte payload.
Returns
string containing two hex digits per incoming byte, with the respective value..

Definition at line 445 of file test_main.hxx.

◆ wait_for_main_executor()

void wait_for_main_executor ( )

Blocks the current thread until the main executor has run out of work.

Use this function in unittest functions to wait for any asynchronous work you may have scheduled on the main executor. This typically happens before expectations. Also if you have stack-allocated objects that will schedule themselves on the main executor, then you must call this function before ending the scope that will deallocate them – it is typical to have this as the last command in a TEST_F.

Definition at line 113 of file test_main.hxx.

◆ wait_for_main_timers()

void wait_for_main_timers ( )

Delays the current thread until all asynchronous processing and all pending timers have completed.

Definition at line 121 of file test_main.hxx.

Variable Documentation

◆ g_executor

Service g_service & g_executor

Global executor thread for tests.

Todo:
maybe this should have 5 bands?

Definition at line 103 of file test_main.hxx.

◆ HEXCHR

const char HEXCHR[17] = "0123456789abcdef"

Definition at line 438 of file test_main.hxx.

◆ mute_log_output

bool mute_log_output = false

Definition at line 72 of file test_main.hxx.