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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
|
// -*- c++ -*-
// Generated by assa-genesis
//------------------------------------------------------------------------------
// $Id: pipe_test.cpp,v 1.11 2006/07/20 02:30:56 vlg Exp $
//------------------------------------------------------------------------------
// PipeTest.cpp
//------------------------------------------------------------------------------
//
// Author : Vladislav Grinchenko
// Date : Sat Jul 20 2002
//------------------------------------------------------------------------------
static const char help_msg[]=
" \n"
" NAME: \n"
" PipeTest - test for Pipe class. \n"
" \n"
" DESCRIPTION: \n"
" \n"
" Test for class Pipe. pipe_test uses 'smoker' as data reader/writer. \n"
" \n"
" Test 1 spans off 'smoker' in writing mode. 'smoker' writes 10 \n"
" messages to its standard output and exits. Test 1 reads 10 messages \n"
" and closes connection. \n"
" \n"
" Test 2 spans off 'smoker' in a writing mode setup to write endless \n"
" stream of messages to its standard output with delay of two seconds. \n"
" Test 2 starts a timer and keeps reading messages from the pipe. When \n"
" timer expires, Test 2 kills 'smoker' and tries to detect if 'smoker' \n"
" has been killed simulating call to system() with Pipe. \n"
" \n"
" Both processes generate log files, pipe_test.log and smoker{1,2}.log, \n"
" respectively. \n"
" \n"
" pipe_test smoker \n"
" -------------- ---------- \n"
" | | \n"
" | pipe | < Test 1 starts \n"
" |--------------->| start t=1 sec \n"
" | | | \n"
" | msg | v \n"
" |<---------------| = \n"
" | | start t=1 sec \n"
" | | | \n"
" | msg | v \n"
" |<---------------| = \n"
" | | \n"
" ... \n"
" | | | \n"
" | msg | v \n"
" |<---------------| = Last (10th) msg sent out \n"
" | | \n"
" | close | \n"
" |<-X-------------| < Test 1 ends \n"
" \n"
" ... \n"
" | | \n"
" | pipe | < Test 2 starts \n"
" start t=5 |--------------->| start t=2 secs \n"
" | | | | \n"
" | | msg | v \n"
" | |<---------------| = \n"
" | | | start t=2 secs \n"
" | | | | \n"
" | | msg | v \n"
" | |<---------------| = \n"
" v | | \n"
" = | kill | \n"
" |------------X-->| \n"
" | | \n"
" | Try to lock ~/.smoker2.pid \n"
" | to test if smoker has been killed \n"
" |----+ | \n"
" | | | \n"
" | | | < Test 2 ends \n"
" |<---+ | \n"
" | | \n"
" \n"
" USAGE: \n"
" \n"
" shell> pipe_test [OPTIONS] \n"
" \n"
" OPTIONS: \n"
" \n"
" --build-dir STRING - Directory where executables are located. \n"
" -D, --log-file NAME - Write debug to NAME file \n"
" -d, --log-stdout - Write debug to standard output \n"
" -z, --log-size NUM - Maximum size debug file can reach (dfl: is 10Mb) \n"
" -m, --mask MASK - Mask (default: ALL = 0x7fffffff) \n"
" -h, --help - Print this messag \n"
" -v, --version - Print version number \n";
/******************************************************************************/
#include <cstdio>
#include <iostream>
#include <string>
using std::string;
using namespace std;
#include "assa/GenServer.h"
#include "assa/Singleton.h"
#include "assa/TimeVal.h"
#include "assa/Pipe.h"
#include "assa/AutoPtr.h"
#include "assa/IPv4Socket.h"
#include "assa/CommonUtils.h"
#include "assa/PidFileLock.h"
using namespace ASSA;
static const bool STILL_RUNNING=true;
/*******************************************************************************
Class PipeTest
*******************************************************************************/
class PipeTest :
public GenServer,
public Singleton<PipeTest>
{
public:
PipeTest ();
virtual void init_service ();
virtual void process_events ();
virtual int handle_read (int fd_);
virtual int handle_timeout (TimerId);
virtual int handle_close (int fd_);
string get_build_dir () const { return m_build_dir; }
private:
void setup_test_1 ();
int run_test_1 ();
void setup_test_2 ();
int run_test_2 ();
private:
enum state_t { start, test1, test2 };
Pipe* m_pipe;
FILE* m_pinstream;
int m_line_count;
int m_max_count_expected;
state_t m_state;
string m_build_dir;
};
// Useful defines
#define PIPETEST PipeTest::get_instance()
#define REACTOR PipeTest::get_instance()->get_reactor()
// Static declarations mandated by Singleton class
ASSA_DECL_SINGLETON(PipeTest);
/*******************************************************************************
Member functions
*******************************************************************************/
PipeTest::
PipeTest () :
m_pipe (NULL), m_pinstream (NULL),
m_line_count (0), m_max_count_expected (0),
m_state (start)
{
trace("PipeTest::PipeTest");
set_id ("PT");
add_opt (0, "build-dir", &m_build_dir);
// ---Configuration---
rm_opt ('f', "config-file" );
rm_opt ('n', "instance" );
rm_opt ('p', "port" );
// ---Process bookkeeping---
rm_opt ('b', "daemon" );
rm_opt ('l', "pidfile" );
rm_opt ('L', "ommit-pidfile");
/*---
* Disable all debugging
*---*/
// m_mask = 0x0;
m_log_size = 1024*1024*10;
m_mask = 0x7fffffff;
m_log_file = "pipe_test.log";
}
void
PipeTest::
init_service ()
{
trace("PipeTest::init_service");
Log::disable_timestamp ();
if (m_build_dir.length () == 0) {
m_build_dir = ASSA::Utils::get_cwd_name ();
}
DL((APP,"build-dir = \"%s\"\n", m_build_dir.c_str ()));
std::cout << "= Running pipe_test Test =" << endl;
DL((APP,"Test reading from pipe\n"));
setup_test_1 ();
}
int
PipeTest::
handle_read (int fd_)
{
trace("PipeTest::handle_read");
int ret;
DL((APP,"fd_ = %d\n", fd_));
switch (m_state) {
case test1:
ret = run_test_1 ();
break;
case test2:
ret = run_test_2 ();
break;
};
return ret;
}
void
PipeTest::
setup_test_1 ()
{
trace("PipeTest::setup_test_11");
string cmd (get_build_dir ());
cmd += "/smoker --write=10 --delay=1 --mask=0x7fffffff "
"--log-file=smoker_t1.log --pidfile=~/.smoker1.pid ";
AutoPtr<Pipe> pipe (new Pipe);
if (pipe->open (cmd.c_str (), "r") == NULL) {
DL((APP,"Reading from pipe failed\n"));
set_exit_value (1);
return;
}
/* Tie the I/O standard stream to iostream */
m_pinstream = pipe->fp ();
Assure_exit (REACTOR->registerIOHandler (this, pipe->fd (), READ_EVENT));
m_pipe = pipe.release ();
m_max_count_expected = 10;
m_state = test1;
}
int
PipeTest::
run_test_1 ()
{
trace("PipeTest::run_test_1");
char buf [128];
char* ret = NULL;
/* fgets() is a blocking call. We keep reading until we
* exhaust the pipe and the close it.
*/
while ((ret = fgets (buf, 128, m_pinstream)) != NULL) {
DL((APP,"gcount = %d, line %d: \"%s\"\n",
strlen (buf), m_line_count++, buf));
}
DL((APP,"Reached EOF on read() from pipe.\n"));
return -1; // close the connection
}
void
PipeTest::
setup_test_2 ()
{
trace("PipeTest::setup_test_11");
::unlink ("smoker2.log");
string cmd (get_build_dir ());
cmd += "/smoker --write=-1 --delay=2 --mask=0x7fffffff "
"--log-file=smoker_t2.log --pidfile=~/.smoker2.pid";
AutoPtr<Pipe> pipe (new Pipe);
if (pipe->open (cmd.c_str (), "r") == NULL) {
DL((APP,"Reading from pipe failed\n"));
set_exit_value (1);
return;
}
m_pinstream = pipe->fp ();
Assure_exit (REACTOR->registerIOHandler (this, pipe->fd (), READ_EVENT));
TimeVal tv (5.0); // 5 seconds delay
REACTOR->registerTimerHandler (this, tv, "5 secs");
m_pipe = pipe.release ();
m_max_count_expected = -1;
m_state = test2;
}
int
PipeTest::
run_test_2 ()
{
trace("PipeTest::run_test_2");
char buf [128];
char* ret = NULL;
/* Read a line at a time */
if ((ret = fgets (buf, 128, m_pinstream)) == NULL) {
DL((APP,"Received unexpected EOF on read() from pipe\n"));
return -1;
}
DL((APP,"gcount = %d, line %d: \"%s\"\n",
strlen (buf), m_line_count++, buf));
/* Following the Reactor's rules, what we should return here
* is the number of bytes left in the file descriptor buffer.
* However, we know that 'smoker' keeps filling the pipe
* and we won't starve.
*/
return 0;
}
/** At the end of the secod test, we kill smoker(2), then wait for
2 seconds and then test to see if its pid file still exists
and, if so, whether we can lock it.
If file exists and we fail to lock it - it means smoker(2) is
still running and our 'kill' failed. If we can lock it, then
it is an indication that our 'kill' caused smoker(2) to core dump
and it didn't have a chance for a clean exit.
*/
int
PipeTest::
handle_timeout (TimerId /* tid_ */)
{
trace("PipeTest::handle_timeout");
switch (m_state) {
case test1:
setup_test_2 ();
break;
case test2:
{
m_pipe->kill (); // knock out pipe forecfully
ASSA::Utils::sleep_for_seconds (2); // give smoker a chance to exit.
struct stat file_stat;
if (stat ("~/.smoker2.pid", &file_stat) < 0 ||
!S_ISREG (file_stat.st_mode))
{
DL ((APP,"smoker2 stopped. Passed test 2.\n"));
cout << "Passed test 2\n";
}
else {
ASSA::PidFileLock pflock;
if (pflock.lock ("~/.smoker2.pid") == STILL_RUNNING) {
DL ((APP,"Test 2 failed: was able to lock smoker(2) PID"
"- still running.\n"));
cout << "Test 2 failed\n";
cout << "error: smoker(2) is still running.\n";
}
else {
DL ((APP,"Test 2 failed: smoker(2) core dumped\n"));
cout << "Test 2 failed\n";
cout << "error: smoker(2) core dumped.\n";
}
}
cout << flush;
REACTOR->removeHandler (this);
}
break;
default:
DL((APP,"Unexpected state %d\n", m_state));
};
return 0;
}
int
PipeTest::
handle_close (int fd_)
{
trace("PipeTest::handle_close");
TimerId tid;
if (m_pipe) {
delete m_pipe;
m_pipe = NULL;
m_pinstream = NULL;
}
switch (m_state) {
case test1:
if (m_line_count == m_max_count_expected) {
DL((APP,"Passed test 1\n"));
cout << "Passed test 1\n";
}
else {
DL((APP,"Test 1 failed\n"));
cout << "Test 1 failed\n";
}
tid = REACTOR->registerTimerHandler (this, TimeVal (0.1), "Inter-test");
break;
case test2:
PIPETEST->stop_service ();
break;
};
return 0;
}
void
PipeTest::
process_events ()
{
trace("PipeTest::process_events");
REACTOR->waitForEvents ();
REACTOR->stopReactor ();
}
int
main (int argc, char* argv[])
{
static const char release[] = "$Revision: 1.11 $";
int patch_level = 0;
PIPETEST->set_version (release, patch_level);
PIPETEST->set_author ("Vladislav Grinchenko");
PIPETEST->set_flags (GenServer::RMLOG);
PIPETEST->init (&argc, argv, help_msg);
PIPETEST->init_service ();
PIPETEST->process_events ();
return PIPETEST->get_exit_value ();
}
|