| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "config.h" |
| 6 #include "modules/fetch/CompositeDataConsumerHandle.h" |
| 7 |
| 8 #include "platform/Task.h" |
| 9 #include "platform/ThreadSafeFunctional.h" |
| 10 #include "platform/heap/Handle.h" |
| 11 #include "public/platform/Platform.h" |
| 12 #include "public/platform/WebThread.h" |
| 13 #include "public/platform/WebTraceLocation.h" |
| 14 #include "public/platform/WebWaitableEvent.h" |
| 15 #include "wtf/Locker.h" |
| 16 |
| 17 #include <gmock/gmock.h> |
| 18 #include <gtest/gtest.h> |
| 19 |
| 20 namespace blink { |
| 21 |
| 22 namespace { |
| 23 |
| 24 using Result = WebDataConsumerHandle::Result; |
| 25 using Flags = WebDataConsumerHandle::Flags; |
| 26 using ::testing::InSequence; |
| 27 using ::testing::Return; |
| 28 using ::testing::StrictMock; |
| 29 using Checkpoint = StrictMock<::testing::MockFunction<void(int)>>; |
| 30 |
| 31 const Result kShouldWait = WebDataConsumerHandle::ShouldWait; |
| 32 const Result kDone = WebDataConsumerHandle::Done; |
| 33 const Result kOk = WebDataConsumerHandle::Ok; |
| 34 const Result kUnexpectedError = WebDataConsumerHandle::UnexpectedError; |
| 35 const Flags kNone = WebDataConsumerHandle::FlagNone; |
| 36 |
| 37 class NoopClient final : public WebDataConsumerHandle::Client { |
| 38 public: |
| 39 void didGetReadable() override { } |
| 40 }; |
| 41 |
| 42 class MockReader : public WebDataConsumerHandle::Reader { |
| 43 public: |
| 44 static PassOwnPtr<StrictMock<MockReader>> create() { return adoptPtr(new Str
ictMock<MockReader>); } |
| 45 |
| 46 MOCK_METHOD4(read, Result(void*, size_t, Flags, size_t*)); |
| 47 MOCK_METHOD3(beginRead, Result(const void**, Flags, size_t*)); |
| 48 MOCK_METHOD1(endRead, Result(size_t)); |
| 49 }; |
| 50 |
| 51 class MockHandle : public WebDataConsumerHandle { |
| 52 public: |
| 53 static PassOwnPtr<StrictMock<MockHandle>> create() { return adoptPtr(new Str
ictMock<MockHandle>); } |
| 54 |
| 55 MOCK_METHOD1(obtainReaderInternal, Reader*(Client*)); |
| 56 }; |
| 57 |
| 58 class ThreadingTestBase : public ThreadSafeRefCounted<ThreadingTestBase> { |
| 59 public: |
| 60 class Context : public ThreadSafeRefCounted<Context> { |
| 61 public: |
| 62 static PassRefPtr<Context> create() { return adoptRef(new Context); } |
| 63 void recordAttach(const String& handle) |
| 64 { |
| 65 MutexLocker locker(m_loggingMutex); |
| 66 m_result.append("A reader is attached to " + handle + " on " + curre
ntThreadName() + ".\n"); |
| 67 } |
| 68 void recordDetach(const String& handle) |
| 69 { |
| 70 MutexLocker locker(m_loggingMutex); |
| 71 m_result.append("A reader is detached from " + handle + " on " + cur
rentThreadName() + ".\n"); |
| 72 } |
| 73 |
| 74 const String& result() |
| 75 { |
| 76 MutexLocker locker(m_loggingMutex); |
| 77 return m_result; |
| 78 } |
| 79 WebThread* readingThread() { return m_readingThread.get(); } |
| 80 WebThread* updatingThread() { return m_updatingThread.get(); } |
| 81 |
| 82 private: |
| 83 Context() |
| 84 : m_readingThread(adoptPtr(Platform::current()->createThread("readin
g thread"))) |
| 85 , m_updatingThread(adoptPtr(Platform::current()->createThread("updat
ing thread"))) |
| 86 { |
| 87 } |
| 88 String currentThreadName() |
| 89 { |
| 90 if (m_readingThread->isCurrentThread()) |
| 91 return "the reading thread"; |
| 92 if (m_updatingThread->isCurrentThread()) |
| 93 return "the updating thread"; |
| 94 return "an unknown thread"; |
| 95 } |
| 96 |
| 97 OwnPtr<WebThread> m_readingThread; |
| 98 OwnPtr<WebThread> m_updatingThread; |
| 99 Mutex m_loggingMutex; |
| 100 String m_result; |
| 101 }; |
| 102 |
| 103 class ReaderImpl final : public WebDataConsumerHandle::Reader { |
| 104 public: |
| 105 ReaderImpl(const String& name, PassRefPtr<Context> context) : m_name(nam
e.isolatedCopy()), m_context(context) |
| 106 { |
| 107 m_context->recordAttach(m_name.isolatedCopy()); |
| 108 } |
| 109 ~ReaderImpl() override { m_context->recordDetach(m_name.isolatedCopy());
} |
| 110 Result read(void*, size_t, Flags, size_t*) override { return kShouldWait
; } |
| 111 Result beginRead(const void**, Flags, size_t*) override { return kShould
Wait; } |
| 112 Result endRead(size_t) override { return kUnexpectedError; } |
| 113 |
| 114 private: |
| 115 const String m_name; |
| 116 RefPtr<Context> m_context; |
| 117 }; |
| 118 class DataConsumerHandle final : public WebDataConsumerHandle { |
| 119 public: |
| 120 DataConsumerHandle(const String& name, PassRefPtr<Context> context) : m_
name(name.isolatedCopy()), m_context(context) { } |
| 121 |
| 122 private: |
| 123 Reader* obtainReaderInternal(Client*) { return new ReaderImpl(m_name, m_
context); } |
| 124 const String m_name; |
| 125 RefPtr<Context> m_context; |
| 126 }; |
| 127 |
| 128 void resetReader() { m_reader = nullptr; } |
| 129 void signalDone() { m_waitableEvent->signal(); } |
| 130 const String& result() { return m_context->result(); } |
| 131 WebThread* readingThread() { return m_context->readingThread(); } |
| 132 WebThread* updatingThread() { return m_context->updatingThread(); } |
| 133 |
| 134 protected: |
| 135 RefPtr<Context> m_context; |
| 136 OwnPtr<CompositeDataConsumerHandle> m_handle; |
| 137 OwnPtr<WebDataConsumerHandle::Reader> m_reader; |
| 138 OwnPtr<WebWaitableEvent> m_waitableEvent; |
| 139 NoopClient m_client; |
| 140 }; |
| 141 |
| 142 class ThreadingRegistrationTest : public ThreadingTestBase { |
| 143 public: |
| 144 using Self = ThreadingRegistrationTest; |
| 145 void run() |
| 146 { |
| 147 m_context = Context::create(); |
| 148 m_waitableEvent = adoptPtr(Platform::current()->createWaitableEvent()); |
| 149 m_handle = CompositeDataConsumerHandle::create(adoptPtr(new DataConsumer
Handle("handle1", m_context))); |
| 150 |
| 151 readingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self::obta
inReader, this))); |
| 152 |
| 153 m_waitableEvent->wait(); |
| 154 } |
| 155 |
| 156 private: |
| 157 void obtainReader() |
| 158 { |
| 159 m_reader = m_handle->obtainReader(&m_client); |
| 160 updatingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self::upd
ate, this))); |
| 161 } |
| 162 void update() |
| 163 { |
| 164 m_handle->update(adoptPtr(new DataConsumerHandle("handle2", m_context)))
; |
| 165 readingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self::rese
tReader, this))); |
| 166 readingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self::sign
alDone, this))); |
| 167 } |
| 168 }; |
| 169 |
| 170 class ThreadingRegistrationDeleteHandleTest : public ThreadingTestBase { |
| 171 public: |
| 172 using Self = ThreadingRegistrationDeleteHandleTest; |
| 173 void run() |
| 174 { |
| 175 m_context = Context::create(); |
| 176 m_waitableEvent = adoptPtr(Platform::current()->createWaitableEvent()); |
| 177 m_handle = CompositeDataConsumerHandle::create(adoptPtr(new DataConsumer
Handle("handle1", m_context))); |
| 178 |
| 179 readingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self::obta
inReader, this))); |
| 180 |
| 181 m_waitableEvent->wait(); |
| 182 } |
| 183 |
| 184 private: |
| 185 void obtainReader() |
| 186 { |
| 187 m_reader = m_handle->obtainReader(&m_client); |
| 188 updatingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self::upd
ate, this))); |
| 189 } |
| 190 void update() |
| 191 { |
| 192 m_handle->update(adoptPtr(new DataConsumerHandle("handle2", m_context)))
; |
| 193 m_handle = nullptr; |
| 194 readingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self::rese
tReader, this))); |
| 195 readingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self::sign
alDone, this))); |
| 196 } |
| 197 }; |
| 198 |
| 199 class ThreadingRegistrationDeleteReaderTest : public ThreadingTestBase { |
| 200 public: |
| 201 using Self = ThreadingRegistrationDeleteReaderTest; |
| 202 void run() |
| 203 { |
| 204 m_context = Context::create(); |
| 205 m_waitableEvent = adoptPtr(Platform::current()->createWaitableEvent()); |
| 206 m_handle = CompositeDataConsumerHandle::create(adoptPtr(new DataConsumer
Handle("handle1", m_context))); |
| 207 |
| 208 readingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self::obta
inReader, this))); |
| 209 |
| 210 m_waitableEvent->wait(); |
| 211 } |
| 212 |
| 213 private: |
| 214 void obtainReader() |
| 215 { |
| 216 m_reader = m_handle->obtainReader(&m_client); |
| 217 updatingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self::upd
ate, this))); |
| 218 } |
| 219 void update() |
| 220 { |
| 221 readingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self::rese
tReader, this))); |
| 222 m_handle->update(adoptPtr(new DataConsumerHandle("handle2", m_context)))
; |
| 223 readingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self::rese
tReader, this))); |
| 224 readingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self::sign
alDone, this))); |
| 225 } |
| 226 }; |
| 227 |
| 228 class ThreadingUpdatingReaderWhileUpdatingTest : public ThreadingTestBase { |
| 229 public: |
| 230 using Self = ThreadingUpdatingReaderWhileUpdatingTest; |
| 231 void run() |
| 232 { |
| 233 m_context = Context::create(); |
| 234 m_waitableEvent = adoptPtr(Platform::current()->createWaitableEvent()); |
| 235 m_updateEvent = adoptPtr(Platform::current()->createWaitableEvent()); |
| 236 m_handle = CompositeDataConsumerHandle::create(adoptPtr(new DataConsumer
Handle("handle1", m_context))); |
| 237 |
| 238 readingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self::obta
inReader, this))); |
| 239 |
| 240 m_waitableEvent->wait(); |
| 241 } |
| 242 |
| 243 private: |
| 244 void obtainReader() |
| 245 { |
| 246 m_reader = m_handle->obtainReader(&m_client); |
| 247 updatingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self::upd
ate, this))); |
| 248 m_updateEvent->wait(); |
| 249 } |
| 250 |
| 251 void update() |
| 252 { |
| 253 readingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self::reob
tainReader, this))); |
| 254 m_handle->update(adoptPtr(new DataConsumerHandle("handle2", m_context)))
; |
| 255 readingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self::rese
tReader, this))); |
| 256 readingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self::sign
alDone, this))); |
| 257 m_updateEvent->signal(); |
| 258 } |
| 259 |
| 260 void reobtainReader() |
| 261 { |
| 262 m_reader = nullptr; |
| 263 m_reader = m_handle->obtainReader(&m_client); |
| 264 } |
| 265 |
| 266 OwnPtr<WebWaitableEvent> m_updateEvent; |
| 267 }; |
| 268 |
| 269 class ThreadingRegistrationUpdateTwiceAtOneTimeTest : public ThreadingTestBase { |
| 270 public: |
| 271 using Self = ThreadingRegistrationUpdateTwiceAtOneTimeTest; |
| 272 void run() |
| 273 { |
| 274 m_context = Context::create(); |
| 275 m_waitableEvent = adoptPtr(Platform::current()->createWaitableEvent()); |
| 276 m_handle = CompositeDataConsumerHandle::create(adoptPtr(new DataConsumer
Handle("handle1", m_context))); |
| 277 |
| 278 readingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self::obta
inReader, this))); |
| 279 |
| 280 m_waitableEvent->wait(); |
| 281 } |
| 282 |
| 283 private: |
| 284 void obtainReader() |
| 285 { |
| 286 m_reader = m_handle->obtainReader(&m_client); |
| 287 updatingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self::upd
ate, this))); |
| 288 } |
| 289 void update() |
| 290 { |
| 291 m_handle->update(adoptPtr(new DataConsumerHandle("handle2", m_context)))
; |
| 292 m_handle->update(adoptPtr(new DataConsumerHandle("handle3", m_context)))
; |
| 293 readingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self::rese
tReader, this))); |
| 294 readingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self::sign
alDone, this))); |
| 295 } |
| 296 }; |
| 297 |
| 298 class ThreadingDoneHandleNotificationTest : public ThreadingTestBase, public Web
DataConsumerHandle::Client { |
| 299 public: |
| 300 using Self = ThreadingDoneHandleNotificationTest; |
| 301 void run() |
| 302 { |
| 303 m_context = Context::create(); |
| 304 m_waitableEvent = adoptPtr(Platform::current()->createWaitableEvent()); |
| 305 m_handle = CompositeDataConsumerHandle::create(CompositeDataConsumerHand
le::createDoneHandle()); |
| 306 |
| 307 readingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self::obta
inReader, this))); |
| 308 |
| 309 m_waitableEvent->wait(); |
| 310 } |
| 311 |
| 312 private: |
| 313 void obtainReader() |
| 314 { |
| 315 m_reader = m_handle->obtainReader(this); |
| 316 } |
| 317 void didGetReadable() override |
| 318 { |
| 319 readingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self::rese
tReader, this))); |
| 320 readingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self::sign
alDone, this))); |
| 321 } |
| 322 }; |
| 323 |
| 324 class ThreadingDoneHandleNoNotificationTest : public ThreadingTestBase, public W
ebDataConsumerHandle::Client { |
| 325 public: |
| 326 using Self = ThreadingDoneHandleNoNotificationTest; |
| 327 void run() |
| 328 { |
| 329 m_context = Context::create(); |
| 330 m_waitableEvent = adoptPtr(Platform::current()->createWaitableEvent()); |
| 331 m_handle = CompositeDataConsumerHandle::create(CompositeDataConsumerHand
le::createDoneHandle()); |
| 332 |
| 333 readingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self::obta
inReader, this))); |
| 334 |
| 335 m_waitableEvent->wait(); |
| 336 } |
| 337 |
| 338 private: |
| 339 void obtainReader() |
| 340 { |
| 341 m_reader = m_handle->obtainReader(this); |
| 342 m_reader = nullptr; |
| 343 readingThread()->postTask(FROM_HERE, new Task(threadSafeBind(&Self::sign
alDone, this))); |
| 344 } |
| 345 void didGetReadable() override |
| 346 { |
| 347 ASSERT_NOT_REACHED(); |
| 348 } |
| 349 }; |
| 350 |
| 351 TEST(CompositeDataConsumerHandleTest, CreateWaitingHandle) |
| 352 { |
| 353 char buffer[20]; |
| 354 const void* p = nullptr; |
| 355 size_t size = 0; |
| 356 OwnPtr<WebDataConsumerHandle> handle = CompositeDataConsumerHandle::createWa
itingHandle(); |
| 357 OwnPtr<WebDataConsumerHandle::Reader> reader = handle->obtainReader(nullptr)
; |
| 358 |
| 359 EXPECT_EQ(kShouldWait, reader->read(buffer, sizeof(buffer), kNone, &size)); |
| 360 EXPECT_EQ(kShouldWait, reader->beginRead(&p, kNone, &size)); |
| 361 EXPECT_EQ(kUnexpectedError, reader->endRead(99)); |
| 362 } |
| 363 |
| 364 TEST(CompositeDataConsumerHandleTest, CreateDoneHandle) |
| 365 { |
| 366 char buffer[20]; |
| 367 const void* p = nullptr; |
| 368 size_t size = 0; |
| 369 OwnPtr<WebDataConsumerHandle> handle = CompositeDataConsumerHandle::createDo
neHandle(); |
| 370 OwnPtr<WebDataConsumerHandle::Reader> reader = handle->obtainReader(nullptr)
; |
| 371 |
| 372 EXPECT_EQ(kDone, reader->read(buffer, sizeof(buffer), kNone, &size)); |
| 373 EXPECT_EQ(kDone, reader->beginRead(&p, kNone, &size)); |
| 374 EXPECT_EQ(kUnexpectedError, reader->endRead(99)); |
| 375 } |
| 376 |
| 377 TEST(CompositeDataConsumerHandleTest, Read) |
| 378 { |
| 379 char buffer[20]; |
| 380 size_t size = 0; |
| 381 NoopClient client; |
| 382 Checkpoint checkpoint; |
| 383 |
| 384 OwnPtr<MockHandle> handle1 = MockHandle::create(); |
| 385 OwnPtr<MockHandle> handle2 = MockHandle::create(); |
| 386 OwnPtr<MockReader> reader1 = MockReader::create(); |
| 387 OwnPtr<MockReader> reader2 = MockReader::create(); |
| 388 |
| 389 InSequence s; |
| 390 EXPECT_CALL(checkpoint, Call(0)); |
| 391 EXPECT_CALL(*handle1, obtainReaderInternal(&client)).WillOnce(Return(reader1
.get())); |
| 392 EXPECT_CALL(checkpoint, Call(1)); |
| 393 EXPECT_CALL(*reader1, read(buffer, sizeof(buffer), kNone, &size)).WillOnce(R
eturn(kOk)); |
| 394 EXPECT_CALL(checkpoint, Call(2)); |
| 395 EXPECT_CALL(*handle2, obtainReaderInternal(&client)).WillOnce(Return(reader2
.get())); |
| 396 EXPECT_CALL(checkpoint, Call(3)); |
| 397 EXPECT_CALL(*reader2, read(buffer, sizeof(buffer), kNone, &size)).WillOnce(R
eturn(kOk)); |
| 398 EXPECT_CALL(checkpoint, Call(4)); |
| 399 |
| 400 // They are adopted by |obtainReader|. |
| 401 ASSERT_TRUE(reader1.leakPtr()); |
| 402 ASSERT_TRUE(reader2.leakPtr()); |
| 403 |
| 404 OwnPtr<CompositeDataConsumerHandle> handle = CompositeDataConsumerHandle::cr
eate(handle1.release()); |
| 405 checkpoint.Call(0); |
| 406 OwnPtr<CompositeDataConsumerHandle::Reader> reader = handle->obtainReader(&c
lient); |
| 407 checkpoint.Call(1); |
| 408 EXPECT_EQ(kOk, reader->read(buffer, sizeof(buffer), kNone, &size)); |
| 409 checkpoint.Call(2); |
| 410 handle->update(handle2.release()); |
| 411 checkpoint.Call(3); |
| 412 EXPECT_EQ(kOk, reader->read(buffer, sizeof(buffer), kNone, &size)); |
| 413 checkpoint.Call(4); |
| 414 } |
| 415 |
| 416 TEST(CompositeDataConsumerHandleTest, TwoPhaseRead) |
| 417 { |
| 418 const void* p = nullptr; |
| 419 size_t size = 0; |
| 420 Checkpoint checkpoint; |
| 421 |
| 422 OwnPtr<MockHandle> handle1 = MockHandle::create(); |
| 423 OwnPtr<MockHandle> handle2 = MockHandle::create(); |
| 424 OwnPtr<MockReader> reader1 = MockReader::create(); |
| 425 OwnPtr<MockReader> reader2 = MockReader::create(); |
| 426 |
| 427 InSequence s; |
| 428 EXPECT_CALL(checkpoint, Call(0)); |
| 429 EXPECT_CALL(*handle1, obtainReaderInternal(nullptr)).WillOnce(Return(reader1
.get())); |
| 430 EXPECT_CALL(checkpoint, Call(1)); |
| 431 EXPECT_CALL(*reader1, beginRead(&p, kNone, &size)).WillOnce(Return(kOk)); |
| 432 EXPECT_CALL(checkpoint, Call(2)); |
| 433 EXPECT_CALL(*reader1, endRead(0)).WillOnce(Return(kOk)); |
| 434 EXPECT_CALL(checkpoint, Call(3)); |
| 435 EXPECT_CALL(*handle2, obtainReaderInternal(nullptr)).WillOnce(Return(reader2
.get())); |
| 436 EXPECT_CALL(checkpoint, Call(4)); |
| 437 EXPECT_CALL(*reader2, beginRead(&p, kNone, &size)).WillOnce(Return(kOk)); |
| 438 EXPECT_CALL(checkpoint, Call(5)); |
| 439 EXPECT_CALL(*reader2, endRead(0)).WillOnce(Return(kOk)); |
| 440 EXPECT_CALL(checkpoint, Call(6)); |
| 441 |
| 442 // They are adopted by |obtainReader|. |
| 443 ASSERT_TRUE(reader1.leakPtr()); |
| 444 ASSERT_TRUE(reader2.leakPtr()); |
| 445 |
| 446 OwnPtr<CompositeDataConsumerHandle> handle = CompositeDataConsumerHandle::cr
eate(handle1.release()); |
| 447 checkpoint.Call(0); |
| 448 OwnPtr<CompositeDataConsumerHandle::Reader> reader = handle->obtainReader(nu
llptr); |
| 449 checkpoint.Call(1); |
| 450 EXPECT_EQ(kOk, reader->beginRead(&p, kNone, &size)); |
| 451 checkpoint.Call(2); |
| 452 EXPECT_EQ(kOk, reader->endRead(0)); |
| 453 checkpoint.Call(3); |
| 454 handle->update(handle2.release()); |
| 455 checkpoint.Call(4); |
| 456 EXPECT_EQ(kOk, reader->beginRead(&p, kNone, &size)); |
| 457 checkpoint.Call(5); |
| 458 EXPECT_EQ(kOk, reader->endRead(0)); |
| 459 checkpoint.Call(6); |
| 460 } |
| 461 |
| 462 TEST(CompositeDataConsumerHandleTest, HangingTwoPhaseRead) |
| 463 { |
| 464 const void* p = nullptr; |
| 465 size_t size = 0; |
| 466 Checkpoint checkpoint; |
| 467 |
| 468 OwnPtr<MockHandle> handle1 = MockHandle::create(); |
| 469 OwnPtr<MockHandle> handle2 = MockHandle::create(); |
| 470 OwnPtr<MockHandle> handle3 = MockHandle::create(); |
| 471 OwnPtr<MockReader> reader1 = MockReader::create(); |
| 472 OwnPtr<MockReader> reader2 = MockReader::create(); |
| 473 OwnPtr<MockReader> reader3 = MockReader::create(); |
| 474 |
| 475 InSequence s; |
| 476 EXPECT_CALL(checkpoint, Call(0)); |
| 477 EXPECT_CALL(*handle1, obtainReaderInternal(nullptr)).WillOnce(Return(reader1
.get())); |
| 478 EXPECT_CALL(checkpoint, Call(1)); |
| 479 EXPECT_CALL(*reader1, beginRead(&p, kNone, &size)).WillOnce(Return(kOk)); |
| 480 EXPECT_CALL(checkpoint, Call(2)); |
| 481 EXPECT_CALL(checkpoint, Call(3)); |
| 482 EXPECT_CALL(*reader1, endRead(0)).WillOnce(Return(kOk)); |
| 483 EXPECT_CALL(*handle2, obtainReaderInternal(nullptr)).WillOnce(Return(reader2
.get())); |
| 484 EXPECT_CALL(checkpoint, Call(4)); |
| 485 EXPECT_CALL(*reader2, beginRead(&p, kNone, &size)).WillOnce(Return(kShouldWa
it)); |
| 486 EXPECT_CALL(checkpoint, Call(5)); |
| 487 EXPECT_CALL(*handle3, obtainReaderInternal(nullptr)).WillOnce(Return(reader3
.get())); |
| 488 EXPECT_CALL(checkpoint, Call(6)); |
| 489 EXPECT_CALL(*reader3, beginRead(&p, kNone, &size)).WillOnce(Return(kOk)); |
| 490 EXPECT_CALL(checkpoint, Call(7)); |
| 491 EXPECT_CALL(*reader3, endRead(0)).WillOnce(Return(kOk)); |
| 492 EXPECT_CALL(checkpoint, Call(8)); |
| 493 |
| 494 // They are adopted by |obtainReader|. |
| 495 ASSERT_TRUE(reader1.leakPtr()); |
| 496 ASSERT_TRUE(reader2.leakPtr()); |
| 497 ASSERT_TRUE(reader3.leakPtr()); |
| 498 |
| 499 OwnPtr<CompositeDataConsumerHandle> handle = CompositeDataConsumerHandle::cr
eate(handle1.release()); |
| 500 checkpoint.Call(0); |
| 501 OwnPtr<CompositeDataConsumerHandle::Reader> reader = handle->obtainReader(nu
llptr); |
| 502 checkpoint.Call(1); |
| 503 EXPECT_EQ(kOk, reader->beginRead(&p, kNone, &size)); |
| 504 checkpoint.Call(2); |
| 505 handle->update(handle2.release()); |
| 506 checkpoint.Call(3); |
| 507 EXPECT_EQ(kOk, reader->endRead(0)); |
| 508 checkpoint.Call(4); |
| 509 EXPECT_EQ(kShouldWait, reader->beginRead(&p, kNone, &size)); |
| 510 checkpoint.Call(5); |
| 511 handle->update(handle3.release()); |
| 512 checkpoint.Call(6); |
| 513 EXPECT_EQ(kOk, reader->beginRead(&p, kNone, &size)); |
| 514 checkpoint.Call(7); |
| 515 EXPECT_EQ(kOk, reader->endRead(0)); |
| 516 checkpoint.Call(8); |
| 517 } |
| 518 |
| 519 TEST(CompositeDataConsumerHandleTest, RegisterClientOnDifferentThreads) |
| 520 { |
| 521 ThreadingRegistrationTest test; |
| 522 test.run(); |
| 523 |
| 524 EXPECT_EQ( |
| 525 "A reader is attached to handle1 on the reading thread.\n" |
| 526 "A reader is detached from handle1 on the reading thread.\n" |
| 527 "A reader is attached to handle2 on the reading thread.\n" |
| 528 "A reader is detached from handle2 on the reading thread.\n", |
| 529 test.result()); |
| 530 } |
| 531 |
| 532 TEST(CompositeDataConsumerHandleTest, DeleteHandleWhileUpdating) |
| 533 { |
| 534 ThreadingRegistrationDeleteHandleTest test; |
| 535 test.run(); |
| 536 |
| 537 EXPECT_EQ( |
| 538 "A reader is attached to handle1 on the reading thread.\n" |
| 539 "A reader is detached from handle1 on the reading thread.\n" |
| 540 "A reader is attached to handle2 on the reading thread.\n" |
| 541 "A reader is detached from handle2 on the reading thread.\n", |
| 542 test.result()); |
| 543 } |
| 544 |
| 545 TEST(CompositeDataConsumerHandleTest, DeleteReaderWhileUpdating) |
| 546 { |
| 547 ThreadingRegistrationDeleteReaderTest test; |
| 548 test.run(); |
| 549 |
| 550 EXPECT_EQ( |
| 551 "A reader is attached to handle1 on the reading thread.\n" |
| 552 "A reader is detached from handle1 on the reading thread.\n", |
| 553 test.result()); |
| 554 } |
| 555 |
| 556 TEST(CompositeDataConsumerHandleTest, UpdateReaderWhileUpdating) |
| 557 { |
| 558 ThreadingUpdatingReaderWhileUpdatingTest test; |
| 559 test.run(); |
| 560 |
| 561 EXPECT_EQ( |
| 562 "A reader is attached to handle1 on the reading thread.\n" |
| 563 "A reader is detached from handle1 on the reading thread.\n" |
| 564 "A reader is attached to handle2 on the reading thread.\n" |
| 565 "A reader is detached from handle2 on the reading thread.\n", |
| 566 test.result()); |
| 567 } |
| 568 |
| 569 TEST(CompositeDataConsumerHandleTest, UpdateTwiceAtOnce) |
| 570 { |
| 571 ThreadingRegistrationUpdateTwiceAtOneTimeTest test; |
| 572 test.run(); |
| 573 |
| 574 EXPECT_EQ( |
| 575 "A reader is attached to handle1 on the reading thread.\n" |
| 576 "A reader is detached from handle1 on the reading thread.\n" |
| 577 "A reader is attached to handle3 on the reading thread.\n" |
| 578 "A reader is detached from handle3 on the reading thread.\n", |
| 579 test.result()); |
| 580 } |
| 581 |
| 582 TEST(CompositeDataConsumerHandleTest, DoneHandleNotification) |
| 583 { |
| 584 ThreadingDoneHandleNotificationTest test; |
| 585 // Test this function returns. |
| 586 test.run(); |
| 587 } |
| 588 |
| 589 TEST(CompositeDataConsumerHandleTest, DoneHandleNoNotification) |
| 590 { |
| 591 ThreadingDoneHandleNoNotificationTest test; |
| 592 // Test this function doesn't crash. |
| 593 test.run(); |
| 594 } |
| 595 |
| 596 } // namespace |
| 597 |
| 598 } // namespace blink |
| OLD | NEW |