OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 #include "webrtc/base/scoped_ptr.h" | 14 #include "webrtc/base/scoped_ptr.h" |
15 #include "webrtc/call.h" | 15 #include "webrtc/call.h" |
16 #include "webrtc/system_wrappers/include/clock.h" | 16 #include "webrtc/system_wrappers/include/clock.h" |
17 #include "webrtc/test/fake_network_pipe.h" | 17 #include "webrtc/test/fake_network_pipe.h" |
18 | 18 |
19 using ::testing::_; | 19 using ::testing::_; |
20 using ::testing::AnyNumber; | 20 using ::testing::AnyNumber; |
21 using ::testing::Return; | 21 using ::testing::Return; |
22 using ::testing::Invoke; | 22 using ::testing::Invoke; |
23 | 23 |
24 namespace webrtc { | 24 namespace webrtc { |
25 | 25 |
26 class MockReceiver : public PacketReceiver { | 26 class TestReceiver : public PacketReceiver { |
27 public: | 27 public: |
28 MockReceiver() {} | 28 TestReceiver() {} |
29 virtual ~MockReceiver() {} | 29 virtual ~TestReceiver() {} |
30 | 30 |
31 void IncomingPacket(const uint8_t* data, size_t length) { | 31 void IncomingPacket(const uint8_t* data, size_t length) { |
32 DeliverPacket(MediaType::ANY, data, length, PacketTime()); | 32 DeliverPacket(MediaType::ANY, data, length, PacketTime()); |
33 delete [] data; | 33 delete [] data; |
34 } | 34 } |
35 | 35 |
36 MOCK_METHOD4( | 36 virtual MOCK_METHOD4( |
37 DeliverPacket, | 37 DeliverPacket, |
38 DeliveryStatus(MediaType, const uint8_t*, size_t, const PacketTime&)); | 38 DeliveryStatus(MediaType, const uint8_t*, size_t, const PacketTime&)); |
39 }; | 39 }; |
40 | 40 |
| 41 class ReorderTestReceiver : public TestReceiver { |
| 42 public: |
| 43 ReorderTestReceiver() {} |
| 44 virtual ~ReorderTestReceiver() {} |
| 45 |
| 46 DeliveryStatus DeliverPacket(MediaType media_type, |
| 47 const uint8_t* packet, |
| 48 size_t length, |
| 49 const PacketTime& packet_time) override { |
| 50 int seq_num; |
| 51 memcpy(&seq_num, packet, sizeof(int)); |
| 52 delivered_sequence_numbers_.push_back(seq_num); |
| 53 return PacketReceiver::DELIVERY_OK; |
| 54 } |
| 55 std::vector<int> delivered_sequence_numbers_; |
| 56 |
| 57 }; |
| 58 |
41 class FakeNetworkPipeTest : public ::testing::Test { | 59 class FakeNetworkPipeTest : public ::testing::Test { |
42 public: | 60 public: |
43 FakeNetworkPipeTest() : fake_clock_(12345) {} | 61 FakeNetworkPipeTest() : fake_clock_(12345) {} |
44 | 62 |
45 protected: | 63 protected: |
46 virtual void SetUp() { | 64 virtual void SetUp() { |
47 receiver_.reset(new MockReceiver()); | 65 receiver_.reset(new TestReceiver()); |
48 ON_CALL(*receiver_, DeliverPacket(_, _, _, _)) | 66 ON_CALL(*receiver_, DeliverPacket(_, _, _, _)) |
49 .WillByDefault(Return(PacketReceiver::DELIVERY_OK)); | 67 .WillByDefault(Return(PacketReceiver::DELIVERY_OK)); |
50 } | 68 } |
51 | 69 |
52 virtual void TearDown() { | 70 virtual void TearDown() { |
53 } | 71 } |
54 | 72 |
55 void SendPackets(FakeNetworkPipe* pipe, int number_packets, int kPacketSize) { | 73 void SendPackets(FakeNetworkPipe* pipe, int number_packets, int packet_size) { |
56 rtc::scoped_ptr<uint8_t[]> packet(new uint8_t[kPacketSize]); | 74 RTC_DCHECK_GE(packet_size, static_cast<int>(sizeof(int))); |
| 75 rtc::scoped_ptr<uint8_t[]> packet(new uint8_t[packet_size]); |
57 for (int i = 0; i < number_packets; ++i) { | 76 for (int i = 0; i < number_packets; ++i) { |
58 pipe->SendPacket(packet.get(), kPacketSize); | 77 // Set a sequence number for the packets by |
| 78 // using the first bytes in the packet. |
| 79 memcpy(packet.get(), &i, sizeof(int)); |
| 80 pipe->SendPacket(packet.get(), packet_size); |
59 } | 81 } |
60 } | 82 } |
61 | 83 |
62 int PacketTimeMs(int capacity_kbps, int kPacketSize) const { | 84 int PacketTimeMs(int capacity_kbps, int packet_size) const { |
63 return 8 * kPacketSize / capacity_kbps; | 85 return 8 * packet_size / capacity_kbps; |
64 } | 86 } |
65 | 87 |
66 SimulatedClock fake_clock_; | 88 SimulatedClock fake_clock_; |
67 rtc::scoped_ptr<MockReceiver> receiver_; | 89 rtc::scoped_ptr<TestReceiver> receiver_; |
68 }; | 90 }; |
69 | 91 |
70 void DeleteMemory(uint8_t* data, int length) { delete [] data; } | 92 void DeleteMemory(uint8_t* data, int length) { delete [] data; } |
71 | 93 |
72 // Test the capacity link and verify we get as many packets as we expect. | 94 // Test the capacity link and verify we get as many packets as we expect. |
73 TEST_F(FakeNetworkPipeTest, CapacityTest) { | 95 TEST_F(FakeNetworkPipeTest, CapacityTest) { |
74 FakeNetworkPipe::Config config; | 96 FakeNetworkPipe::Config config; |
75 config.queue_length_packets = 20; | 97 config.queue_length_packets = 20; |
76 config.link_capacity_kbps = 80; | 98 config.link_capacity_kbps = 80; |
77 rtc::scoped_ptr<FakeNetworkPipe> pipe( | 99 rtc::scoped_ptr<FakeNetworkPipe> pipe( |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(1); | 323 EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(1); |
302 pipe->Process(); | 324 pipe->Process(); |
303 } | 325 } |
304 | 326 |
305 // Check that all the packets were sent. | 327 // Check that all the packets were sent. |
306 EXPECT_EQ(static_cast<size_t>(2 * kNumPackets), pipe->sent_packets()); | 328 EXPECT_EQ(static_cast<size_t>(2 * kNumPackets), pipe->sent_packets()); |
307 fake_clock_.AdvanceTimeMilliseconds(pipe->TimeUntilNextProcess()); | 329 fake_clock_.AdvanceTimeMilliseconds(pipe->TimeUntilNextProcess()); |
308 EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(0); | 330 EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(0); |
309 pipe->Process(); | 331 pipe->Process(); |
310 } | 332 } |
| 333 |
| 334 // At first disallow reordering and then allow reordering. |
| 335 TEST_F(FakeNetworkPipeTest, DisallowReorderingThenAllowReordering) { |
| 336 FakeNetworkPipe::Config config; |
| 337 config.queue_length_packets = 1000; |
| 338 config.link_capacity_kbps = 800; |
| 339 config.queue_delay_ms = 100; |
| 340 config.delay_standard_deviation_ms = 10; |
| 341 rtc::scoped_ptr<FakeNetworkPipe> pipe( |
| 342 new FakeNetworkPipe(&fake_clock_, config)); |
| 343 ReorderTestReceiver* receiver = new ReorderTestReceiver(); |
| 344 receiver_.reset(receiver); |
| 345 pipe->SetReceiver(receiver_.get()); |
| 346 |
| 347 const uint32_t kNumPackets = 100; |
| 348 const int kPacketSize = 10; |
| 349 SendPackets(pipe.get(), kNumPackets, kPacketSize); |
| 350 fake_clock_.AdvanceTimeMilliseconds(1000); |
| 351 pipe->Process(); |
| 352 |
| 353 // Confirm that all packets have been delivered in order. |
| 354 EXPECT_EQ(kNumPackets, receiver->delivered_sequence_numbers_.size()); |
| 355 int last_seq_num = -1; |
| 356 for(int seq_num : receiver->delivered_sequence_numbers_) { |
| 357 EXPECT_GT(seq_num, last_seq_num); |
| 358 last_seq_num = seq_num; |
| 359 } |
| 360 |
| 361 config.allow_reordering = true; |
| 362 pipe->SetConfig(config); |
| 363 SendPackets(pipe.get(), kNumPackets, kPacketSize); |
| 364 fake_clock_.AdvanceTimeMilliseconds(1000); |
| 365 receiver->delivered_sequence_numbers_.clear(); |
| 366 pipe->Process(); |
| 367 |
| 368 // Confirm that all packets have been delivered |
| 369 // and that reordering has occured. |
| 370 EXPECT_EQ(kNumPackets, receiver->delivered_sequence_numbers_.size()); |
| 371 bool reordering_has_occured = false; |
| 372 last_seq_num = -1; |
| 373 for(int seq_num : receiver->delivered_sequence_numbers_) { |
| 374 if(last_seq_num > seq_num) { |
| 375 reordering_has_occured = true; |
| 376 break; |
| 377 } |
| 378 last_seq_num = seq_num; |
| 379 } |
| 380 EXPECT_TRUE(reordering_has_occured); |
| 381 } |
311 } // namespace webrtc | 382 } // namespace webrtc |
OLD | NEW |