Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(333)

Side by Side Diff: net/base/test_completion_callback.h

Issue 1142843002: net: Make TestCompletionCallback use RunLoops. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Reuploading to re-run tests Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/callback.h"
9 #include "base/compiler_specific.h" 9 #include "base/compiler_specific.h"
10 #include "base/memory/scoped_ptr.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. 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.
19 // 22 //
20 // NOTE: Since this runs a message loop to wait for the completion callback, 23 // 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 24 // there could be other side-effects resulting from WaitForResult. For this
22 // reason, this class is probably not ideal for a general application. 25 // reason, this class is probably not ideal for a general application.
23 // 26 //
27 namespace base {
28 class RunLoop;
29 }
24 30
25 namespace net { 31 namespace net {
26 32
27 class IOBuffer; 33 class IOBuffer;
28 34
29 namespace internal { 35 namespace internal {
30 36
31 class TestCompletionCallbackBaseInternal { 37 class TestCompletionCallbackBaseInternal {
32 public: 38 public:
33 bool have_result() const { return have_result_; } 39 bool have_result() const { return have_result_; }
34 40
35 protected: 41 protected:
36 TestCompletionCallbackBaseInternal(); 42 TestCompletionCallbackBaseInternal();
37 virtual ~TestCompletionCallbackBaseInternal(); 43 virtual ~TestCompletionCallbackBaseInternal();
38 44
39 void DidSetResult(); 45 void DidSetResult();
40 void WaitForResult(); 46 void WaitForResult();
41 47
48 private:
49 // RunLoop. Only non-NULL during the call to WaitForResult, so the class is
50 // reusable.
51 scoped_ptr<base::RunLoop> run_loop_;
42 bool have_result_; 52 bool have_result_;
43 bool waiting_for_result_;
44 53
45 private:
46 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackBaseInternal); 54 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackBaseInternal);
47 }; 55 };
48 56
49 template <typename R> 57 template <typename R>
50 class TestCompletionCallbackTemplate 58 class TestCompletionCallbackTemplate
51 : public TestCompletionCallbackBaseInternal { 59 : public TestCompletionCallbackBaseInternal {
52 public: 60 public:
53 virtual ~TestCompletionCallbackTemplate() override {} 61 virtual ~TestCompletionCallbackTemplate() override {}
54 62
55 R WaitForResult() { 63 R WaitForResult() {
56 TestCompletionCallbackBaseInternal::WaitForResult(); 64 TestCompletionCallbackBaseInternal::WaitForResult();
57 return result_; 65 return result_;
58 } 66 }
59 67
60 R GetResult(R result) { 68 R GetResult(R result) {
61 if (ERR_IO_PENDING != result) 69 if (ERR_IO_PENDING != result)
62 return result; 70 return result;
63 return WaitForResult(); 71 return WaitForResult();
64 } 72 }
65 73
66 protected: 74 protected:
75 TestCompletionCallbackTemplate() : result_(R()) {}
76
67 // Override this method to gain control as the callback is running. 77 // Override this method to gain control as the callback is running.
68 virtual void SetResult(R result) { 78 virtual void SetResult(R result) {
69 result_ = result; 79 result_ = result;
70 DidSetResult(); 80 DidSetResult();
71 } 81 }
72 82
73 TestCompletionCallbackTemplate() : result_(R()) {} 83 private:
74 R result_; 84 R result_;
75 85
76 private:
77 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackTemplate); 86 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackTemplate);
78 }; 87 };
79 88
80 } // namespace internal 89 } // namespace internal
81 90
82 class TestClosure 91 class TestClosure : public internal::TestCompletionCallbackBaseInternal {
83 : public internal::TestCompletionCallbackBaseInternal {
84 public: 92 public:
85 using internal::TestCompletionCallbackBaseInternal::WaitForResult; 93 using internal::TestCompletionCallbackBaseInternal::WaitForResult;
86 94
87 TestClosure(); 95 TestClosure();
88 ~TestClosure() override; 96 ~TestClosure() override;
89 97
90 const base::Closure& closure() const { return closure_; } 98 const base::Closure& closure() const { return closure_; }
91 99
92 private: 100 private:
93 const base::Closure closure_; 101 const base::Closure closure_;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 private: 145 private:
138 void SetResult(int result) override; 146 void SetResult(int result) override;
139 147
140 IOBuffer* buffer_; 148 IOBuffer* buffer_;
141 DISALLOW_COPY_AND_ASSIGN(ReleaseBufferCompletionCallback); 149 DISALLOW_COPY_AND_ASSIGN(ReleaseBufferCompletionCallback);
142 }; 150 };
143 151
144 } // namespace net 152 } // namespace net
145 153
146 #endif // NET_BASE_TEST_COMPLETION_CALLBACK_H_ 154 #endif // NET_BASE_TEST_COMPLETION_CALLBACK_H_
OLDNEW
« no previous file with comments | « no previous file | net/base/test_completion_callback.cc » ('j') | net/base/test_completion_callback.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698