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

Side by Side Diff: net/spdy/spdy_header_block_test.cc

Issue 2611173004: Modify SpdyHeaderBlock's internals to consolidate header values only on first access. (Closed)
Patch Set: Nit. Created 3 years, 11 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
« net/base/arena.h ('K') | « net/spdy/spdy_header_block.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/spdy/spdy_header_block.h" 5 #include "net/spdy/spdy_header_block.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/values.h" 10 #include "base/values.h"
11 #include "net/log/net_log_capture_mode.h" 11 #include "net/log/net_log_capture_mode.h"
12 #include "net/spdy/spdy_test_utils.h" 12 #include "net/spdy/spdy_test_utils.h"
13 #include "testing/gmock/include/gmock/gmock.h" 13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
15 15
16 using base::StringPiece;
16 using std::make_pair; 17 using std::make_pair;
17 using std::string; 18 using std::string;
19 using std::vector;
Ryan Hamilton 2017/01/10 22:46:12 nit: IIRC, the internal code has been modified to
Bence 2017/01/11 14:25:13 This is definitely worth exploring. For the time
18 using ::testing::ElementsAre; 20 using ::testing::ElementsAre;
19 21
20 namespace net { 22 namespace net {
21 namespace test { 23 namespace test {
22 24
23 class ValueProxyPeer { 25 class ValueProxyPeer {
24 public: 26 public:
25 static base::StringPiece key(SpdyHeaderBlock::ValueProxy* p) { 27 static StringPiece key(SpdyHeaderBlock::ValueProxy* p) { return p->key_; }
26 return p->key_;
27 }
28 }; 28 };
29 29
30 std::pair<base::StringPiece, base::StringPiece> Pair(base::StringPiece k, 30 std::pair<StringPiece, StringPiece> Pair(StringPiece k, StringPiece v) {
31 base::StringPiece v) {
32 return make_pair(k, v); 31 return make_pair(k, v);
33 } 32 }
34 33
35 // This test verifies that SpdyHeaderBlock behaves correctly when empty. 34 // This test verifies that SpdyHeaderBlock behaves correctly when empty.
36 TEST(SpdyHeaderBlockTest, EmptyBlock) { 35 TEST(SpdyHeaderBlockTest, EmptyBlock) {
37 SpdyHeaderBlock block; 36 SpdyHeaderBlock block;
38 EXPECT_TRUE(block.empty()); 37 EXPECT_TRUE(block.empty());
39 EXPECT_EQ(0u, block.size()); 38 EXPECT_EQ(0u, block.size());
40 EXPECT_EQ(block.end(), block.find("foo")); 39 EXPECT_EQ(block.end(), block.find("foo"));
41 EXPECT_TRUE(block.end() == block.begin()); 40 EXPECT_TRUE(block.end() == block.begin());
42 41
43 // Should have no effect. 42 // Should have no effect.
44 block.erase("bar"); 43 block.erase("bar");
45 } 44 }
46 45
47 TEST(SpdyHeaderBlockTest, KeyMemoryReclaimedOnLookup) { 46 TEST(SpdyHeaderBlockTest, KeyMemoryReclaimedOnLookup) {
48 SpdyHeaderBlock block; 47 SpdyHeaderBlock block;
49 base::StringPiece copied_key1; 48 StringPiece copied_key1;
50 { 49 {
51 auto proxy1 = block["some key name"]; 50 auto proxy1 = block["some key name"];
52 copied_key1 = ValueProxyPeer::key(&proxy1); 51 copied_key1 = ValueProxyPeer::key(&proxy1);
53 } 52 }
54 base::StringPiece copied_key2; 53 StringPiece copied_key2;
55 { 54 {
56 auto proxy2 = block["some other key name"]; 55 auto proxy2 = block["some other key name"];
57 copied_key2 = ValueProxyPeer::key(&proxy2); 56 copied_key2 = ValueProxyPeer::key(&proxy2);
58 } 57 }
59 // Because proxy1 was never used to modify the block, the memory used for the 58 // Because proxy1 was never used to modify the block, the memory used for the
60 // key could be reclaimed and used for the second call to operator[]. 59 // key could be reclaimed and used for the second call to operator[].
61 // Therefore, we expect the pointers of the two StringPieces to be equal. 60 // Therefore, we expect the pointers of the two StringPieces to be equal.
62 EXPECT_EQ(copied_key1.data(), copied_key2.data()); 61 EXPECT_EQ(copied_key1.data(), copied_key2.data());
63 62
64 { 63 {
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 179
181 block.AppendValueOrAddHeader("h3", "h3v2"); 180 block.AppendValueOrAddHeader("h3", "h3v2");
182 block.AppendValueOrAddHeader("h2", "h2v2"); 181 block.AppendValueOrAddHeader("h2", "h2v2");
183 block.AppendValueOrAddHeader("h1", "h1v2"); 182 block.AppendValueOrAddHeader("h1", "h1v2");
184 block.AppendValueOrAddHeader("cookie", "key2=value2"); 183 block.AppendValueOrAddHeader("cookie", "key2=value2");
185 184
186 block.AppendValueOrAddHeader("cookie", "key3=value3"); 185 block.AppendValueOrAddHeader("cookie", "key3=value3");
187 block.AppendValueOrAddHeader("h1", "h1v3"); 186 block.AppendValueOrAddHeader("h1", "h1v3");
188 block.AppendValueOrAddHeader("h2", "h2v3"); 187 block.AppendValueOrAddHeader("h2", "h2v3");
189 block.AppendValueOrAddHeader("h3", "h3v3"); 188 block.AppendValueOrAddHeader("h3", "h3v3");
189 block.AppendValueOrAddHeader("h4", "singleton");
190 190
191 EXPECT_EQ("key1=value1; key2=value2; key3=value3", block["cookie"]); 191 EXPECT_EQ("key1=value1; key2=value2; key3=value3", block["cookie"]);
192 EXPECT_EQ("baz", block["foo"]); 192 EXPECT_EQ("baz", block["foo"]);
193 EXPECT_EQ(string("h1v1\0h1v2\0h1v3", 14), block["h1"]); 193 EXPECT_EQ(string("h1v1\0h1v2\0h1v3", 14), block["h1"]);
194 EXPECT_EQ(string("h2v1\0h2v2\0h2v3", 14), block["h2"]); 194 EXPECT_EQ(string("h2v1\0h2v2\0h2v3", 14), block["h2"]);
195 EXPECT_EQ(string("h3v2\0h3v3", 9), block["h3"]); 195 EXPECT_EQ(string("h3v2\0h3v3", 9), block["h3"]);
196 EXPECT_EQ("singleton", block["h4"]);
197 }
198
199 TEST(JoinTest, JoinEmpty) {
200 vector<StringPiece> empty;
201 StringPiece separator = ", ";
202 char buf[10] = "";
203 size_t written = Join(buf, empty, separator);
204 EXPECT_EQ(0u, written);
205 }
206
207 TEST(JoinTest, JoinOne) {
208 vector<StringPiece> v = {"one"};
209 StringPiece separator = ", ";
210 char buf[15];
211 size_t written = Join(buf, v, separator);
212 EXPECT_EQ(3u, written);
213 EXPECT_EQ("one", StringPiece(buf, written));
214 }
215
216 TEST(JoinTest, JoinMultiple) {
217 vector<StringPiece> v = {"one", "two", "three"};
218 StringPiece separator = ", ";
219 char buf[15];
220 size_t written = Join(buf, v, separator);
221 EXPECT_EQ(15u, written);
222 EXPECT_EQ("one, two, three", StringPiece(buf, written));
196 } 223 }
197 224
198 } // namespace test 225 } // namespace test
199 } // namespace net 226 } // namespace net
OLDNEW
« net/base/arena.h ('K') | « net/spdy/spdy_header_block.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698