root/trunk/whisperlib/common/sync/test/process_test.cc

Revision 7, 6.5 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
1 // Copyright (c) 2009, Whispersoft s.r.l.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Whispersoft s.r.l. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Cosmin Tudorache
31
32 #include <signal.h>
33 #include "common/base/types.h"
34 #include "common/base/log.h"
35 #include "common/base/system.h"
36 #include "common/base/errno.h"
37 #include "common/sync/event.h"
38 #include "common/sync/process.h"
39 #include "common/base/scoped_ptr.h"
40
41 #define LOG_TEST LOG(-1)
42
43 void ProcessTerminatedCallback(int exit_status) {
44   LOG_TEST << "ProcessTerminatedCallback exit_status = " << exit_status;
45 }
46
47 int main(int argc, char** argv) {
48   common::Init(argc, argv);
49
50   const char* path = "/bin/sleep";
51
52   LOG_TEST << "Testing run & wait with timeout.";
53   {
54     scoped_ptr<process::Process> process(new process::Process(
55                                            path, "3", NULL));
56     process->SetExitCallback(NewCallback(&ProcessTerminatedCallback));
57     if ( !process->Start() ) {
58       LOG_FATAL << "failed to start process: "
59                 << GetLastSystemErrorDescription();
60     }
61     LOG_TEST << "You should see child process path=\""
62              << path << "\" , pid=" << process->Pid() << " running.";
63     LOG_TEST << "Close the child process whenever you want.";
64
65     int exit_status;
66     while ( true ) {
67       if ( process->Wait(5000, &exit_status) ) {
68         break;
69       }
70       LOG_TEST << "Timeout... please close the child.";
71     }
72     LOG_TEST << "Child process terminated. exit_status = " << exit_status;
73   }
74   LOG_TEST << "Pass.";
75
76   LOG_TEST << "Testing run & kill on timeout.";
77   {
78     scoped_ptr<process::Process> process(new process::Process(
79                                            path, "10", NULL));
80     process->SetExitCallback(NewCallback(&ProcessTerminatedCallback));
81     if ( !process->Start() ) {
82       LOG_FATAL << "failed to start process: "
83                 << GetLastSystemErrorDescription();
84     }
85     LOG_TEST << "You should see child process path=\""
86              << path << "\" , pid=" << process->Pid() << " running.";
87     LOG_TEST << "Do not kill the child. Waiting 5 seconds.";
88
89     int exit_status;
90     if ( process->Wait(5000, &exit_status) ) {
91       LOG_FATAL << "Child process terminated unexpectedly. exit_status = "
92                << exit_status;
93     }
94
95     LOG_TEST << "Timeout... killing the child.";
96     process->Kill();
97     if ( !process->Wait(5000, &exit_status) ) {
98       LOG_FATAL << "Failed to kill the child.";
99     }
100   }
101   LOG_TEST << "Pass.";
102
103   LOG_TEST << "Testing run & detach & wait with timeout.";
104   {
105     pid_t pid = 0;
106     {
107       scoped_ptr<process::Process> process(new process::Process(
108                                              path, "5", NULL));
109       process->SetExitCallback(NewCallback(&ProcessTerminatedCallback));
110       if ( !process->Start() ) {
111         LOG_FATAL << "failed to start process: "
112                   << GetLastSystemErrorDescription();
113       }
114       pid = process->Pid();
115       LOG_TEST << "You should see child process path=\""
116                << path << "\" , pid=" << process->Pid() << " running.";
117       process->Detach();
118       LOG_TEST << "Detached from child process.";
119     }
120     {
121       scoped_ptr<process::Process> process(new process::Process(pid));
122       process->SetExitCallback(NewCallback(&ProcessTerminatedCallback));
123       if ( !process->Start() ) {
124         LOG_FATAL << "failed to start process: "
125                   << GetLastSystemErrorDescription();
126       }
127       LOG_TEST << "Attached to process pid=" << process->Pid();
128       LOG_TEST << "Close the child process whenever you want.";
129
130       int exit_status;
131       while ( true ) {
132         if ( process->Wait(5000, &exit_status) ) {
133           break;
134         }
135         LOG_TEST << "Timeout... please close the child.";
136       }
137       LOG_TEST << "Child process terminated. exit_status = " << exit_status;
138     }
139   }
140   LOG_TEST << "Pass.";
141
142   LOG_TEST << "Testing run & detach & kill on timeout.";
143   {
144     pid_t pid = 0;
145     {
146       scoped_ptr<process::Process> process(new process::Process(
147                                              path, "20", NULL));
148       process->SetExitCallback(NewCallback(&ProcessTerminatedCallback));
149       if ( !process->Start() ) {
150         LOG_FATAL << "failed to start process: "
151                   << GetLastSystemErrorDescription();
152       }
153       pid = process->Pid();
154       LOG_TEST << "You should see child process path=\""
155                << path << "\" , pid=" << process->Pid() << " running.";
156       process->Detach();
157       LOG_TEST << "Detached from child process.";
158     }
159     {
160       scoped_ptr<process::Process> process(new process::Process(pid));
161       process->SetExitCallback(NewCallback(&ProcessTerminatedCallback));
162       if ( !process->Start() ) {
163         LOG_FATAL << "failed to start process: "
164                   << GetLastSystemErrorDescription();
165       }
166       LOG_TEST << "Attached to process pid=" << process->Pid();
167       LOG_TEST << "Waiting 5 seconds before killing the child.";
168
169       int exit_status;
170       if ( process->Wait(5000, &exit_status) ) {
171         LOG_FATAL << "Child process terminated unexpectedly. exit_status = "
172                  << exit_status;
173       }
174       LOG_TEST << "Timeout... killing the child.";
175       process->Kill();
176     }
177   }
178   LOG_TEST << "Pass.";
179
180   common::Exit(0);
181 }
Note: See TracBrowser for help on using the browser.