File: feature.cpp

package info (click to toggle)
restbed 4.0~dfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,388 kB
  • sloc: cpp: 15,273; sh: 37; makefile: 14
file content (80 lines) | stat: -rw-r--r-- 1,902 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright 2013-2016, Corvusoft Ltd, All Rights Reserved.
 */

//System Includes
#include <map>
#include <thread>
#include <string>
#include <memory>
#include <chrono>
#include <ciso646>
#include <csignal>
#include <stdexcept>
#include <functional>

//Project Includes
#include <restbed>

//External Includes
#include <catch.hpp>

//System Namespaces
using std::raise;
using std::thread;
using std::string;
using std::multimap;
using std::shared_ptr;
using std::make_shared;
using std::chrono::seconds;

//Project Namespaces
using namespace restbed;

//External Namespaces

int signal_number_actual = 0;

void signal_handler( const int signal_number )
{
    signal_number_actual = signal_number;
}

SCENARIO( "Handle specific signal", "[service]" )
{
    auto settings = make_shared< Settings >( );
    settings->set_port( 1984 );
    
    shared_ptr< thread > worker = nullptr;
    
    Service service;
    service.set_signal_handler( SIGHUP, signal_handler );
    service.set_ready_handler( [ &worker ]( Service & service )
    {
        worker = make_shared< thread >( [ &service ] ( )
        {
            GIVEN( "I start a service with a 'SIGHUP' signal handler" )
            {
                WHEN( "I generate a 'SIGHUP' event" )
                {
                    THEN( "I should see a 'SIGHUP' signal number" )
                    {
                        signal_number_actual = 0;
                        REQUIRE( signal_number_actual == 0 );
                        
                        raise( SIGHUP );
                        
                        std::this_thread::sleep_for( seconds( 1 ) );
                        
                        REQUIRE( signal_number_actual == SIGHUP );
                    }
                }
                
                service.stop( );
            }
        } );
    } );
    
    service.start( settings );
    worker->join( );
}