Skip to content

Commit

Permalink
Cleanup of QuicInMemoryCache to sync with internal version.
Browse files Browse the repository at this point in the history
Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=225588

Review URL: https://codereview.chromium.org/24596008

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@225856 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
rch@chromium.org committed Sep 28, 2013
1 parent ddeb501 commit 55bbd5c
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 61 deletions.
3 changes: 3 additions & 0 deletions net/net.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -1939,6 +1939,8 @@
'tools/quic/test_tools/quic_client_peer.h',
'tools/quic/test_tools/quic_epoll_connection_helper_peer.cc',
'tools/quic/test_tools/quic_epoll_connection_helper_peer.h',
'tools/quic/test_tools/quic_in_memory_cache_peer.h',
'tools/quic/test_tools/quic_in_memory_cache_peer.cc',
'tools/quic/test_tools/quic_test_client.cc',
'tools/quic/test_tools/quic_test_client.h',
'tools/quic/test_tools/quic_test_utils.cc',
Expand Down Expand Up @@ -2665,6 +2667,7 @@
'tools/flip_server/epoll_server.h',
'tools/flip_server/http_message_constants.cc',
'tools/flip_server/http_message_constants.h',
'tools/flip_server/noop_balsa_visitor.h',
'tools/flip_server/split.h',
'tools/flip_server/split.cc',
],
Expand Down
1 change: 0 additions & 1 deletion net/tools/flip_server/balsa_visitor_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,3 @@ class BalsaVisitorInterface {
} // namespace net

#endif // NET_TOOLS_FLIP_SERVER_BALSA_VISITOR_INTERFACE_H_

60 changes: 60 additions & 0 deletions net/tools/flip_server/noop_balsa_visitor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2013 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.
//
// Provides empty BalsaVisitorInterface overrides for convenience.
// Intended to be used as a base class for BalsaVisitorInterface subclasses that
// only need to override a small number of methods.

#ifndef NET_TOOLS_FLIP_SERVER_NOOP_BALSA_VISITOR_H_
#define NET_TOOLS_FLIP_SERVER_NOOP_BALSA_VISITOR_H_

#include "net/tools/flip_server/balsa_visitor_interface.h"

namespace net {

// See file comment above.
class NoOpBalsaVisitor : public BalsaVisitorInterface {
public:
NoOpBalsaVisitor() { }
virtual ~NoOpBalsaVisitor() { }

virtual void ProcessBodyInput(const char* input, size_t size) OVERRIDE { }
virtual void ProcessBodyData(const char* input, size_t size) OVERRIDE { }
virtual void ProcessHeaderInput(const char* input, size_t size) OVERRIDE { }
virtual void ProcessTrailerInput(const char* input, size_t size) OVERRIDE { }
virtual void ProcessHeaders(const BalsaHeaders& headers) OVERRIDE { }

virtual void ProcessRequestFirstLine(const char* line_input,
size_t line_length,
const char* method_input,
size_t method_length,
const char* request_uri_input,
size_t request_uri_length,
const char* version_input,
size_t version_length) OVERRIDE { }
virtual void ProcessResponseFirstLine(const char* line_input,
size_t line_length,
const char* version_input,
size_t version_length,
const char* status_input,
size_t status_length,
const char* reason_input,
size_t reason_length) OVERRIDE { }
virtual void ProcessChunkLength(size_t chunk_length) OVERRIDE { }
virtual void ProcessChunkExtensions(const char* input, size_t size) OVERRIDE {
}
virtual void HeaderDone() OVERRIDE { }
virtual void MessageDone() OVERRIDE { }
virtual void HandleHeaderError(BalsaFrame* framer) OVERRIDE { }
virtual void HandleHeaderWarning(BalsaFrame* framer) OVERRIDE { }
virtual void HandleChunkingError(BalsaFrame* framer) OVERRIDE { }
virtual void HandleBodyError(BalsaFrame* framer) OVERRIDE { }

private:
DISALLOW_COPY_AND_ASSIGN(NoOpBalsaVisitor);
};

} // namespace net

