Bug 1874684 - Part 25: Editorial updates. r=dminor
[gecko.git] / mozglue / tests / TestTimeStampWin.cpp
bloba69e2be59f50e68d193407a3d592b9bee3400738
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
7 #include "mozilla/CmdLineAndEnvUtils.h"
8 #include "mozilla/TimeStamp.h"
10 #include "nsWindowsHelpers.h"
12 #include <stdio.h>
13 #include <windows.h>
15 static wchar_t kChildArg[] = L"--child";
17 static nsReturnRef<HANDLE> CreateProcessWrapper(const wchar_t* aPath) {
18 nsAutoHandle empty;
20 const wchar_t* childArgv[] = {aPath, kChildArg};
21 mozilla::UniquePtr<wchar_t[]> cmdLine(
22 mozilla::MakeCommandLine(mozilla::ArrayLength(childArgv), childArgv));
24 STARTUPINFOW si = {sizeof(si)};
25 PROCESS_INFORMATION pi;
26 BOOL ok = ::CreateProcessW(aPath, cmdLine.get(), nullptr, nullptr, FALSE, 0,
27 nullptr, nullptr, &si, &pi);
28 if (!ok) {
29 printf(
30 "TEST-FAILED | TimeStampWin | "
31 "CreateProcess failed - %08lx\n",
32 GetLastError());
33 return empty.out();
36 nsAutoHandle proc(pi.hProcess);
37 nsAutoHandle thd(pi.hThread);
39 return proc.out();
42 int ChildMain() {
43 // Make sure a process creation timestamp is always not bigger than
44 // the current timestamp.
45 auto t0 = mozilla::TimeStamp::ProcessCreation();
46 auto t1 = mozilla::TimeStamp::Now();
47 if (t0 > t1) {
48 printf(
49 "TEST-FAILED | TimeStampWin | "
50 "Process creation timestamp is bigger than the current "
51 "timestamp!\n");
52 return 1;
54 return 0;
57 int wmain(int argc, wchar_t* argv[]) {
58 if (argc == 2 && wcscmp(argv[1], kChildArg) == 0) {
59 return ChildMain();
62 if (argc != 1) {
63 printf(
64 "TEST-FAILED | TimeStampWin | "
65 "Unexpected argc\n");
66 return 1;
69 // Start a child process successively, checking any of them terminates with
70 // a non-zero value which means an error.
71 for (int i = 0; i < 20; ++i) {
72 nsAutoHandle childProc(CreateProcessWrapper(argv[0]));
74 if (::WaitForSingleObject(childProc, 60000) != WAIT_OBJECT_0) {
75 printf(
76 "TEST-FAILED | TimeStampWin | "
77 "Unexpected result from WaitForSingleObject\n");
78 return 1;
81 DWORD childExitCode;
82 if (!::GetExitCodeProcess(childProc.get(), &childExitCode)) {
83 printf(
84 "TEST-FAILED | TimeStampWin | "
85 "GetExitCodeProcess failed - %08lx\n",
86 GetLastError());
87 return 1;
90 if (childExitCode != 0) {
91 return childExitCode;
95 return 0;