[email protected] | fe57eb2 | 2012-02-09 05:59:40 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/base/mock_file_stream.h" |
| 6 | |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 7 | #include "base/bind.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame^] | 8 | #include "base/location.h" |
| 9 | #include "base/single_thread_task_runner.h" |
| 10 | #include "base/thread_task_runner_handle.h" |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 11 | |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 12 | namespace net { |
| 13 | |
| 14 | namespace testing { |
| 15 | |
[email protected] | 671e95fd | 2014-04-30 11:21:36 | [diff] [blame] | 16 | MockFileStream::MockFileStream( |
| 17 | const scoped_refptr<base::TaskRunner>& task_runner) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 18 | : FileStream(task_runner), |
| 19 | forced_error_(OK), |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 20 | async_error_(false), |
| 21 | throttled_(false), |
| 22 | weak_factory_(this) { |
| 23 | } |
| 24 | |
| 25 | MockFileStream::MockFileStream( |
[email protected] | be6fb60b | 2014-04-03 18:33:58 | [diff] [blame] | 26 | base::File file, |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 27 | const scoped_refptr<base::TaskRunner>& task_runner) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 28 | : FileStream(file.Pass(), task_runner), |
| 29 | forced_error_(OK), |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 30 | async_error_(false), |
| 31 | throttled_(false), |
| 32 | weak_factory_(this) { |
| 33 | } |
| 34 | |
| 35 | MockFileStream::~MockFileStream() { |
| 36 | } |
| 37 | |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame] | 38 | int MockFileStream::Seek(base::File::Whence whence, |
| 39 | int64_t offset, |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 40 | const Int64CompletionCallback& callback) { |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 41 | Int64CompletionCallback wrapped_callback = |
| 42 | base::Bind(&MockFileStream::DoCallback64, |
| 43 | weak_factory_.GetWeakPtr(), callback); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 44 | if (forced_error_ == OK) |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 45 | return FileStream::Seek(whence, offset, wrapped_callback); |
| 46 | return ErrorCallback64(wrapped_callback); |
[email protected] | cf02541b | 2012-04-11 08:02:17 | [diff] [blame] | 47 | } |
| 48 | |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 49 | int MockFileStream::Read(IOBuffer* buf, |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 50 | int buf_len, |
| 51 | const CompletionCallback& callback) { |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 52 | CompletionCallback wrapped_callback = base::Bind(&MockFileStream::DoCallback, |
| 53 | weak_factory_.GetWeakPtr(), |
| 54 | callback); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 55 | if (forced_error_ == OK) |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 56 | return FileStream::Read(buf, buf_len, wrapped_callback); |
| 57 | return ErrorCallback(wrapped_callback); |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 58 | } |
| 59 | |
[email protected] | 9f49afb | 2012-02-16 09:59:20 | [diff] [blame] | 60 | int MockFileStream::Write(IOBuffer* buf, |
[email protected] | 5eb431e2 | 2011-10-12 08:51:38 | [diff] [blame] | 61 | int buf_len, |
| 62 | const CompletionCallback& callback) { |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 63 | CompletionCallback wrapped_callback = base::Bind(&MockFileStream::DoCallback, |
| 64 | weak_factory_.GetWeakPtr(), |
| 65 | callback); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 66 | if (forced_error_ == OK) |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 67 | return FileStream::Write(buf, buf_len, wrapped_callback); |
| 68 | return ErrorCallback(wrapped_callback); |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 69 | } |
| 70 | |
[email protected] | a95ce076 | 2012-10-01 05:54:26 | [diff] [blame] | 71 | int MockFileStream::Flush(const CompletionCallback& callback) { |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 72 | CompletionCallback wrapped_callback = base::Bind(&MockFileStream::DoCallback, |
| 73 | weak_factory_.GetWeakPtr(), |
| 74 | callback); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 75 | if (forced_error_ == OK) |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 76 | return FileStream::Flush(wrapped_callback); |
| 77 | return ErrorCallback(wrapped_callback); |
[email protected] | a95ce076 | 2012-10-01 05:54:26 | [diff] [blame] | 78 | } |
| 79 | |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 80 | void MockFileStream::ThrottleCallbacks() { |
| 81 | CHECK(!throttled_); |
| 82 | throttled_ = true; |
| 83 | } |
| 84 | |
| 85 | void MockFileStream::ReleaseCallbacks() { |
| 86 | CHECK(throttled_); |
| 87 | throttled_ = false; |
| 88 | |
| 89 | if (!throttled_task_.is_null()) { |
| 90 | base::Closure throttled_task = throttled_task_; |
| 91 | throttled_task_.Reset(); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame^] | 92 | base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, throttled_task); |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | void MockFileStream::DoCallback(const CompletionCallback& callback, |
| 97 | int result) { |
| 98 | if (!throttled_) { |
| 99 | callback.Run(result); |
| 100 | return; |
| 101 | } |
| 102 | CHECK(throttled_task_.is_null()); |
| 103 | throttled_task_ = base::Bind(callback, result); |
| 104 | } |
| 105 | |
| 106 | void MockFileStream::DoCallback64(const Int64CompletionCallback& callback, |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame] | 107 | int64_t result) { |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 108 | if (!throttled_) { |
| 109 | callback.Run(result); |
| 110 | return; |
| 111 | } |
| 112 | CHECK(throttled_task_.is_null()); |
| 113 | throttled_task_ = base::Bind(callback, result); |
| 114 | } |
| 115 | |
| 116 | int MockFileStream::ErrorCallback(const CompletionCallback& callback) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 117 | CHECK_NE(OK, forced_error_); |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 118 | if (async_error_) { |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame^] | 119 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 120 | FROM_HERE, base::Bind(callback, forced_error_)); |
| 121 | clear_forced_error(); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 122 | return ERR_IO_PENDING; |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 123 | } |
| 124 | int ret = forced_error_; |
| 125 | clear_forced_error(); |
| 126 | return ret; |
| 127 | } |
| 128 | |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame] | 129 | int64_t MockFileStream::ErrorCallback64( |
| 130 | const Int64CompletionCallback& callback) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 131 | CHECK_NE(OK, forced_error_); |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 132 | if (async_error_) { |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame^] | 133 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 134 | FROM_HERE, base::Bind(callback, forced_error_)); |
| 135 | clear_forced_error(); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 136 | return ERR_IO_PENDING; |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 137 | } |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame] | 138 | int64_t ret = forced_error_; |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 139 | clear_forced_error(); |
| 140 | return ret; |
| 141 | } |
| 142 | |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 143 | } // namespace testing |
| 144 | |
| 145 | } // namespace net |