runnable_utils_unittest.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. // Original author: ekr@rtfm.com
  6. #include <iostream>
  7. #include "prio.h"
  8. #include "nsCOMPtr.h"
  9. #include "nsNetCID.h"
  10. #include "nsXPCOM.h"
  11. #include "nsXPCOMGlue.h"
  12. #include "mozilla/RefPtr.h"
  13. #include "nsIComponentManager.h"
  14. #include "nsIComponentRegistrar.h"
  15. #include "nsIIOService.h"
  16. #include "nsIServiceManager.h"
  17. #include "nsISocketTransportService.h"
  18. #include "nsASocketHandler.h"
  19. #include "nsServiceManagerUtils.h"
  20. #include "nsThreadUtils.h"
  21. #include "runnable_utils.h"
  22. #define GTEST_HAS_RTTI 0
  23. #include "gtest/gtest.h"
  24. #include "gtest_utils.h"
  25. using namespace mozilla;
  26. namespace {
  27. class Destructor {
  28. private:
  29. ~Destructor() {
  30. std::cerr << "Destructor called" << std::endl;
  31. *destroyed_ = true;
  32. }
  33. public:
  34. explicit Destructor(bool* destroyed) : destroyed_(destroyed) {}
  35. NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Destructor)
  36. private:
  37. bool *destroyed_;
  38. };
  39. class TargetClass {
  40. public:
  41. explicit TargetClass(int *ran) : ran_(ran) {}
  42. void m1(int x) {
  43. std::cerr << __FUNCTION__ << " " << x << std::endl;
  44. *ran_ = 1;
  45. }
  46. void m2(int x, int y) {
  47. std::cerr << __FUNCTION__ << " " << x << " " << y << std::endl;
  48. *ran_ = 2;
  49. }
  50. void m1set(bool *z) {
  51. std::cerr << __FUNCTION__ << std::endl;
  52. *z = true;
  53. }
  54. int return_int(int x) {
  55. std::cerr << __FUNCTION__ << std::endl;
  56. return x;
  57. }
  58. void destructor_target(Destructor*) {
  59. }
  60. void destructor_target_ref(RefPtr<Destructor> destructor) {
  61. }
  62. int *ran_;
  63. };
  64. class RunnableArgsTest : public MtransportTest {
  65. public:
  66. RunnableArgsTest() : MtransportTest(), ran_(0), cl_(&ran_){}
  67. void Test1Arg() {
  68. Runnable * r = WrapRunnable(&cl_, &TargetClass::m1, 1);
  69. r->Run();
  70. ASSERT_EQ(1, ran_);
  71. }
  72. void Test2Args() {
  73. Runnable* r = WrapRunnable(&cl_, &TargetClass::m2, 1, 2);
  74. r->Run();
  75. ASSERT_EQ(2, ran_);
  76. }
  77. private:
  78. int ran_;
  79. TargetClass cl_;
  80. };
  81. class DispatchTest : public MtransportTest {
  82. public:
  83. DispatchTest() : MtransportTest(), ran_(0), cl_(&ran_) {}
  84. void SetUp() {
  85. MtransportTest::SetUp();
  86. nsresult rv;
  87. target_ = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
  88. ASSERT_TRUE(NS_SUCCEEDED(rv));
  89. }
  90. void Test1Arg() {
  91. Runnable* r = WrapRunnable(&cl_, &TargetClass::m1, 1);
  92. target_->Dispatch(r, NS_DISPATCH_SYNC);
  93. ASSERT_EQ(1, ran_);
  94. }
  95. void Test2Args() {
  96. Runnable* r = WrapRunnable(&cl_, &TargetClass::m2, 1, 2);
  97. target_->Dispatch(r, NS_DISPATCH_SYNC);
  98. ASSERT_EQ(2, ran_);
  99. }
  100. void Test1Set() {
  101. bool x = false;
  102. target_->Dispatch(WrapRunnable(&cl_, &TargetClass::m1set, &x),
  103. NS_DISPATCH_SYNC);
  104. ASSERT_TRUE(x);
  105. }
  106. void TestRet() {
  107. int z;
  108. int x = 10;
  109. target_->Dispatch(WrapRunnableRet(&z, &cl_, &TargetClass::return_int, x),
  110. NS_DISPATCH_SYNC);
  111. ASSERT_EQ(10, z);
  112. }
  113. protected:
  114. int ran_;
  115. TargetClass cl_;
  116. nsCOMPtr<nsIEventTarget> target_;
  117. };
  118. TEST_F(RunnableArgsTest, OneArgument) {
  119. Test1Arg();
  120. }
  121. TEST_F(RunnableArgsTest, TwoArguments) {
  122. Test2Args();
  123. }
  124. TEST_F(DispatchTest, OneArgument) {
  125. Test1Arg();
  126. }
  127. TEST_F(DispatchTest, TwoArguments) {
  128. Test2Args();
  129. }
  130. TEST_F(DispatchTest, Test1Set) {
  131. Test1Set();
  132. }
  133. TEST_F(DispatchTest, TestRet) {
  134. TestRet();
  135. }
  136. void SetNonMethod(TargetClass *cl, int x) {
  137. cl->m1(x);
  138. }
  139. int SetNonMethodRet(TargetClass *cl, int x) {
  140. cl->m1(x);
  141. return x;
  142. }
  143. TEST_F(DispatchTest, TestNonMethod) {
  144. test_utils_->sts_target()->Dispatch(
  145. WrapRunnableNM(SetNonMethod, &cl_, 10), NS_DISPATCH_SYNC);
  146. ASSERT_EQ(1, ran_);
  147. }
  148. TEST_F(DispatchTest, TestNonMethodRet) {
  149. int z;
  150. test_utils_->sts_target()->Dispatch(
  151. WrapRunnableNMRet(&z, SetNonMethodRet, &cl_, 10), NS_DISPATCH_SYNC);
  152. ASSERT_EQ(1, ran_);
  153. ASSERT_EQ(10, z);
  154. }
  155. TEST_F(DispatchTest, TestDestructor) {
  156. bool destroyed = false;
  157. RefPtr<Destructor> destructor = new Destructor(&destroyed);
  158. target_->Dispatch(WrapRunnable(&cl_, &TargetClass::destructor_target,
  159. destructor),
  160. NS_DISPATCH_SYNC);
  161. ASSERT_FALSE(destroyed);
  162. destructor = nullptr;
  163. ASSERT_TRUE(destroyed);
  164. }
  165. TEST_F(DispatchTest, TestDestructorRef) {
  166. bool destroyed = false;
  167. RefPtr<Destructor> destructor = new Destructor(&destroyed);
  168. target_->Dispatch(WrapRunnable(&cl_, &TargetClass::destructor_target_ref,
  169. destructor),
  170. NS_DISPATCH_SYNC);
  171. ASSERT_FALSE(destroyed);
  172. destructor = nullptr;
  173. ASSERT_TRUE(destroyed);
  174. }
  175. } // end of namespace