OSDN Git Service

am ab7f3975: am 58cd4204: am c7d19e4d: Adding 64 bit emmc_rand_perf
[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 "utils.h"
22 #include "workload.h"
23
24 static volatile bool signaled;
25 static void signal_handler(int) {
26   signaled = true;
27 }
28
29 TEST(workload, success) {
30   signaled = false;
31   SignalHandlerRegister signal_handler_register({SIGCHLD}, signal_handler);
32   auto workload = Workload::CreateWorkload({"sleep", "1"});
33   ASSERT_TRUE(workload != nullptr);
34   ASSERT_TRUE(workload->GetPid() != 0);
35   ASSERT_TRUE(workload->Start());
36   while (!signaled) {
37   }
38 }
39
40 TEST(workload, execvp_failure) {
41   auto workload = Workload::CreateWorkload({"/dev/null"});
42   ASSERT_TRUE(workload != nullptr);
43   ASSERT_FALSE(workload->Start());
44 }
45
46 static void run_signaled_workload() {
47   {
48     signaled = false;
49     SignalHandlerRegister signal_handler_register({SIGCHLD}, signal_handler);
50     auto workload = Workload::CreateWorkload({"sleep", "10"});
51     ASSERT_TRUE(workload != nullptr);
52     ASSERT_TRUE(workload->Start());
53     ASSERT_EQ(0, kill(workload->GetPid(), SIGABRT));
54     while (!signaled) {
55     }
56   }
57   // Make sure all destructors are called before exit().
58   exit(0);
59 }
60
61 TEST(workload, signaled_warning) {
62   ASSERT_EXIT(run_signaled_workload(), testing::ExitedWithCode(0),
63               "child process was terminated by signal");
64 }
65
66 static void run_exit_nonzero_workload() {
67   {
68     signaled = false;
69     SignalHandlerRegister signal_handler_register({SIGCHLD}, signal_handler);
70     auto workload = Workload::CreateWorkload({"ls", "nonexistdir"});
71     ASSERT_TRUE(workload != nullptr);
72     ASSERT_TRUE(workload->Start());
73     while (!signaled) {
74     }
75   }
76   // Make sure all destructors are called before exit().
77   exit(0);
78 }
79
80 TEST(workload, exit_nonzero_warning) {
81   ASSERT_EXIT(run_exit_nonzero_workload(), testing::ExitedWithCode(0),
82               "child process exited with exit code");
83 }