Chromium Code Reviews
Help | Chromium Project | Sign in
(497)

Side by Side Diff: net/quic/quic_stream_sequencer_test.cc

Issue 11300020: Add QuicStream and friends to QUIC code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: License Created 1 year, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "net/quic/quic_stream_sequencer.h"
6
7 #include <utility>
8 #include <vector>
9
10 #include "base/rand_util.h"
11 #include "net/quic/reliable_quic_stream.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 using base::StringPiece;
16 using std::min;
17 using std::pair;
18 using std::vector;
19 using testing::_;
20 using testing::AnyNumber;
21 using testing::InSequence;
22 using testing::Return;
23 using testing::StrEq;
24
25 namespace net {
26
27 class QuicStreamSequencerPeer : public QuicStreamSequencer {
28 public:
29 explicit QuicStreamSequencerPeer(ReliableQuicStream* stream)
30 : QuicStreamSequencer(stream) {
31 }
32
33 QuicStreamSequencerPeer(int32 max_mem, ReliableQuicStream* stream)
34 : QuicStreamSequencer(max_mem, stream) {}
35
36 virtual bool OnFrame(QuicStreamOffset byte_offset,
37 const char* data,
jar 2012/11/01 22:21:10 nit: indent
Ryan Hamilton 2012/11/01 22:52:20 Fixed upstream.
38 uint32 data_len) {
39 QuicStreamFrame frame;
40 frame.stream_id = 1;
41 frame.offset = byte_offset;
42 frame.data = StringPiece(data, data_len);
43 return OnStreamFrame(frame);
44 }
45
46 void SetMemoryLimit(size_t limit) {
47 max_frame_memory_ = limit;
48 }
49
50 ReliableQuicStream* stream() { return stream_; }
jar 2012/11/01 22:21:10 nit: most of these should be const methods. line 5
Ryan Hamilton 2012/11/01 22:52:20 Fixed upstream.
51 uint64 num_bytes_consumed() { return num_bytes_consumed_; }
52 FrameMap* frames() { return &frames_; }
53 int32 max_frame_memory() { return max_frame_memory_; }
54 QuicStreamOffset close_offset() { return close_offset_; }
55 };
56
57 class MockStream : public ReliableQuicStream {
58 public:
59 MockStream(QuicSession* session, QuicStreamId id)
60 : ReliableQuicStream(id, session) {
61 }
62
63 MOCK_METHOD1(TerminateFromPeer, void(bool half_close));
64 MOCK_METHOD2(ProcessData, uint32(const char* data, uint32 data_len));
65 MOCK_METHOD1(Close, void(QuicErrorCode error));
66 };
67
68 namespace {
69
70 static const char kPayload[] =
71 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
72
73 class QuicStreamSequencerTest : public ::testing::Test {
74 protected:
75 QuicStreamSequencerTest()
76 : session_(NULL),
77 stream_(session_, 1),
78 sequencer_(new QuicStreamSequencerPeer(&stream_)) {
79 }
80
81 QuicSession* session_;
82 testing::StrictMock<MockStream> stream_;
83 scoped_ptr<QuicStreamSequencerPeer> sequencer_;
84 };
85
86 TEST_F(QuicStreamSequencerTest, RejectOldFrame) {
87 EXPECT_CALL(stream_, ProcessData("abc", 3))
88 .WillOnce(Return(3));
89
90 EXPECT_TRUE(sequencer_->OnFrame(0, "abc", 3));
91 EXPECT_EQ(0u, sequencer_->frames()->size());
92 EXPECT_EQ(3u, sequencer_->num_bytes_consumed());
93 // Nack this - it matches a past sequence number and we should not see it
94 // again.
95 EXPECT_FALSE(sequencer_->OnFrame(0, "def", 3));
96 EXPECT_EQ(0u, sequencer_->frames()->size());
97 }
98
99 TEST_F(QuicStreamSequencerTest, RejectOverlyLargeFrame) {
100 /*
101 EXPECT_DFATAL(sequencer_.reset(new QuicStreamSequencerPeer(2, &stream_)),
102 "Setting max frame memory to 2. "
103 "Some frames will be impossible to handle.");
104
105 EXPECT_DEBUG_DEATH(sequencer_->OnFrame(0, "abc", 3), "");
106 */
107 }
108
109 TEST_F(QuicStreamSequencerTest, DropFramePastBuffering) {
110 sequencer_->SetMemoryLimit(3);
111
112 EXPECT_FALSE(sequencer_->OnFrame(3, "abc", 3));
113 }
114
115 TEST_F(QuicStreamSequencerTest, RejectBufferedFrame) {
116 EXPECT_CALL(stream_, ProcessData("abc", 3));
117
118 EXPECT_TRUE(sequencer_->OnFrame(0, "abc", 3));
119 EXPECT_EQ(1u, sequencer_->frames()->size());
120 EXPECT_EQ(0u, sequencer_->num_bytes_consumed());
121 // Ignore this - it matches a buffered frame.
122 // Right now there's no checking that the payload is consistent.
123 EXPECT_FALSE(sequencer_->OnFrame(0, "def", 3));
124 EXPECT_EQ(1u, sequencer_->frames()->size());
125 }
126
127 TEST_F(QuicStreamSequencerTest, FullFrameConsumed) {
128 EXPECT_CALL(stream_, ProcessData("abc", 3))
129 .WillOnce(Return(3));
jar 2012/11/01 22:21:10 nit: indent
Ryan Hamilton 2012/11/01 22:52:20 Fixed upstream.
130
131 EXPECT_TRUE(sequencer_->OnFrame(0, "abc", 3));
132 EXPECT_EQ(0u, sequencer_->frames()->size());
133 EXPECT_EQ(3u, sequencer_->num_bytes_consumed());
134 }
135
136 TEST_F(QuicStreamSequencerTest, PartialFrameConsumed) {
137 EXPECT_CALL(stream_, ProcessData("abc", 3))
138 .WillOnce(Return(2));
jar 2012/11/01 22:21:10 nit: indent (happens several more times).
Ryan Hamilton 2012/11/01 22:52:20 Fixed upstream.
139
140 EXPECT_TRUE(sequencer_->OnFrame(0, "abc", 3));
141 EXPECT_EQ(1u, sequencer_->frames()->size());
142 EXPECT_EQ(2u, sequencer_->num_bytes_consumed());
143 EXPECT_EQ("c", sequencer_->frames()->find(2)->second);
144 }
145
146 TEST_F(QuicStreamSequencerTest, NextxFrameNotConsumed) {
147 EXPECT_CALL(stream_, ProcessData("abc", 3))
148 .WillOnce(Return(0));
149
150 EXPECT_TRUE(sequencer_->OnFrame(0, "abc", 3));
151 EXPECT_EQ(1u, sequencer_->frames()->size());
152 EXPECT_EQ(0u, sequencer_->num_bytes_consumed());
153 EXPECT_EQ("abc", sequencer_->frames()->find(0)->second);
154 }
155
156 TEST_F(QuicStreamSequencerTest, FutureFrameNotProcessed) {
157 EXPECT_TRUE(sequencer_->OnFrame(3, "abc", 3));
158 EXPECT_EQ(1u, sequencer_->frames()->size());
159 EXPECT_EQ(0u, sequencer_->num_bytes_consumed());
160 EXPECT_EQ("abc", sequencer_->frames()->find(3)->second);
161 }
162
163 TEST_F(QuicStreamSequencerTest, OutOfOrderFrameProcessed) {
164 // Buffer the first
165 EXPECT_TRUE(sequencer_->OnFrame(6, "ghi", 3));
166 EXPECT_EQ(1u, sequencer_->frames()->size());
167 EXPECT_EQ(0u, sequencer_->num_bytes_consumed());
168 // Buffer the second
169 EXPECT_TRUE(sequencer_->OnFrame(3, "def", 3));
170 EXPECT_EQ(2u, sequencer_->frames()->size());
171 EXPECT_EQ(0u, sequencer_->num_bytes_consumed());
172
173 InSequence s;
174 EXPECT_CALL(stream_, ProcessData("abc", 3)).WillOnce(Return(3));
175 EXPECT_CALL(stream_, ProcessData(StrEq("def"), 3)).WillOnce(Return(3));
176 EXPECT_CALL(stream_, ProcessData(StrEq("ghi"), 3)).WillOnce(Return(3));
177
178 // Ack right away
179 EXPECT_TRUE(sequencer_->OnFrame(0, "abc", 3));
180 EXPECT_EQ(9u, sequencer_->num_bytes_consumed());
181
182 EXPECT_EQ(0u, sequencer_->frames()->size());
183 }
184
185 TEST_F(QuicStreamSequencerTest, OutOfOrderFramesProcessedWithBuffering) {
186 sequencer_->SetMemoryLimit(9);
187
188 // Too far to buffer.
189 EXPECT_FALSE(sequencer_->OnFrame(9, "jkl", 3));
190
191 // We can afford to buffer this.
192 EXPECT_TRUE(sequencer_->OnFrame(6, "ghi", 3));
193 EXPECT_EQ(0u, sequencer_->num_bytes_consumed());
194
195 InSequence s;
196 EXPECT_CALL(stream_, ProcessData("abc", 3)).WillOnce(Return(3));
197
198 // Ack right away
199 EXPECT_TRUE(sequencer_->OnFrame(0, "abc", 3));
200 EXPECT_EQ(3u, sequencer_->num_bytes_consumed());
201
202 // We should be willing to buffer this now.
203 EXPECT_TRUE(sequencer_->OnFrame(9, "jkl", 3));
204 EXPECT_EQ(3u, sequencer_->num_bytes_consumed());
205
206 EXPECT_CALL(stream_, ProcessData(StrEq("def"), 3)).WillOnce(Return(3));
207 EXPECT_CALL(stream_, ProcessData(StrEq("ghi"), 3)).WillOnce(Return(3));
208 EXPECT_CALL(stream_, ProcessData(StrEq("jkl"), 3)).WillOnce(Return(3));
209
210 EXPECT_TRUE(sequencer_->OnFrame(3, "def", 3));
211 EXPECT_EQ(12u, sequencer_->num_bytes_consumed());
212 EXPECT_EQ(0u, sequencer_->frames()->size());
213 }
214
215 TEST_F(QuicStreamSequencerTest, OutOfOrderFramesBlockignWithReadv) {
216 sequencer_->SetMemoryLimit(9);
217 char buffer[20];
218 iovec iov[2];
219 iov[0].iov_base = &buffer[0];
220 iov[0].iov_len = 1;
221 iov[1].iov_base = &buffer[1];
222 iov[1].iov_len = 2;
223
224 // Push abc - process.
225 // Push jkl - buffer (not next data)
226 // Push def - don't process.
227 // Push mno - drop (too far out)
228 // Push ghi - buffer (def not processed)
229 // Read 2.
230 // Push mno - buffer (not all read)
231 // Read all
232 // Push pqr - process
233
234 InSequence s;
235 EXPECT_CALL(stream_, ProcessData(StrEq("abc"), 3)).WillOnce(Return(3));
236 EXPECT_CALL(stream_, ProcessData(StrEq("def"), 3)).WillOnce(Return(0));
237 EXPECT_CALL(stream_, ProcessData(StrEq("pqr"), 3)).WillOnce(Return(3));
238
239 EXPECT_TRUE(sequencer_->OnFrame(0, "abc", 3));
240 EXPECT_TRUE(sequencer_->OnFrame(3, "def", 3));
241 EXPECT_TRUE(sequencer_->OnFrame(9, "jkl", 3));
242 EXPECT_FALSE(sequencer_->OnFrame(12, "mno", 3));
243 EXPECT_TRUE(sequencer_->OnFrame(6, "ghi", 3));
244
245 // Read 3 bytes.
246 EXPECT_EQ(3u, sequencer_->Readv(iov, 2));
247 EXPECT_EQ(0, strncmp(buffer, "def", 3));
248
249 // Now we have space to bufer this.
250 EXPECT_TRUE(sequencer_->OnFrame(12, "mno", 3));
251
252 // Read the remaining 9 bytes.
253 iov[1].iov_len = 19;
254 EXPECT_EQ(9u, sequencer_->Readv(iov, 2));
255 EXPECT_EQ(0, strncmp(buffer, "ghijklmno", 9));
256
257 EXPECT_TRUE(sequencer_->OnFrame(15, "pqr", 3));
258 }
259
260 // Same as above, just using a different method for reading.
261 TEST_F(QuicStreamSequencerTest, OutOfOrderFramesBlockignWithGetReadableRegion) {
262 sequencer_->SetMemoryLimit(9);
263 iovec iov[5];
jar 2012/11/01 22:21:10 nit: move closer to first use.
Ryan Hamilton 2012/11/01 22:52:20 Fixed upstream.
264
265 InSequence s;
266 EXPECT_CALL(stream_, ProcessData(StrEq("abc"), 3)).WillOnce(Return(3));
267 EXPECT_CALL(stream_, ProcessData(StrEq("def"), 3)).WillOnce(Return(0));
268 EXPECT_CALL(stream_, ProcessData(StrEq("pqr"), 3)).WillOnce(Return(3));
269
270 EXPECT_TRUE(sequencer_->OnFrame(0, "abc", 3));
271 EXPECT_TRUE(sequencer_->OnFrame(3, "def", 3));
272 EXPECT_TRUE(sequencer_->OnFrame(9, "jkl", 3));
273 EXPECT_FALSE(sequencer_->OnFrame(12, "mno", 3));
274 EXPECT_TRUE(sequencer_->OnFrame(6, "ghi", 3));
275
276 // Read 3 bytes.
277 ASSERT_EQ(3u, sequencer_->GetReadableRegions(iov, 5));
278 ASSERT_EQ(3u, iov[0].iov_len);
279 ASSERT_EQ(3u, iov[1].iov_len);
280 ASSERT_EQ(3u, iov[2].iov_len);
281 ASSERT_EQ(0, memcmp(iov[0].iov_base, "def", 3));
282 ASSERT_EQ(0, memcmp(iov[1].iov_base, "ghi", 3));
283 ASSERT_EQ(0, memcmp(iov[2].iov_base, "jkl", 3));
284 sequencer_->AdvanceReadablePtr(3);
285
286 // Now we have space to bufer this.
287 EXPECT_TRUE(sequencer_->OnFrame(12, "mno", 3));
288
289 // Read the remaining 9 bytes.
290 ASSERT_EQ(3u, sequencer_->GetReadableRegions(iov, 5));
291 ASSERT_EQ(3u, iov[0].iov_len);
292 ASSERT_EQ(3u, iov[1].iov_len);
293 ASSERT_EQ(3u, iov[2].iov_len);
294 ASSERT_EQ(0, memcmp(iov[0].iov_base, "ghi", 3));
295 ASSERT_EQ(0, memcmp(iov[1].iov_base, "jkl", 3));
296 ASSERT_EQ(0, memcmp(iov[2].iov_base, "mno", 3));
297 sequencer_->AdvanceReadablePtr(9);
298
299 EXPECT_TRUE(sequencer_->OnFrame(15, "pqr", 3));
300 }
301
302 TEST_F(QuicStreamSequencerTest, BasicCloseOrdered) {
303 InSequence s;
304 EXPECT_CALL(stream_, ProcessData(StrEq("abc"), 3)).WillOnce(Return(3));
305 EXPECT_TRUE(sequencer_->OnFrame(0, "abc", 3));
306
307 EXPECT_CALL(stream_, TerminateFromPeer(false));
308 sequencer_->CloseStreamAtOffset(3, false);
309 EXPECT_EQ(3u, sequencer_->close_offset());
310 }
311
312 TEST_F(QuicStreamSequencerTest, BasicHalfOrdered) {
313 InSequence s;
314
315 EXPECT_CALL(stream_, ProcessData(StrEq("abc"), 3)).WillOnce(Return(3));
316 EXPECT_TRUE(sequencer_->OnFrame(0, "abc", 3));
317
318 EXPECT_CALL(stream_, TerminateFromPeer(true));
319 sequencer_->CloseStreamAtOffset(3, true);
320 EXPECT_EQ(3u, sequencer_->close_offset());
321 }
322
323 TEST_F(QuicStreamSequencerTest, BasicCloseUnordered) {
324 sequencer_->CloseStreamAtOffset(3, false);
325 EXPECT_EQ(3u, sequencer_->close_offset());
326
327 InSequence s;
328 EXPECT_CALL(stream_, ProcessData(StrEq("abc"), 3)).WillOnce(Return(3));
329 EXPECT_CALL(stream_, TerminateFromPeer(false));
330
331 EXPECT_TRUE(sequencer_->OnFrame(0, "abc", 3));
332 }
333
334 TEST_F(QuicStreamSequencerTest, BasicHalfUnorderedWithFlush) {
335 sequencer_->CloseStreamAtOffset(6, true);
336 EXPECT_EQ(6u, sequencer_->close_offset());
337 InSequence s;
338 EXPECT_CALL(stream_, ProcessData(StrEq("abc"), 3)).WillOnce(Return(3));
339 EXPECT_CALL(stream_, ProcessData(StrEq("def"), 3)).WillOnce(Return(3));
340 EXPECT_CALL(stream_, TerminateFromPeer(true));
341
342 EXPECT_TRUE(sequencer_->OnFrame(3, "def", 3));
343 EXPECT_TRUE(sequencer_->OnFrame(0, "abc", 3));
344 }
345
346 TEST_F(QuicStreamSequencerTest, BasicCloseUnorderedWithFlush) {
347 sequencer_->CloseStreamAtOffset(6, false);
348 EXPECT_EQ(6u, sequencer_->close_offset());
349
350 InSequence s;
351 EXPECT_CALL(stream_, ProcessData(StrEq("abc"), 3)).WillOnce(Return(3));
352 EXPECT_CALL(stream_, ProcessData(StrEq("def"), 3)).WillOnce(Return(3));
353 EXPECT_CALL(stream_, TerminateFromPeer(false));
354
355 EXPECT_TRUE(sequencer_->OnFrame(3, "def", 3));
356 EXPECT_TRUE(sequencer_->OnFrame(0, "abc", 3));
357 }
358
359 TEST_F(QuicStreamSequencerTest, BasicHalfUnordered) {
360 sequencer_->CloseStreamAtOffset(3, true);
361 EXPECT_EQ(3u, sequencer_->close_offset());
362 InSequence s;
363 EXPECT_CALL(stream_, ProcessData(StrEq("abc"), 3)).WillOnce(Return(3));
364 EXPECT_CALL(stream_, TerminateFromPeer(true));
365
366 EXPECT_TRUE(sequencer_->OnFrame(0, "abc", 3));
367 }
368
369 TEST_F(QuicStreamSequencerTest, CloseStreamBeforeCloseEqual) {
370 sequencer_->CloseStreamAtOffset(3, true);
371 EXPECT_EQ(3u, sequencer_->close_offset());
372
373 sequencer_->CloseStreamAtOffset(3, false);
374 EXPECT_EQ(3u, sequencer_->close_offset());
375
376 InSequence s;
377 EXPECT_CALL(stream_, ProcessData(StrEq("abc"), 3)).WillOnce(Return(3));
378 EXPECT_CALL(stream_, TerminateFromPeer(false));
379 EXPECT_TRUE(sequencer_->OnFrame(0, "abc", 3));
380 }
381
382 TEST_F(QuicStreamSequencerTest, CloseBeforeTermianteEqual) {
383 sequencer_->CloseStreamAtOffset(3, false);
384 EXPECT_EQ(3u, sequencer_->close_offset());
385
386 sequencer_->CloseStreamAtOffset(3, true);
387 EXPECT_EQ(3u, sequencer_->close_offset());
388
389 InSequence s;
390 EXPECT_CALL(stream_, ProcessData(StrEq("abc"), 3)).WillOnce(Return(3));
391 EXPECT_CALL(stream_, TerminateFromPeer(false));
392 EXPECT_TRUE(sequencer_->OnFrame(0, "abc", 3));
393 }
394
395 TEST_F(QuicStreamSequencerTest, TerminateWithReadv) {
396 char buffer[3];
397 iovec iov = { &buffer[0], 3 };
jar 2012/11/01 22:21:10 nit: move closer to first use. (next test as well)
Ryan Hamilton 2012/11/01 22:52:20 Fixed upstream.
398
399 sequencer_->CloseStreamAtOffset(3, true);
400 EXPECT_EQ(3u, sequencer_->close_offset());
401
402 EXPECT_FALSE(sequencer_->IsHalfClosed());
403
404 EXPECT_CALL(stream_, ProcessData(StrEq("abc"), 3)).WillOnce(Return(0));
405 EXPECT_TRUE(sequencer_->OnFrame(0, "abc", 3));
406
407 int bytes_read = sequencer_->Readv(&iov, 1);
408 EXPECT_EQ(3, bytes_read);
409 EXPECT_TRUE(sequencer_->IsHalfClosed());
410 EXPECT_FALSE(sequencer_->IsClosed());
411 }
412
413 TEST_F(QuicStreamSequencerTest, CloseWithReadv) {
414 char buffer[3];
415 iovec iov = { &buffer[0], 3 };
416
417 sequencer_->CloseStreamAtOffset(3, false);
418 EXPECT_EQ(3u, sequencer_->close_offset());
419
420 EXPECT_FALSE(sequencer_->IsClosed());
421
422 EXPECT_CALL(stream_, ProcessData(StrEq("abc"), 3)).WillOnce(Return(0));
423 EXPECT_TRUE(sequencer_->OnFrame(0, "abc", 3));
424
425 int bytes_read = sequencer_->Readv(&iov, 1);
426 EXPECT_EQ(3, bytes_read);
427 EXPECT_TRUE(sequencer_->IsHalfClosed());
428 EXPECT_TRUE(sequencer_->IsClosed());
429 }
430
431 TEST_F(QuicStreamSequencerTest, MutipleOffsets) {
432 sequencer_->CloseStreamAtOffset(3, false);
433 EXPECT_EQ(3u, sequencer_->close_offset());
434
435 EXPECT_CALL(stream_, Close(QUIC_MULTIPLE_TERMINATION_OFFSETS));
436 sequencer_->CloseStreamAtOffset(5, false);
437 EXPECT_EQ(3u, sequencer_->close_offset());
438
439 EXPECT_CALL(stream_, Close(QUIC_MULTIPLE_TERMINATION_OFFSETS));
440 sequencer_->CloseStreamAtOffset(1, false);
441 EXPECT_EQ(3u, sequencer_->close_offset());
442
443 sequencer_->CloseStreamAtOffset(3, false);
444 EXPECT_EQ(3u, sequencer_->close_offset());
445 }
446
447 class QuicSequencerRandomTest : public QuicStreamSequencerTest {
448 public:
449 typedef pair<int, string> Frame;
450 typedef vector<Frame> FrameList;
451
452 void CreateFrames() {
453 int payload_size = arraysize(kPayload) - 1;
454 int remaining_payload = payload_size;
455 while (remaining_payload != 0) {
456 int size = min(OneToN(6), remaining_payload);
457 int idx = payload_size - remaining_payload;
jar 2012/11/01 22:21:10 nit: (assuming you went with the longer spelling i
Ryan Hamilton 2012/11/01 22:52:20 Fixed upstream.
458 list_.push_back(make_pair(idx, string(kPayload + idx, size)));
459 remaining_payload -= size;
460 }
461 }
462
463 QuicSequencerRandomTest() {
464 //int32 seed = ACMRandom::HostnamePidTimeSeed();
465 //LOG(INFO) << "**** The current seed is " << seed << " ****";
466 //random_.reset(new ACMRandom(seed));
467
468 CreateFrames();
469 }
470
471 int OneToN(int n) {
472 return base::RandInt(1, n);
473 }
474
475 int MaybeProcessMaybeBuffer(const char* data, uint32 len) {
476 int to_process = len;
477 if (base::RandUint64() % 2 != 0) {
478 to_process = base::RandInt(0, len);
479 }
480 output_.append(data, to_process);
481 LOG(ERROR) << output_;
482 return to_process;
483 }
484
485 string output_;
486 //scoped_ptr<ACMRandom> random_;
487 FrameList list_;
488 };
489
490 // All frames are processed as soon as we have sequential data.
491 // Infinite buffering, so all frames are acked right away.
492 TEST_F(QuicSequencerRandomTest, RandomFramesNoDroppingNoBackup) {
493 InSequence s;
494 for (size_t i = 0; i < list_.size(); ++i) {
495 string* data = &list_[i].second;
496 EXPECT_CALL(stream_, ProcessData(StrEq(*data), data->size()))
497 .WillOnce(Return(data->size()));
498 }
499
500 while (list_.size() != 0) {
501 int idx = OneToN(list_.size()) - 1;
502 LOG(ERROR) << "Sending index " << idx << " " << list_[idx].second.c_str();
503 EXPECT_TRUE(sequencer_->OnFrame(
504 list_[idx].first, list_[idx].second.c_str(),
jar 2012/11/01 22:21:10 nit: probably avoid c_str() (in favor of data()) u
Ryan Hamilton 2012/11/01 22:52:20 Fixed upstream.
505 list_[idx].second.size()));
506 list_.erase(list_.begin() + idx);
507 }
508 }
509
510 // All frames are processed as soon as we have sequential data.
511 // Buffering, so some frames are rejected.
512 TEST_F(QuicSequencerRandomTest, RandomFramesDroppingNoBackup) {
513 sequencer_->SetMemoryLimit(26);
514
515 InSequence s;
516 for (size_t i = 0; i < list_.size(); ++i) {
517 string* data = &list_[i].second;
518 EXPECT_CALL(stream_, ProcessData(StrEq(*data), data->size()))
519 .WillOnce(Return(data->size()));
520 }
521
522 while (list_.size() != 0) {
523 int idx = OneToN(list_.size()) - 1;
524 LOG(ERROR) << "Sending index " << idx << " " << list_[idx].second.c_str();
525 bool acked = sequencer_->OnFrame(
526 list_[idx].first, list_[idx].second.c_str(),
527 list_[idx].second.size());
528 if (acked) {
529 list_.erase(list_.begin() + idx);
530 }
531 }
532 }
533
534 TEST_F(QuicSequencerRandomTest, RandomFramesNoDroppingBackup) {
535 char buffer[10];
536 iovec iov[2];
537 iov[0].iov_base = &buffer[0];
538 iov[0].iov_len = 5;
539 iov[1].iov_base = &buffer[5];
540 iov[1].iov_len = 5;
541
542 EXPECT_CALL(stream_, ProcessData(_, _))
543 .Times(AnyNumber())
544 .WillRepeatedly(Invoke(
545 this, &QuicSequencerRandomTest::MaybeProcessMaybeBuffer));
546
547 while (output_.size() != arraysize(kPayload) - 1) {
548 if (list_.size() != 0 && base::RandUint64() % 2 != 0) { // Send data
549 int idx = OneToN(list_.size()) - 1;
550 ASSERT_TRUE(sequencer_->OnFrame(
551 list_[idx].first, list_[idx].second.c_str(),
552 list_[idx].second.size()));
553 list_.erase(list_.begin() + idx);
554 } else { // Read data
555 int bytes_read = sequencer_->Readv(iov, 1);
556 output_.append(buffer, bytes_read);
557 }
558 }
559 EXPECT_EQ(0, strcmp(kPayload, output_.c_str()));
560 }
561
562 TEST_F(QuicSequencerRandomTest, RandomFramesNoDroppingBackupLocalBuffers) {
563 EXPECT_CALL(stream_, ProcessData(_, _))
564 .Times(AnyNumber())
565 .WillRepeatedly(Invoke(
566 this, &QuicSequencerRandomTest::MaybeProcessMaybeBuffer));
567
568 while (output_.size() != arraysize(kPayload) - 1) {
569 if (list_.size() != 0 && base::RandUint64() % 2 == 0) { // Send data
570 int idx = OneToN(list_.size()) - 1;
571 ASSERT_TRUE(sequencer_->OnFrame(
572 list_[idx].first, list_[idx].second.c_str(),
573 list_[idx].second.size()));
574 list_.erase(list_.begin() + idx);
575 } else { // Read data
576 bool has_bytes = sequencer_->HasBytesToRead();
577 iovec iov[2];
578 int iovs = sequencer_->GetReadableRegions(iov, 2);
579 int initial_size = output_.size();
580 if (has_bytes) {
581 ASSERT_LT(0, iovs);
582 } else {
583 ASSERT_EQ(0, iovs);
584 }
585 for (int i = 0; i < iovs; ++i) {
586 output_.append(static_cast<char*>(iov[i].iov_base), iov[i].iov_len);
587 }
588 sequencer_->AdvanceReadablePtr(output_.size() - initial_size);
589 }
590 }
591 EXPECT_EQ(0, strcmp(kPayload, output_.c_str()));
592 }
593
594 TEST_F(QuicSequencerRandomTest, RandomFramesDroppingBackup) {
595 sequencer_->SetMemoryLimit(26);
596
597 EXPECT_CALL(stream_, ProcessData(_, _))
598 .Times(AnyNumber())
599 .WillRepeatedly(Invoke(
600 this, &QuicSequencerRandomTest::MaybeProcessMaybeBuffer));
601
602 while (output_.size() != arraysize(kPayload) - 1) {
603 if (list_.size() != 0 && base::RandUint64() % 2 == 0) { // Send data
604 int idx = OneToN(list_.size()) - 1;
605 if (sequencer_->OnFrame(list_[idx].first, list_[idx].second.c_str(),
606 list_[idx].second.size())) {
607 list_.erase(list_.begin() + idx);
608 }
609 } else { // Read data
610 iovec iov[2];
611 int iovs = sequencer_->GetReadableRegions(iov, 2);
612 int initial_size = output_.size();
613 for (int i = 0; i < iovs; ++i) {
614 output_.append(static_cast<char*>(iov[i].iov_base), iov[i].iov_len);
615 }
616 sequencer_->AdvanceReadablePtr(output_.size() - initial_size);
617 }
618 }
619 EXPECT_EQ(0, strcmp(kPayload, output_.c_str()));
620 }
621
622 } // namespace
623
624 } // namespace net
OLDNEW

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld 1280:2d3e6564b7b6