OSDN Git Service

mkuserimg.sh: set default size according to the $SRC_DIR size
[android-x86/system-extras.git] / simpleperf / workload_test.cpp
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <gtest/gtest.h>
18
19 #include <signal.h>
20
21 #include "scoped_signal_handler.h"
22 #include "utils.h"
23 #include "workload.h"
24
25 static volatile bool signaled;
26 static void signal_handler(int) {
27   signaled = true;
28 }
29
30 TEST(workload, success) {
31   signaled = false;
32   ScopedSignalHandler scoped_signal_handler({SIGCHLD}, signal_handler);
33   auto workload = Workload::CreateWorkload({"sleep", "1"});
34   ASSERT_TRUE(workload != nullptr);
35   ASSERT_TRUE(workload->GetPid() != 0);
36   ASSERT_TRUE(workload->Start());
37   while (!signaled) {
38   }
39 }
40
41 TEST(workload, execvp_failure) {
42   auto workload = Workload::CreateWorkload({"/dev/null"});
43   ASSERT_TRUE(workload != nullptr);
44   ASSERT_FALSE(workload->Start());
45 }
46
47 static void run_signaled_workload() {
48   {
49     signaled = false;
50     ScopedSignalHandler scoped_signal_handler({SIGCHLD}, signal_handler);
51     auto workload = Workload::CreateWorkload({"sleep", "10"});
52     ASSERT_TRUE(workload != nullptr);
53     ASSERT_TRUE(workload->Start());
54     ASSERT_EQ(0, kill(workload->GetPid(), SIGKILL));
55     while (!signaled) {
56     }
57   }
58   // Make sure all destructors are called before exit().
59   exit(0);
60 }
61
62 TEST(workload, signaled_warning) {
63   ASSERT_EXIT(run_signaled_workload(), testing::ExitedWithCode(0),
64               "child process was terminated by signal");
65 }
66
67 static void run_exit_nonzero_workload() {
68   {
69     signaled = false;
70     ScopedSignalHandler scoped_signal_handler({SIGCHLD}, signal_handler);
71     auto workload = Workload::CreateWorkload({"ls", "nonexistdir"});
72     ASSERT_TRUE(workload != nullptr);
73     ASSERT_TRUE(workload->Start());
74     while (!signaled) {
75     }
76   }
77   // Make sure all destructors are called before exit().
78   exit(0);
79 }
80
81 TEST(workload, exit_nonzero_warning) {
82   ASSERT_EXIT(run_exit_nonzero_workload(), testing::ExitedWithCode(0),
83               "child process exited with exit code");
84 }