| 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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/message_loop.h" | |
| 11 #include "net/base/completion_callback.h" | |
| 12 #include "net/base/net_errors.h" | |
| 13 | 10 |
| 14 //----------------------------------------------------------------------------- | 11 //----------------------------------------------------------------------------- |
| 15 // completion callback helper | 12 // completion callback helper |
| 16 | 13 |
| 17 // A helper class for completion callbacks, designed to make it easy to run | 14 // A helper class for completion callbacks, designed to make it easy to run |
| 18 // tests involving asynchronous operations. Just call WaitForResult to wait | 15 // tests involving asynchronous operations. Just call WaitForResult to wait |
| 19 // for the asynchronous operation to complete. | 16 // for the asynchronous operation to complete. |
| 20 // | 17 // |
| 21 // NOTE: Since this runs a message loop to wait for the completion callback, | 18 // NOTE: Since this runs a message loop to wait for the completion callback, |
| 22 // there could be other side-effects resulting from WaitForResult. For this | 19 // there could be other side-effects resulting from WaitForResult. For this |
| 23 // reason, this class is probably not ideal for a general application. | 20 // reason, this class is probably not ideal for a general application. |
| 24 // | 21 // |
| 25 class TestCompletionCallback : public CallbackRunner< Tuple1<int> > { | 22 class TestCompletionCallback : public CallbackRunner< Tuple1<int> > { |
| 26 public: | 23 public: |
| 27 TestCompletionCallback() | 24 TestCompletionCallback(); |
| 28 : result_(0), | 25 virtual ~TestCompletionCallback(); |
| 29 have_result_(false), | |
| 30 waiting_for_result_(false) { | |
| 31 } | |
| 32 | 26 |
| 33 int WaitForResult() { | 27 int WaitForResult(); |
| 34 DCHECK(!waiting_for_result_); | |
| 35 while (!have_result_) { | |
| 36 waiting_for_result_ = true; | |
| 37 MessageLoop::current()->Run(); | |
| 38 waiting_for_result_ = false; | |
| 39 } | |
| 40 have_result_ = false; // auto-reset for next callback | |
| 41 return result_; | |
| 42 } | |
| 43 | 28 |
| 44 int GetResult(int result) { | 29 int GetResult(int result); |
| 45 if (net::ERR_IO_PENDING != result) | |
| 46 return result; | |
| 47 return WaitForResult(); | |
| 48 } | |
| 49 | 30 |
| 50 bool have_result() const { return have_result_; } | 31 bool have_result() const { return have_result_; } |
| 51 | 32 |
| 52 virtual void RunWithParams(const Tuple1<int>& params) { | 33 virtual void RunWithParams(const Tuple1<int>& params); |
| 53 result_ = params.a; | |
| 54 have_result_ = true; | |
| 55 if (waiting_for_result_) | |
| 56 MessageLoop::current()->Quit(); | |
| 57 } | |
| 58 | 34 |
| 59 private: | 35 private: |
| 60 int result_; | 36 int result_; |
| 61 bool have_result_; | 37 bool have_result_; |
| 62 bool waiting_for_result_; | 38 bool waiting_for_result_; |
| 63 }; | 39 }; |
| 64 | 40 |
| 65 #endif // NET_BASE_TEST_COMPLETION_CALLBACK_H_ | 41 #endif // NET_BASE_TEST_COMPLETION_CALLBACK_H_ |
| OLD | NEW |