bthread_list_unittest.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 <gtest/gtest.h>
  18. #include "butil/time.h"
  19. #include "butil/macros.h"
  20. #include "butil/logging.h"
  21. #include "bthread/task_group.h"
  22. #include "bthread/bthread.h"
  23. namespace {
  24. void* sleeper(void* arg) {
  25. bthread_usleep((long)arg);
  26. return NULL;
  27. }
  28. TEST(ListTest, join_thread_by_list) {
  29. bthread_list_t list;
  30. ASSERT_EQ(0, bthread_list_init(&list, 0, 0));
  31. std::vector<bthread_t> tids;
  32. for (size_t i = 0; i < 10; ++i) {
  33. bthread_t th;
  34. ASSERT_EQ(0, bthread_start_urgent(
  35. &th, NULL, sleeper, (void*)10000/*10ms*/));
  36. ASSERT_EQ(0, bthread_list_add(&list, th));
  37. tids.push_back(th);
  38. }
  39. ASSERT_EQ(0, bthread_list_join(&list));
  40. for (size_t i = 0; i < tids.size(); ++i) {
  41. ASSERT_FALSE(bthread::TaskGroup::exists(tids[i]));
  42. }
  43. bthread_list_destroy(&list);
  44. }
  45. TEST(ListTest, join_a_destroyed_list) {
  46. bthread_list_t list;
  47. ASSERT_EQ(0, bthread_list_init(&list, 0, 0));
  48. bthread_t th;
  49. ASSERT_EQ(0, bthread_start_urgent(
  50. &th, NULL, sleeper, (void*)10000/*10ms*/));
  51. ASSERT_EQ(0, bthread_list_add(&list, th));
  52. ASSERT_EQ(0, bthread_list_join(&list));
  53. bthread_list_destroy(&list);
  54. ASSERT_EQ(EINVAL, bthread_list_join(&list));
  55. }
  56. } // namespace