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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: blimp/net/blob_channel/blob_channel_receiver_unittest.cc
diff --git a/blimp/net/blob_channel/blob_channel_receiver_unittest.cc b/blimp/net/blob_channel/blob_channel_receiver_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..5ab3640c7ec06e8efba9bb3db0862d9d3673d484
--- /dev/null
+++ b/blimp/net/blob_channel/blob_channel_receiver_unittest.cc
@@ -0,0 +1,160 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <algorithm>
+#include <memory>
+
+#include "base/memory/ptr_util.h"
+#include "blimp/common/blob_cache/mock_blob_cache.h"
+#include "blimp/net/blob_channel/blob_channel_receiver.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace blimp {
+namespace {
+
+using testing::_;
+using testing::Pointee;
+using testing::Return;
+using testing::SaveArg;
+
+const char kBlobId1[] = "blob-1";
+const char kBlobId2[] = "blob-2";
+const char kBlobPayload1[] = "blob-1-payload";
+const char kBlobPayload2[] = "blob-2-payload";
+
+// Helper function for creating a cache payload vector from a string.
+scoped_refptr<RefCountedVector> CreatePayload(const std::string& input) {
+ scoped_refptr<RefCountedVector> output(new RefCountedVector);
+ output->data.assign(input.begin(), input.end());
+ return output;
+}
+
+MATCHER_P(RefCountedVectorsEqual, expected, "") {
+ return expected->data == arg->data;
+}
+
+class MockBlobReceiverDelegate : public BlobChannelReceiver::Delegate {
+ public:
+ MockBlobReceiverDelegate() {}
+ ~MockBlobReceiverDelegate() {}
+
+ MOCK_METHOD1(Request, void(const BlobId&));
+};
+
+class BlobChannelReceiverTest : public testing::Test {
+ public:
+ BlobChannelReceiverTest() {}
+ ~BlobChannelReceiverTest() override {}
+
+ void SetUp() override {
+ blob_receiver_.reset(new BlobChannelReceiver(&mock_cache_, &delegate_));
+ }
+
+ MOCK_METHOD2(OnBlobReceived, void(const BlobId&, BlobData));
+
+ testing::StrictMock<MockBlobReceiverDelegate> delegate_;
+ testing::StrictMock<MockBlobCache> mock_cache_;
+ std::unique_ptr<BlobChannelReceiver> blob_receiver_;
+};
+
+TEST_F(BlobChannelReceiverTest, TestGetSync) {
+ auto payload1 = CreatePayload(kBlobPayload1);
+ EXPECT_CALL(mock_cache_, Contains(kBlobId1)).WillOnce(Return(true));
+ EXPECT_CALL(mock_cache_, Get(kBlobId1)).WillOnce(Return(payload1));
+ auto result = blob_receiver_->Get(
+ kBlobId1, BlobChannelReceiver::BlobReceivedCallback());
+ ASSERT_NE(result.get(), nullptr);
+ EXPECT_EQ(result->data, payload1->data);
+
+ auto payload2 = CreatePayload(kBlobPayload2);
+ EXPECT_CALL(mock_cache_, Contains(kBlobId2)).WillOnce(Return(true));
+ EXPECT_CALL(mock_cache_, Get(kBlobId2)).WillOnce(Return(payload2));
+ result = blob_receiver_->Get(kBlobId2,
+ BlobChannelReceiver::BlobReceivedCallback());
+ ASSERT_NE(result.get(), nullptr);
+ EXPECT_EQ(result->data, payload2->data);
+}
+
+// Test concurrent async reads across multiple blobs.
+// kBlobId1: Get() called twice.
+// kBlobId2: Get() called once.
+TEST_F(BlobChannelReceiverTest, TestGetAsync) {
+ auto payload1 = CreatePayload(kBlobPayload1);
+ auto payload2 = CreatePayload(kBlobPayload2);
+
+ EXPECT_CALL(mock_cache_, Contains(kBlobId1))
+ .Times(3)
+ .WillRepeatedly(Return(false));
+ EXPECT_CALL(delegate_, Request(kBlobId1));
+ EXPECT_CALL(mock_cache_, Contains(kBlobId2))
+ .Times(2)
+ .WillRepeatedly(Return(false));
+ EXPECT_CALL(delegate_, Request(kBlobId2));
+
+ // Async Get() operations are started.
+ ASSERT_EQ(
+ nullptr,
+ blob_receiver_
+ ->Get(kBlobId1, base::Bind(&BlobChannelReceiverTest::OnBlobReceived,
+ base::Unretained(this)))
+ .get());
+ ASSERT_EQ(
+ nullptr,
+ blob_receiver_
+ ->Get(kBlobId1, base::Bind(&BlobChannelReceiverTest::OnBlobReceived,
+ base::Unretained(this)))
+ .get());
+ ASSERT_EQ(
+ nullptr,
+ blob_receiver_
+ ->Get(kBlobId2, base::Bind(&BlobChannelReceiverTest::OnBlobReceived,
+ base::Unretained(this)))
+ .get());
+
+ // Blobs start arriving asynchronously. Verify that our read callback is hit.
+ EXPECT_CALL(mock_cache_, Put(kBlobId1, RefCountedVectorsEqual(payload1)));
+ EXPECT_CALL(mock_cache_, Put(kBlobId2, RefCountedVectorsEqual(payload2)));
+ EXPECT_CALL(*this, OnBlobReceived(kBlobId1, RefCountedVectorsEqual(payload1)))
+ .Times(2);
+ EXPECT_CALL(*this,
+ OnBlobReceived(kBlobId2, RefCountedVectorsEqual(payload2)));
+ delegate_.OnBlobReceived(kBlobId1, payload1);
+ delegate_.OnBlobReceived(kBlobId2, payload2);
+
+ // Pretend that the cache was evicted. See if another request is issued.
+ EXPECT_CALL(mock_cache_, Contains(kBlobId1)).WillOnce(Return(false));
+ EXPECT_CALL(delegate_, Request(kBlobId1));
+ ASSERT_EQ(
+ nullptr,
+ blob_receiver_
+ ->Get(kBlobId1, base::Bind(&BlobChannelReceiverTest::OnBlobReceived,
+ base::Unretained(this)))
+ .get());
+}
+
+TEST_F(BlobChannelReceiverTest, TestGetRedundantArrival) {
+ auto payload = CreatePayload(kBlobPayload1);
+
+ EXPECT_CALL(mock_cache_, Contains(kBlobId1))
+ .WillOnce(Return(false))
+ .WillOnce(Return(true));
+ EXPECT_CALL(delegate_, Request(kBlobId1));
+
+ // Async Get() operations are started.
+ ASSERT_EQ(
+ nullptr,
+ blob_receiver_
+ ->Get(kBlobId1, base::Bind(&BlobChannelReceiverTest::OnBlobReceived,
+ base::Unretained(this)))
+ .get());
+
+ // Blobs start arriving asynchronously. Verify that our read callback is hit.
+ EXPECT_CALL(mock_cache_, Put(kBlobId1, RefCountedVectorsEqual(payload)));
+ EXPECT_CALL(*this, OnBlobReceived(kBlobId1, RefCountedVectorsEqual(payload)));
+ delegate_.OnBlobReceived(kBlobId1, payload);
+}
+
+} // namespace
+} // namespace blimp

Powered by Google App Engine
This is Rietveld 408576698