|
| 1 | +// Copyright 2017 The Ray Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "ray/util/pipe.h" |
| 16 | + |
| 17 | +#include <gtest/gtest.h> |
| 18 | + |
| 19 | +#include <string> |
| 20 | + |
| 21 | +#include "absl/strings/str_cat.h" |
| 22 | +#include "ray/util/process.h" |
| 23 | + |
| 24 | +namespace ray { |
| 25 | + |
| 26 | +TEST(PipeTest, ParentReadChildWrite) { |
| 27 | + Pipe pipe; |
| 28 | + intptr_t writer_handle = pipe.MakeWriterHandle(); |
| 29 | + |
| 30 | + std::string python_code = absl::StrCat( |
| 31 | + "import os; " |
| 32 | + "os.write(", |
| 33 | + writer_handle, |
| 34 | + ", b'hello from child'); " |
| 35 | + "os.close(", |
| 36 | + writer_handle, |
| 37 | + ")"); |
| 38 | + |
| 39 | + auto [proc, ec] = Process::Spawn({"python", "-c", python_code}, /*decouple=*/false); |
| 40 | + ASSERT_FALSE(ec) << ec.message(); |
| 41 | + |
| 42 | + pipe.CloseWriterHandle(); |
| 43 | + |
| 44 | + auto result = pipe.Read(/*timeout_s=*/5); |
| 45 | + ASSERT_TRUE(result.ok()) << result.status().message(); |
| 46 | + EXPECT_EQ(*result, "hello from child"); |
| 47 | + |
| 48 | + int exit_code = proc.Wait(); |
| 49 | + EXPECT_EQ(exit_code, 0); |
| 50 | +} |
| 51 | + |
| 52 | +TEST(PipeTest, SiblingProcessesCommunicate) { |
| 53 | + Pipe pipe; |
| 54 | + |
| 55 | + // Spawn writer first |
| 56 | + intptr_t writer_handle = pipe.MakeWriterHandle(); |
| 57 | + std::string writer_code = absl::StrCat( |
| 58 | + "import os; " |
| 59 | + "os.write(", |
| 60 | + writer_handle, |
| 61 | + ", b'hello from sibling'); " |
| 62 | + "os.close(", |
| 63 | + writer_handle, |
| 64 | + ")"); |
| 65 | + |
| 66 | + auto [writer_proc, writer_ec] = |
| 67 | + Process::Spawn({"python", "-c", writer_code}, /*decouple=*/false); |
| 68 | + ASSERT_FALSE(writer_ec) << writer_ec.message(); |
| 69 | + pipe.CloseWriterHandle(); |
| 70 | + |
| 71 | + // Then spawn reader |
| 72 | + intptr_t reader_handle = pipe.MakeReaderHandle(); |
| 73 | + std::string reader_code = absl::StrCat( |
| 74 | + "import os, sys; " |
| 75 | + "data = os.read(", |
| 76 | + reader_handle, |
| 77 | + ", 100); " |
| 78 | + "os.close(", |
| 79 | + reader_handle, |
| 80 | + "); " |
| 81 | + "sys.exit(0 if data == b'hello from sibling' else 1)"); |
| 82 | + |
| 83 | + auto [reader_proc, reader_ec] = |
| 84 | + Process::Spawn({"python", "-c", reader_code}, /*decouple=*/false); |
| 85 | + ASSERT_FALSE(reader_ec) << reader_ec.message(); |
| 86 | + pipe.CloseReaderHandle(); |
| 87 | + |
| 88 | + int writer_exit = writer_proc.Wait(); |
| 89 | + EXPECT_EQ(writer_exit, 0); |
| 90 | + |
| 91 | + int reader_exit = reader_proc.Wait(); |
| 92 | + EXPECT_EQ(reader_exit, 0); |
| 93 | +} |
| 94 | + |
| 95 | +TEST(PipeTest, ReadTimeout) { |
| 96 | + Pipe pipe; |
| 97 | + auto result = pipe.Read(/*timeout_s=*/0.1); |
| 98 | + EXPECT_FALSE(result.ok()); |
| 99 | +} |
| 100 | + |
| 101 | +TEST(PipeTest, OperationsAfterClose) { |
| 102 | + Pipe pipe; |
| 103 | + pipe.Close(); |
| 104 | + |
| 105 | + auto read_result = pipe.Read(); |
| 106 | + EXPECT_FALSE(read_result.ok()); |
| 107 | + |
| 108 | + Pipe pipe2; |
| 109 | + pipe2.Close(); |
| 110 | + auto write_result = pipe2.Write("data"); |
| 111 | + EXPECT_FALSE(write_result.ok()); |
| 112 | +} |
| 113 | + |
| 114 | +} // namespace ray |
0 commit comments