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

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

Issue 269423003: Guard SpdyWriteQueue::Enqueue, and test all reentrancy points. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: No change. Re-trying with proper base r268730. Created 6 years, 7 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 | Annotate | Revision Log
« no previous file with comments | « net/spdy/spdy_write_queue.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) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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_write_queue.h" 5 #include "net/spdy/spdy_write_queue.h"
6 6
7 #include <cstddef> 7 #include <cstddef>
8 #include <cstring> 8 #include <cstring>
9 #include <string> 9 #include <string>
10 10
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
14 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
15 #include "net/base/net_log.h" 15 #include "net/base/net_log.h"
16 #include "net/base/request_priority.h" 16 #include "net/base/request_priority.h"
17 #include "net/spdy/spdy_buffer_producer.h" 17 #include "net/spdy/spdy_buffer_producer.h"
18 #include "net/spdy/spdy_stream.h" 18 #include "net/spdy/spdy_stream.h"
19 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "url/gurl.h" 20 #include "url/gurl.h"
21 21
22 namespace net { 22 namespace net {
23 23
24 namespace { 24 namespace {
25 25
26 using std::string;
27
28 const char kOriginal[] = "original";
29 const char kRequed[] = "requed";
willchan no longer on Chromium 2014/05/07 21:27:47 spelling nazi here, requed=>requeued :)
Johnny 2014/05/08 02:49:14 Done.
30
26 class SpdyWriteQueueTest : public ::testing::Test {}; 31 class SpdyWriteQueueTest : public ::testing::Test {};
27 32
28 // Makes a SpdyFrameProducer producing a frame with the data in the 33 // Makes a SpdyFrameProducer producing a frame with the data in the
29 // given string. 34 // given string.
30 scoped_ptr<SpdyBufferProducer> StringToProducer(const std::string& s) { 35 scoped_ptr<SpdyBufferProducer> StringToProducer(const std::string& s) {
31 scoped_ptr<char[]> data(new char[s.size()]); 36 scoped_ptr<char[]> data(new char[s.size()]);
32 std::memcpy(data.get(), s.data(), s.size()); 37 std::memcpy(data.get(), s.data(), s.size());
33 return scoped_ptr<SpdyBufferProducer>( 38 return scoped_ptr<SpdyBufferProducer>(
34 new SimpleBufferProducer( 39 new SimpleBufferProducer(
35 scoped_ptr<SpdyBuffer>( 40 scoped_ptr<SpdyBuffer>(
36 new SpdyBuffer( 41 new SpdyBuffer(
37 scoped_ptr<SpdyFrame>( 42 scoped_ptr<SpdyFrame>(
38 new SpdyFrame(data.release(), s.size(), true)))))); 43 new SpdyFrame(data.release(), s.size(), true))))));
39 } 44 }
40 45
41 // Makes a SpdyBufferProducer producing a frame with the data in the 46 // Makes a SpdyBufferProducer producing a frame with the data in the
42 // given int (converted to a string). 47 // given int (converted to a string).
43 scoped_ptr<SpdyBufferProducer> IntToProducer(int i) { 48 scoped_ptr<SpdyBufferProducer> IntToProducer(int i) {
44 return StringToProducer(base::IntToString(i)); 49 return StringToProducer(base::IntToString(i));
45 } 50 }
46 51
52 // Producer whose produced buffer will enqueue yet another buffer into the
53 // SpdyWriteQueue upon destruction.
54 class RequeingBufferProducer : public SpdyBufferProducer {
55 public:
56 RequeingBufferProducer(SpdyWriteQueue* queue) {
57 buffer_.reset(new SpdyBuffer(kOriginal, arraysize(kOriginal)));
58 buffer_->AddConsumeCallback(
59 base::Bind(RequeingBufferProducer::ConsumeCallback, queue));
60 }
61
62 virtual scoped_ptr<SpdyBuffer> ProduceBuffer() OVERRIDE {
63 return buffer_.Pass();
64 }
65
66 static void ConsumeCallback(SpdyWriteQueue* queue,
67 size_t size,
68 SpdyBuffer::ConsumeSource source) {
69 scoped_ptr<SpdyBufferProducer> producer(new SimpleBufferProducer(
70 scoped_ptr<SpdyBuffer>(new SpdyBuffer(kRequed, arraysize(kRequed)))));
71
72 queue->Enqueue(
73 MEDIUM, RST_STREAM, producer.Pass(), base::WeakPtr<SpdyStream>());
74 }
75
76 private:
77 scoped_ptr<SpdyBuffer> buffer_;
78 };
79
47 // Produces a frame with the given producer and returns a copy of its 80 // Produces a frame with the given producer and returns a copy of its
48 // data as a string. 81 // data as a string.
49 std::string ProducerToString(scoped_ptr<SpdyBufferProducer> producer) { 82 std::string ProducerToString(scoped_ptr<SpdyBufferProducer> producer) {
50 scoped_ptr<SpdyBuffer> buffer = producer->ProduceBuffer(); 83 scoped_ptr<SpdyBuffer> buffer = producer->ProduceBuffer();
51 return std::string(buffer->GetRemainingData(), buffer->GetRemainingSize()); 84 return std::string(buffer->GetRemainingData(), buffer->GetRemainingSize());
52 } 85 }
53 86
54 // Produces a frame with the given producer and returns a copy of its 87 // Produces a frame with the given producer and returns a copy of its
55 // data as an int (converted from a string). 88 // data as an int (converted from a string).
56 int ProducerToInt(scoped_ptr<SpdyBufferProducer> producer) { 89 int ProducerToInt(scoped_ptr<SpdyBufferProducer> producer) {
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 } 273 }
241 274
242 write_queue.Clear(); 275 write_queue.Clear();
243 276
244 SpdyFrameType frame_type = DATA; 277 SpdyFrameType frame_type = DATA;
245 scoped_ptr<SpdyBufferProducer> frame_producer; 278 scoped_ptr<SpdyBufferProducer> frame_producer;
246 base::WeakPtr<SpdyStream> stream; 279 base::WeakPtr<SpdyStream> stream;
247 EXPECT_FALSE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); 280 EXPECT_FALSE(write_queue.Dequeue(&frame_type, &frame_producer, &stream));
248 } 281 }
249 282
283 TEST_F(SpdyWriteQueueTest, RequeingProducerWithoutReentrance) {
284 SpdyWriteQueue queue;
285 queue.Enqueue(
286 DEFAULT_PRIORITY,
287 SYN_STREAM,
288 scoped_ptr<SpdyBufferProducer>(new RequeingBufferProducer(&queue)),
289 base::WeakPtr<SpdyStream>());
290 {
291 SpdyFrameType frame_type;
292 scoped_ptr<SpdyBufferProducer> producer;
293 base::WeakPtr<SpdyStream> stream;
294
295 EXPECT_TRUE(queue.Dequeue(&frame_type, &producer, &stream));
296 EXPECT_TRUE(queue.IsEmpty());
297 EXPECT_EQ(string(kOriginal), producer->ProduceBuffer()->GetRemainingData());
298 }
299 // |producer| was destroyed, and a buffer is re-queued.
300 EXPECT_FALSE(queue.IsEmpty());
301
302 SpdyFrameType frame_type;
303 scoped_ptr<SpdyBufferProducer> producer;
304 base::WeakPtr<SpdyStream> stream;
305
306 EXPECT_TRUE(queue.Dequeue(&frame_type, &producer, &stream));
307 EXPECT_EQ(string(kRequed), producer->ProduceBuffer()->GetRemainingData());
308 }
309
310 TEST_F(SpdyWriteQueueTest, ReentranceOnClear) {
311 SpdyWriteQueue queue;
312 queue.Enqueue(
313 DEFAULT_PRIORITY,
314 SYN_STREAM,
315 scoped_ptr<SpdyBufferProducer>(new RequeingBufferProducer(&queue)),
316 base::WeakPtr<SpdyStream>());
317
318 queue.Clear();
319 EXPECT_FALSE(queue.IsEmpty());
320
321 SpdyFrameType frame_type;
322 scoped_ptr<SpdyBufferProducer> producer;
323 base::WeakPtr<SpdyStream> stream;
324
325 EXPECT_TRUE(queue.Dequeue(&frame_type, &producer, &stream));
326 EXPECT_EQ(string(kRequed), producer->ProduceBuffer()->GetRemainingData());
327 }
328
329 TEST_F(SpdyWriteQueueTest, ReentranceOnRemovePendingWritesAfter) {
330 scoped_ptr<SpdyStream> stream(MakeTestStream(DEFAULT_PRIORITY));
331 stream->set_stream_id(2);
332
333 SpdyWriteQueue queue;
334 queue.Enqueue(
335 DEFAULT_PRIORITY,
336 SYN_STREAM,
337 scoped_ptr<SpdyBufferProducer>(new RequeingBufferProducer(&queue)),
338 stream->GetWeakPtr());
339
340 queue.RemovePendingWritesForStreamsAfter(1);
341 EXPECT_FALSE(queue.IsEmpty());
342
343 SpdyFrameType frame_type;
344 scoped_ptr<SpdyBufferProducer> producer;
345 base::WeakPtr<SpdyStream> weak_stream;
346
347 EXPECT_TRUE(queue.Dequeue(&frame_type, &producer, &weak_stream));
348 EXPECT_EQ(string(kRequed), producer->ProduceBuffer()->GetRemainingData());
349 }
350
351 TEST_F(SpdyWriteQueueTest, ReentranceOnRemovePendingWritesForStream) {
352 scoped_ptr<SpdyStream> stream(MakeTestStream(DEFAULT_PRIORITY));
353 stream->set_stream_id(2);
354
355 SpdyWriteQueue queue;
356 queue.Enqueue(
357 DEFAULT_PRIORITY,
358 SYN_STREAM,
359 scoped_ptr<SpdyBufferProducer>(new RequeingBufferProducer(&queue)),
360 stream->GetWeakPtr());
361
362 queue.RemovePendingWritesForStream(stream->GetWeakPtr());
363 EXPECT_FALSE(queue.IsEmpty());
364
365 SpdyFrameType frame_type;
366 scoped_ptr<SpdyBufferProducer> producer;
367 base::WeakPtr<SpdyStream> weak_stream;
368
369 EXPECT_TRUE(queue.Dequeue(&frame_type, &producer, &weak_stream));
370 EXPECT_EQ(string(kRequed), producer->ProduceBuffer()->GetRemainingData());
371 }
372
250 } // namespace 373 } // namespace
251 374
252 } // namespace net 375 } // namespace net
OLDNEW
« no previous file with comments | « net/spdy/spdy_write_queue.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698