feisty meow concerns codebase  2.140
test_break_signal.cpp
Go to the documentation of this file.
1 /*****************************************************************************\
2 * *
3 * Name : test_break_signal *
4 * Author : Chris Koeritz *
5 * *
6 *******************************************************************************
7 * Copyright (c) 2004-$now By Author. This program is free software; you can *
8 * redistribute it and/or modify it under the terms of the GNU General Public *
9 * License as published by the Free Software Foundation; either version 2 of *
10 * the License or (at your option) any later version. This is online at: *
11 * http://www.fsf.org/copyleft/gpl.html *
12 * Please send any updates to: fred@gruntose.com *
13 \*****************************************************************************/
14 
15 #include <basis/functions.h>
16 #include <basis/guards.h>
17 #include <basis/astring.h>
18 #include <timely/time_control.h>
19 #include <timely/time_stamp.h>
22 #include <loggers/console_logger.h>
25 #include <filesystem/filename.h>
27 #include <unit_test/unit_base.h>
28 
29 #include <signal.h>
30 //hmmm: much better if we had signal handling wrapped in a class instead of using bare calls to the OS signal library.
31 #include <stdio.h>
32 //hmmm: also a flush mechanism for i/o being inside feisty code would be better than this call to stdio.
33 
34 using namespace application;
35 using namespace basis;
36 using namespace loggers;
37 using namespace timely;
38 using namespace unit_test;
39 
40 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
41 
42 static bool _leave_now = false;
43 
44 const int DEFAULT_PAUSE_TIME = 1; // how long we'll wait, unless told a different time.
45 
46 class test_break_signal : virtual public unit_base, virtual public application_shell
47 {
48 public:
49  test_break_signal() : application_shell() {}
50  DEFINE_CLASS_NAME("test_break_signal");
51  virtual int execute();
52 };
53 
54 void handle_break(int formal(signal))
55 {
56  #undef static_class_name
57  #define static_class_name() "test_break_signal"
58  FUNCDEF("handle_break");
59  LOG("we have hit the signal handler for break!");
60  _leave_now = true;
61  #undef static_class_name
62 }
63 
64 int test_break_signal::execute()
65 {
66  FUNCDEF("execute");
67 
68  int pause_time = DEFAULT_PAUSE_TIME;
69  if (application::_global_argc >= 2) {
70  astring passed_pause = application::_global_argv[1];
71  pause_time = passed_pause.convert(DEFAULT_PAUSE_TIME);
72  }
73 
74 
75  signal(SIGINT, handle_break);
76  LOG("starting loop--hit ctrl-C to exit or wait for timeout.");
77  time_stamp leave_time(pause_time * SECOND_ms);
78  while (!_leave_now && (time_stamp() < leave_time) ) {
79  time_control::sleep_ms(20);
80  }
81 
82  // we jump to here when catching the signal.
83  fflush(NULL_POINTER);
84 
85  critical_events::alert_message(astring(class_name()) + ": works for those functions tested.");
86 
87  return 0;
88 }
89 
90 HOOPLE_MAIN(test_break_signal, )
91 
The application_shell is a base object for console programs.
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
int convert(int default_value) const
Converts the string into a corresponding integer.
Definition: astring.cpp:760
Represents a point in time relative to the operating system startup time.
Definition: time_stamp.h:38
#define formal(parameter)
This macro just eats what it's passed; it marks unused formal parameters.
Definition: definitions.h:48
#define NULL_POINTER
The value representing a pointer to nothing.
Definition: definitions.h:32
#define DEFINE_CLASS_NAME(objname)
Defines the name of a class by providing a couple standard methods.
Definition: enhance_cpp.h:42
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Definition: enhance_cpp.h:54
Provides macros that implement the 'main' program of an application.
#define HOOPLE_MAIN(obj_name, obj_args)
options that should work for most unix and linux apps.
Definition: hoople_main.h:61
Implements an application lock to ensure only one is running at once.
char ** _global_argv
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
const int SECOND_ms
Number of milliseconds in a second.
Definition: definitions.h:120
A logger that sends to the console screen using the standard output device.
#include <time.h>
Definition: earth_time.cpp:37
Useful support functions for unit testing, especially within hoople.
Definition: unit_base.cpp:35
void handle_break(int formal(signal))
#define LOG(s)
const int DEFAULT_PAUSE_TIME