1 // Copyright 2013 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.
5 #ifndef NET_TOOLS_QUIC_TEST_TOOLS_PACKET_DROPPING_TEST_WRITER_H_
6 #define NET_TOOLS_QUIC_TEST_TOOLS_PACKET_DROPPING_TEST_WRITER_H_
11 #include "base/basictypes.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/synchronization/lock.h"
15 #include "net/quic/quic_alarm.h"
16 #include "net/quic/test_tools/quic_test_utils.h"
17 #include "net/tools/quic/quic_epoll_clock.h"
18 #include "net/tools/quic/quic_packet_writer_wrapper.h"
19 #include "net/tools/quic/test_tools/quic_test_client.h"
20 #include "net/tools/quic/test_tools/quic_test_utils.h"
26 // Simulates a connection that drops packets a configured percentage of the time
27 // and has a blocked socket a configured percentage of the time. Also provides
28 // the options to delay packets and reorder packets if delay is enabled.
29 class PacketDroppingTestWriter
: public QuicPacketWriterWrapper
{
33 virtual ~Delegate() {}
34 virtual void OnPacketSent(WriteResult result
) = 0;
35 virtual void OnCanWrite() = 0;
38 PacketDroppingTestWriter();
40 ~PacketDroppingTestWriter() override
;
42 // Must be called before blocking, reordering or delaying (loss is OK). May be
43 // called after connecting if the helper is not available before.
44 // |on_can_write| will be triggered when fake-unblocking; ownership will be
46 void Initialize(QuicConnectionHelperInterface
* helper
,
47 Delegate
* on_can_write
);
49 // QuicPacketWriter methods:
50 WriteResult
WritePacket(const char* buffer
,
52 const IPAddressNumber
& self_address
,
53 const IPEndPoint
& peer_address
) override
;
55 bool IsWriteBlocked() const override
;
57 void SetWritable() override
;
59 // Writes out any packet which should have been sent by now
60 // to the contained writer and returns the time
61 // for the next delayed packet to be written.
62 QuicTime
ReleaseOldPackets();
66 // The percent of time a packet is simulated as being lost.
67 void set_fake_packet_loss_percentage(int32 fake_packet_loss_percentage
) {
68 base::AutoLock
locked(config_mutex_
);
69 fake_packet_loss_percentage_
= fake_packet_loss_percentage
;
72 // Simulate dropping the first n packets unconditionally.
73 // Subsequent packets will be lost at fake_packet_loss_percentage_ if set.
74 void set_fake_drop_first_n_packets(int32 fake_drop_first_n_packets
) {
75 base::AutoLock
locked(config_mutex_
);
76 fake_drop_first_n_packets_
= fake_drop_first_n_packets
;
79 // The percent of time WritePacket will block and set WriteResult's status
80 // to WRITE_STATUS_BLOCKED.
81 void set_fake_blocked_socket_percentage(
82 int32 fake_blocked_socket_percentage
) {
84 base::AutoLock
locked(config_mutex_
);
85 fake_blocked_socket_percentage_
= fake_blocked_socket_percentage
;
88 // The percent of time a packet is simulated as being reordered.
89 void set_fake_reorder_percentage(int32 fake_packet_reorder_percentage
) {
91 base::AutoLock
locked(config_mutex_
);
92 DCHECK(!fake_packet_delay_
.IsZero());
93 fake_packet_reorder_percentage_
= fake_packet_reorder_percentage
;
96 // The delay before writing this packet.
97 void set_fake_packet_delay(QuicTime::Delta fake_packet_delay
) {
99 base::AutoLock
locked(config_mutex_
);
100 fake_packet_delay_
= fake_packet_delay
;
103 // The maximum bandwidth and buffer size of the connection. When these are
104 // set, packets will be delayed until a connection with that bandwidth would
105 // transmit it. Once the |buffer_size| is reached, all new packets are
107 void set_max_bandwidth_and_buffer_size(QuicBandwidth fake_bandwidth
,
108 QuicByteCount buffer_size
) {
110 base::AutoLock
locked(config_mutex_
);
111 fake_bandwidth_
= fake_bandwidth
;
112 buffer_size_
= buffer_size
;
115 // Useful for reproducing very flaky issues.
116 void set_seed(uint64 seed
) {
117 simple_random_
.set_seed(seed
);
121 // Writes out the next packet to the contained writer and returns the time
122 // for the next delayed packet to be written.
123 QuicTime
ReleaseNextPacket();
125 // A single packet which will be sent at the supplied send_time.
126 struct DelayedWrite
{
128 DelayedWrite(const char* buffer
,
130 const IPAddressNumber
& self_address
,
131 const IPEndPoint
& peer_address
,
136 const IPAddressNumber self_address
;
137 const IPEndPoint peer_address
;
141 typedef std::list
<DelayedWrite
> DelayedPacketList
;
143 const QuicClock
* clock_
;
144 scoped_ptr
<QuicAlarm
> write_unblocked_alarm_
;
145 scoped_ptr
<QuicAlarm
> delay_alarm_
;
146 scoped_ptr
<Delegate
> on_can_write_
;
147 net::test::SimpleRandom simple_random_
;
148 // Stored packets delayed by fake packet delay or bandwidth restrictions.
149 DelayedPacketList delayed_packets_
;
150 QuicByteCount cur_buffer_size_
;
151 uint64 num_calls_to_write_
;
153 base::Lock config_mutex_
;
154 int32 fake_packet_loss_percentage_
;
155 int32 fake_drop_first_n_packets_
;
156 int32 fake_blocked_socket_percentage_
;
157 int32 fake_packet_reorder_percentage_
;
158 QuicTime::Delta fake_packet_delay_
;
159 QuicBandwidth fake_bandwidth_
;
160 QuicByteCount buffer_size_
;
162 DISALLOW_COPY_AND_ASSIGN(PacketDroppingTestWriter
);
169 #endif // NET_TOOLS_QUIC_TEST_TOOLS_PACKET_DROPPING_TEST_WRITER_H_