test_completion_callback.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef NET_BASE_TEST_COMPLETION_CALLBACK_H_
  5. #define NET_BASE_TEST_COMPLETION_CALLBACK_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <utility>
  9. #include "base/callback.h"
  10. #include "base/compiler_specific.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "net/base/completion_once_callback.h"
  13. #include "net/base/net_errors.h"
  14. #include "third_party/abseil-cpp/absl/types/optional.h"
  15. //-----------------------------------------------------------------------------
  16. // completion callback helper
  17. // A helper class for completion callbacks, designed to make it easy to run
  18. // tests involving asynchronous operations. Just call WaitForResult to wait
  19. // for the asynchronous operation to complete. Uses a RunLoop to spin the
  20. // current MessageLoop while waiting. The callback must be invoked on the same
  21. // thread WaitForResult is called on.
  22. //
  23. // NOTE: Since this runs a message loop to wait for the completion callback,
  24. // there could be other side-effects resulting from WaitForResult. For this
  25. // reason, this class is probably not ideal for a general application.
  26. //
  27. namespace base {
  28. class RunLoop;
  29. }
  30. namespace net {
  31. class IOBuffer;
  32. namespace internal {
  33. class TestCompletionCallbackBaseInternal {
  34. public:
  35. TestCompletionCallbackBaseInternal(
  36. const TestCompletionCallbackBaseInternal&) = delete;
  37. TestCompletionCallbackBaseInternal& operator=(
  38. const TestCompletionCallbackBaseInternal&) = delete;
  39. bool have_result() const { return have_result_; }
  40. protected:
  41. TestCompletionCallbackBaseInternal();
  42. virtual ~TestCompletionCallbackBaseInternal();
  43. void DidSetResult();
  44. void WaitForResult();
  45. private:
  46. // RunLoop. Only non-NULL during the call to WaitForResult, so the class is
  47. // reusable.
  48. std::unique_ptr<base::RunLoop> run_loop_;
  49. bool have_result_ = false;
  50. };
  51. template <typename R>
  52. struct NetErrorIsPendingHelper {
  53. bool operator()(R status) const { return status == ERR_IO_PENDING; }
  54. };
  55. template <typename R, typename IsPendingHelper = NetErrorIsPendingHelper<R>>
  56. class TestCompletionCallbackTemplate
  57. : public TestCompletionCallbackBaseInternal {
  58. public:
  59. TestCompletionCallbackTemplate(const TestCompletionCallbackTemplate&) =
  60. delete;
  61. TestCompletionCallbackTemplate& operator=(
  62. const TestCompletionCallbackTemplate&) = delete;
  63. ~TestCompletionCallbackTemplate() override = default;
  64. R WaitForResult() {
  65. TestCompletionCallbackBaseInternal::WaitForResult();
  66. return std::move(result_);
  67. }
  68. R GetResult(R result) {
  69. IsPendingHelper check_pending;
  70. if (!check_pending(result))
  71. return std::move(result);
  72. return WaitForResult();
  73. }
  74. protected:
  75. TestCompletionCallbackTemplate() : result_(R()) {}
  76. // Override this method to gain control as the callback is running.
  77. virtual void SetResult(R result) {
  78. result_ = std::move(result);
  79. DidSetResult();
  80. }
  81. private:
  82. R result_;
  83. };
  84. } // namespace internal
  85. class TestClosure : public internal::TestCompletionCallbackBaseInternal {
  86. public:
  87. using internal::TestCompletionCallbackBaseInternal::WaitForResult;
  88. TestClosure() = default;
  89. TestClosure(const TestClosure&) = delete;
  90. TestClosure& operator=(const TestClosure&) = delete;
  91. ~TestClosure() override;
  92. base::OnceClosure closure() {
  93. return base::BindOnce(&TestClosure::DidSetResult, base::Unretained(this));
  94. }
  95. };
  96. // Base class overridden by custom implementations of TestCompletionCallback.
  97. typedef internal::TestCompletionCallbackTemplate<int>
  98. TestCompletionCallbackBase;
  99. typedef internal::TestCompletionCallbackTemplate<int64_t>
  100. TestInt64CompletionCallbackBase;
  101. class TestCompletionCallback : public TestCompletionCallbackBase {
  102. public:
  103. TestCompletionCallback() = default;
  104. TestCompletionCallback(const TestCompletionCallback&) = delete;
  105. TestCompletionCallback& operator=(const TestCompletionCallback&) = delete;
  106. ~TestCompletionCallback() override;
  107. CompletionOnceCallback callback() {
  108. return base::BindOnce(&TestCompletionCallback::SetResult,
  109. base::Unretained(this));
  110. }
  111. };
  112. class TestInt64CompletionCallback : public TestInt64CompletionCallbackBase {
  113. public:
  114. TestInt64CompletionCallback() = default;
  115. TestInt64CompletionCallback(const TestInt64CompletionCallback&) = delete;
  116. TestInt64CompletionCallback& operator=(const TestInt64CompletionCallback&) =
  117. delete;
  118. ~TestInt64CompletionCallback() override;
  119. Int64CompletionOnceCallback callback() {
  120. return base::BindOnce(&TestInt64CompletionCallback::SetResult,
  121. base::Unretained(this));
  122. }
  123. };
  124. // Makes sure that the buffer is not referenced when the callback runs.
  125. class ReleaseBufferCompletionCallback: public TestCompletionCallback {
  126. public:
  127. explicit ReleaseBufferCompletionCallback(IOBuffer* buffer);
  128. ReleaseBufferCompletionCallback(const ReleaseBufferCompletionCallback&) =
  129. delete;
  130. ReleaseBufferCompletionCallback& operator=(
  131. const ReleaseBufferCompletionCallback&) = delete;
  132. ~ReleaseBufferCompletionCallback() override;
  133. private:
  134. void SetResult(int result) override;
  135. raw_ptr<IOBuffer> buffer_;
  136. };
  137. } // namespace net
  138. #endif // NET_BASE_TEST_COMPLETION_CALLBACK_H_