FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/core/system/ProcessTests.cpp at master · andschar/rstudio · GitHub
andschar
/
rstudio
Public
forked from
rstudio/rstudio
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
rstudio
/
src
/
cpp
/
core
/
system
/
ProcessTests.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
203 lines (156 loc) · 5.28 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
core
/
system
/
ProcessTests.cpp
Copy path
File metadata and controls
203 lines (156 loc) · 5.28 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* ProcessTests.cpp
*
* Copyright (C) 2017 by RStudio, Inc.
*
* Unless you have received this program directly from RStudio pursuant
* to the terms of a commercial license agreement with RStudio, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
#
ifndef
_WIN32
#
include
<
boost/bind.hpp
>
#
include
<
boost/foreach.hpp
>
#
include
<
boost/thread.hpp
>
#
include
<
core/system/PosixProcess.hpp
>
#
include
<
core/system/PosixChildProcess.hpp
>
#
include
<
core/Thread.hpp
>
#
include
<
tests/TestThat.hpp
>
namespace
rstudio
{
namespace
core
{
namespace
system
{
namespace
tests
{
void
checkExitCode
(
int
exitCode,
int
* outExitCode)
{
*outExitCode = exitCode;
}
void
signalExit
(
int
exitCode,
int
* outExitCode, boost::mutex* mutex, boost::condition_variable* signal)
{
*outExitCode = exitCode;
LOCK_MUTEX
(*mutex)
{
signal->
notify_all
();
}
END_LOCK_MUTEX
}
void
appendOutput
(
const
std::string& output, std::string* pOutput)
{
pOutput->
append
(output);
}
struct
IoServiceFixture
{
boost::asio::io_service ioService;
boost::asio::io_service::work work;
boost::shared_ptr<boost::thread> pThread;
void
runServiceThread
()
{
ioService.
run
();
}
IoServiceFixture
() :
ioService
(), work(ioService),
pThread
(
new
boost::thread(boost::bind(&IoServiceFixture::runServiceThread,
this
)))
{
}
~IoServiceFixture
()
{
ioService.
stop
();
pThread->
join
();
}
};
context
(
"
ProcessTests
"
)
{
test_that
(
"
AsioProcessSupervisor can run program
"
)
{
IoServiceFixture fixture;
//
create new supervisor
AsioProcessSupervisor
supervisor
(fixture.
ioService
);
//
create process options and callbacks
ProcessOptions options;
ProcessCallbacks callbacks;
int
exitCode = -
1
;
callbacks.
onExit
=
boost::bind
(&checkExitCode, _1, &exitCode);
//
construct program arguments
std::vector<std::string> args;
args.
push_back
(
"
Hello, world! This is a string to echo!
"
);
//
run program
supervisor.
runProgram
(
"
/bin/echo
"
, args, options, callbacks);
//
wait for it to exit
bool
success = supervisor.
wait
(
boost::posix_time::seconds
(
5
));
//
verify process exited successfully
CHECK
(success);
CHECK
(exitCode ==
0
);
}
test_that
(
"
AsioProcessSupervisor returns correct output from stdout
"
)
{
IoServiceFixture fixture;
//
create new supervisor
AsioProcessSupervisor
supervisor
(fixture.
ioService
);
//
create process options and callbacks
ProcessOptions options;
ProcessCallbacks callbacks;
int
exitCode = -
1
;
std::string output;
callbacks.
onExit
=
boost::bind
(&checkExitCode, _1, &exitCode);
callbacks.
onStdout
=
boost::bind
(&appendOutput, _2, &output);
//
run command
std::string command =
"
bash -c
\"
python -c $'for i in range(10):
\n
print(i)'
\"
"
;
supervisor.
runCommand
(command, options, callbacks);
//
wait for it to exit
bool
success = supervisor.
wait
(
boost::posix_time::seconds
(
5
));
//
verify process exited successfully and we got the expected output
std::string expectedOutput =
"
0
\n
1
\n
2
\n
3
\n
4
\n
5
\n
6
\n
7
\n
8
\n
9
\n
"
;
CHECK
(success);
CHECK
(exitCode ==
0
);
CHECK
(output == expectedOutput);
}
test_that
(
"
AsioProcessSupervisor returns correct error code for failure exit
"
)
{
IoServiceFixture fixture;
//
create new supervisor
AsioProcessSupervisor
supervisor
(fixture.
ioService
);
//
create process options and callbacks
ProcessOptions options;
ProcessCallbacks callbacks;
int
exitCode = -
1
;
std::string output;
callbacks.
onExit
=
boost::bind
(&checkExitCode, _1, &exitCode);
//
run command
std::string command =
"
this is not a valid command
"
;
supervisor.
runCommand
(command, options, callbacks);
//
wait for it to exit
bool
success = supervisor.
wait
(
boost::posix_time::seconds
(
5
));
CHECK
(success);
CHECK
(exitCode ==
127
);
}
test_that
(
"
AsioAsyncChildProcess can write to std in
"
)
{
IoServiceFixture fixture;
ProcessOptions options;
ProcessCallbacks callbacks;
int
exitCode = -
1
;
std::string output;
boost::condition_variable signal;
boost::mutex mutex;
callbacks.
onExit
=
boost::bind
(&signalExit, _1, &exitCode, &mutex, &signal);
callbacks.
onStdout
=
boost::bind
(&appendOutput, _2, &output);
AsioAsyncChildProcess
proc
(fixture.
ioService
,
"
cat
"
, options);
proc.
run
(callbacks);
proc.
asyncWriteToStdin
(
"
Hello
\n
"
,
false
);
proc.
asyncWriteToStdin
(
"
world!
\n
"
,
true
);
std::string expectedOutput =
"
Hello
\n
world!
\n
"
;
boost::unique_lock<boost::mutex>
lock
(mutex);
bool
timedOut = !signal.
timed_wait
<boost::posix_time::seconds>(lock,
boost::posix_time::seconds
(
5
));
CHECK
(!timedOut);
CHECK
(exitCode ==
0
);
CHECK
(output == expectedOutput);
}
}
}
//
end namespace tests
}
//
end namespace system
}
//
end namespace core
}
//
end namespace rstudio
#
endif
//
!_WIN32
Back
|
FazBrowse Home
|
New Git URL