OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "headless/public/util/deterministic_dispatcher.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <string> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/run_loop.h" |
| 12 #include "base/single_thread_task_runner.h" |
| 13 #include "headless/public/util/testing/fake_managed_dispatch_url_request_job.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 using testing::ElementsAre; |
| 18 |
| 19 namespace headless { |
| 20 |
| 21 class DeterministicDispatcherTest : public ::testing::Test { |
| 22 protected: |
| 23 DeterministicDispatcherTest() {} |
| 24 ~DeterministicDispatcherTest() override {} |
| 25 |
| 26 void SetUp() override { |
| 27 deterministic_dispatcher_.reset( |
| 28 new DeterministicDispatcher(loop_.task_runner())); |
| 29 } |
| 30 |
| 31 base::MessageLoop loop_; |
| 32 std::unique_ptr<DeterministicDispatcher> deterministic_dispatcher_; |
| 33 }; |
| 34 |
| 35 TEST_F(DeterministicDispatcherTest, DispatchDataReadyInReverseOrder) { |
| 36 std::vector<std::string> notifications; |
| 37 std::unique_ptr<FakeManagedDispatchURLRequestJob> job1( |
| 38 new FakeManagedDispatchURLRequestJob(deterministic_dispatcher_.get(), 1, |
| 39 ¬ifications)); |
| 40 std::unique_ptr<FakeManagedDispatchURLRequestJob> job2( |
| 41 new FakeManagedDispatchURLRequestJob(deterministic_dispatcher_.get(), 2, |
| 42 ¬ifications)); |
| 43 std::unique_ptr<FakeManagedDispatchURLRequestJob> job3( |
| 44 new FakeManagedDispatchURLRequestJob(deterministic_dispatcher_.get(), 3, |
| 45 ¬ifications)); |
| 46 std::unique_ptr<FakeManagedDispatchURLRequestJob> job4( |
| 47 new FakeManagedDispatchURLRequestJob(deterministic_dispatcher_.get(), 4, |
| 48 ¬ifications)); |
| 49 job4->DispatchHeadersComplete(); |
| 50 job3->DispatchHeadersComplete(); |
| 51 job2->DispatchHeadersComplete(); |
| 52 job1->DispatchHeadersComplete(); |
| 53 |
| 54 EXPECT_TRUE(notifications.empty()); |
| 55 |
| 56 base::RunLoop().RunUntilIdle(); |
| 57 EXPECT_THAT(notifications, ElementsAre("id: 1 OK")); |
| 58 |
| 59 job1.reset(); |
| 60 base::RunLoop().RunUntilIdle(); |
| 61 EXPECT_THAT(notifications, ElementsAre("id: 1 OK", "id: 2 OK")); |
| 62 |
| 63 job2.reset(); |
| 64 base::RunLoop().RunUntilIdle(); |
| 65 EXPECT_THAT(notifications, ElementsAre("id: 1 OK", "id: 2 OK", "id: 3 OK")); |
| 66 |
| 67 job3.reset(); |
| 68 base::RunLoop().RunUntilIdle(); |
| 69 EXPECT_THAT(notifications, |
| 70 ElementsAre("id: 1 OK", "id: 2 OK", "id: 3 OK", "id: 4 OK")); |
| 71 } |
| 72 |
| 73 TEST_F(DeterministicDispatcherTest, |
| 74 ErrorsAndDataReadyDispatchedInCreationOrder) { |
| 75 std::vector<std::string> notifications; |
| 76 std::unique_ptr<FakeManagedDispatchURLRequestJob> job1( |
| 77 new FakeManagedDispatchURLRequestJob(deterministic_dispatcher_.get(), 1, |
| 78 ¬ifications)); |
| 79 std::unique_ptr<FakeManagedDispatchURLRequestJob> job2( |
| 80 new FakeManagedDispatchURLRequestJob(deterministic_dispatcher_.get(), 2, |
| 81 ¬ifications)); |
| 82 std::unique_ptr<FakeManagedDispatchURLRequestJob> job3( |
| 83 new FakeManagedDispatchURLRequestJob(deterministic_dispatcher_.get(), 3, |
| 84 ¬ifications)); |
| 85 std::unique_ptr<FakeManagedDispatchURLRequestJob> job4( |
| 86 new FakeManagedDispatchURLRequestJob(deterministic_dispatcher_.get(), 4, |
| 87 ¬ifications)); |
| 88 job4->DispatchHeadersComplete(); |
| 89 job3->DispatchStartError(static_cast<net::Error>(-123)); |
| 90 job2->DispatchHeadersComplete(); |
| 91 job1->DispatchHeadersComplete(); |
| 92 |
| 93 EXPECT_TRUE(notifications.empty()); |
| 94 |
| 95 base::RunLoop().RunUntilIdle(); |
| 96 EXPECT_THAT(notifications, ElementsAre("id: 1 OK")); |
| 97 |
| 98 job1.reset(); |
| 99 base::RunLoop().RunUntilIdle(); |
| 100 EXPECT_THAT(notifications, ElementsAre("id: 1 OK", "id: 2 OK")); |
| 101 |
| 102 job2.reset(); |
| 103 base::RunLoop().RunUntilIdle(); |
| 104 EXPECT_THAT(notifications, |
| 105 ElementsAre("id: 1 OK", "id: 2 OK", "id: 3 err: -123")); |
| 106 |
| 107 job3.reset(); |
| 108 base::RunLoop().RunUntilIdle(); |
| 109 EXPECT_THAT(notifications, ElementsAre("id: 1 OK", "id: 2 OK", |
| 110 "id: 3 err: -123", "id: 4 OK")); |
| 111 } |
| 112 |
| 113 } // namespace headless |
OLD | NEW |