Chromium Code Reviews| 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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/tuple.h" | 10 #include "base/tuple.h" |
| 11 #include "net/base/completion_callback.h" | 11 #include "net/base/completion_callback.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 // Base class overridden by custom implementations of TestCompletionCallback. | 26 namespace internal { |
|
willchan no longer on Chromium
2012/04/09 12:33:57
This should be nested within the net namespace. Th
kinuko
2012/04/09 17:04:36
Done.
| |
| 26 class TestCompletionCallbackBase { | 27 |
| 28 class TestCompletionCallbackBaseInternal { | |
| 27 public: | 29 public: |
| 28 void SetResult(int result); | |
| 29 int WaitForResult(); | |
| 30 int GetResult(int result); | |
| 31 bool have_result() const { return have_result_; } | 30 bool have_result() const { return have_result_; } |
| 32 | 31 |
| 33 protected: | 32 protected: |
| 34 TestCompletionCallbackBase(); | 33 TestCompletionCallbackBaseInternal(); |
| 34 void DidSetResult(); | |
| 35 void WaitForResult(); | |
| 35 | 36 |
| 36 int result_; | |
| 37 bool have_result_; | 37 bool have_result_; |
| 38 bool waiting_for_result_; | 38 bool waiting_for_result_; |
| 39 | 39 |
| 40 private: | 40 private: |
| 41 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackBase); | 41 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackBaseInternal); |
| 42 }; | 42 }; |
| 43 | 43 |
| 44 template <typename R> | |
| 45 class TestCompletionCallbackTemplate | |
| 46 : public TestCompletionCallbackBaseInternal { | |
| 47 public: | |
| 48 void SetResult(R result) { | |
| 49 result_ = result; | |
| 50 DidSetResult(); | |
| 51 } | |
| 52 | |
| 53 R WaitForResult() { | |
| 54 TestCompletionCallbackBaseInternal::WaitForResult(); | |
| 55 have_result_ = false; // Auto-reset for next callback. | |
|
willchan no longer on Chromium
2012/04/09 12:33:57
Why did this move out to the template? Why isn't t
kinuko
2012/04/09 17:04:36
Fixed.
| |
| 56 return result_; | |
| 57 } | |
| 58 | |
| 59 R GetResult(R result) { | |
| 60 if (net::ERR_IO_PENDING != result) | |
| 61 return result; | |
| 62 return WaitForResult(); | |
| 63 } | |
| 64 | |
| 65 protected: | |
| 66 TestCompletionCallbackTemplate() : result_(R()) {} | |
| 67 R result_; | |
| 68 | |
| 69 private: | |
| 70 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallbackTemplate); | |
| 71 }; | |
| 72 | |
| 73 } // namespace internal | |
| 74 | |
| 75 // Base class overridden by custom implementations of TestCompletionCallback. | |
| 76 typedef internal::TestCompletionCallbackTemplate<int> | |
| 77 TestCompletionCallbackBase; | |
| 78 | |
| 79 typedef internal::TestCompletionCallbackTemplate<int64> | |
| 80 TestInt64CompletionCallbackBase; | |
| 81 | |
| 44 namespace net { | 82 namespace net { |
| 45 | 83 |
| 46 class TestCompletionCallback : public TestCompletionCallbackBase { | 84 class TestCompletionCallback : public TestCompletionCallbackBase { |
| 47 public: | 85 public: |
| 48 TestCompletionCallback(); | 86 TestCompletionCallback(); |
| 49 ~TestCompletionCallback(); | 87 ~TestCompletionCallback(); |
| 50 | 88 |
| 51 const CompletionCallback& callback() const { return callback_; } | 89 const CompletionCallback& callback() const { return callback_; } |
| 52 | 90 |
| 53 private: | 91 private: |
| 54 const CompletionCallback callback_; | 92 const CompletionCallback callback_; |
| 55 | 93 |
| 56 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallback); | 94 DISALLOW_COPY_AND_ASSIGN(TestCompletionCallback); |
| 57 }; | 95 }; |
| 58 | 96 |
| 97 class TestInt64CompletionCallback : public TestInt64CompletionCallbackBase { | |
| 98 public: | |
| 99 TestInt64CompletionCallback(); | |
| 100 ~TestInt64CompletionCallback(); | |
| 101 | |
| 102 const Int64CompletionCallback& callback() const { return callback_; } | |
| 103 | |
| 104 private: | |
| 105 const Int64CompletionCallback callback_; | |
| 106 | |
| 107 DISALLOW_COPY_AND_ASSIGN(TestInt64CompletionCallback); | |
| 108 }; | |
| 109 | |
| 59 } // namespace net | 110 } // namespace net |
| 60 | 111 |
| 61 #endif // NET_BASE_TEST_COMPLETION_CALLBACK_H_ | 112 #endif // NET_BASE_TEST_COMPLETION_CALLBACK_H_ |
| OLD | NEW |