File: timer_queue_test.cpp

package info (click to toggle)
libassa 3.5.1-8.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,424 kB
  • sloc: cpp: 15,703; sh: 12,083; makefile: 379; perl: 51
file content (77 lines) | stat: -rw-r--r-- 2,047 bytes parent folder | download | duplicates (9)
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
// -*- c++ -*-
//------------------------------------------------------------------------------
//                          timer_queue_test.cpp
//------------------------------------------------------------------------------
// $Id: timer_queue_test.cpp,v 1.4 2006/07/20 02:30:56 vlg Exp $
//------------------------------------------------------------------------------
//  Copyright (c) 2002 by Vladislav Grinchenko
//
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version
//  2 of the License, or (at your option) any later version.
//------------------------------------------------------------------------------

#include <iostream>
using namespace std;
#include <assert.h>

#include "assa/Logger.h"
#include "assa/EventHandler.h"
#include "assa/Reactor.h"
#include "assa/CommonUtils.h"
#include "assa/TimerCountdown.h"

using namespace ASSA;

struct T : public EventHandler 
{ 
	int handle_timeout (TimerId /* tid */) { return 0; }
};

int main ()
{
	::unlink ("timer_queue_test.log");
	Log::open_log_file ("timer_queue_test.log");

	std::cout << "= Running timer_queue_test Test =\n";

	TimerQueue tq;
	T eh, eh1;
	TimeVal tv (TimeVal::gettimeofday ());
	TimeVal one_sec  (1.0);
	TimeVal two_secs (2.0);
	TimeVal three_secs (3.);

	TimerId tids[4];

	tids[0] = tq.insert (&eh,  tv,              0, "now");
	tids[1] = tq.insert (&eh,  tv + one_sec,    0, "in 1 sec");
	tids[2] = tq.insert (&eh,  tv + two_secs,   0, "in 2 secs");
	tids[2] = tq.insert (&eh1, tv + three_secs, 0, "in 3 secs");

	tq.dump ();

	assert (tq.isEmpty () == false);
	assert (tq.top () == tv);
	assert (tq.remove (tids[1]));

	tq.dump ();

	int ret = tq.remove (&eh);

	tq.dump ();
	
	assert ( ret == 2);

	ASSA::Utils::sleep_for_seconds (3);
	assert (tq.expire (TimeVal::gettimeofday ()) == 1);
	assert (tq.isEmpty ());

	tq.insert (&eh, tv, 0, "dummy");	// Testing destructor

	std::cout << "Test passed\n";
	return 0;
}