| 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 <algorithm> |
| 6 #include <memory> |
| 7 |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "blimp/common/blob_cache/mock_blob_cache.h" |
| 10 #include "blimp/net/blob_channel/blob_channel_receiver.h" |
| 11 #include "blimp/net/blob_channel/mock_blob_channel_bindings.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace blimp { |
| 16 namespace { |
| 17 |
| 18 using testing::_; |
| 19 using testing::Pointee; |
| 20 using testing::Return; |
| 21 using testing::SaveArg; |
| 22 |
| 23 const char kBlobId1[] = "blob-1"; |
| 24 const char kBlobId2[] = "blob-2"; |
| 25 const char kBlobPayload1[] = "blob-1-payload"; |
| 26 const char kBlobPayload2[] = "blob-2-payload"; |
| 27 |
| 28 // Helper function for creating a cache payload vector from a string. |
| 29 scoped_refptr<RefCountedVector> CreatePayload(const std::string& input) { |
| 30 scoped_refptr<RefCountedVector> output(new RefCountedVector); |
| 31 output->data.assign(input.begin(), input.end()); |
| 32 return output; |
| 33 } |
| 34 |
| 35 MATCHER_P(RefCountedVectorsEqual, expected, "") { |
| 36 return expected->data == arg->data; |
| 37 } |
| 38 |
| 39 class BlobChannelReceiverTest : public testing::Test { |
| 40 public: |
| 41 BlobChannelReceiverTest() {} |
| 42 ~BlobChannelReceiverTest() override {} |
| 43 |
| 44 void SetUp() override { |
| 45 EXPECT_CALL(mock_bindings_, SetDelegate(_)) |
| 46 .WillOnce(SaveArg<0>(&delegate_)); |
| 47 blob_receiver_.reset( |
| 48 new BlobChannelReceiver(&mock_cache_, &mock_bindings_)); |
| 49 ASSERT_NE(nullptr, delegate_); |
| 50 } |
| 51 |
| 52 MOCK_METHOD2(OnBlobReceived, |
| 53 void(const std::string&, scoped_refptr<RefCountedVector>)); |
| 54 |
| 55 testing::StrictMock<MockBlobReceiverBindings> mock_bindings_; |
| 56 testing::StrictMock<MockBlobCache> mock_cache_; |
| 57 std::unique_ptr<BlobChannelReceiver> blob_receiver_; |
| 58 BlobReceiverBindings::Delegate* delegate_ = nullptr; |
| 59 }; |
| 60 |
| 61 TEST_F(BlobChannelReceiverTest, TestGetSync) { |
| 62 auto payload1 = CreatePayload(kBlobPayload1); |
| 63 EXPECT_CALL(mock_cache_, Contains(kBlobId1)).WillOnce(Return(true)); |
| 64 EXPECT_CALL(mock_cache_, Get(kBlobId1)).WillOnce(Return(payload1)); |
| 65 auto result = blob_receiver_->Get( |
| 66 kBlobId1, BlobChannelReceiver::BlobReceivedCallback()); |
| 67 ASSERT_NE(result.get(), nullptr); |
| 68 EXPECT_EQ(result->data, payload1->data); |
| 69 |
| 70 auto payload2 = CreatePayload(kBlobPayload2); |
| 71 EXPECT_CALL(mock_cache_, Contains(kBlobId2)).WillOnce(Return(true)); |
| 72 EXPECT_CALL(mock_cache_, Get(kBlobId2)).WillOnce(Return(payload2)); |
| 73 result = blob_receiver_->Get(kBlobId2, |
| 74 BlobChannelReceiver::BlobReceivedCallback()); |
| 75 ASSERT_NE(result.get(), nullptr); |
| 76 EXPECT_EQ(result->data, payload2->data); |
| 77 } |
| 78 |
| 79 // Test concurrent async reads across multiple blobs. |
| 80 // kBlobId1: Get() called twice. |
| 81 // kBlobId2: Get() called once. |
| 82 TEST_F(BlobChannelReceiverTest, TestGetAsync) { |
| 83 auto payload1 = CreatePayload(kBlobPayload1); |
| 84 auto payload2 = CreatePayload(kBlobPayload2); |
| 85 |
| 86 EXPECT_CALL(mock_cache_, Contains(kBlobId1)) |
| 87 .Times(3) |
| 88 .WillRepeatedly(Return(false)); |
| 89 EXPECT_CALL(mock_bindings_, Get(kBlobId1)); |
| 90 EXPECT_CALL(mock_cache_, Contains(kBlobId2)) |
| 91 .Times(2) |
| 92 .WillRepeatedly(Return(false)); |
| 93 EXPECT_CALL(mock_bindings_, Get(kBlobId2)); |
| 94 |
| 95 // Async Get() operations are started. |
| 96 ASSERT_EQ( |
| 97 nullptr, |
| 98 blob_receiver_ |
| 99 ->Get(kBlobId1, base::Bind(&BlobChannelReceiverTest::OnBlobReceived, |
| 100 base::Unretained(this))) |
| 101 .get()); |
| 102 ASSERT_EQ( |
| 103 nullptr, |
| 104 blob_receiver_ |
| 105 ->Get(kBlobId1, base::Bind(&BlobChannelReceiverTest::OnBlobReceived, |
| 106 base::Unretained(this))) |
| 107 .get()); |
| 108 ASSERT_EQ( |
| 109 nullptr, |
| 110 blob_receiver_ |
| 111 ->Get(kBlobId2, base::Bind(&BlobChannelReceiverTest::OnBlobReceived, |
| 112 base::Unretained(this))) |
| 113 .get()); |
| 114 |
| 115 // Blobs start arriving asynchronously. Verify that our read callback is hit. |
| 116 EXPECT_CALL(mock_cache_, Put(kBlobId1, RefCountedVectorsEqual(payload1))); |
| 117 EXPECT_CALL(mock_cache_, Put(kBlobId2, RefCountedVectorsEqual(payload2))); |
| 118 EXPECT_CALL(*this, OnBlobReceived(kBlobId1, RefCountedVectorsEqual(payload1))) |
| 119 .Times(2); |
| 120 EXPECT_CALL(*this, |
| 121 OnBlobReceived(kBlobId2, RefCountedVectorsEqual(payload2))); |
| 122 delegate_->OnBlobReceived( |
| 123 kBlobId1, base::WrapUnique(new std::vector<uint8_t>(payload1->data))); |
| 124 delegate_->OnBlobReceived( |
| 125 kBlobId2, base::WrapUnique(new std::vector<uint8_t>(payload2->data))); |
| 126 |
| 127 // Pretend that the cache was evicted. See if another request is issued. |
| 128 EXPECT_CALL(mock_cache_, Contains(kBlobId1)).WillOnce(Return(false)); |
| 129 EXPECT_CALL(mock_bindings_, Get(kBlobId1)); |
| 130 ASSERT_EQ( |
| 131 nullptr, |
| 132 blob_receiver_ |
| 133 ->Get(kBlobId1, base::Bind(&BlobChannelReceiverTest::OnBlobReceived, |
| 134 base::Unretained(this))) |
| 135 .get()); |
| 136 } |
| 137 |
| 138 TEST_F(BlobChannelReceiverTest, TestGetRedundantArrival) { |
| 139 auto payload = CreatePayload(kBlobPayload1); |
| 140 |
| 141 EXPECT_CALL(mock_cache_, Contains(kBlobId1)) |
| 142 .WillOnce(Return(false)) |
| 143 .WillOnce(Return(true)); |
| 144 EXPECT_CALL(mock_bindings_, Get(kBlobId1)); |
| 145 |
| 146 // Async Get() operations are started. |
| 147 ASSERT_EQ( |
| 148 nullptr, |
| 149 blob_receiver_ |
| 150 ->Get(kBlobId1, base::Bind(&BlobChannelReceiverTest::OnBlobReceived, |
| 151 base::Unretained(this))) |
| 152 .get()); |
| 153 |
| 154 // Blobs start arriving asynchronously. Verify that our read callback is hit. |
| 155 EXPECT_CALL(mock_cache_, Put(kBlobId1, RefCountedVectorsEqual(payload))); |
| 156 EXPECT_CALL(*this, OnBlobReceived(kBlobId1, RefCountedVectorsEqual(payload))); |
| 157 delegate_->OnBlobReceived( |
| 158 kBlobId1, base::WrapUnique(new std::vector<uint8_t>(payload->data))); |
| 159 } |
| 160 |
| 161 } // namespace |
| 162 } // namespace blimp |
| OLD | NEW |