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.
5 #ifndef NET_BASE_TEST_COMPLETION_CALLBACK_H_
6 #define NET_BASE_TEST_COMPLETION_CALLBACK_H_
8 #include "base/callback.h"
9 #include "base/compiler_specific.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "net/base/completion_callback.h"
12 #include "net/base/net_errors.h"
14 //-----------------------------------------------------------------------------
15 // 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.
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.
37 class TestCompletionCallbackBaseInternal
{
39 bool have_result() const { return have_result_
; }
42 TestCompletionCallbackBaseInternal();
43 virtual ~TestCompletionCallbackBaseInternal();
49 // RunLoop. Only non-NULL during the call to WaitForResult, so the class is
51 scoped_ptr
<base::RunLoop
> run_loop_
;
54 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackBaseInternal
);
58 class TestCompletionCallbackTemplate
59 : public TestCompletionCallbackBaseInternal
{
61 virtual ~TestCompletionCallbackTemplate() override
{}
64 TestCompletionCallbackBaseInternal::WaitForResult();
68 R
GetResult(R result
) {
69 if (ERR_IO_PENDING
!= result
)
71 return WaitForResult();
75 TestCompletionCallbackTemplate() : result_(R()) {}
77 // Override this method to gain control as the callback is running.
78 virtual void SetResult(R result
) {
86 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackTemplate
);
89 } // namespace internal
91 class TestClosure
: public internal::TestCompletionCallbackBaseInternal
{
93 using internal::TestCompletionCallbackBaseInternal::WaitForResult
;
96 ~TestClosure() override
;
98 const base::Closure
& closure() const { return closure_
; }
101 const base::Closure closure_
;
103 DISALLOW_COPY_AND_ASSIGN(TestClosure
);
106 // Base class overridden by custom implementations of TestCompletionCallback.
107 typedef internal::TestCompletionCallbackTemplate
<int>
108 TestCompletionCallbackBase
;
110 typedef internal::TestCompletionCallbackTemplate
<int64_t>
111 TestInt64CompletionCallbackBase
;
113 class TestCompletionCallback
: public TestCompletionCallbackBase
{
115 TestCompletionCallback();
116 ~TestCompletionCallback() override
;
118 const CompletionCallback
& callback() const { return callback_
; }
121 const CompletionCallback callback_
;
123 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallback
);
126 class TestInt64CompletionCallback
: public TestInt64CompletionCallbackBase
{
128 TestInt64CompletionCallback();
129 ~TestInt64CompletionCallback() override
;
131 const Int64CompletionCallback
& callback() const { return callback_
; }
134 const Int64CompletionCallback callback_
;
136 DISALLOW_COPY_AND_ASSIGN(TestInt64CompletionCallback
);
139 // Makes sure that the buffer is not referenced when the callback runs.
140 class ReleaseBufferCompletionCallback
: public TestCompletionCallback
{
142 explicit ReleaseBufferCompletionCallback(IOBuffer
* buffer
);
143 ~ReleaseBufferCompletionCallback() override
;
146 void SetResult(int result
) override
;
149 DISALLOW_COPY_AND_ASSIGN(ReleaseBufferCompletionCallback
);
154 #endif // NET_BASE_TEST_COMPLETION_CALLBACK_H_