| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "net/base/completion_callback.h" | 9 #include "net/base/completion_callback.h" |
| 10 #include "net/base/net_errors.h" |
| 10 | 11 |
| 11 //----------------------------------------------------------------------------- | 12 //----------------------------------------------------------------------------- |
| 12 // completion callback helper | 13 // completion callback helper |
| 13 | 14 |
| 14 // A helper class for completion callbacks, designed to make it easy to run | 15 // A helper class for completion callbacks, designed to make it easy to run |
| 15 // tests involving asynchronous operations. Just call WaitForResult to wait | 16 // tests involving asynchronous operations. Just call WaitForResult to wait |
| 16 // for the asynchronous operation to complete. | 17 // for the asynchronous operation to complete. |
| 17 // | 18 // |
| 18 // NOTE: Since this runs a message loop to wait for the completion callback, | 19 // NOTE: Since this runs a message loop to wait for the completion callback, |
| 19 // there could be other side-effects resulting from WaitForResult. For this | 20 // there could be other side-effects resulting from WaitForResult. For this |
| (...skipping 11 matching lines...) Expand all Loading... |
| 31 DCHECK(!waiting_for_result_); | 32 DCHECK(!waiting_for_result_); |
| 32 while (!have_result_) { | 33 while (!have_result_) { |
| 33 waiting_for_result_ = true; | 34 waiting_for_result_ = true; |
| 34 MessageLoop::current()->Run(); | 35 MessageLoop::current()->Run(); |
| 35 waiting_for_result_ = false; | 36 waiting_for_result_ = false; |
| 36 } | 37 } |
| 37 have_result_ = false; // auto-reset for next callback | 38 have_result_ = false; // auto-reset for next callback |
| 38 return result_; | 39 return result_; |
| 39 } | 40 } |
| 40 | 41 |
| 42 int GetResult(int result) { |
| 43 if (net::ERR_IO_PENDING != result) |
| 44 return result; |
| 45 return WaitForResult(); |
| 46 } |
| 47 |
| 41 bool have_result() const { return have_result_; } | 48 bool have_result() const { return have_result_; } |
| 42 | 49 |
| 43 virtual void RunWithParams(const Tuple1<int>& params) { | 50 virtual void RunWithParams(const Tuple1<int>& params) { |
| 44 result_ = params.a; | 51 result_ = params.a; |
| 45 have_result_ = true; | 52 have_result_ = true; |
| 46 if (waiting_for_result_) | 53 if (waiting_for_result_) |
| 47 MessageLoop::current()->Quit(); | 54 MessageLoop::current()->Quit(); |
| 48 } | 55 } |
| 49 | 56 |
| 50 private: | 57 private: |
| 51 int result_; | 58 int result_; |
| 52 bool have_result_; | 59 bool have_result_; |
| 53 bool waiting_for_result_; | 60 bool waiting_for_result_; |
| 54 }; | 61 }; |
| 55 | 62 |
| 56 #endif // NET_BASE_TEST_COMPLETION_CALLBACK_H_ | 63 #endif // NET_BASE_TEST_COMPLETION_CALLBACK_H_ |
| OLD | NEW |