| Index: util/mach/scoped_task_suspend_test.cc
|
| diff --git a/util/mach/scoped_task_suspend_test.cc b/util/mach/scoped_task_suspend_test.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..df699271ff712ce7e25888e3111b78287b20bb5c
|
| --- /dev/null
|
| +++ b/util/mach/scoped_task_suspend_test.cc
|
| @@ -0,0 +1,157 @@
|
| +// Copyright 2014 The Crashpad Authors. All rights reserved.
|
| +//
|
| +// Licensed under the Apache License, Version 2.0 (the "License");
|
| +// you may not use this file except in compliance with the License.
|
| +// You may obtain a copy of the License at
|
| +//
|
| +// http://www.apache.org/licenses/LICENSE-2.0
|
| +//
|
| +// Unless required by applicable law or agreed to in writing, software
|
| +// distributed under the License is distributed on an "AS IS" BASIS,
|
| +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| +// See the License for the specific language governing permissions and
|
| +// limitations under the License.
|
| +
|
| +#include "util/mach/scoped_task_suspend.h"
|
| +
|
| +#include <errno.h>
|
| +#include <fcntl.h>
|
| +#include <stdint.h>
|
| +
|
| +#include <algorithm>
|
| +
|
| +#include "base/logging.h"
|
| +#include "gtest/gtest.h"
|
| +#include "util/file/fd_io.h"
|
| +#include "util/misc/clock.h"
|
| +#include "util/posix/scoped_fcntl_flags.h"
|
| +#include "util/test/errors.h"
|
| +#include "util/test/mac/mach_multiprocess.h"
|
| +
|
| +namespace crashpad {
|
| +namespace test {
|
| +namespace {
|
| +
|
| +// Reads a byte from |fd|. Returns |true| if a byte could be read, and |false|
|
| +// otherwise. If |fd| is a file descriptor set to O_NONBLOCK mode, this will
|
| +// return |false| immediately if no more data is available to be read.
|
| +// Otherwise, this will only return false when |fd| is at end-of-file.
|
| +// Terminates execution if an error condition is detected.
|
| +bool ReadByte(int fd) {
|
| + char c;
|
| + ssize_t rv = ReadFD(fd, &c, sizeof(c));
|
| + if (rv < 0 && errno == EAGAIN) {
|
| + rv = 0;
|
| + }
|
| + PCHECK(rv >= 0) << "read";
|
| + CHECK(rv <= static_cast<ssize_t>(sizeof(c))) << "read";
|
| +
|
| + return rv > 0;
|
| +}
|
| +
|
| +// Reads all data from |fd| until there is no more data to be read. If |fd| is
|
| +// a file descriptor set to O_NONBLOCK mode, this will return as soon as no more
|
| +// data is available to be read. Otherwise, this will not return until |fd| is
|
| +// at end-of-file.
|
| +void DrainPipe(int fd) {
|
| + while (ReadByte(fd)) {
|
| + }
|
| +}
|
| +
|
| +class ScopedTaskSuspendTest final : public MachMultiprocess {
|
| + public:
|
| + ScopedTaskSuspendTest() : MachMultiprocess() {}
|
| + ~ScopedTaskSuspendTest() {}
|
| +
|
| + private:
|
| + // MachMultiprocess:
|
| +
|
| + virtual void MachMultiprocessParent() override {
|
| + int read_fd = ReadPipeFD();
|
| +
|
| + // Make sure that the child is running. This is a blocking read, because the
|
| + // parent should wait for the child to start sending data.
|
| + char c;
|
| + CheckedReadFD(read_fd, &c, sizeof(c));
|
| +
|
| + {
|
| + // Suspend the child process.
|
| + ScopedTaskSuspend suspend(ChildTask());
|
| +
|
| + // Make the read pipe non-blocking so that it can be drained and tested
|
| + // for EOF.
|
| + ScopedFcntlFlags fcntl_flags(read_fd, O_NONBLOCK, 0);
|
| +
|
| + DrainPipe(read_fd);
|
| +
|
| + // This extra sleep and drain shouldn’t be necessary, but there seem to
|
| + // be scheduling bugs or delays in data appearing on the pipe.
|
| + SleepNanoseconds(1E5); // 100 microseconds
|
| + DrainPipe(read_fd);
|
| +
|
| + EXPECT_FALSE(ReadByte(read_fd));
|
| +
|
| + // Sleep for a little while, giving the child process a chance to measure
|
| + // that one of its loop iterations took a long time.
|
| + SleepNanoseconds(1E7); // 10 milliseconds
|
| +
|
| + // Make sure that the child hasn’t sent anything else while it was
|
| + // supposed to be suspended.
|
| + EXPECT_FALSE(ReadByte(read_fd));
|
| + }
|
| +
|
| + // Make sure that the child has resumed. This is a blocking read, because
|
| + // the parent should wait for the child to resume sending data.
|
| + CheckedReadFD(read_fd, &c, sizeof(c));
|
| +
|
| + // Tell the child that it’s permitted to leave its loop.
|
| + CheckedWriteFD(WritePipeFD(), &c, sizeof(c));
|
| +
|
| + // This is a blocking drain, which waits for read_fd to reach EOF.
|
| + DrainPipe(read_fd);
|
| + CheckedReadFDAtEOF(read_fd);
|
| + }
|
| +
|
| + virtual void MachMultiprocessChild() override {
|
| + int read_fd = ReadPipeFD();
|
| + int write_fd = WritePipeFD();
|
| +
|
| + // Make the read pipe non-blocking so that it can be monitored to read the
|
| + // byte that will cause the loop to terminate.
|
| + ScopedFcntlFlags fcntl_flags(read_fd, O_NONBLOCK, 0);
|
| +
|
| + // Loop for a while, computing the longest duration for an iteration of the
|
| + // loop. Inside each loop, send a byte to the parent process via the pipe,
|
| + // so that the parent will be able to tell when the child is running and
|
| + // when it is suspended.
|
| + uint64_t now = ClockMonotonicNanoseconds();
|
| + uint64_t last = now;
|
| + uint64_t max_delay = 0;
|
| + do {
|
| + char c = '\0';
|
| + CheckedWriteFD(write_fd, &c, sizeof(c));
|
| +
|
| + uint64_t delay = now - last;
|
| + max_delay = std::max(max_delay, delay);
|
| + last = now;
|
| + now = ClockMonotonicNanoseconds();
|
| + } while (!ReadByte(read_fd));
|
| +
|
| + // The longest iteration of the loop should be at least as long as the time
|
| + // that the parent process had this process suspended, but because of the
|
| + // tricky nature of scheduling, just make sure that the longest iteration
|
| + // took a perceptible amount of time.
|
| + EXPECT_GE(max_delay, 1E4); // 10 microseconds
|
| + }
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(ScopedTaskSuspendTest);
|
| +};
|
| +
|
| +TEST(ScopedTaskSuspend, ScopedTaskSuspend) {
|
| + ScopedTaskSuspendTest scoped_task_suspend_test;
|
| + scoped_task_suspend_test.Run();
|
| +}
|
| +
|
| +} // namespace
|
| +} // namespace test
|
| +} // namespace crashpad
|
|
|