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 UnitTestReceiver : public PacketReceiver { |
stefan-webrtc
2016/01/25 13:16:57
TestReceiver would be enough
philipel
2016/01/26 10:50:47
Done.
| |
27 public: | 27 public: |
28 MockReceiver() {} | 28 UnitTestReceiver() {} |
29 virtual ~MockReceiver() {} | 29 virtual ~UnitTestReceiver() {} |
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 UnitTestImplReceiver : public UnitTestReceiver { | |
stefan-webrtc
2016/01/25 13:16:57
Impl suggests that this is implementing from an in
philipel
2016/01/26 10:50:47
I have tried to do this but then the receiver_ has
stefan-webrtc
2016/01/26 11:50:46
OK, I can live with how it is now.
| |
42 public: | |
43 UnitTestImplReceiver() {} | |
44 virtual ~UnitTestImplReceiver() {} | |
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 UnitTestReceiver()); |
48 ON_CALL(*receiver_, DeliverPacket(_, _, _, _)) | 65 ON_CALL(*receiver_, DeliverPacket(_, _, _, _)) |
49 .WillByDefault(Return(PacketReceiver::DELIVERY_OK)); | 66 .WillByDefault(Return(PacketReceiver::DELIVERY_OK)); |
67 receiver_impl_.reset(new UnitTestImplReceiver()); | |
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<UnitTestReceiver> receiver_; |
90 rtc::scoped_ptr<UnitTestImplReceiver> receiver_impl_; | |
68 }; | 91 }; |
69 | 92 |
70 void DeleteMemory(uint8_t* data, int length) { delete [] data; } | 93 void DeleteMemory(uint8_t* data, int length) { delete [] data; } |
71 | 94 |
72 // Test the capacity link and verify we get as many packets as we expect. | 95 // Test the capacity link and verify we get as many packets as we expect. |
73 TEST_F(FakeNetworkPipeTest, CapacityTest) { | 96 TEST_F(FakeNetworkPipeTest, CapacityTest) { |
74 FakeNetworkPipe::Config config; | 97 FakeNetworkPipe::Config config; |
75 config.queue_length_packets = 20; | 98 config.queue_length_packets = 20; |
76 config.link_capacity_kbps = 80; | 99 config.link_capacity_kbps = 80; |
77 rtc::scoped_ptr<FakeNetworkPipe> pipe( | 100 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); | 324 EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(1); |
302 pipe->Process(); | 325 pipe->Process(); |
303 } | 326 } |
304 | 327 |
305 // Check that all the packets were sent. | 328 // Check that all the packets were sent. |
306 EXPECT_EQ(static_cast<size_t>(2 * kNumPackets), pipe->sent_packets()); | 329 EXPECT_EQ(static_cast<size_t>(2 * kNumPackets), pipe->sent_packets()); |
307 fake_clock_.AdvanceTimeMilliseconds(pipe->TimeUntilNextProcess()); | 330 fake_clock_.AdvanceTimeMilliseconds(pipe->TimeUntilNextProcess()); |
308 EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(0); | 331 EXPECT_CALL(*receiver_, DeliverPacket(_, _, _, _)).Times(0); |
309 pipe->Process(); | 332 pipe->Process(); |
310 } | 333 } |
334 | |
335 // At first don't disallow reordering and then allow reordering. | |
philipel
2016/01/20 15:25:23
Woops, commend fixed.
| |
336 TEST_F(FakeNetworkPipeTest, DisallowReorderingThenAllowReordering) { | |
337 FakeNetworkPipe::Config config; | |
338 config.queue_length_packets = 1000; | |
339 config.link_capacity_kbps = 800; | |
340 config.queue_delay_ms = 100; | |
341 config.delay_standard_deviation_ms = 10; | |
342 rtc::scoped_ptr<FakeNetworkPipe> pipe( | |
343 new FakeNetworkPipe(&fake_clock_, config)); | |
344 pipe->SetReceiver(receiver_impl_.get()); | |
345 | |
346 // Add 1000 packets of 1000 bytes, = 10 kB. | |
stefan-webrtc
2016/01/25 13:16:57
Remove ","
philipel
2016/01/26 10:50:47
Removed comment all together.
| |
347 const uint32_t kNumPackets = 100; | |
348 const int kPacketSize = 10; | |
stefan-webrtc
2016/01/25 13:16:57
How does this add 1000 packets of 1000 bytes? Look
philipel
2016/01/26 10:50:47
No idea :)
| |
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(receiver_impl_->delivered_sequence_numbers_.size(), kNumPackets); | |
355 int last_seq_num = -1; | |
356 for(int seq_num : receiver_impl_->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_impl_->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(receiver_impl_->delivered_sequence_numbers_.size(), kNumPackets); | |
stefan-webrtc
2016/01/25 13:16:57
Expectation first, i.e., EXPECT_EQ(kNumPackets, ..
| |
371 bool reordering_has_occured = false; | |
372 last_seq_num = -1; | |
373 for(int seq_num : receiver_impl_->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); | |
stefan-webrtc
2016/01/25 13:16:57
Can this be flaky, or can you make sure it always
philipel
2016/01/26 10:50:47
Can't say for sure if it's flaky. The random funct
stefan-webrtc
2016/01/26 11:50:46
Good. rand() doesn't generate the same sequences o
| |
381 } | |
311 } // namespace webrtc | 382 } // namespace webrtc |
OLD | NEW |