Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(844)

Side by Side Diff: blimp/net/blob_channel/blob_channel_receiver_unittest.cc

Issue 1891083002: Blimp: Add BlobChannelReceiver and bindings interface. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@blobchannel-sender
Patch Set: git rm *bindings* Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace blimp {
15 namespace {
16
17 using testing::_;
18 using testing::Pointee;
19 using testing::Return;
20 using testing::SaveArg;
21
22 const char kBlobId1[] = "blob-1";
23 const char kBlobId2[] = "blob-2";
24 const char kBlobPayload1[] = "blob-1-payload";
25 const char kBlobPayload2[] = "blob-2-payload";
26
27 // Helper function for creating a cache payload vector from a string.
28 scoped_refptr<RefCountedVector> CreatePayload(const std::string& input) {
29 scoped_refptr<RefCountedVector> output(new RefCountedVector);
30 output->data.assign(input.begin(), input.end());
31 return output;
32 }
33
34 MATCHER_P(RefCountedVectorsEqual, expected, "") {
35 return expected->data == arg->data;
36 }
37
38 class MockBlobReceiverDelegate : public BlobChannelReceiver::Delegate {
39 public:
40 MockBlobReceiverDelegate() {}
41 ~MockBlobReceiverDelegate() {}
42
43 MOCK_METHOD1(Request, void(const BlobId&));
44 };
45
46 class BlobChannelReceiverTest : public testing::Test {
47 public:
48 BlobChannelReceiverTest() {}
49 ~BlobChannelReceiverTest() override {}
50
51 void SetUp() override {
52 blob_receiver_.reset(new BlobChannelReceiver(&mock_cache_, &delegate_));
53 }
54
55 MOCK_METHOD2(OnBlobReceived, void(const BlobId&, BlobData));
56
57 testing::StrictMock<MockBlobReceiverDelegate> delegate_;
58 testing::StrictMock<MockBlobCache> mock_cache_;
59 std::unique_ptr<BlobChannelReceiver> blob_receiver_;
60 };
61
62 TEST_F(BlobChannelReceiverTest, TestGetSync) {
63 auto payload1 = CreatePayload(kBlobPayload1);
64 EXPECT_CALL(mock_cache_, Contains(kBlobId1)).WillOnce(Return(true));
65 EXPECT_CALL(mock_cache_, Get(kBlobId1)).WillOnce(Return(payload1));
66 auto result = blob_receiver_->Get(
67 kBlobId1, BlobChannelReceiver::BlobReceivedCallback());
68 ASSERT_NE(result.get(), nullptr);
69 EXPECT_EQ(result->data, payload1->data);
70
71 auto payload2 = CreatePayload(kBlobPayload2);
72 EXPECT_CALL(mock_cache_, Contains(kBlobId2)).WillOnce(Return(true));
73 EXPECT_CALL(mock_cache_, Get(kBlobId2)).WillOnce(Return(payload2));
74 result = blob_receiver_->Get(kBlobId2,
75 BlobChannelReceiver::BlobReceivedCallback());
76 ASSERT_NE(result.get(), nullptr);
77 EXPECT_EQ(result->data, payload2->data);
78 }
79
80 // Test concurrent async reads across multiple blobs.
81 // kBlobId1: Get() called twice.
82 // kBlobId2: Get() called once.
83 TEST_F(BlobChannelReceiverTest, TestGetAsync) {
84 auto payload1 = CreatePayload(kBlobPayload1);
85 auto payload2 = CreatePayload(kBlobPayload2);
86
87 EXPECT_CALL(mock_cache_, Contains(kBlobId1))
88 .Times(3)
89 .WillRepeatedly(Return(false));
90 EXPECT_CALL(delegate_, Request(kBlobId1));
91 EXPECT_CALL(mock_cache_, Contains(kBlobId2))
92 .Times(2)
93 .WillRepeatedly(Return(false));
94 EXPECT_CALL(delegate_, Request(kBlobId2));
95
96 // Async Get() operations are started.
97 ASSERT_EQ(
98 nullptr,
99 blob_receiver_
100 ->Get(kBlobId1, base::Bind(&BlobChannelReceiverTest::OnBlobReceived,
101 base::Unretained(this)))
102 .get());
103 ASSERT_EQ(
104 nullptr,
105 blob_receiver_
106 ->Get(kBlobId1, base::Bind(&BlobChannelReceiverTest::OnBlobReceived,
107 base::Unretained(this)))
108 .get());
109 ASSERT_EQ(
110 nullptr,
111 blob_receiver_
112 ->Get(kBlobId2, base::Bind(&BlobChannelReceiverTest::OnBlobReceived,
113 base::Unretained(this)))
114 .get());
115
116 // Blobs start arriving asynchronously. Verify that our read callback is hit.
117 EXPECT_CALL(mock_cache_, Put(kBlobId1, RefCountedVectorsEqual(payload1)));
118 EXPECT_CALL(mock_cache_, Put(kBlobId2, RefCountedVectorsEqual(payload2)));
119 EXPECT_CALL(*this, OnBlobReceived(kBlobId1, RefCountedVectorsEqual(payload1)))
120 .Times(2);
121 EXPECT_CALL(*this,
122 OnBlobReceived(kBlobId2, RefCountedVectorsEqual(payload2)));
123 delegate_.OnBlobReceived(kBlobId1, payload1);
124 delegate_.OnBlobReceived(kBlobId2, payload2);
125
126 // Pretend that the cache was evicted. See if another request is issued.
127 EXPECT_CALL(mock_cache_, Contains(kBlobId1)).WillOnce(Return(false));
128 EXPECT_CALL(delegate_, Request(kBlobId1));
129 ASSERT_EQ(
130 nullptr,
131 blob_receiver_
132 ->Get(kBlobId1, base::Bind(&BlobChannelReceiverTest::OnBlobReceived,
133 base::Unretained(this)))
134 .get());
135 }
136
137 TEST_F(BlobChannelReceiverTest, TestGetRedundantArrival) {
138 auto payload = CreatePayload(kBlobPayload1);
139
140 EXPECT_CALL(mock_cache_, Contains(kBlobId1))
141 .WillOnce(Return(false))
142 .WillOnce(Return(true));
143 EXPECT_CALL(delegate_, Request(kBlobId1));
144
145 // Async Get() operations are started.
146 ASSERT_EQ(
147 nullptr,
148 blob_receiver_
149 ->Get(kBlobId1, base::Bind(&BlobChannelReceiverTest::OnBlobReceived,
150 base::Unretained(this)))
151 .get());
152
153 // Blobs start arriving asynchronously. Verify that our read callback is hit.
154 EXPECT_CALL(mock_cache_, Put(kBlobId1, RefCountedVectorsEqual(payload)));
155 EXPECT_CALL(*this, OnBlobReceived(kBlobId1, RefCountedVectorsEqual(payload)));
156 delegate_.OnBlobReceived(kBlobId1, payload);
157 }
158
159 } // namespace
160 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698