bthread_fd_unittest.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. #include "butil/compat.h"
  18. #include <sys/types.h>
  19. #include <sys/socket.h>
  20. #include <sys/utsname.h> // uname
  21. #include <fcntl.h>
  22. #include <gtest/gtest.h>
  23. #include <pthread.h>
  24. #include "butil/gperftools_profiler.h"
  25. #include "butil/time.h"
  26. #include "butil/macros.h"
  27. #include "butil/fd_utility.h"
  28. #include "butil/logging.h"
  29. #include "bthread/task_control.h"
  30. #include "bthread/task_group.h"
  31. #include "bthread/interrupt_pthread.h"
  32. #include "bthread/bthread.h"
  33. #include "bthread/unstable.h"
  34. #if defined(OS_MACOSX)
  35. #include <sys/types.h> // struct kevent
  36. #include <sys/event.h> // kevent(), kqueue()
  37. #endif
  38. #ifndef NDEBUG
  39. namespace bthread {
  40. extern butil::atomic<int> break_nums;
  41. extern TaskControl* global_task_control;
  42. int stop_and_join_epoll_threads();
  43. }
  44. #endif
  45. namespace {
  46. TEST(FDTest, read_kernel_version) {
  47. utsname name;
  48. uname(&name);
  49. std::cout << "sysname=" << name.sysname << std::endl
  50. << "nodename=" << name.nodename << std::endl
  51. << "release=" << name.release << std::endl
  52. << "version=" << name.version << std::endl
  53. << "machine=" << name.machine << std::endl;
  54. }
  55. #define RUN_CLIENT_IN_BTHREAD 1
  56. //#define USE_BLOCKING_EPOLL 1
  57. //#define RUN_EPOLL_IN_BTHREAD 1
  58. //#define CREATE_THREAD_TO_PROCESS 1
  59. volatile bool stop = false;
  60. struct SocketMeta {
  61. int fd;
  62. int epfd;
  63. };
  64. struct BAIDU_CACHELINE_ALIGNMENT ClientMeta {
  65. int fd;
  66. size_t count;
  67. size_t times;
  68. };
  69. struct EpollMeta {
  70. int epfd;
  71. };
  72. const size_t NCLIENT = 30;
  73. void* process_thread(void* arg) {
  74. SocketMeta* m = (SocketMeta*)arg;
  75. size_t count;
  76. //printf("begin to process fd=%d\n", m->fd);
  77. ssize_t n = read(m->fd, &count, sizeof(count));
  78. if (n != sizeof(count)) {
  79. LOG(FATAL) << "Should not happen in this test";
  80. return NULL;
  81. }
  82. count += NCLIENT;
  83. //printf("write result=%lu to fd=%d\n", count, m->fd);
  84. if (write(m->fd, &count, sizeof(count)) != sizeof(count)) {
  85. LOG(FATAL) << "Should not happen in this test";
  86. return NULL;
  87. }
  88. #ifdef CREATE_THREAD_TO_PROCESS
  89. # if defined(OS_LINUX)
  90. epoll_event evt = { EPOLLIN | EPOLLONESHOT, { m } };
  91. if (epoll_ctl(m->epfd, EPOLL_CTL_MOD, m->fd, &evt) < 0) {
  92. epoll_ctl(m->epfd, EPOLL_CTL_ADD, m->fd, &evt);
  93. }
  94. # elif defined(OS_MACOSX)
  95. struct kevent kqueue_event;
  96. EV_SET(&kqueue_event, m->fd, EVFILT_READ, EV_ADD | EV_ENABLE | EV_ONESHOT,
  97. 0, 0, m);
  98. kevent(m->epfd, &kqueue_event, 1, NULL, 0, NULL);
  99. # endif
  100. #endif
  101. return NULL;
  102. }
  103. void* epoll_thread(void* arg) {
  104. bthread_usleep(1);
  105. EpollMeta* m = (EpollMeta*)arg;
  106. const int epfd = m->epfd;
  107. #if defined(OS_LINUX)
  108. epoll_event e[32];
  109. #elif defined(OS_MACOSX)
  110. struct kevent e[32];
  111. #endif
  112. while (!stop) {
  113. #if defined(OS_LINUX)
  114. # ifndef USE_BLOCKING_EPOLL
  115. const int n = epoll_wait(epfd, e, ARRAY_SIZE(e), 0);
  116. if (stop) {
  117. break;
  118. }
  119. if (n == 0) {
  120. bthread_fd_wait(epfd, EPOLLIN);
  121. continue;
  122. }
  123. # else
  124. const int n = epoll_wait(epfd, e, ARRAY_SIZE(e), -1);
  125. if (stop) {
  126. break;
  127. }
  128. if (n == 0) {
  129. continue;
  130. }
  131. # endif
  132. #elif defined(OS_MACOSX)
  133. const int n = kevent(epfd, NULL, 0, e, ARRAY_SIZE(e), NULL);
  134. if (stop) {
  135. break;
  136. }
  137. if (n == 0) {
  138. continue;
  139. }
  140. #endif
  141. if (n < 0) {
  142. if (EINTR == errno) {
  143. continue;
  144. }
  145. #if defined(OS_LINUX)
  146. PLOG(FATAL) << "Fail to epoll_wait";
  147. #elif defined(OS_MACOSX)
  148. PLOG(FATAL) << "Fail to kevent";
  149. #endif
  150. break;
  151. }
  152. #ifdef CREATE_THREAD_TO_PROCESS
  153. bthread_fvec vec[n];
  154. for (int i = 0; i < n; ++i) {
  155. vec[i].fn = process_thread;
  156. # if defined(OS_LINUX)
  157. vec[i].arg = e[i].data.ptr;
  158. # elif defined(OS_MACOSX)
  159. vec[i].arg = e[i].udata;
  160. # endif
  161. }
  162. bthread_t tid[n];
  163. bthread_startv(tid, vec, n, &BTHREAD_ATTR_SMALL);
  164. #else
  165. for (int i = 0; i < n; ++i) {
  166. # if defined(OS_LINUX)
  167. process_thread(e[i].data.ptr);
  168. # elif defined(OS_MACOSX)
  169. process_thread(e[i].udata);
  170. # endif
  171. }
  172. #endif
  173. }
  174. return NULL;
  175. }
  176. void* client_thread(void* arg) {
  177. ClientMeta* m = (ClientMeta*)arg;
  178. for (size_t i = 0; i < m->times; ++i) {
  179. if (write(m->fd, &m->count, sizeof(m->count)) != sizeof(m->count)) {
  180. LOG(FATAL) << "Should not happen in this test";
  181. return NULL;
  182. }
  183. #ifdef RUN_CLIENT_IN_BTHREAD
  184. ssize_t rc;
  185. do {
  186. # if defined(OS_LINUX)
  187. const int wait_rc = bthread_fd_wait(m->fd, EPOLLIN);
  188. # elif defined(OS_MACOSX)
  189. const int wait_rc = bthread_fd_wait(m->fd, EVFILT_READ);
  190. # endif
  191. EXPECT_EQ(0, wait_rc) << berror();
  192. rc = read(m->fd, &m->count, sizeof(m->count));
  193. } while (rc < 0 && errno == EAGAIN);
  194. #else
  195. ssize_t rc = read(m->fd, &m->count, sizeof(m->count));
  196. #endif
  197. if (rc != sizeof(m->count)) {
  198. PLOG(FATAL) << "Should not happen in this test, rc=" << rc;
  199. return NULL;
  200. }
  201. }
  202. return NULL;
  203. }
  204. inline uint32_t fmix32 ( uint32_t h ) {
  205. h ^= h >> 16;
  206. h *= 0x85ebca6b;
  207. h ^= h >> 13;
  208. h *= 0xc2b2ae35;
  209. h ^= h >> 16;
  210. return h;
  211. }
  212. // Disable temporarily due to epoll's bug. The bug is fixed by
  213. // a kernel patch that lots of machines currently don't have
  214. TEST(FDTest, ping_pong) {
  215. #ifndef NDEBUG
  216. bthread::break_nums = 0;
  217. #endif
  218. const size_t REP = 30000;
  219. const size_t NEPOLL = 2;
  220. int epfd[NEPOLL];
  221. #ifdef RUN_EPOLL_IN_BTHREAD
  222. bthread_t eth[NEPOLL];
  223. #else
  224. pthread_t eth[NEPOLL];
  225. #endif
  226. int fds[2 * NCLIENT];
  227. #ifdef RUN_CLIENT_IN_BTHREAD
  228. bthread_t cth[NCLIENT];
  229. #else
  230. pthread_t cth[NCLIENT];
  231. #endif
  232. ClientMeta* cm[NCLIENT];
  233. for (size_t i = 0; i < NEPOLL; ++i) {
  234. #if defined(OS_LINUX)
  235. epfd[i] = epoll_create(1024);
  236. #elif defined(OS_MACOSX)
  237. epfd[i] = kqueue();
  238. #endif
  239. ASSERT_GT(epfd[i], 0);
  240. }
  241. for (size_t i = 0; i < NCLIENT; ++i) {
  242. ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds + 2 * i));
  243. //printf("Created fd=%d,%d i=%lu\n", fds[2*i], fds[2*i+1], i);
  244. SocketMeta* m = new SocketMeta;
  245. m->fd = fds[i * 2];
  246. m->epfd = epfd[fmix32(i) % NEPOLL];
  247. ASSERT_EQ(0, fcntl(m->fd, F_SETFL, fcntl(m->fd, F_GETFL, 0) | O_NONBLOCK));
  248. #ifdef CREATE_THREAD_TO_PROCESS
  249. # if defined(OS_LINUX)
  250. epoll_event evt = { EPOLLIN | EPOLLONESHOT, { m } };
  251. # elif defined(OS_MACOSX)
  252. struct kevent kqueue_event;
  253. EV_SET(&kqueue_event, m->fd, EVFILT_READ, EV_ADD | EV_ENABLE | EV_ONESHOT,
  254. 0, 0, m);
  255. # endif
  256. #else
  257. # if defined(OS_LINUX)
  258. epoll_event evt = { EPOLLIN, { m } };
  259. # elif defined(OS_MACOSX)
  260. struct kevent kqueue_event;
  261. EV_SET(&kqueue_event, m->fd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, m);
  262. # endif
  263. #endif
  264. #if defined(OS_LINUX)
  265. ASSERT_EQ(0, epoll_ctl(m->epfd, EPOLL_CTL_ADD, m->fd, &evt));
  266. #elif defined(OS_MACOSX)
  267. ASSERT_EQ(0, kevent(m->epfd, &kqueue_event, 1, NULL, 0, NULL));
  268. #endif
  269. cm[i] = new ClientMeta;
  270. cm[i]->fd = fds[i * 2 + 1];
  271. cm[i]->count = i;
  272. cm[i]->times = REP;
  273. #ifdef RUN_CLIENT_IN_BTHREAD
  274. butil::make_non_blocking(cm[i]->fd);
  275. ASSERT_EQ(0, bthread_start_urgent(&cth[i], NULL, client_thread, cm[i]));
  276. #else
  277. ASSERT_EQ(0, pthread_create(&cth[i], NULL, client_thread, cm[i]));
  278. #endif
  279. }
  280. ProfilerStart("ping_pong.prof");
  281. butil::Timer tm;
  282. tm.start();
  283. for (size_t i = 0; i < NEPOLL; ++i) {
  284. EpollMeta *em = new EpollMeta;
  285. em->epfd = epfd[i];
  286. #ifdef RUN_EPOLL_IN_BTHREAD
  287. ASSERT_EQ(0, bthread_start_urgent(&eth[i], epoll_thread, em, NULL);
  288. #else
  289. ASSERT_EQ(0, pthread_create(&eth[i], NULL, epoll_thread, em));
  290. #endif
  291. }
  292. for (size_t i = 0; i < NCLIENT; ++i) {
  293. #ifdef RUN_CLIENT_IN_BTHREAD
  294. bthread_join(cth[i], NULL);
  295. #else
  296. pthread_join(cth[i], NULL);
  297. #endif
  298. ASSERT_EQ(i + REP * NCLIENT, cm[i]->count);
  299. }
  300. tm.stop();
  301. ProfilerStop();
  302. LOG(INFO) << "tid=" << REP*NCLIENT*1000000L/tm.u_elapsed();
  303. stop = true;
  304. for (size_t i = 0; i < NEPOLL; ++i) {
  305. #if defined(OS_LINUX)
  306. epoll_event evt = { EPOLLOUT, { NULL } };
  307. ASSERT_EQ(0, epoll_ctl(epfd[i], EPOLL_CTL_ADD, 0, &evt));
  308. #elif defined(OS_MACOSX)
  309. struct kevent kqueue_event;
  310. EV_SET(&kqueue_event, 0, EVFILT_WRITE, EV_ADD | EV_ENABLE, 0, 0, NULL);
  311. ASSERT_EQ(0, kevent(epfd[i], &kqueue_event, 1, NULL, 0, NULL));
  312. #endif
  313. #ifdef RUN_EPOLL_IN_BTHREAD
  314. bthread_join(eth[i], NULL);
  315. #else
  316. pthread_join(eth[i], NULL);
  317. #endif
  318. }
  319. //bthread::stop_and_join_epoll_threads();
  320. bthread_usleep(100000);
  321. #ifndef NDEBUG
  322. std::cout << "break_nums=" << bthread::break_nums << std::endl;
  323. #endif
  324. }
  325. TEST(FDTest, mod_closed_fd) {
  326. #if defined(OS_LINUX)
  327. // Conclusion:
  328. // If fd is never added into epoll, MOD returns ENOENT
  329. // If fd is inside epoll and valid, MOD returns 0
  330. // If fd is closed and not-reused, MOD returns EBADF
  331. // If fd is closed and reused, MOD returns ENOENT again
  332. const int epfd = epoll_create(1024);
  333. int new_fd[2];
  334. int fd[2];
  335. ASSERT_EQ(0, pipe(fd));
  336. epoll_event e = { EPOLLIN, { NULL } };
  337. errno = 0;
  338. ASSERT_EQ(-1, epoll_ctl(epfd, EPOLL_CTL_MOD, fd[0], &e));
  339. ASSERT_EQ(ENOENT, errno);
  340. ASSERT_EQ(0, epoll_ctl(epfd, EPOLL_CTL_ADD, fd[0], &e));
  341. // mod after add
  342. ASSERT_EQ(0, epoll_ctl(epfd, EPOLL_CTL_MOD, fd[0], &e));
  343. // mod after mod
  344. ASSERT_EQ(0, epoll_ctl(epfd, EPOLL_CTL_MOD, fd[0], &e));
  345. ASSERT_EQ(0, close(fd[0]));
  346. ASSERT_EQ(0, close(fd[1]));
  347. errno = 0;
  348. ASSERT_EQ(-1, epoll_ctl(epfd, EPOLL_CTL_MOD, fd[0], &e));
  349. ASSERT_EQ(EBADF, errno) << berror();
  350. ASSERT_EQ(0, pipe(new_fd));
  351. ASSERT_EQ(fd[0], new_fd[0]);
  352. ASSERT_EQ(fd[1], new_fd[1]);
  353. errno = 0;
  354. ASSERT_EQ(-1, epoll_ctl(epfd, EPOLL_CTL_MOD, fd[0], &e));
  355. ASSERT_EQ(ENOENT, errno) << berror();
  356. ASSERT_EQ(0, close(epfd));
  357. #endif
  358. }
  359. TEST(FDTest, add_existing_fd) {
  360. #if defined(OS_LINUX)
  361. const int epfd = epoll_create(1024);
  362. epoll_event e = { EPOLLIN, { NULL } };
  363. ASSERT_EQ(0, epoll_ctl(epfd, EPOLL_CTL_ADD, 0, &e));
  364. errno = 0;
  365. ASSERT_EQ(-1, epoll_ctl(epfd, EPOLL_CTL_ADD, 0, &e));
  366. ASSERT_EQ(EEXIST, errno);
  367. ASSERT_EQ(0, close(epfd));
  368. #endif
  369. }
  370. void* epoll_waiter(void* arg) {
  371. #if defined(OS_LINUX)
  372. epoll_event e;
  373. if (1 == epoll_wait((int)(intptr_t)arg, &e, 1, -1)) {
  374. std::cout << e.events << std::endl;
  375. }
  376. #elif defined(OS_MACOSX)
  377. struct kevent e;
  378. if (1 == kevent((int)(intptr_t)arg, NULL, 0, &e, 1, NULL)) {
  379. std::cout << e.flags << std::endl;
  380. }
  381. #endif
  382. std::cout << pthread_self() << " quits" << std::endl;
  383. return NULL;
  384. }
  385. TEST(FDTest, interrupt_pthread) {
  386. #if defined(OS_LINUX)
  387. const int epfd = epoll_create(1024);
  388. #elif defined(OS_MACOSX)
  389. const int epfd = kqueue();
  390. #endif
  391. pthread_t th, th2;
  392. ASSERT_EQ(0, pthread_create(&th, NULL, epoll_waiter, (void*)(intptr_t)epfd));
  393. ASSERT_EQ(0, pthread_create(&th2, NULL, epoll_waiter, (void*)(intptr_t)epfd));
  394. bthread_usleep(100000L);
  395. std::cout << "wake up " << th << std::endl;
  396. bthread::interrupt_pthread(th);
  397. bthread_usleep(100000L);
  398. std::cout << "wake up " << th2 << std::endl;
  399. bthread::interrupt_pthread(th2);
  400. pthread_join(th, NULL);
  401. pthread_join(th2, NULL);
  402. }
  403. void* close_the_fd(void* arg) {
  404. bthread_usleep(10000/*10ms*/);
  405. EXPECT_EQ(0, bthread_close(*(int*)arg));
  406. return NULL;
  407. }
  408. TEST(FDTest, invalid_epoll_events) {
  409. errno = 0;
  410. #if defined(OS_LINUX)
  411. ASSERT_EQ(-1, bthread_fd_wait(-1, EPOLLIN));
  412. #elif defined(OS_MACOSX)
  413. ASSERT_EQ(-1, bthread_fd_wait(-1, EVFILT_READ));
  414. #endif
  415. ASSERT_EQ(EINVAL, errno);
  416. errno = 0;
  417. #if defined(OS_LINUX)
  418. ASSERT_EQ(-1, bthread_fd_timedwait(-1, EPOLLIN, NULL));
  419. #elif defined(OS_MACOSX)
  420. ASSERT_EQ(-1, bthread_fd_timedwait(-1, EVFILT_READ, NULL));
  421. #endif
  422. ASSERT_EQ(EINVAL, errno);
  423. int fds[2];
  424. ASSERT_EQ(0, pipe(fds));
  425. #if defined(OS_LINUX)
  426. ASSERT_EQ(-1, bthread_fd_wait(fds[0], EPOLLET));
  427. ASSERT_EQ(EINVAL, errno);
  428. #endif
  429. bthread_t th;
  430. ASSERT_EQ(0, bthread_start_urgent(&th, NULL, close_the_fd, &fds[1]));
  431. butil::Timer tm;
  432. tm.start();
  433. #if defined(OS_LINUX)
  434. ASSERT_EQ(0, bthread_fd_wait(fds[0], EPOLLIN | EPOLLET));
  435. #elif defined(OS_MACOSX)
  436. ASSERT_EQ(0, bthread_fd_wait(fds[0], EVFILT_READ));
  437. #endif
  438. tm.stop();
  439. ASSERT_LT(tm.m_elapsed(), 20);
  440. ASSERT_EQ(0, bthread_join(th, NULL));
  441. ASSERT_EQ(0, bthread_close(fds[0]));
  442. }
  443. void* wait_for_the_fd(void* arg) {
  444. timespec ts = butil::milliseconds_from_now(50);
  445. #if defined(OS_LINUX)
  446. bthread_fd_timedwait(*(int*)arg, EPOLLIN, &ts);
  447. #elif defined(OS_MACOSX)
  448. bthread_fd_timedwait(*(int*)arg, EVFILT_READ, &ts);
  449. #endif
  450. return NULL;
  451. }
  452. TEST(FDTest, timeout) {
  453. int fds[2];
  454. ASSERT_EQ(0, pipe(fds));
  455. pthread_t th;
  456. ASSERT_EQ(0, pthread_create(&th, NULL, wait_for_the_fd, &fds[0]));
  457. bthread_t bth;
  458. ASSERT_EQ(0, bthread_start_urgent(&bth, NULL, wait_for_the_fd, &fds[0]));
  459. butil::Timer tm;
  460. tm.start();
  461. ASSERT_EQ(0, pthread_join(th, NULL));
  462. ASSERT_EQ(0, bthread_join(bth, NULL));
  463. tm.stop();
  464. ASSERT_LT(tm.m_elapsed(), 80);
  465. ASSERT_EQ(0, bthread_close(fds[0]));
  466. ASSERT_EQ(0, bthread_close(fds[1]));
  467. }
  468. TEST(FDTest, close_should_wakeup_waiter) {
  469. int fds[2];
  470. ASSERT_EQ(0, pipe(fds));
  471. bthread_t bth;
  472. ASSERT_EQ(0, bthread_start_urgent(&bth, NULL, wait_for_the_fd, &fds[0]));
  473. butil::Timer tm;
  474. tm.start();
  475. ASSERT_EQ(0, bthread_close(fds[0]));
  476. ASSERT_EQ(0, bthread_join(bth, NULL));
  477. tm.stop();
  478. ASSERT_LT(tm.m_elapsed(), 5);
  479. // Launch again, should quit soon due to EBADF
  480. #if defined(OS_LINUX)
  481. ASSERT_EQ(-1, bthread_fd_timedwait(fds[0], EPOLLIN, NULL));
  482. #elif defined(OS_MACOSX)
  483. ASSERT_EQ(-1, bthread_fd_timedwait(fds[0], EVFILT_READ, NULL));
  484. #endif
  485. ASSERT_EQ(EBADF, errno);
  486. ASSERT_EQ(0, bthread_close(fds[1]));
  487. }
  488. TEST(FDTest, close_definitely_invalid) {
  489. int ec = 0;
  490. ASSERT_EQ(-1, close(-1));
  491. ec = errno;
  492. ASSERT_EQ(-1, bthread_close(-1));
  493. ASSERT_EQ(ec, errno);
  494. }
  495. TEST(FDTest, bthread_close_fd_which_did_not_call_bthread_functions) {
  496. int fds[2];
  497. ASSERT_EQ(0, pipe(fds));
  498. ASSERT_EQ(0, bthread_close(fds[0]));
  499. ASSERT_EQ(0, bthread_close(fds[1]));
  500. }
  501. TEST(FDTest, double_close) {
  502. int fds[2];
  503. ASSERT_EQ(0, pipe(fds));
  504. ASSERT_EQ(0, close(fds[0]));
  505. int ec = 0;
  506. ASSERT_EQ(-1, close(fds[0]));
  507. ec = errno;
  508. ASSERT_EQ(0, bthread_close(fds[1]));
  509. ASSERT_EQ(-1, bthread_close(fds[1]));
  510. ASSERT_EQ(ec, errno);
  511. }
  512. } // namespace