1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
// Copyright (c) 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/spdy/spdy_write_queue.h"
#include <cstddef>
#include <cstring>
#include <string>
#include <utility>
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "net/base/request_priority.h"
#include "net/log/net_log.h"
#include "net/spdy/spdy_buffer_producer.h"
#include "net/spdy/spdy_stream.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace net {
namespace {
using std::string;
const char kOriginal[] = "original";
const char kRequeued[] = "requeued";
class SpdyWriteQueueTest : public ::testing::Test {};
// Makes a SpdyFrameProducer producing a frame with the data in the
// given string.
scoped_ptr<SpdyBufferProducer> StringToProducer(const std::string& s) {
scoped_ptr<char[]> data(new char[s.size()]);
std::memcpy(data.get(), s.data(), s.size());
return scoped_ptr<SpdyBufferProducer>(
new SimpleBufferProducer(
scoped_ptr<SpdyBuffer>(
new SpdyBuffer(
scoped_ptr<SpdyFrame>(
new SpdyFrame(data.release(), s.size(), true))))));
}
// Makes a SpdyBufferProducer producing a frame with the data in the
// given int (converted to a string).
scoped_ptr<SpdyBufferProducer> IntToProducer(int i) {
return StringToProducer(base::IntToString(i));
}
// Producer whose produced buffer will enqueue yet another buffer into the
// SpdyWriteQueue upon destruction.
class RequeingBufferProducer : public SpdyBufferProducer {
public:
RequeingBufferProducer(SpdyWriteQueue* queue) {
buffer_.reset(new SpdyBuffer(kOriginal, arraysize(kOriginal)));
buffer_->AddConsumeCallback(
base::Bind(RequeingBufferProducer::ConsumeCallback, queue));
}
scoped_ptr<SpdyBuffer> ProduceBuffer() override { return std::move(buffer_); }
static void ConsumeCallback(SpdyWriteQueue* queue,
size_t size,
SpdyBuffer::ConsumeSource source) {
scoped_ptr<SpdyBufferProducer> producer(
new SimpleBufferProducer(scoped_ptr<SpdyBuffer>(
new SpdyBuffer(kRequeued, arraysize(kRequeued)))));
queue->Enqueue(MEDIUM, RST_STREAM, std::move(producer),
base::WeakPtr<SpdyStream>());
}
private:
scoped_ptr<SpdyBuffer> buffer_;
};
// Produces a frame with the given producer and returns a copy of its
// data as a string.
std::string ProducerToString(scoped_ptr<SpdyBufferProducer> producer) {
scoped_ptr<SpdyBuffer> buffer = producer->ProduceBuffer();
return std::string(buffer->GetRemainingData(), buffer->GetRemainingSize());
}
// Produces a frame with the given producer and returns a copy of its
// data as an int (converted from a string).
int ProducerToInt(scoped_ptr<SpdyBufferProducer> producer) {
int i = 0;
EXPECT_TRUE(base::StringToInt(ProducerToString(std::move(producer)), &i));
return i;
}
// Makes a SpdyStream with the given priority and a NULL SpdySession
// -- be careful to not call any functions that expect the session to
// be there.
SpdyStream* MakeTestStream(RequestPriority priority) {
return new SpdyStream(
SPDY_BIDIRECTIONAL_STREAM, base::WeakPtr<SpdySession>(),
GURL(), priority, 0, 0, BoundNetLog());
}
// Add some frame producers of different priority. The producers
// should be dequeued in priority order with their associated stream.
TEST_F(SpdyWriteQueueTest, DequeuesByPriority) {
SpdyWriteQueue write_queue;
scoped_ptr<SpdyBufferProducer> producer_low = StringToProducer("LOW");
scoped_ptr<SpdyBufferProducer> producer_medium = StringToProducer("MEDIUM");
scoped_ptr<SpdyBufferProducer> producer_highest = StringToProducer("HIGHEST");
scoped_ptr<SpdyStream> stream_medium(MakeTestStream(MEDIUM));
scoped_ptr<SpdyStream> stream_highest(MakeTestStream(HIGHEST));
// A NULL stream should still work.
write_queue.Enqueue(LOW, SYN_STREAM, std::move(producer_low),
base::WeakPtr<SpdyStream>());
write_queue.Enqueue(MEDIUM, SYN_REPLY, std::move(producer_medium),
stream_medium->GetWeakPtr());
write_queue.Enqueue(HIGHEST, RST_STREAM, std::move(producer_highest),
stream_highest->GetWeakPtr());
SpdyFrameType frame_type = DATA;
scoped_ptr<SpdyBufferProducer> frame_producer;
base::WeakPtr<SpdyStream> stream;
ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream));
EXPECT_EQ(RST_STREAM, frame_type);
EXPECT_EQ("HIGHEST", ProducerToString(std::move(frame_producer)));
EXPECT_EQ(stream_highest.get(), stream.get());
ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream));
EXPECT_EQ(SYN_REPLY, frame_type);
EXPECT_EQ("MEDIUM", ProducerToString(std::move(frame_producer)));
EXPECT_EQ(stream_medium.get(), stream.get());
ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream));
EXPECT_EQ(SYN_STREAM, frame_type);
EXPECT_EQ("LOW", ProducerToString(std::move(frame_producer)));
EXPECT_EQ(nullptr, stream.get());
EXPECT_FALSE(write_queue.Dequeue(&frame_type, &frame_producer, &stream));
}
// Add some frame producers with the same priority. The producers
// should be dequeued in FIFO order with their associated stream.
TEST_F(SpdyWriteQueueTest, DequeuesFIFO) {
SpdyWriteQueue write_queue;
scoped_ptr<SpdyBufferProducer> producer1 = IntToProducer(1);
scoped_ptr<SpdyBufferProducer> producer2 = IntToProducer(2);
scoped_ptr<SpdyBufferProducer> producer3 = IntToProducer(3);
scoped_ptr<SpdyStream> stream1(MakeTestStream(DEFAULT_PRIORITY));
scoped_ptr<SpdyStream> stream2(MakeTestStream(DEFAULT_PRIORITY));
scoped_ptr<SpdyStream> stream3(MakeTestStream(DEFAULT_PRIORITY));
write_queue.Enqueue(DEFAULT_PRIORITY, SYN_STREAM, std::move(producer1),
stream1->GetWeakPtr());
write_queue.Enqueue(DEFAULT_PRIORITY, SYN_REPLY, std::move(producer2),
stream2->GetWeakPtr());
write_queue.Enqueue(DEFAULT_PRIORITY, RST_STREAM, std::move(producer3),
stream3->GetWeakPtr());
SpdyFrameType frame_type = DATA;
scoped_ptr<SpdyBufferProducer> frame_producer;
base::WeakPtr<SpdyStream> stream;
ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream));
EXPECT_EQ(SYN_STREAM, frame_type);
EXPECT_EQ(1, ProducerToInt(std::move(frame_producer)));
EXPECT_EQ(stream1.get(), stream.get());
ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream));
EXPECT_EQ(SYN_REPLY, frame_type);
EXPECT_EQ(2, ProducerToInt(std::move(frame_producer)));
EXPECT_EQ(stream2.get(), stream.get());
ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream));
EXPECT_EQ(RST_STREAM, frame_type);
EXPECT_EQ(3, ProducerToInt(std::move(frame_producer)));
EXPECT_EQ(stream3.get(), stream.get());
EXPECT_FALSE(write_queue.Dequeue(&frame_type, &frame_producer, &stream));
}
// Enqueue a bunch of writes and then call
// RemovePendingWritesForStream() on one of the streams. No dequeued
// write should be for that stream.
TEST_F(SpdyWriteQueueTest, RemovePendingWritesForStream) {
SpdyWriteQueue write_queue;
scoped_ptr<SpdyStream> stream1(MakeTestStream(DEFAULT_PRIORITY));
scoped_ptr<SpdyStream> stream2(MakeTestStream(DEFAULT_PRIORITY));
for (int i = 0; i < 100; ++i) {
base::WeakPtr<SpdyStream> stream =
(((i % 3) == 0) ? stream1 : stream2)->GetWeakPtr();
write_queue.Enqueue(DEFAULT_PRIORITY, SYN_STREAM, IntToProducer(i), stream);
}
write_queue.RemovePendingWritesForStream(stream2->GetWeakPtr());
for (int i = 0; i < 100; i += 3) {
SpdyFrameType frame_type = DATA;
scoped_ptr<SpdyBufferProducer> frame_producer;
base::WeakPtr<SpdyStream> stream;
ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream));
EXPECT_EQ(SYN_STREAM, frame_type);
EXPECT_EQ(i, ProducerToInt(std::move(frame_producer)));
EXPECT_EQ(stream1.get(), stream.get());
}
SpdyFrameType frame_type = DATA;
scoped_ptr<SpdyBufferProducer> frame_producer;
base::WeakPtr<SpdyStream> stream;
EXPECT_FALSE(write_queue.Dequeue(&frame_type, &frame_producer, &stream));
}
// Enqueue a bunch of writes and then call
// RemovePendingWritesForStreamsAfter(). No dequeued write should be for
// those streams without a stream id, or with a stream_id after that
// argument.
TEST_F(SpdyWriteQueueTest, RemovePendingWritesForStreamsAfter) {
SpdyWriteQueue write_queue;
scoped_ptr<SpdyStream> stream1(MakeTestStream(DEFAULT_PRIORITY));
stream1->set_stream_id(1);
scoped_ptr<SpdyStream> stream2(MakeTestStream(DEFAULT_PRIORITY));
stream2->set_stream_id(3);
scoped_ptr<SpdyStream> stream3(MakeTestStream(DEFAULT_PRIORITY));
stream3->set_stream_id(5);
// No stream id assigned.
scoped_ptr<SpdyStream> stream4(MakeTestStream(DEFAULT_PRIORITY));
base::WeakPtr<SpdyStream> streams[] = {
stream1->GetWeakPtr(), stream2->GetWeakPtr(),
stream3->GetWeakPtr(), stream4->GetWeakPtr()
};
for (int i = 0; i < 100; ++i) {
write_queue.Enqueue(DEFAULT_PRIORITY, SYN_STREAM, IntToProducer(i),
streams[i % arraysize(streams)]);
}
write_queue.RemovePendingWritesForStreamsAfter(stream1->stream_id());
for (int i = 0; i < 100; i += arraysize(streams)) {
SpdyFrameType frame_type = DATA;
scoped_ptr<SpdyBufferProducer> frame_producer;
base::WeakPtr<SpdyStream> stream;
ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream))
<< "Unable to Dequeue i: " << i;
EXPECT_EQ(SYN_STREAM, frame_type);
EXPECT_EQ(i, ProducerToInt(std::move(frame_producer)));
EXPECT_EQ(stream1.get(), stream.get());
}
SpdyFrameType frame_type = DATA;
scoped_ptr<SpdyBufferProducer> frame_producer;
base::WeakPtr<SpdyStream> stream;
EXPECT_FALSE(write_queue.Dequeue(&frame_type, &frame_producer, &stream));
}
// Enqueue a bunch of writes and then call Clear(). The write queue
// should clean up the memory properly, and Dequeue() should return
// false.
TEST_F(SpdyWriteQueueTest, Clear) {
SpdyWriteQueue write_queue;
for (int i = 0; i < 100; ++i) {
write_queue.Enqueue(DEFAULT_PRIORITY, SYN_STREAM, IntToProducer(i),
base::WeakPtr<SpdyStream>());
}
write_queue.Clear();
SpdyFrameType frame_type = DATA;
scoped_ptr<SpdyBufferProducer> frame_producer;
base::WeakPtr<SpdyStream> stream;
EXPECT_FALSE(write_queue.Dequeue(&frame_type, &frame_producer, &stream));
}
TEST_F(SpdyWriteQueueTest, RequeingProducerWithoutReentrance) {
SpdyWriteQueue queue;
queue.Enqueue(
DEFAULT_PRIORITY,
SYN_STREAM,
scoped_ptr<SpdyBufferProducer>(new RequeingBufferProducer(&queue)),
base::WeakPtr<SpdyStream>());
{
SpdyFrameType frame_type;
scoped_ptr<SpdyBufferProducer> producer;
base::WeakPtr<SpdyStream> stream;
EXPECT_TRUE(queue.Dequeue(&frame_type, &producer, &stream));
EXPECT_TRUE(queue.IsEmpty());
EXPECT_EQ(string(kOriginal), producer->ProduceBuffer()->GetRemainingData());
}
// |producer| was destroyed, and a buffer is re-queued.
EXPECT_FALSE(queue.IsEmpty());
SpdyFrameType frame_type;
scoped_ptr<SpdyBufferProducer> producer;
base::WeakPtr<SpdyStream> stream;
EXPECT_TRUE(queue.Dequeue(&frame_type, &producer, &stream));
EXPECT_EQ(string(kRequeued), producer->ProduceBuffer()->GetRemainingData());
}
TEST_F(SpdyWriteQueueTest, ReentranceOnClear) {
SpdyWriteQueue queue;
queue.Enqueue(
DEFAULT_PRIORITY,
SYN_STREAM,
scoped_ptr<SpdyBufferProducer>(new RequeingBufferProducer(&queue)),
base::WeakPtr<SpdyStream>());
queue.Clear();
EXPECT_FALSE(queue.IsEmpty());
SpdyFrameType frame_type;
scoped_ptr<SpdyBufferProducer> producer;
base::WeakPtr<SpdyStream> stream;
EXPECT_TRUE(queue.Dequeue(&frame_type, &producer, &stream));
EXPECT_EQ(string(kRequeued), producer->ProduceBuffer()->GetRemainingData());
}
TEST_F(SpdyWriteQueueTest, ReentranceOnRemovePendingWritesAfter) {
scoped_ptr<SpdyStream> stream(MakeTestStream(DEFAULT_PRIORITY));
stream->set_stream_id(2);
SpdyWriteQueue queue;
queue.Enqueue(
DEFAULT_PRIORITY,
SYN_STREAM,
scoped_ptr<SpdyBufferProducer>(new RequeingBufferProducer(&queue)),
stream->GetWeakPtr());
queue.RemovePendingWritesForStreamsAfter(1);
EXPECT_FALSE(queue.IsEmpty());
SpdyFrameType frame_type;
scoped_ptr<SpdyBufferProducer> producer;
base::WeakPtr<SpdyStream> weak_stream;
EXPECT_TRUE(queue.Dequeue(&frame_type, &producer, &weak_stream));
EXPECT_EQ(string(kRequeued), producer->ProduceBuffer()->GetRemainingData());
}
TEST_F(SpdyWriteQueueTest, ReentranceOnRemovePendingWritesForStream) {
scoped_ptr<SpdyStream> stream(MakeTestStream(DEFAULT_PRIORITY));
stream->set_stream_id(2);
SpdyWriteQueue queue;
queue.Enqueue(
DEFAULT_PRIORITY,
SYN_STREAM,
scoped_ptr<SpdyBufferProducer>(new RequeingBufferProducer(&queue)),
stream->GetWeakPtr());
queue.RemovePendingWritesForStream(stream->GetWeakPtr());
EXPECT_FALSE(queue.IsEmpty());
SpdyFrameType frame_type;
scoped_ptr<SpdyBufferProducer> producer;
base::WeakPtr<SpdyStream> weak_stream;
EXPECT_TRUE(queue.Dequeue(&frame_type, &producer, &weak_stream));
EXPECT_EQ(string(kRequeued), producer->ProduceBuffer()->GetRemainingData());
}
} // namespace
} // namespace net
|