OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_BASE_TEST_COMPLETION_CALLBACK_H_ | 5 #ifndef NET_BASE_TEST_COMPLETION_CALLBACK_H_ |
6 #define NET_BASE_TEST_COMPLETION_CALLBACK_H_ | 6 #define NET_BASE_TEST_COMPLETION_CALLBACK_H_ |
7 | 7 |
| 8 #include "base/callback.h" |
8 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
9 #include "base/tuple.h" | 10 #include "base/tuple.h" |
10 #include "net/base/completion_callback.h" | 11 #include "net/base/completion_callback.h" |
11 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
12 | 13 |
13 //----------------------------------------------------------------------------- | 14 //----------------------------------------------------------------------------- |
14 // completion callback helper | 15 // completion callback helper |
15 | 16 |
16 // A helper class for completion callbacks, designed to make it easy to run | 17 // A helper class for completion callbacks, designed to make it easy to run |
17 // tests involving asynchronous operations. Just call WaitForResult to wait | 18 // tests involving asynchronous operations. Just call WaitForResult to wait |
18 // for the asynchronous operation to complete. | 19 // for the asynchronous operation to complete. |
19 // | 20 // |
20 // NOTE: Since this runs a message loop to wait for the completion callback, | 21 // NOTE: Since this runs a message loop to wait for the completion callback, |
21 // there could be other side-effects resulting from WaitForResult. For this | 22 // there could be other side-effects resulting from WaitForResult. For this |
22 // reason, this class is probably not ideal for a general application. | 23 // reason, this class is probably not ideal for a general application. |
23 // | 24 // |
24 | 25 |
25 namespace net { | 26 namespace net { |
26 | 27 |
27 class IOBuffer; | 28 class IOBuffer; |
28 | 29 |
29 namespace internal { | 30 namespace internal { |
30 | 31 |
31 class TestCompletionCallbackBaseInternal { | 32 class TestCompletionCallbackBaseInternal { |
32 public: | 33 public: |
33 bool have_result() const { return have_result_; } | 34 bool have_result() const { return have_result_; } |
34 | 35 |
35 protected: | 36 protected: |
36 TestCompletionCallbackBaseInternal(); | 37 TestCompletionCallbackBaseInternal(); |
| 38 virtual ~TestCompletionCallbackBaseInternal(); |
| 39 |
37 void DidSetResult(); | 40 void DidSetResult(); |
38 void WaitForResult(); | 41 void WaitForResult(); |
39 | 42 |
40 bool have_result_; | 43 bool have_result_; |
41 bool waiting_for_result_; | 44 bool waiting_for_result_; |
42 | 45 |
43 private: | 46 private: |
44 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackBaseInternal); | 47 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackBaseInternal); |
45 }; | 48 }; |
46 | 49 |
47 template <typename R> | 50 template <typename R> |
48 class TestCompletionCallbackTemplate | 51 class TestCompletionCallbackTemplate |
49 : public TestCompletionCallbackBaseInternal { | 52 : public TestCompletionCallbackBaseInternal { |
50 public: | 53 public: |
51 virtual ~TestCompletionCallbackTemplate() {} | 54 virtual ~TestCompletionCallbackTemplate() override {} |
52 | 55 |
53 R WaitForResult() { | 56 R WaitForResult() { |
54 TestCompletionCallbackBaseInternal::WaitForResult(); | 57 TestCompletionCallbackBaseInternal::WaitForResult(); |
55 return result_; | 58 return result_; |
56 } | 59 } |
57 | 60 |
58 R GetResult(R result) { | 61 R GetResult(R result) { |
59 if (net::ERR_IO_PENDING != result) | 62 if (net::ERR_IO_PENDING != result) |
60 return result; | 63 return result; |
61 return WaitForResult(); | 64 return WaitForResult(); |
62 } | 65 } |
63 | 66 |
64 protected: | 67 protected: |
65 // Override this method to gain control as the callback is running. | 68 // Override this method to gain control as the callback is running. |
66 virtual void SetResult(R result) { | 69 virtual void SetResult(R result) { |
67 result_ = result; | 70 result_ = result; |
68 DidSetResult(); | 71 DidSetResult(); |
69 } | 72 } |
70 | 73 |
71 TestCompletionCallbackTemplate() : result_(R()) {} | 74 TestCompletionCallbackTemplate() : result_(R()) {} |
72 R result_; | 75 R result_; |
73 | 76 |
74 private: | 77 private: |
75 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackTemplate); | 78 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackTemplate); |
76 }; | 79 }; |
77 | 80 |
78 } // namespace internal | 81 } // namespace internal |
79 | 82 |
| 83 class TestClosure |
| 84 : public internal::TestCompletionCallbackBaseInternal { |
| 85 public: |
| 86 using internal::TestCompletionCallbackBaseInternal::WaitForResult; |
| 87 |
| 88 TestClosure(); |
| 89 virtual ~TestClosure() override; |
| 90 |
| 91 const base::Closure& closure() const { return closure_; } |
| 92 |
| 93 private: |
| 94 const base::Closure closure_; |
| 95 |
| 96 DISALLOW_COPY_AND_ASSIGN(TestClosure); |
| 97 }; |
| 98 |
80 // Base class overridden by custom implementations of TestCompletionCallback. | 99 // Base class overridden by custom implementations of TestCompletionCallback. |
81 typedef internal::TestCompletionCallbackTemplate<int> | 100 typedef internal::TestCompletionCallbackTemplate<int> |
82 TestCompletionCallbackBase; | 101 TestCompletionCallbackBase; |
83 | 102 |
84 typedef internal::TestCompletionCallbackTemplate<int64> | 103 typedef internal::TestCompletionCallbackTemplate<int64> |
85 TestInt64CompletionCallbackBase; | 104 TestInt64CompletionCallbackBase; |
86 | 105 |
87 class TestCompletionCallback : public TestCompletionCallbackBase { | 106 class TestCompletionCallback : public TestCompletionCallbackBase { |
88 public: | 107 public: |
89 TestCompletionCallback(); | 108 TestCompletionCallback(); |
90 ~TestCompletionCallback() override; | 109 virtual ~TestCompletionCallback() override; |
91 | 110 |
92 const CompletionCallback& callback() const { return callback_; } | 111 const CompletionCallback& callback() const { return callback_; } |
93 | 112 |
94 private: | 113 private: |
95 const CompletionCallback callback_; | 114 const CompletionCallback callback_; |
96 | 115 |
97 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallback); | 116 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallback); |
98 }; | 117 }; |
99 | 118 |
100 class TestInt64CompletionCallback : public TestInt64CompletionCallbackBase { | 119 class TestInt64CompletionCallback : public TestInt64CompletionCallbackBase { |
101 public: | 120 public: |
102 TestInt64CompletionCallback(); | 121 TestInt64CompletionCallback(); |
103 ~TestInt64CompletionCallback() override; | 122 virtual ~TestInt64CompletionCallback() override; |
104 | 123 |
105 const Int64CompletionCallback& callback() const { return callback_; } | 124 const Int64CompletionCallback& callback() const { return callback_; } |
106 | 125 |
107 private: | 126 private: |
108 const Int64CompletionCallback callback_; | 127 const Int64CompletionCallback callback_; |
109 | 128 |
110 DISALLOW_COPY_AND_ASSIGN(TestInt64CompletionCallback); | 129 DISALLOW_COPY_AND_ASSIGN(TestInt64CompletionCallback); |
111 }; | 130 }; |
112 | 131 |
113 // Makes sure that the buffer is not referenced when the callback runs. | 132 // Makes sure that the buffer is not referenced when the callback runs. |
114 class ReleaseBufferCompletionCallback: public TestCompletionCallback { | 133 class ReleaseBufferCompletionCallback: public TestCompletionCallback { |
115 public: | 134 public: |
116 explicit ReleaseBufferCompletionCallback(IOBuffer* buffer); | 135 explicit ReleaseBufferCompletionCallback(IOBuffer* buffer); |
117 ~ReleaseBufferCompletionCallback() override; | 136 virtual ~ReleaseBufferCompletionCallback() override; |
118 | 137 |
119 private: | 138 private: |
120 void SetResult(int result) override; | 139 void SetResult(int result) override; |
121 | 140 |
122 IOBuffer* buffer_; | 141 IOBuffer* buffer_; |
123 DISALLOW_COPY_AND_ASSIGN(ReleaseBufferCompletionCallback); | 142 DISALLOW_COPY_AND_ASSIGN(ReleaseBufferCompletionCallback); |
124 }; | 143 }; |
125 | 144 |
126 } // namespace net | 145 } // namespace net |
127 | 146 |
128 #endif // NET_BASE_TEST_COMPLETION_CALLBACK_H_ | 147 #endif // NET_BASE_TEST_COMPLETION_CALLBACK_H_ |
OLD | NEW |