| 1613 | } |
| 1614 | |
| 1615 | void TestShutdownWrite() { |
| 1616 | const size_t REP = 100; |
| 1617 | int fds[2]; |
| 1618 | ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds)); |
| 1619 | |
| 1620 | brpc::SocketId id = 8888; |
| 1621 | butil::EndPoint dummy; |
| 1622 | ASSERT_EQ(0, str2endpoint("192.168.1.26:8080", &dummy)); |
| 1623 | brpc::SocketOptions options; |
| 1624 | options.fd = fds[1]; |
| 1625 | options.remote_side = dummy; |
| 1626 | options.user = new CheckRecycle; |
| 1627 | ASSERT_EQ(0, brpc::Socket::Create(options, &id)); |
| 1628 | brpc::SocketUniquePtr s; |
| 1629 | ASSERT_EQ(0, brpc::Socket::Address(id, &s)); |
| 1630 | s->_ssl_state = brpc::SSL_OFF; |
| 1631 | ASSERT_EQ(2, brpc::NRefOfVRef(s->_versioned_ref)); |
| 1632 | global_sock = s.get(); |
| 1633 | ASSERT_TRUE(s.get()); |
| 1634 | ASSERT_EQ(fds[1], s->fd()); |
| 1635 | ASSERT_EQ(dummy, s->remote_side()); |
| 1636 | ASSERT_EQ(id, s->id()); |
| 1637 | ASSERT_FALSE(s->IsWriteShutdown()); |
| 1638 | |
| 1639 | pthread_t rth; |
| 1640 | ReaderArg reader_arg = { fds[0], 0 }; |
| 1641 | pthread_create(&rth, NULL, reader, &reader_arg); |
| 1642 | |
| 1643 | bthread_t th[3]; |
| 1644 | ShutdownWriterArg args[ARRAY_SIZE(th)]; |
| 1645 | for (size_t i = 0; i < ARRAY_SIZE(th); ++i) { |
| 1646 | args[i].times = REP; |
| 1647 | args[i].socket_id = id; |
| 1648 | args[i].total_count = 0; |
| 1649 | args[i].success_count = 0; |
| 1650 | bthread_start_background(&th[i], NULL, ShutdownWriter, &args[i]); |
| 1651 | } |
| 1652 | |
| 1653 | for (size_t i = 0; i < ARRAY_SIZE(th); ++i) { |
| 1654 | ASSERT_EQ(0, bthread_join(th[i], NULL)); |
| 1655 | } |
| 1656 | bthread_usleep(50 * 1000); |
| 1657 | |
| 1658 | ASSERT_TRUE(s->IsWriteShutdown()); |
| 1659 | ASSERT_FALSE(s->Failed()); |
| 1660 | ASSERT_EQ(0, s->SetFailed()); |
| 1661 | s.release()->Dereference(); |
| 1662 | pthread_join(rth, NULL); |
| 1663 | ASSERT_EQ((brpc::Socket*)NULL, global_sock); |
| 1664 | close(fds[0]); |
| 1665 | |
| 1666 | size_t total_count = 0; |
| 1667 | size_t success_count = 0; |
| 1668 | for (auto & arg : args) { |
| 1669 | total_count += arg.total_count; |
| 1670 | success_count += arg.success_count; |
| 1671 | } |
| 1672 | ASSERT_EQ(REP * ARRAY_SIZE(th), total_count); |
no test coverage detected