blob: 80fb9413000621e469d8f4c3eb13a196a6f60b31 [file] [log] [blame]
// Copyright 2023 Google Inc. 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 "stdio_redirection.h"
#include "async_loop.h"
#include "test.h"
TEST(StdioAsyncStringRedirector, Test) {
auto async_loop = AsyncLoop::CreateLocal();
std::string result;
{
StdioAsyncStringRedirector async_stdout(*async_loop, stdout);
// IMPORTANT: Do not include \n in the input because fprintf()
// may or may not translate that into \r\n depending on too many
// factors to handle here.
fprintf(stdout, "Hello!");
EXPECT_TRUE(async_stdout.WaitForResult(&result));
}
EXPECT_EQ(result, "Hello!");
}
TEST(StdioAsyncStringRedirector, TestWithTimeout) {
auto async_loop = AsyncLoop::CreateLocal();
std::string result;
bool ret;
IpcHandle duplicate_stdout_fd;
{
StdioAsyncStringRedirector async_stdout(*async_loop, stdout);
fprintf(stdout, "Hello!");
fflush(stdout);
// Duplicate stdout handle, which will prevent the internal
// pipe to be closed properly by WaitForResult() which will
// timeout after 10ms.
duplicate_stdout_fd = IpcHandle::CloneFromStdio(stdout);
ret = async_stdout.WaitForResult(&result, 10LL);
}
EXPECT_FALSE(ret);
EXPECT_TRUE(duplicate_stdout_fd);
EXPECT_EQ(result, "Hello!");
}