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 |
41 class FakeNetworkPipeTest : public ::testing::Test { | 58 class FakeNetworkPipeTest : public ::testing::Test { |
42 public: | 59 public: |
43 FakeNetworkPipeTest() : fake_clock_(12345) {} | 60 FakeNetworkPipeTest() : fake_clock_(12345) {} |
44 | 61 |
45 protected: | 62 protected: |
46 virtual void SetUp() { | 63 virtual void SetUp() { |
47 receiver_.reset(new MockReceiver()); | 64 receiver_.reset(new TestReceiver()); |
48 ON_CALL(*receiver_, DeliverPacket(_, _, _, _)) | 65 ON_CALL(*receiver_, DeliverPacket(_, _, _, _)) |
49 .WillByDefault(Return(PacketReceiver::DELIVERY_OK)); | 66 .WillByDefault(Return(PacketReceiver::DELIVERY_OK)); |
50 } | 67 } |
51 | 68 |
52 virtual void TearDown() { | 69 virtual void TearDown() { |
53 } | 70 } |
54 | 71 |
55 void SendPackets(FakeNetworkPipe* pipe, int number_packets, int kPacketSize) { | 72 void SendPackets(FakeNetworkPipe* pipe, int number_packets, int packet_size) { |
56 rtc::scoped_ptr<uint8_t[]> packet(new uint8_t[kPacketSize]); | 73 RTC_DCHECK_GE(packet_size, static_cast<int>(sizeof(int))); |
| 74 rtc::scoped_ptr<uint8_t[]> packet(new uint8_t[packet_size]); |
57 for (int i = 0; i < number_packets; ++i) { | 75 for (int i = 0; i < number_packets; ++i) { |
58 pipe->SendPacket(packet.get(), kPacketSize); | 76 // Set a sequence number for the packets by |
| 77 // using the first bytes in the packet. |
| 78 memcpy(packet.get(), &i, sizeof(int)); |
| 79 pipe->SendPacket(packet.get(), packet_size); |
59 } | 80 } |
60 } | 81 } |
61 | 82 |
62 int PacketTimeMs(int capacity_kbps, int kPacketSize) const { | 83 int PacketTimeMs(int capacity_kbps, int packet_size) const { |
63 return 8 * kPacketSize / capacity_kbps; | 84 return 8 * packet_size / capacity_kbps; |
64 } | 85 } |
65 | 86 |
66 SimulatedClock fake_clock_; | 87 SimulatedClock fake_clock_; |
67 rtc::scoped_ptr<MockReceiver> receiver_; | 88 rtc::scoped_ptr<TestReceiver> receiver_; |
68 }; | 89 }; |
69 | 90 |
70 void DeleteMemory(uint8_t* data, int length) { delete [] data; } | 91 void DeleteMemory(uint8_t* data, int length) { delete [] data; } |
71 | 92 |
72 // Test the capacity link and verify we get as many packets as we expect. | 93 // Test the capacity link and verify we get as many packets as we expect. |
73 TEST_F(FakeNetworkPipeTest, CapacityTest) { | 94 TEST_F(FakeNetworkPipeTest, CapacityTest) { |
74 FakeNetworkPipe::Config config; | 95 FakeNetworkPipe::Config config; |
75 config.queue_length_packets = 20; | 96 config.queue_length_packets = 20; |
76 config.link_capacity_kbps = 80; | 97 config.link_capacity_kbps = 80; |
77 rtc::scoped_ptr<FakeNetworkPipe> pipe( | 98 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); | 322 EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(1); |
302 pipe->Process(); | 323 pipe->Process(); |
303 } | 324 } |
304 | 325 |
305 // Check that all the packets were sent. | 326 // Check that all the packets were sent. |
306 EXPECT_EQ(static_cast<size_t>(2 * kNumPackets), pipe->sent_packets()); | 327 EXPECT_EQ(static_cast<size_t>(2 * kNumPackets), pipe->sent_packets()); |
307 fake_clock_.AdvanceTimeMilliseconds(pipe->TimeUntilNextProcess()); | 328 fake_clock_.AdvanceTimeMilliseconds(pipe->TimeUntilNextProcess()); |
308 EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(0); | 329 EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(0); |
309 pipe->Process(); | 330 pipe->Process(); |
310 } | 331 } |
| 332 |
| 333 // At first disallow reordering and then allow reordering. |
| 334 TEST_F(FakeNetworkPipeTest, DisallowReorderingThenAllowReordering) { |
| 335 FakeNetworkPipe::Config config; |
| 336 config.queue_length_packets = 1000; |
| 337 config.link_capacity_kbps = 800; |
| 338 config.queue_delay_ms = 100; |
| 339 config.delay_standard_deviation_ms = 10; |
| 340 rtc::scoped_ptr<FakeNetworkPipe> pipe( |
| 341 new FakeNetworkPipe(&fake_clock_, config)); |
| 342 ReorderTestReceiver* receiver = new ReorderTestReceiver(); |
| 343 receiver_.reset(receiver); |
| 344 pipe->SetReceiver(receiver_.get()); |
| 345 |
| 346 const uint32_t kNumPackets = 100; |
| 347 const int kPacketSize = 10; |
| 348 SendPackets(pipe.get(), kNumPackets, kPacketSize); |
| 349 fake_clock_.AdvanceTimeMilliseconds(1000); |
| 350 pipe->Process(); |
| 351 |
| 352 // Confirm that all packets have been delivered in order. |
| 353 EXPECT_EQ(kNumPackets, receiver->delivered_sequence_numbers_.size()); |
| 354 int last_seq_num = -1; |
| 355 for (int seq_num : receiver->delivered_sequence_numbers_) { |
| 356 EXPECT_GT(seq_num, last_seq_num); |
| 357 last_seq_num = seq_num; |
| 358 } |
| 359 |
| 360 config.allow_reordering = true; |
| 361 pipe->SetConfig(config); |
| 362 SendPackets(pipe.get(), kNumPackets, kPacketSize); |
| 363 fake_clock_.AdvanceTimeMilliseconds(1000); |
| 364 receiver->delivered_sequence_numbers_.clear(); |
| 365 pipe->Process(); |
| 366 |
| 367 // Confirm that all packets have been delivered |
| 368 // and that reordering has occured. |
| 369 EXPECT_EQ(kNumPackets, receiver->delivered_sequence_numbers_.size()); |
| 370 bool reordering_has_occured = false; |
| 371 last_seq_num = -1; |
| 372 for (int seq_num : receiver->delivered_sequence_numbers_) { |
| 373 if (last_seq_num > seq_num) { |
| 374 reordering_has_occured = true; |
| 375 break; |
| 376 } |
| 377 last_seq_num = seq_num; |
| 378 } |
| 379 EXPECT_TRUE(reordering_has_occured); |
| 380 } |
311 } // namespace webrtc | 381 } // namespace webrtc |
OLD | NEW |