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/compiler_specific.h"
9 #include "base/tuple.h"
10 #include "net/base/completion_callback.h"
11 #include "net/base/net_errors.h"
13 //-----------------------------------------------------------------------------
14 // completion callback helper
16 // A helper class for completion callbacks, designed to make it easy to run
17 // tests involving asynchronous operations. Just call WaitForResult to wait
18 // for the asynchronous operation to complete.
20 // 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 // reason, this class is probably not ideal for a general application.
31 class TestCompletionCallbackBaseInternal {
33 bool have_result() const { return have_result_; }
36 TestCompletionCallbackBaseInternal();
41 bool waiting_for_result_;
44 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackBaseInternal);
48 class TestCompletionCallbackTemplate
49 : public TestCompletionCallbackBaseInternal {
51 virtual ~TestCompletionCallbackTemplate() {}
54 TestCompletionCallbackBaseInternal::WaitForResult();
58 R GetResult(R result) {
59 if (net::ERR_IO_PENDING != result)
61 return WaitForResult();
65 // Override this method to gain control as the callback is running.
66 virtual void SetResult(R result) {
71 TestCompletionCallbackTemplate() : result_(R()) {}
75 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackTemplate);
78 } // namespace internal
80 // Base class overridden by custom implementations of TestCompletionCallback.
81 typedef internal::TestCompletionCallbackTemplate<int>
82 TestCompletionCallbackBase;
84 typedef internal::TestCompletionCallbackTemplate<int64>
85 TestInt64CompletionCallbackBase;
87 class TestCompletionCallback : public TestCompletionCallbackBase {
89 TestCompletionCallback();
90 virtual ~TestCompletionCallback();
92 const CompletionCallback& callback() const { return callback_; }
95 const CompletionCallback callback_;
97 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallback);
100 class TestInt64CompletionCallback : public TestInt64CompletionCallbackBase {
102 TestInt64CompletionCallback();
103 virtual ~TestInt64CompletionCallback();
105 const Int64CompletionCallback& callback() const { return callback_; }
108 const Int64CompletionCallback callback_;
110 DISALLOW_COPY_AND_ASSIGN(TestInt64CompletionCallback);
113 // Makes sure that the buffer is not referenced when the callback runs.
114 class ReleaseBufferCompletionCallback: public TestCompletionCallback {
116 explicit ReleaseBufferCompletionCallback(IOBuffer* buffer);
117 virtual ~ReleaseBufferCompletionCallback();
120 virtual void SetResult(int result) OVERRIDE;
123 DISALLOW_COPY_AND_ASSIGN(ReleaseBufferCompletionCallback);
128 #endif // NET_BASE_TEST_COMPLETION_CALLBACK_H_