1
我寫了一個高級包裝和命令集合,它使用RProc/RPMsg接口與微處理器進行通信,爲了儘可能多地測試我爲它編寫了單元測試一個Linux僞終端代替'真實'的界面。無法從僞終端讀取
我的測試沒有奏效。我最終簡化了測試,它沒有包含我的代碼痕跡點 - 它仍然不能正常工作:
BOOST_AUTO_TEST_CASE(constructor)
{
const auto master_pty = ::posix_openpt(O_RDWR);
BOOST_REQUIRE(master_pty != -1);
BOOST_REQUIRE(::grantpt(master_pty) != -1);
BOOST_REQUIRE(::unlockpt(master_pty) != -1);
const auto slave_pty_name = ::ptsname(master_pty);
const auto slave_pty = ::open(slave_pty_name, O_RDWR);
BOOST_REQUIRE(slave_pty != -1);
const auto in_data = std::array<std::uint8_t, 4>{1, 2, 3, 4};
const auto bytes_written = ::write(master_pty, in_data.data(), in_data.size());
if (bytes_written < 0) {
BOOST_FAIL("Failed to write: " << errno);
}
BOOST_REQUIRE_EQUAL(in_data.size(), bytes_written);
auto out_data = std::array<std::uint8_t, in_data.size()>{};
const auto bytes_read = ::read(slave_pty, out_data.data(), out_data.size());
BOOST_CHECK_EQUAL(bytes_read, bytes_written);
if (bytes_read < 0) {
BOOST_FAIL("::read failed: " << errno);
}
::close(slave_pty);
::close(master_pty);
}
它的輸出是:
*** 1 failure is detected in the test module "cmPTP Test Suite"
# cmPTP-manager-test -l all --run_test=comms_rproc_interface/constructor
Running 1 test case...
Entering test module "cmPTP Test Suite"
interface_test.cpp(17): Entering test suite "comms_rproc_interface"
interface_test.cpp(19): Entering test case "constructor"
interface_test.cpp(23): info: check master_pty != -1 has passed
interface_test.cpp(24): info: check grantpt(master_pty) != -1 has passed
interface_test.cpp(25): info: check unlockpt(master_pty) != -1 has passed
interface_test.cpp(32): info: check slave_pty != -1 has passed
interface_test.cpp(72): info: check in_data.size() == bytes_written has passed
interface_test.cpp(77): error: in "comms_rproc_interface/constructor": check bytes_read == bytes_written has failed [0 != 4]
interface_test.cpp(19): Leaving test case "constructor"; testing time: 15282us
interface_test.cpp(17): Leaving test suite "comms_rproc_interface"; testing time: 15931us
Leaving test module "cmPTP Test Suite"; testing time: 16879us
*** 1 failure is detected in the test module "cmPTP Test Suite"
我可以寫我的4字節好,但是從來沒有任何東西存在。爲什麼?
這看起來並不像讀出的實際上是失敗了,但它並沒有檢索字節。寫入緩衝區是否已被刷新? – starturtle
@starturtle應該有:「POSIX要求在write()返回後可以證明發生的讀(2)返回新數據。注意並非所有的文件系統都符合POSIX標準。」我當然認爲Linux/EXT4符合POSIX標準......我如何強制刷新? – cmannett85
@starturtle其實它只是在我身上發現PTS是_two_文件描述符,所以上述不適用。 – cmannett85