Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #include "modules/fetch/BytesConsumerTestUtil.h" | 5 #include "modules/fetch/BytesConsumerTestUtil.h" |
| 6 | 6 |
| 7 #include "core/dom/ExecutionContext.h" | 7 #include "core/dom/ExecutionContext.h" |
| 8 #include "core/dom/TaskRunnerHelper.h" | 8 #include "core/dom/TaskRunnerHelper.h" |
| 9 #include "platform/testing/UnitTestHelpers.h" | 9 #include "platform/testing/UnitTestHelpers.h" |
| 10 #include "public/platform/WebTaskRunner.h" | 10 #include "public/platform/WebTaskRunner.h" |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 142 ++m_notificationToken; | 142 ++m_notificationToken; |
| 143 } | 143 } |
| 144 | 144 |
| 145 DEFINE_TRACE(BytesConsumerTestUtil::ReplayingBytesConsumer) | 145 DEFINE_TRACE(BytesConsumerTestUtil::ReplayingBytesConsumer) |
| 146 { | 146 { |
| 147 visitor->trace(m_executionContext); | 147 visitor->trace(m_executionContext); |
| 148 visitor->trace(m_client); | 148 visitor->trace(m_client); |
| 149 BytesConsumer::trace(visitor); | 149 BytesConsumer::trace(visitor); |
| 150 } | 150 } |
| 151 | 151 |
| 152 BytesConsumerTestUtil::Reader::Reader(BytesConsumer* consumer) | |
| 153 : m_consumer(consumer) | |
| 154 { | |
| 155 m_consumer->setClient(this); | |
| 156 } | |
| 157 | |
| 158 void BytesConsumerTestUtil::Reader::onStateChange() | |
| 159 { | |
| 160 while (true) { | |
| 161 // We choose 3 here because of the following reasons. | |
| 162 // - We want to split a string with multiple chunks, so we need to | |
| 163 // choose a small number. | |
| 164 // - An odd number is preferable to check an out-of-range error. | |
| 165 // - With 1, every chunk consists of one byte it's too simple. | |
| 166 char buffer[3]; | |
| 167 size_t read = 0; | |
| 168 switch (m_consumer->read(buffer, sizeof(buffer), &read)) { | |
| 169 case BytesConsumer::Result::Ok: | |
| 170 m_data.append(buffer, read); | |
| 171 break; | |
| 172 case BytesConsumer::Result::ShouldWait: | |
| 173 return; | |
| 174 case BytesConsumer::Result::Done: | |
| 175 m_result = BytesConsumer::Result::Done; | |
| 176 return; | |
| 177 case BytesConsumer::Result::Error: | |
| 178 m_result = BytesConsumer::Result::Error; | |
| 179 return; | |
| 180 } | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 std::pair<BytesConsumer::Result, Vector<char>> BytesConsumerTestUtil::Reader::ru n() | |
| 185 { | |
| 186 onStateChange(); | |
| 187 while (m_result != BytesConsumer::Result::Done && m_result != BytesConsumer: :Result::Error) | |
| 188 testing::runPendingTasks(); | |
| 189 testing::runPendingTasks(); | |
| 190 return std::make_pair(m_result, std::move(m_data)); | |
| 191 } | |
| 192 | |
| 193 BytesConsumerTestUtil::TwoPhaseReader::TwoPhaseReader(BytesConsumer* consumer) | 152 BytesConsumerTestUtil::TwoPhaseReader::TwoPhaseReader(BytesConsumer* consumer) |
| 194 : m_consumer(consumer) | 153 : m_consumer(consumer) |
| 195 { | 154 { |
| 196 m_consumer->setClient(this); | 155 m_consumer->setClient(this); |
| 197 } | 156 } |
| 198 | 157 |
| 199 | 158 |
| 200 void BytesConsumerTestUtil::TwoPhaseReader::onStateChange() | 159 void BytesConsumerTestUtil::TwoPhaseReader::onStateChange() |
| 201 { | 160 { |
| 202 while (true) { | 161 while (true) { |
| 203 const char* buffer = nullptr; | 162 const char* buffer = nullptr; |
| 204 size_t available = 0; | 163 size_t available = 0; |
| 205 switch (m_consumer->beginRead(&buffer, &available)) { | 164 switch (m_consumer->beginRead(&buffer, &available)) { |
| 206 case BytesConsumer::Result::Ok: { | 165 case BytesConsumer::Result::Ok: { |
| 207 // We don't use |available| as-is to test cases where endRead | 166 // We don't use |available| as-is to test cases where endRead |
| 208 // is called with a number smaller than |available|. We choose 3 | 167 // is called with a number smaller than |available|. We choose 3 |
| 209 // because of the same reasons as Reader::onStateChange. | 168 // because of the same reasons as Reader::onStateChange. |
| 210 size_t read = std::max(static_cast<size_t>(3), available); | 169 size_t read = std::min(static_cast<size_t>(3), available); |
|
yhirano
2016/09/20 08:44:41
This is a separate bug. I found this bug in the pr
| |
| 211 m_data.append(buffer, read); | 170 m_data.append(buffer, read); |
| 212 if (m_consumer->endRead(read) != BytesConsumer::Result::Ok) { | 171 if (m_consumer->endRead(read) != BytesConsumer::Result::Ok) { |
| 213 m_result = BytesConsumer::Result::Error; | 172 m_result = BytesConsumer::Result::Error; |
| 214 return; | 173 return; |
| 215 } | 174 } |
| 216 break; | 175 break; |
| 217 } | 176 } |
| 218 case BytesConsumer::Result::ShouldWait: | 177 case BytesConsumer::Result::ShouldWait: |
| 219 return; | 178 return; |
| 220 case BytesConsumer::Result::Done: | 179 case BytesConsumer::Result::Done: |
| 221 m_result = BytesConsumer::Result::Done; | 180 m_result = BytesConsumer::Result::Done; |
| 222 return; | 181 return; |
| 223 case BytesConsumer::Result::Error: | 182 case BytesConsumer::Result::Error: |
| 224 m_result = BytesConsumer::Result::Error; | 183 m_result = BytesConsumer::Result::Error; |
| 225 return; | 184 return; |
| 226 } | 185 } |
| 227 } | 186 } |
| 228 } | 187 } |
| 229 | 188 |
| 230 std::pair<BytesConsumer::Result, Vector<char>> BytesConsumerTestUtil::TwoPhaseRe ader::run() | 189 std::pair<BytesConsumer::Result, Vector<char>> BytesConsumerTestUtil::TwoPhaseRe ader::run() |
| 231 { | 190 { |
| 232 onStateChange(); | 191 onStateChange(); |
| 233 while (m_result != BytesConsumer::Result::Done && m_result != BytesConsumer: :Result::Error) | 192 while (m_result != BytesConsumer::Result::Done && m_result != BytesConsumer: :Result::Error) |
| 234 testing::runPendingTasks(); | 193 testing::runPendingTasks(); |
| 235 testing::runPendingTasks(); | 194 testing::runPendingTasks(); |
| 236 return std::make_pair(m_result, std::move(m_data)); | 195 return std::make_pair(m_result, std::move(m_data)); |
| 237 } | 196 } |
| 238 | 197 |
| 239 } // namespace blink | 198 } // namespace blink |
| OLD | NEW |