summaryrefslogtreecommitdiffstats
path: root/chrome/common/process_watcher_unittest.cc
blob: 9bde1ac6b083e14573ca0b788f7aa2d5af0e405c (plain)
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
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/common/process_watcher.h"

#if defined(OS_POSIX)
#include <sys/wait.h>

#include "base/eintr_wrapper.h"
#include "base/process_util.h"
#include "base/test/multiprocess_test.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/multiprocess_func_list.h"

class ProcessWatcherTest : public base::MultiProcessTest {
};

namespace {

bool IsProcessDead(base::ProcessHandle child) {
  // waitpid() will actually reap the process which is exactly NOT what we
  // want to test for.  The good thing is that if it can't find the process
  // we'll get a nice value for errno which we can test for.
  const pid_t result = HANDLE_EINTR(waitpid(child, NULL, WNOHANG));
  return result == -1 && errno == ECHILD;
}

}  // namespace

TEST_F(ProcessWatcherTest, DelayedTermination) {
  base::ProcessHandle child_process =
      SpawnChild("process_watcher_test_never_die", false);
  ProcessWatcher::EnsureProcessTerminated(child_process);
  base::WaitForSingleProcess(child_process, 5000);

  // Check that process was really killed.
  EXPECT_TRUE(IsProcessDead(child_process));
  base::CloseProcessHandle(child_process);
}

MULTIPROCESS_TEST_MAIN(process_watcher_test_never_die) {
  while (1) {
    sleep(500);
  }
  return 0;
}

TEST_F(ProcessWatcherTest, ImmediateTermination) {
  base::ProcessHandle child_process =
      SpawnChild("process_watcher_test_die_immediately", false);
  // Give it time to die.
  sleep(2);
  ProcessWatcher::EnsureProcessTerminated(child_process);

  // Check that process was really killed.
  EXPECT_TRUE(IsProcessDead(child_process));
  base::CloseProcessHandle(child_process);
}

MULTIPROCESS_TEST_MAIN(process_watcher_test_die_immediately) {
  return 0;
}

#endif  // OS_POSIX