#endif // NET_TOOLS_FLIP_SERVER_NOOP_BALSA_VISITOR_H_
13 changes: 7 additions & 6 deletions net/tools/quic/end_to_end_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "net/tools/quic/test_tools/http_message_test_utils.h"
#include "net/tools/quic/test_tools/quic_client_peer.h"
#include "net/tools/quic/test_tools/quic_epoll_connection_helper_peer.h"
#include "net/tools/quic/test_tools/quic_in_memory_cache_peer.h"
#include "net/tools/quic/test_tools/quic_test_client.h"
#include "net/tools/quic/test_tools/server_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
Expand Down Expand Up @@ -58,11 +59,6 @@ void GenerateBody(string* body, int length) {
}

class EndToEndTest : public ::testing::TestWithParam<QuicVersion> {
public:
static void SetUpTestCase() {
QuicInMemoryCache::GetInstance()->ResetForTests();
}

protected:
EndToEndTest()
: server_hostname_("example.com"),
Expand All @@ -76,6 +72,7 @@ class EndToEndTest : public ::testing::TestWithParam<QuicVersion> {
client_config_.SetDefaults();
server_config_.SetDefaults();

QuicInMemoryCachePeer::ResetForTests();
AddToCache("GET", kLargeRequest, "HTTP/1.1", "200", "OK", kFooResponseBody);
AddToCache("GET", "https://www.google.com/foo",
"HTTP/1.1", "200", "OK", kFooResponseBody);
Expand All @@ -84,6 +81,10 @@ class EndToEndTest : public ::testing::TestWithParam<QuicVersion> {
version_ = GetParam();
}

virtual ~EndToEndTest() {
QuicInMemoryCachePeer::ResetForTests();
}

virtual QuicTestClient* CreateQuicClient() {
QuicTestClient* client = new QuicTestClient(server_address_,
server_hostname_,
Expand Down Expand Up @@ -131,7 +132,7 @@ class EndToEndTest : public ::testing::TestWithParam<QuicVersion> {
StringPiece response_code,
StringPiece response_detail,
StringPiece body) {
QuicInMemoryCache::GetInstance()->AddOrVerifyResponse(
QuicInMemoryCache::GetInstance()->AddSimpleResponse(
method, path, version, response_code, response_detail, body);
}

Expand Down
57 changes: 15 additions & 42 deletions net/tools/quic/quic_in_memory_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "base/files/file_enumerator.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "net/tools/flip_server/balsa_headers.h"

using base::FilePath;
using base::StringPiece;
Expand All @@ -20,20 +21,17 @@ using std::string;
namespace net {
namespace tools {

std::string FLAGS_quic_in_memory_cache_dir = "/tmp/quic-data";
std::string FLAGS_quic_in_memory_cache_dir = "";

namespace {

// BalsaVisitor implementation (glue) which caches response bodies.
class CachingBalsaVisitor : public BalsaVisitorInterface {
class CachingBalsaVisitor : public NoOpBalsaVisitor {
public:
CachingBalsaVisitor() : done_framing_(false) {}
virtual void ProcessBodyData(const char* input, size_t size) OVERRIDE {
AppendToBody(input, size);
}
virtual void ProcessTrailers(const BalsaHeaders& trailer) {
LOG(DFATAL) << "Trailers not supported.";
}
virtual void MessageDone() OVERRIDE {
done_framing_ = true;
}
Expand All @@ -43,8 +41,6 @@ class CachingBalsaVisitor : public BalsaVisitorInterface {
virtual void HandleHeaderWarning(BalsaFrame* framer) OVERRIDE {
UnhandledError();
}
virtual void HandleTrailerError(BalsaFrame* framer) { UnhandledError(); }
virtual void HandleTrailerWarning(BalsaFrame* framer) { UnhandledError(); }
virtual void HandleChunkingError(BalsaFrame* framer) OVERRIDE {
UnhandledError();
}
Expand All @@ -54,20 +50,6 @@ class CachingBalsaVisitor : public BalsaVisitorInterface {
void UnhandledError() {
LOG(DFATAL) << "Unhandled error framing HTTP.";
}
virtual void ProcessBodyInput(const char*, size_t) OVERRIDE {}
virtual void ProcessHeaderInput(const char*, size_t) OVERRIDE {}
virtual void ProcessTrailerInput(const char*, size_t) OVERRIDE {}
virtual void ProcessHeaders(const net::BalsaHeaders&) OVERRIDE {}
virtual void ProcessRequestFirstLine(
const char*, size_t, const char*, size_t,
const char*, size_t, const char*, size_t) OVERRIDE {}
virtual void ProcessResponseFirstLine(
const char*, size_t, const char*,
size_t, const char*, size_t, const char*, size_t) OVERRIDE {}
virtual void ProcessChunkLength(size_t) OVERRIDE {}
virtual void ProcessChunkExtensions(const char*, size_t) OVERRIDE {}
virtual void HeaderDone() OVERRIDE {}

void AppendToBody(const char* input, size_t size) {
body_.append(input, size);
}
Expand All @@ -81,6 +63,7 @@ class CachingBalsaVisitor : public BalsaVisitorInterface {

} // namespace

// static
QuicInMemoryCache* QuicInMemoryCache::GetInstance() {
return Singleton<QuicInMemoryCache>::get();
}
Expand All @@ -94,12 +77,12 @@ const QuicInMemoryCache::Response* QuicInMemoryCache::GetResponse(
return it->second;
}

void QuicInMemoryCache::AddOrVerifyResponse(StringPiece method,
StringPiece path,
StringPiece version,
StringPiece response_code,
StringPiece response_detail,
StringPiece body) {
void QuicInMemoryCache::AddSimpleResponse(StringPiece method,
StringPiece path,
StringPiece version,
StringPiece response_code,
StringPiece response_detail,
StringPiece body) {
BalsaHeaders request_headers, response_headers;
request_headers.SetRequestFirstlineFromStringPieces(method,
path,
Expand All @@ -110,18 +93,7 @@ void QuicInMemoryCache::AddOrVerifyResponse(StringPiece method,
response_headers.AppendHeader("content-length",
base::IntToString(body.length()));

// Check if response already exists and matches.
const QuicInMemoryCache::Response* cached_response =
GetResponse(request_headers);
if (cached_response == NULL) {
AddResponse(request_headers, response_headers, body);
return;
}
string cached_response_headers_str, response_headers_str;
cached_response->headers().DumpToString(&cached_response_headers_str);
response_headers.DumpToString(&response_headers_str);
CHECK_EQ(cached_response_headers_str, response_headers_str);
CHECK_EQ(cached_response->body(), body);
AddResponse(request_headers, response_headers, body);
}

void QuicInMemoryCache::AddResponse(const BalsaHeaders& request_headers,
Expand All @@ -130,19 +102,20 @@ void QuicInMemoryCache::AddResponse(const BalsaHeaders& request_headers,
LOG(INFO) << "Adding response for: " << GetKey(request_headers);
if (ContainsKey(responses_, GetKey(request_headers))) {
LOG(DFATAL) << "Response for given request already exists!";
return;
}
Response* new_response = new Response();
new_response->set_headers(response_headers);
new_response->set_body(response_body);
responses_[GetKey(request_headers)] = new_response;
}

void QuicInMemoryCache::ResetForTests() {
STLDeleteValues(&responses_);
QuicInMemoryCache::QuicInMemoryCache() {
Initialize();
}

QuicInMemoryCache::QuicInMemoryCache() {
void QuicInMemoryCache::ResetForTests() {
STLDeleteValues(&responses_);
Initialize();
}

Expand Down
29 changes: 19 additions & 10 deletions net/tools/quic/quic_in_memory_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@
#include "base/strings/string_piece.h"
#include "net/tools/flip_server/balsa_frame.h"
#include "net/tools/flip_server/balsa_headers.h"
#include "net/tools/flip_server/noop_balsa_visitor.h"

template <typename T> struct DefaultSingletonTraits;

namespace net {
namespace tools {

namespace test {
class QuicInMemoryCachePeer;
} // namespace

extern std::string FLAGS_quic_in_memory_cache_dir;

class QuicServer;
Expand Down Expand Up @@ -51,37 +56,41 @@ class QuicInMemoryCache {

DISALLOW_COPY_AND_ASSIGN(Response);
};

// Returns the singleton instance of the cache.
static QuicInMemoryCache* GetInstance();

// Retrieve a response from this cache for a given request.
// If no appropriate response exists, NULL is returned.
// Currently, responses are selected based on request URI only.
const Response* GetResponse(const BalsaHeaders& request_headers) const;

// Adds a response to the cache if no matching entry exists.
// Otherwise it verifies that the existing entry matches.
void AddOrVerifyResponse(base::StringPiece method,
base::StringPiece path,
base::StringPiece version,
base::StringPiece response_code,
base::StringPiece response_detail,
base::StringPiece body);
// Adds a simple response to the cache. The response headers will
// only contain the "content-length" header with the lenght of |body|.
void AddSimpleResponse(base::StringPiece method,
base::StringPiece path,
base::StringPiece version,
base::StringPiece response_code,
base::StringPiece response_detail,
base::StringPiece body);

// Add a response to the cache.
void AddResponse(const BalsaHeaders& request_headers,
const BalsaHeaders& response_headers,
base::StringPiece response_body);

void ResetForTests();

private:
typedef base::hash_map<std::string, Response*> ResponseMap;
friend struct DefaultSingletonTraits<QuicInMemoryCache>;
friend class test::QuicInMemoryCachePeer;

QuicInMemoryCache();
~QuicInMemoryCache();

void ResetForTests();

void Initialize();

std::string GetKey(const BalsaHeaders& response_headers) const;

// Cached responses.
Expand Down
3 changes: 2 additions & 1 deletion net/tools/quic/quic_in_memory_cache_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "base/strings/string_piece.h"
#include "net/tools/flip_server/balsa_headers.h"
#include "net/tools/quic/quic_in_memory_cache.h"
#include "net/tools/quic/test_tools/quic_in_memory_cache_peer.h"
#include "testing/gtest/include/gtest/gtest.h"

using base::IntToString;
Expand Down Expand Up @@ -39,7 +40,7 @@ class QuicInMemoryCacheTest : public ::testing::Test {
}

virtual void SetUp() {
QuicInMemoryCache::GetInstance()->ResetForTests();
QuicInMemoryCachePeer::ResetForTests();
}

// This method was copied from end_to_end_test.cc in this directory.
Expand Down
3 changes: 2 additions & 1 deletion net/tools/quic/quic_reliable_server_stream_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "net/tools/quic/quic_in_memory_cache.h"
#include "net/tools/quic/quic_spdy_server_stream.h"
#include "net/tools/quic/spdy_utils.h"
#include "net/tools/quic/test_tools/quic_in_memory_cache_peer.h"
#include "net/tools/quic/test_tools/quic_test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
Expand Down Expand Up @@ -80,7 +81,7 @@ class QuicReliableServerStreamTest : public ::testing::Test {
}

static void SetUpTestCase() {
QuicInMemoryCache::GetInstance()->ResetForTests();
QuicInMemoryCachePeer::ResetForTests();
}

virtual void SetUp() {
Expand Down
20 changes: 20 additions & 0 deletions net/tools/quic/test_tools/quic_in_memory_cache_peer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2013 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 "net/tools/quic/test_tools/quic_in_memory_cache_peer.h"

#include "net/tools/quic/quic_in_memory_cache.h"

namespace net {
namespace tools {
namespace test {

// static
void QuicInMemoryCachePeer::ResetForTests() {
QuicInMemoryCache::GetInstance()->ResetForTests();
}

} // namespace test
} // namespace tools
} // namespace net
Loading

0 comments on commit 55bbd5c

Please sign in to comment.