Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / net / quic / quic_connection_test.cc
index 73706f1..6ca7c30 100644 (file)
@@ -8,12 +8,13 @@
 #include "base/bind.h"
 #include "base/stl_util.h"
 #include "net/base/net_errors.h"
+#include "net/quic/congestion_control/loss_detection_interface.h"
 #include "net/quic/congestion_control/receive_algorithm_interface.h"
 #include "net/quic/congestion_control/send_algorithm_interface.h"
 #include "net/quic/crypto/null_encrypter.h"
 #include "net/quic/crypto/quic_decrypter.h"
 #include "net/quic/crypto/quic_encrypter.h"
-#include "net/quic/crypto/quic_random.h"
+#include "net/quic/quic_flags.h"
 #include "net/quic/quic_protocol.h"
 #include "net/quic/quic_utils.h"
 #include "net/quic/test_tools/mock_clock.h"
@@ -21,7 +22,9 @@
 #include "net/quic/test_tools/quic_connection_peer.h"
 #include "net/quic/test_tools/quic_framer_peer.h"
 #include "net/quic/test_tools/quic_packet_creator_peer.h"
+#include "net/quic/test_tools/quic_sent_packet_manager_peer.h"
 #include "net/quic/test_tools/quic_test_utils.h"
+#include "net/quic/test_tools/simple_quic_framer.h"
 #include "testing/gmock/include/gmock/gmock.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
@@ -30,7 +33,9 @@ using std::map;
 using std::vector;
 using testing::_;
 using testing::AnyNumber;
+using testing::AtLeast;
 using testing::ContainerEq;
+using testing::Contains;
 using testing::DoAll;
 using testing::InSequence;
 using testing::InvokeWithoutArgs;
@@ -74,8 +79,8 @@ class TestReceiveAlgorithm : public ReceiveAlgorithmInterface {
     return true;
   }
 
-  MOCK_METHOD4(RecordIncomingPacket,
-               void(QuicByteCount, QuicPacketSequenceNumber, QuicTime, bool));
+  MOCK_METHOD3(RecordIncomingPacket,
+               void(QuicByteCount, QuicPacketSequenceNumber, QuicTime));
 
  private:
   QuicCongestionFeedbackFrame* feedback_;
@@ -142,6 +147,8 @@ class TaggingEncrypter : public QuicEncrypter {
   };
 
   const uint8 tag_;
+
+  DISALLOW_COPY_AND_ASSIGN(TaggingEncrypter);
 };
 
 // TaggingDecrypter ensures that the final kTagSize bytes of the message all
@@ -269,62 +276,42 @@ class TestConnectionHelper : public QuicConnectionHelperInterface {
 
 class TestPacketWriter : public QuicPacketWriter {
  public:
-  TestPacketWriter()
-      : last_packet_size_(0),
-        blocked_(false),
+  explicit TestPacketWriter(QuicVersion version)
+      : version_(version),
+        framer_(SupportedVersions(version_)),
+        last_packet_size_(0),
+        write_blocked_(false),
+        block_on_next_write_(false),
         is_write_blocked_data_buffered_(false),
-        is_server_(true),
+        final_bytes_of_last_packet_(0),
+        final_bytes_of_previous_packet_(0),
         use_tagging_decrypter_(false),
         packets_write_attempts_(0) {
   }
 
-  virtual ~TestPacketWriter() {
-    STLDeleteElements(&stream_data_);
-  }
-
-  // QuicPacketWriter
+  // QuicPacketWriter interface
   virtual WriteResult WritePacket(
       const char* buffer, size_t buf_len,
       const IPAddressNumber& self_address,
-      const IPEndPoint& peer_address,
-      QuicBlockedWriterInterface* blocked_writer) OVERRIDE {
+      const IPEndPoint& peer_address) OVERRIDE {
     QuicEncryptedPacket packet(buffer, buf_len);
     ++packets_write_attempts_;
 
     if (packet.length() >= sizeof(final_bytes_of_last_packet_)) {
+      final_bytes_of_previous_packet_ = final_bytes_of_last_packet_;
       memcpy(&final_bytes_of_last_packet_, packet.data() + packet.length() - 4,
              sizeof(final_bytes_of_last_packet_));
     }
 
-    QuicFramer framer(QuicSupportedVersions(), QuicTime::Zero(), !is_server_);
     if (use_tagging_decrypter_) {
-      framer.SetDecrypter(new TaggingDecrypter);
-    }
-    FramerVisitorCapturingFrames visitor;
-    framer.set_visitor(&visitor);
-    EXPECT_TRUE(framer.ProcessPacket(packet));
-    header_ = *visitor.header();
-    frame_count_ = visitor.frame_count();
-    if (visitor.ack()) {
-      ack_.reset(new QuicAckFrame(*visitor.ack()));
+      framer_.framer()->SetDecrypter(new TaggingDecrypter, ENCRYPTION_NONE);
     }
-    if (visitor.feedback()) {
-      feedback_.reset(new QuicCongestionFeedbackFrame(*visitor.feedback()));
+    EXPECT_TRUE(framer_.ProcessPacket(packet));
+    if (block_on_next_write_) {
+      write_blocked_ = true;
+      block_on_next_write_ = false;
     }
-    if (visitor.stream_frames() != NULL && !visitor.stream_frames()->empty()) {
-      stream_frames_ = *visitor.stream_frames();
-      // Also make a copy of underlying data, since the data that the frames in
-      // |stream_frames_| point to is bound to the |visitor|'s scope.
-      for (size_t i = 0; i < stream_frames_.size(); ++i) {
-        stream_data_.push_back(new string(*visitor.stream_data()[i]));
-        stream_frames_[i].data = *(stream_data_.back());
-      }
-    }
-    if (visitor.version_negotiation_packet() != NULL) {
-      version_negotiation_packet_.reset(new QuicVersionNegotiationPacket(
-          *visitor.version_negotiation_packet()));
-    }
-    if (blocked_) {
+    if (IsWriteBlocked()) {
       return WriteResult(WRITE_STATUS_BLOCKED, -1);
     }
     last_packet_size_ = packet.length();
@@ -335,33 +322,57 @@ class TestPacketWriter : public QuicPacketWriter {
     return is_write_blocked_data_buffered_;
   }
 
-  QuicPacketHeader* header() { return &header_; }
+  virtual bool IsWriteBlocked() const OVERRIDE { return write_blocked_; }
+
+  virtual void SetWritable() OVERRIDE { write_blocked_ = false; }
+
+  void BlockOnNextWrite() { block_on_next_write_ = true; }
+
+  const QuicPacketHeader& header() { return framer_.header(); }
+
+  size_t frame_count() const { return framer_.num_frames(); }
+
+  const vector<QuicAckFrame>& ack_frames() const {
+    return framer_.ack_frames();
+  }
+
+  const vector<QuicCongestionFeedbackFrame>& feedback_frames() const {
+    return framer_.feedback_frames();
+  }
 
-  size_t frame_count() const { return frame_count_; }
+  const vector<QuicStopWaitingFrame>& stop_waiting_frames() const {
+    return framer_.stop_waiting_frames();
+  }
 
-  QuicAckFrame* ack() { return ack_.get(); }
+  const vector<QuicConnectionCloseFrame>& connection_close_frames() const {
+    return framer_.connection_close_frames();
+  }
 
-  QuicCongestionFeedbackFrame* feedback() { return feedback_.get(); }
+  const vector<QuicStreamFrame>& stream_frames() const {
+    return framer_.stream_frames();
+  }
 
-  const vector<QuicStreamFrame>* stream_frames() const {
-    return &stream_frames_;
+  const vector<QuicPingFrame>& ping_frames() const {
+    return framer_.ping_frames();
   }
 
   size_t last_packet_size() {
     return last_packet_size_;
   }
 
-  QuicVersionNegotiationPacket* version_negotiation_packet() {
-    return version_negotiation_packet_.get();
+  const QuicVersionNegotiationPacket* version_negotiation_packet() {
+    return framer_.version_negotiation_packet();
   }
 
-  void set_blocked(bool blocked) { blocked_ = blocked; }
-
   void set_is_write_blocked_data_buffered(bool buffered) {
     is_write_blocked_data_buffered_ = buffered;
   }
 
-  void set_is_server(bool is_server) { is_server_ = is_server; }
+  void set_is_server(bool is_server) {
+    // We invert is_server here, because the framer needs to parse packets
+    // we send.
+    QuicFramerPeer::SetIsServer(framer_.framer(), !is_server);
+  }
 
   // final_bytes_of_last_packet_ returns the last four bytes of the previous
   // packet as a little-endian, uint32. This is intended to be used with a
@@ -369,25 +380,32 @@ class TestPacketWriter : public QuicPacketWriter {
   // a given packet.
   uint32 final_bytes_of_last_packet() { return final_bytes_of_last_packet_; }
 
+  // Returns the final bytes of the second to last packet.
+  uint32 final_bytes_of_previous_packet() {
+    return final_bytes_of_previous_packet_;
+  }
+
   void use_tagging_decrypter() {
     use_tagging_decrypter_ = true;
   }
 
   uint32 packets_write_attempts() { return packets_write_attempts_; }
 
+  void Reset() { framer_.Reset(); }
+
+  void SetSupportedVersions(const QuicVersionVector& versions) {
+    framer_.SetSupportedVersions(versions);
+  }
+
  private:
-  QuicPacketHeader header_;
-  size_t frame_count_;
-  scoped_ptr<QuicAckFrame> ack_;
-  scoped_ptr<QuicCongestionFeedbackFrame> feedback_;
-  vector<QuicStreamFrame> stream_frames_;
-  vector<string*> stream_data_;
-  scoped_ptr<QuicVersionNegotiationPacket> version_negotiation_packet_;
+  QuicVersion version_;
+  SimpleQuicFramer framer_;
   size_t last_packet_size_;
-  bool blocked_;
+  bool write_blocked_;
+  bool block_on_next_write_;
   bool is_write_blocked_data_buffered_;
-  bool is_server_;
   uint32 final_bytes_of_last_packet_;
+  uint32 final_bytes_of_previous_packet_;
   bool use_tagging_decrypter_;
   uint32 packets_write_attempts_;
 
@@ -396,15 +414,20 @@ class TestPacketWriter : public QuicPacketWriter {
 
 class TestConnection : public QuicConnection {
  public:
-  TestConnection(QuicGuid guid,
+  TestConnection(QuicConnectionId connection_id,
                  IPEndPoint address,
                  TestConnectionHelper* helper,
                  TestPacketWriter* writer,
-                 bool is_server)
-      : QuicConnection(guid, address, helper, writer, is_server,
-                       QuicSupportedVersions()),
-        helper_(helper),
+                 bool is_server,
+                 QuicVersion version,
+                 uint32 flow_control_send_window)
+      : QuicConnection(connection_id, address, helper, writer, is_server,
+                       SupportedVersions(version),
+                       flow_control_send_window),
         writer_(writer) {
+    // Disable tail loss probes for most tests.
+    QuicSentPacketManagerPeer::SetMaxTailLossProbes(
+        QuicConnectionPeer::GetSentPacketManager(this), 0);
     writer_->set_is_server(is_server);
   }
 
@@ -420,6 +443,11 @@ class TestConnection : public QuicConnection {
     QuicConnectionPeer::SetSendAlgorithm(this, send_algorithm);
   }
 
+  void SetLossAlgorithm(LossDetectionInterface* loss_algorithm) {
+    QuicSentPacketManagerPeer::SetLossAlgorithm(
+        QuicConnectionPeer::GetSentPacketManager(this), loss_algorithm);
+  }
+
   void SendPacket(EncryptionLevel level,
                   QuicPacketSequenceNumber sequence_number,
                   QuicPacket* packet,
@@ -433,18 +461,7 @@ class TestConnection : public QuicConnection {
                          packet, entropy_hash, retransmittable_frames));
   }
 
-  QuicConsumedData SendStreamDataWithString(QuicStreamId id,
-                                            StringPiece data,
-                                            QuicStreamOffset offset,
-                                            bool fin) {
-    IOVector data_iov;
-    if (!data.empty()) {
-      data_iov.Append(const_cast<char*>(data.data()), data.size());
-    }
-    return QuicConnection::SendStreamData(id, data_iov, offset, fin);
-  }
-
-  QuicConsumedData SendStreamDataWithStringAndNotifyWhenAcked(
+  QuicConsumedData SendStreamDataWithString(
       QuicStreamId id,
       StringPiece data,
       QuicStreamOffset offset,
@@ -454,17 +471,21 @@ class TestConnection : public QuicConnection {
     if (!data.empty()) {
       data_iov.Append(const_cast<char*>(data.data()), data.size());
     }
-    return QuicConnection::SendStreamDataAndNotifyWhenAcked(id, data_iov,
-                                                            offset, fin,
-                                                            delegate);
+    return QuicConnection::SendStreamData(id, data_iov, offset, fin, delegate);
   }
 
   QuicConsumedData SendStreamData3() {
-    return SendStreamDataWithString(kStreamId3, "food", 0, !kFin);
+    return SendStreamDataWithString(kStreamId3, "food", 0, !kFin, NULL);
   }
 
   QuicConsumedData SendStreamData5() {
-    return SendStreamDataWithString(kStreamId5, "food2", 0, !kFin);
+    return SendStreamDataWithString(kStreamId5, "food2", 0, !kFin, NULL);
+  }
+
+  // Ensures the connection can write stream data before writing.
+  QuicConsumedData EnsureWritableAndSendStreamData5() {
+    EXPECT_TRUE(CanWriteStreamData());
+    return SendStreamData5();
   }
 
   // The crypto stream has special semantics so that it is not blocked by a
@@ -475,7 +496,7 @@ class TestConnection : public QuicConnection {
   QuicConsumedData SendCryptoStreamData() {
     this->Flush();
     QuicConsumedData consumed =
-        SendStreamDataWithString(kCryptoStreamId, "chlo", 0, !kFin);
+        SendStreamDataWithString(kCryptoStreamId, "chlo", 0, !kFin, NULL);
     this->Flush();
     return consumed;
   }
@@ -485,7 +506,12 @@ class TestConnection : public QuicConnection {
   }
 
   void set_version(QuicVersion version) {
-    framer_.set_version(version);
+    QuicConnectionPeer::GetFramer(this)->set_version(version);
+  }
+
+  void SetSupportedVersions(const QuicVersionVector& versions) {
+    QuicConnectionPeer::GetFramer(this)->SetSupportedVersions(versions);
+    writer_->SetSupportedVersions(versions);
   }
 
   void set_is_server(bool is_server) {
@@ -500,6 +526,16 @@ class TestConnection : public QuicConnection {
         QuicConnectionPeer::GetAckAlarm(this));
   }
 
+  TestConnectionHelper::TestAlarm* GetPingAlarm() {
+    return reinterpret_cast<TestConnectionHelper::TestAlarm*>(
+        QuicConnectionPeer::GetPingAlarm(this));
+  }
+
+  TestConnectionHelper::TestAlarm* GetResumeWritesAlarm() {
+    return reinterpret_cast<TestConnectionHelper::TestAlarm*>(
+        QuicConnectionPeer::GetResumeWritesAlarm(this));
+  }
+
   TestConnectionHelper::TestAlarm* GetRetransmissionAlarm() {
     return reinterpret_cast<TestConnectionHelper::TestAlarm*>(
         QuicConnectionPeer::GetRetransmissionAlarm(this));
@@ -510,11 +546,6 @@ class TestConnection : public QuicConnection {
         QuicConnectionPeer::GetSendAlarm(this));
   }
 
-  TestConnectionHelper::TestAlarm* GetResumeWritesAlarm() {
-    return reinterpret_cast<TestConnectionHelper::TestAlarm*>(
-        QuicConnectionPeer::GetResumeWritesAlarm(this));
-  }
-
   TestConnectionHelper::TestAlarm* GetTimeoutAlarm() {
     return reinterpret_cast<TestConnectionHelper::TestAlarm*>(
         QuicConnectionPeer::GetTimeoutAlarm(this));
@@ -523,54 +554,78 @@ class TestConnection : public QuicConnection {
   using QuicConnection::SelectMutualVersion;
 
  private:
-  TestConnectionHelper* helper_;
   TestPacketWriter* writer_;
 
   DISALLOW_COPY_AND_ASSIGN(TestConnection);
 };
 
-class QuicConnectionTest : public ::testing::TestWithParam<bool> {
+// Used for testing packets revived from FEC packets.
+class FecQuicConnectionDebugVisitor
+    : public QuicConnectionDebugVisitorInterface {
+ public:
+  virtual void OnRevivedPacket(const QuicPacketHeader& header,
+                               StringPiece data) OVERRIDE {
+    revived_header_ = header;
+  }
+
+  // Public accessor method.
+  QuicPacketHeader revived_header() const {
+    return revived_header_;
+  }
+
+ private:
+  QuicPacketHeader revived_header_;
+};
+
+class QuicConnectionTest : public ::testing::TestWithParam<QuicVersion> {
  protected:
   QuicConnectionTest()
-      : guid_(42),
-        framer_(QuicSupportedVersions(), QuicTime::Zero(), false),
-        creator_(guid_, &framer_, QuicRandom::GetInstance(), false),
+      : connection_id_(42),
+        framer_(SupportedVersions(version()), QuicTime::Zero(), false),
+        creator_(connection_id_, &framer_, &random_generator_, false),
         send_algorithm_(new StrictMock<MockSendAlgorithm>),
+        loss_algorithm_(new MockLossAlgorithm()),
         helper_(new TestConnectionHelper(&clock_, &random_generator_)),
-        writer_(new TestPacketWriter()),
-        connection_(guid_, IPEndPoint(), helper_.get(), writer_.get(), false),
-        frame1_(1, false, 0, data1),
-        frame2_(1, false, 3, data2),
-        accept_packet_(true) {
-    // TODO(rtenneti): remove g_* flags.
-    FLAGS_track_retransmission_history = true;
+        writer_(new TestPacketWriter(version())),
+        connection_(connection_id_, IPEndPoint(), helper_.get(),
+                    writer_.get(), false, version(),
+                    kDefaultFlowControlSendWindow),
+        frame1_(1, false, 0, MakeIOVector(data1)),
+        frame2_(1, false, 3, MakeIOVector(data2)),
+        sequence_number_length_(PACKET_6BYTE_SEQUENCE_NUMBER),
+        connection_id_length_(PACKET_8BYTE_CONNECTION_ID) {
     connection_.set_visitor(&visitor_);
     connection_.SetSendAlgorithm(send_algorithm_);
+    connection_.SetLossAlgorithm(loss_algorithm_);
     framer_.set_received_entropy_calculator(&entropy_calculator_);
     // Simplify tests by not sending feedback unless specifically configured.
     SetFeedback(NULL);
     EXPECT_CALL(
-        *send_algorithm_, TimeUntilSend(_, _, _, _)).WillRepeatedly(Return(
+        *send_algorithm_, TimeUntilSend(_, _, _)).WillRepeatedly(Return(
             QuicTime::Delta::Zero()));
     EXPECT_CALL(*receive_algorithm_,
-                RecordIncomingPacket(_, _, _, _)).Times(AnyNumber());
+                RecordIncomingPacket(_, _, _)).Times(AnyNumber());
     EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _))
         .Times(AnyNumber());
     EXPECT_CALL(*send_algorithm_, RetransmissionDelay()).WillRepeatedly(
         Return(QuicTime::Delta::Zero()));
-    EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillRepeatedly(Return(
-        QuicBandwidth::FromKBitsPerSecond(100)));
-    EXPECT_CALL(*send_algorithm_, SmoothedRtt()).WillRepeatedly(Return(
-        QuicTime::Delta::FromMilliseconds(100)));
+    EXPECT_CALL(*send_algorithm_, GetCongestionWindow()).WillRepeatedly(
+        Return(kMaxPacketSize));
     ON_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _))
         .WillByDefault(Return(true));
+    EXPECT_CALL(visitor_, HasPendingWrites()).Times(AnyNumber());
     EXPECT_CALL(visitor_, HasPendingHandshake()).Times(AnyNumber());
-    EXPECT_CALL(visitor_, OnCanWrite()).Times(AnyNumber()).WillRepeatedly(
-        Return(true));
+    EXPECT_CALL(visitor_, OnCanWrite()).Times(AnyNumber());
+    EXPECT_CALL(visitor_, HasOpenDataStreams()).WillRepeatedly(Return(false));
+
+    EXPECT_CALL(*loss_algorithm_, GetLossTimeout())
+        .WillRepeatedly(Return(QuicTime::Zero()));
+    EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
+        .WillRepeatedly(Return(SequenceNumberSet()));
   }
 
-  void SetUp() {
-    FLAGS_bundle_ack_with_outgoing_packet = GetParam();
+  QuicVersion version() {
+    return GetParam();
   }
 
   QuicAckFrame* outgoing_ack() {
@@ -578,24 +633,17 @@ class QuicConnectionTest : public ::testing::TestWithParam<bool> {
     return outgoing_ack_.get();
   }
 
-  QuicAckFrame* last_ack() {
-    return writer_->ack();
-  }
-
-  QuicCongestionFeedbackFrame* last_feedback() {
-    return writer_->feedback();
-  }
-
-  QuicPacketHeader* last_header() {
-    return writer_->header();
-  }
-
-  size_t last_sent_packet_size() {
-    return writer_->last_packet_size();
-  }
-
-  uint32 final_bytes_of_last_packet() {
-    return writer_->final_bytes_of_last_packet();
+  QuicPacketSequenceNumber least_unacked() {
+    if (version() <= QUIC_VERSION_15) {
+      if (writer_->ack_frames().empty()) {
+        return 0;
+      }
+      return writer_->ack_frames()[0].sent_info.least_unacked;
+    }
+    if (writer_->stop_waiting_frames().empty()) {
+      return 0;
+    }
+    return writer_->stop_waiting_frames()[0].least_unacked;
   }
 
   void use_tagging_decrypter() {
@@ -603,7 +651,7 @@ class QuicConnectionTest : public ::testing::TestWithParam<bool> {
   }
 
   void ProcessPacket(QuicPacketSequenceNumber number) {
-    EXPECT_CALL(visitor_, OnStreamFrames(_)).WillOnce(Return(accept_packet_));
+    EXPECT_CALL(visitor_, OnStreamFrames(_)).Times(1);
     ProcessDataPacket(number, 0, !kEntropyFlag);
   }
 
@@ -649,12 +697,12 @@ class QuicConnectionTest : public ::testing::TestWithParam<bool> {
   }
 
   size_t ProcessFecProtectedPacket(QuicPacketSequenceNumber number,
-                                 bool expect_revival, bool entropy_flag) {
+                                   bool expect_revival, bool entropy_flag) {
     if (expect_revival) {
-      EXPECT_CALL(visitor_, OnStreamFrames(_)).WillOnce(Return(accept_packet_));
+      EXPECT_CALL(visitor_, OnStreamFrames(_)).Times(1);
     }
-    EXPECT_CALL(visitor_, OnStreamFrames(_)).WillOnce(Return(accept_packet_))
-        .RetiresOnSaturation();
+    EXPECT_CALL(visitor_, OnStreamFrames(_)).Times(1).
+          RetiresOnSaturation();
     return ProcessDataPacket(number, 1, entropy_flag);
   }
 
@@ -666,7 +714,7 @@ class QuicConnectionTest : public ::testing::TestWithParam<bool> {
                           bool entropy_flag,
                           QuicPacket* packet) {
     if (expect_revival) {
-      EXPECT_CALL(visitor_, OnStreamFrames(_)).WillOnce(Return(accept_packet_));
+      EXPECT_CALL(visitor_, OnStreamFrames(_)).Times(1);
     }
 
     // Construct the decrypted data packet so we can compute the correct
@@ -679,12 +727,14 @@ class QuicConnectionTest : public ::testing::TestWithParam<bool> {
       data_packet.reset(ConstructDataPacket(number, 1, !kEntropyFlag));
     }
 
-    header_.public_header.guid = guid_;
+    header_.public_header.connection_id = connection_id_;
     header_.public_header.reset_flag = false;
     header_.public_header.version_flag = false;
+    header_.public_header.sequence_number_length = sequence_number_length_;
+    header_.public_header.connection_id_length = connection_id_length_;
+    header_.packet_sequence_number = number;
     header_.entropy_flag = entropy_flag;
     header_.fec_flag = true;
-    header_.packet_sequence_number = number;
     header_.is_in_fec_group = IN_FEC_GROUP;
     header_.fec_group = min_protected_packet;
     QuicFecData fec_data;
@@ -695,7 +745,7 @@ class QuicConnectionTest : public ::testing::TestWithParam<bool> {
     // with itself, depending on the number of packets.
     if (((number - min_protected_packet) % 2) == 0) {
       for (size_t i = GetStartOfFecProtectedData(
-               header_.public_header.guid_length,
+               header_.public_header.connection_id_length,
                header_.public_header.version_flag,
                header_.public_header.sequence_number_length);
            i < data_packet->length(); ++i) {
@@ -720,8 +770,8 @@ class QuicConnectionTest : public ::testing::TestWithParam<bool> {
                                      QuicPacketSequenceNumber* last_packet) {
     QuicByteCount packet_size;
     EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _))
-        .WillOnce(DoAll(SaveArg<2>(&packet_size), Return(true)));
-    connection_.SendStreamDataWithString(id, data, offset, fin);
+        .WillOnce(DoAll(SaveArg<3>(&packet_size), Return(true)));
+    connection_.SendStreamDataWithString(id, data, offset, fin, NULL);
     if (last_packet != NULL) {
       *last_packet =
           QuicConnectionPeer::GetPacketCreator(&connection_)->sequence_number();
@@ -742,6 +792,10 @@ class QuicConnectionTest : public ::testing::TestWithParam<bool> {
     return ProcessFramePacket(QuicFrame(frame));
   }
 
+  QuicPacketEntropyHash ProcessStopWaitingPacket(QuicStopWaitingFrame* frame) {
+    return ProcessFramePacket(QuicFrame(frame));
+  }
+
   QuicPacketEntropyHash ProcessGoAwayPacket(QuicGoAwayFrame* frame) {
     return ProcessFramePacket(QuicFrame(frame));
   }
@@ -753,9 +807,11 @@ class QuicConnectionTest : public ::testing::TestWithParam<bool> {
   QuicPacket* ConstructDataPacket(QuicPacketSequenceNumber number,
                                   QuicFecGroupNumber fec_group,
                                   bool entropy_flag) {
-    header_.public_header.guid = guid_;
+    header_.public_header.connection_id = connection_id_;
     header_.public_header.reset_flag = false;
     header_.public_header.version_flag = false;
+    header_.public_header.sequence_number_length = sequence_number_length_;
+    header_.public_header.connection_id_length = connection_id_length_;
     header_.entropy_flag = entropy_flag;
     header_.fec_flag = false;
     header_.packet_sequence_number = number;
@@ -773,7 +829,7 @@ class QuicConnectionTest : public ::testing::TestWithParam<bool> {
 
   QuicPacket* ConstructClosePacket(QuicPacketSequenceNumber number,
                                    QuicFecGroupNumber fec_group) {
-    header_.public_header.guid = guid_;
+    header_.public_header.connection_id = connection_id_;
     header_.packet_sequence_number = number;
     header_.public_header.reset_flag = false;
     header_.public_header.version_flag = false;
@@ -784,7 +840,6 @@ class QuicConnectionTest : public ::testing::TestWithParam<bool> {
 
     QuicConnectionCloseFrame qccf;
     qccf.error_code = QUIC_PEER_GOING_AWAY;
-    qccf.ack_frame = QuicAckFrame(0, QuicTime::Zero(), 1);
 
     QuicFrames frames;
     QuicFrame frame(&qccf);
@@ -808,12 +863,69 @@ class QuicConnectionTest : public ::testing::TestWithParam<bool> {
     return QuicTime::Delta::FromMilliseconds(kMinRetransmissionTimeMs/2);
   }
 
-  QuicGuid guid_;
+  // Initialize a frame acknowledging all packets up to largest_observed.
+  const QuicAckFrame InitAckFrame(QuicPacketSequenceNumber largest_observed,
+                                  QuicPacketSequenceNumber least_unacked) {
+    QuicAckFrame frame(MakeAckFrame(largest_observed, least_unacked));
+    if (largest_observed > 0) {
+      frame.received_info.entropy_hash =
+        QuicConnectionPeer::GetSentEntropyHash(&connection_, largest_observed);
+    }
+    return frame;
+  }
+
+  const QuicStopWaitingFrame InitStopWaitingFrame(
+      QuicPacketSequenceNumber least_unacked) {
+    QuicStopWaitingFrame frame;
+    frame.least_unacked = least_unacked;
+    return frame;
+  }
+  // Explicitly nack a packet.
+  void NackPacket(QuicPacketSequenceNumber missing, QuicAckFrame* frame) {
+    frame->received_info.missing_packets.insert(missing);
+    frame->received_info.entropy_hash ^=
+      QuicConnectionPeer::GetSentEntropyHash(&connection_, missing);
+    if (missing > 1) {
+      frame->received_info.entropy_hash ^=
+        QuicConnectionPeer::GetSentEntropyHash(&connection_, missing - 1);
+    }
+  }
+
+  // Undo nacking a packet within the frame.
+  void AckPacket(QuicPacketSequenceNumber arrived, QuicAckFrame* frame) {
+    EXPECT_THAT(frame->received_info.missing_packets, Contains(arrived));
+    frame->received_info.missing_packets.erase(arrived);
+    frame->received_info.entropy_hash ^=
+      QuicConnectionPeer::GetSentEntropyHash(&connection_, arrived);
+    if (arrived > 1) {
+      frame->received_info.entropy_hash ^=
+        QuicConnectionPeer::GetSentEntropyHash(&connection_, arrived - 1);
+    }
+  }
+
+  void TriggerConnectionClose() {
+    // Send an erroneous packet to close the connection.
+    EXPECT_CALL(visitor_,
+                OnConnectionClosed(QUIC_INVALID_PACKET_HEADER, false));
+    // Call ProcessDataPacket rather than ProcessPacket, as we should not get a
+    // packet call to the visitor.
+    ProcessDataPacket(6000, 0, !kEntropyFlag);
+    EXPECT_FALSE(
+        QuicConnectionPeer::GetConnectionClosePacket(&connection_) == NULL);
+  }
+
+  void BlockOnNextWrite() {
+    writer_->BlockOnNextWrite();
+    EXPECT_CALL(visitor_, OnWriteBlocked()).Times(AtLeast(1));
+  }
+
+  QuicConnectionId connection_id_;
   QuicFramer framer_;
   QuicPacketCreator creator_;
   MockEntropyCalculator entropy_calculator_;
 
   MockSendAlgorithm* send_algorithm_;
+  MockLossAlgorithm* loss_algorithm_;
   TestReceiveAlgorithm* receive_algorithm_;
   MockClock clock_;
   MockRandom random_generator_;
@@ -826,15 +938,17 @@ class QuicConnectionTest : public ::testing::TestWithParam<bool> {
   QuicStreamFrame frame1_;
   QuicStreamFrame frame2_;
   scoped_ptr<QuicAckFrame> outgoing_ack_;
-  bool accept_packet_;
+  QuicSequenceNumberLength sequence_number_length_;
+  QuicConnectionIdLength connection_id_length_;
 
  private:
   DISALLOW_COPY_AND_ASSIGN(QuicConnectionTest);
 };
 
-INSTANTIATE_TEST_CASE_P(BundleAckWithPacket,
+// Run all end to end tests with all supported versions.
+INSTANTIATE_TEST_CASE_P(SupportedVersion,
                         QuicConnectionTest,
-                        ::testing::Values(false, true));
+                        ::testing::ValuesIn(QuicSupportedVersions()));
 
 TEST_P(QuicConnectionTest, PacketsInOrder) {
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
@@ -852,20 +966,6 @@ TEST_P(QuicConnectionTest, PacketsInOrder) {
   EXPECT_EQ(0u, outgoing_ack()->received_info.missing_packets.size());
 }
 
-TEST_P(QuicConnectionTest, PacketsRejected) {
-  EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
-
-  ProcessPacket(1);
-  EXPECT_EQ(1u, outgoing_ack()->received_info.largest_observed);
-  EXPECT_EQ(0u, outgoing_ack()->received_info.missing_packets.size());
-
-  accept_packet_ = false;
-  ProcessPacket(2);
-  // We should not have an ack for two.
-  EXPECT_EQ(1u, outgoing_ack()->received_info.largest_observed);
-  EXPECT_EQ(0u, outgoing_ack()->received_info.missing_packets.size());
-}
-
 TEST_P(QuicConnectionTest, PacketsOutOfOrder) {
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
 
@@ -923,7 +1023,8 @@ TEST_P(QuicConnectionTest, PacketsOutOfOrderWithAdditionsAndLeastAwaiting) {
   // awaiting' is 4.  The connection should then realize 1 will not be
   // retransmitted, and will remove it from the missing list.
   creator_.set_sequence_number(5);
-  QuicAckFrame frame(0, QuicTime::Zero(), 4);
+  QuicAckFrame frame = InitAckFrame(1, 4);
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(_, _, _, _));
   ProcessAckPacket(&frame);
 
   // Force an ack to be sent.
@@ -932,74 +1033,70 @@ TEST_P(QuicConnectionTest, PacketsOutOfOrderWithAdditionsAndLeastAwaiting) {
 }
 
 TEST_P(QuicConnectionTest, RejectPacketTooFarOut) {
+  EXPECT_CALL(visitor_,
+              OnConnectionClosed(QUIC_INVALID_PACKET_HEADER, false));
   // Call ProcessDataPacket rather than ProcessPacket, as we should not get a
   // packet call to the visitor.
-  EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_INVALID_PACKET_HEADER, false));
   ProcessDataPacket(6000, 0, !kEntropyFlag);
+  EXPECT_FALSE(
+      QuicConnectionPeer::GetConnectionClosePacket(&connection_) == NULL);
 }
 
-// TODO(rtenneti): Delete this when QUIC_VERSION_11 is deprecated.
-TEST_P(QuicConnectionTest, TruncatedAck11) {
-  if (QuicVersionMax() > QUIC_VERSION_11) {
-    return;
-  }
+TEST_P(QuicConnectionTest, RejectUnencryptedStreamData) {
+  // Process an unencrypted packet from the non-crypto stream.
+  frame1_.stream_id = 3;
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
-  EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(2);
-  EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(1);
-  for (int i = 0; i < 200; ++i) {
-    SendStreamDataToPeer(1, "foo", i * 3, !kFin, NULL);
-  }
-
-  QuicAckFrame frame(0, QuicTime::Zero(), 1);
-  frame.received_info.largest_observed = 193;
-  InsertMissingPacketsBetween(&frame.received_info, 1, 193);
-  frame.received_info.entropy_hash =
-      QuicConnectionPeer::GetSentEntropyHash(&connection_, 193) ^
-      QuicConnectionPeer::GetSentEntropyHash(&connection_, 192);
-
-  ProcessAckPacket(&frame);
-
-  EXPECT_TRUE(QuicConnectionPeer::GetReceivedTruncatedAck(&connection_));
-
-  frame.received_info.missing_packets.erase(192);
-  frame.received_info.entropy_hash =
-      QuicConnectionPeer::GetSentEntropyHash(&connection_, 193) ^
-      QuicConnectionPeer::GetSentEntropyHash(&connection_, 191);
-
-  ProcessAckPacket(&frame);
-  EXPECT_FALSE(QuicConnectionPeer::GetReceivedTruncatedAck(&connection_));
+  EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_UNENCRYPTED_STREAM_DATA,
+                                           false));
+  ProcessDataPacket(1, 0, !kEntropyFlag);
+  EXPECT_FALSE(
+      QuicConnectionPeer::GetConnectionClosePacket(&connection_) == NULL);
+  const vector<QuicConnectionCloseFrame>& connection_close_frames =
+      writer_->connection_close_frames();
+  EXPECT_EQ(1u, connection_close_frames.size());
+  EXPECT_EQ(QUIC_UNENCRYPTED_STREAM_DATA,
+            connection_close_frames[0].error_code);
 }
 
 TEST_P(QuicConnectionTest, TruncatedAck) {
-  // TODO(rtenneti): Delete this when QUIC_VERSION_11 is deprecated.
-  if (QuicVersionMax() <= QUIC_VERSION_11) {
-    return;
-  }
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
-  EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(256);
-  EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(2);
-  int num_packets = 256 * 2 + 1;
-  for (int i = 0; i < num_packets; ++i) {
-    SendStreamDataToPeer(1, "foo", i * 3, !kFin, NULL);
+  QuicPacketSequenceNumber num_packets = 256 * 2 + 1;
+  for (QuicPacketSequenceNumber i = 0; i < num_packets; ++i) {
+    SendStreamDataToPeer(3, "foo", i * 3, !kFin, NULL);
   }
 
-  QuicAckFrame frame(num_packets, QuicTime::Zero(), 1);
+  QuicAckFrame frame = InitAckFrame(num_packets, 1);
+  SequenceNumberSet lost_packets;
   // Create an ack with 256 nacks, none adjacent to one another.
   for (QuicPacketSequenceNumber i = 1; i <= 256; ++i) {
-    frame.received_info.missing_packets.insert(i * 2);
+    NackPacket(i * 2, &frame);
+    if (i < 256) {  // Last packet is nacked, but not lost.
+      lost_packets.insert(i * 2);
+    }
   }
+  EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
+      .WillOnce(Return(lost_packets));
   EXPECT_CALL(entropy_calculator_,
               EntropyHash(511)).WillOnce(testing::Return(0));
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
   ProcessAckPacket(&frame);
 
-  EXPECT_TRUE(QuicConnectionPeer::GetReceivedTruncatedAck(&connection_));
+  QuicReceivedPacketManager* received_packet_manager =
+      QuicConnectionPeer::GetReceivedPacketManager(&connection_);
+  // A truncated ack will not have the true largest observed.
+  EXPECT_GT(num_packets,
+            received_packet_manager->peer_largest_observed_packet());
 
-  frame.received_info.missing_packets.erase(192);
+  AckPacket(192, &frame);
 
-  // Removing one missing packet allows us to ack 192 and one more range.
-  EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(2);
+  // Removing one missing packet allows us to ack 192 and one more range, but
+  // 192 has already been declared lost, so it doesn't register as an ack.
+  EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
+      .WillOnce(Return(SequenceNumberSet()));
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
   ProcessAckPacket(&frame);
-  EXPECT_FALSE(QuicConnectionPeer::GetReceivedTruncatedAck(&connection_));
+  EXPECT_EQ(num_packets,
+            received_packet_manager->peer_largest_observed_packet());
 }
 
 TEST_P(QuicConnectionTest, AckReceiptCausesAckSendBadEntropy) {
@@ -1008,18 +1105,18 @@ TEST_P(QuicConnectionTest, AckReceiptCausesAckSendBadEntropy) {
   ProcessPacket(1);
   // Delay sending, then queue up an ack.
   EXPECT_CALL(*send_algorithm_,
-              TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce(
+              TimeUntilSend(_, _, _)).WillOnce(
                   testing::Return(QuicTime::Delta::FromMicroseconds(1)));
   QuicConnectionPeer::SendAck(&connection_);
 
   // Process an ack with a least unacked of the received ack.
   // This causes an ack to be sent when TimeUntilSend returns 0.
   EXPECT_CALL(*send_algorithm_,
-              TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillRepeatedly(
+              TimeUntilSend(_, _, _)).WillRepeatedly(
                   testing::Return(QuicTime::Delta::Zero()));
   // Skip a packet and then record an ack.
   creator_.set_sequence_number(2);
-  QuicAckFrame frame(0, QuicTime::Zero(), 3);
+  QuicAckFrame frame = InitAckFrame(0, 3);
   ProcessAckPacket(&frame);
 }
 
@@ -1045,45 +1142,63 @@ TEST_P(QuicConnectionTest, OutOfOrderReceiptCausesAckSend) {
 
 TEST_P(QuicConnectionTest, AckReceiptCausesAckSend) {
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
-  EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(1);
+
   QuicPacketSequenceNumber original;
   QuicByteCount packet_size;
-  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, NOT_RETRANSMISSION, _))
-      .WillOnce(DoAll(SaveArg<1>(&original), SaveArg<2>(&packet_size),
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _))
+      .WillOnce(DoAll(SaveArg<2>(&original), SaveArg<3>(&packet_size),
                       Return(true)));
-  EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(1, _)).Times(1);
-  connection_.SendStreamDataWithString(1, "foo", 0, !kFin);
-  QuicAckFrame frame(original, QuicTime::Zero(), 1);
-  frame.received_info.missing_packets.insert(original);
-  frame.received_info.entropy_hash = QuicConnectionPeer::GetSentEntropyHash(
-      &connection_, original - 1);
-  ProcessAckPacket(&frame);
-  ProcessAckPacket(&frame);
-  // Third nack should retransmit the largest observed packet.
+  connection_.SendStreamDataWithString(3, "foo", 0, !kFin, NULL);
+  QuicAckFrame frame = InitAckFrame(original, 1);
+  NackPacket(original, &frame);
+  // First nack triggers early retransmit.
+  SequenceNumberSet lost_packets;
+  lost_packets.insert(1);
+  EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
+      .WillOnce(Return(lost_packets));
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
   QuicPacketSequenceNumber retransmission;
   EXPECT_CALL(*send_algorithm_,
-              OnPacketSent(_, _, packet_size - kQuicVersionSize,
-                           NACK_RETRANSMISSION, _))
-      .WillOnce(DoAll(SaveArg<1>(&retransmission), Return(true)));
+              OnPacketSent(_, _, _, packet_size - kQuicVersionSize, _))
+      .WillOnce(DoAll(SaveArg<2>(&retransmission), Return(true)));
 
   ProcessAckPacket(&frame);
 
-  QuicAckFrame frame2(retransmission, QuicTime::Zero(), 1);
-  frame2.received_info.missing_packets.insert(original);
-  frame2.received_info.entropy_hash =
-      QuicConnectionPeer::GetSentEntropyHash(&connection_, retransmission) ^
-      QuicConnectionPeer::GetSentEntropyHash(&connection_, original);
-  EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _));
-
+  QuicAckFrame frame2 = InitAckFrame(retransmission, 1);
+  NackPacket(original, &frame2);
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
+  EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
+      .WillOnce(Return(SequenceNumberSet()));
   ProcessAckPacket(&frame2);
+
   // Now if the peer sends an ack which still reports the retransmitted packet
-  // as missing, then that will count as a packet which instigates an ack.
-  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, NOT_RETRANSMISSION, _));
-  ProcessAckPacket(&frame2);
+  // as missing, that will bundle an ack with data after two acks in a row
+  // indicate the high water mark needs to be raised.
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _,
+                                             HAS_RETRANSMITTABLE_DATA));
+  connection_.SendStreamDataWithString(3, "foo", 3, !kFin, NULL);
+  // No ack sent.
+  EXPECT_EQ(1u, writer_->frame_count());
+  EXPECT_EQ(1u, writer_->stream_frames().size());
+
+  // No more packet loss for the rest of the test.
+  EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
+      .WillRepeatedly(Return(SequenceNumberSet()));
   ProcessAckPacket(&frame2);
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _,
+                                             HAS_RETRANSMITTABLE_DATA));
+  connection_.SendStreamDataWithString(3, "foo", 3, !kFin, NULL);
+  // Ack bundled.
+  if (version() > QUIC_VERSION_15) {
+    EXPECT_EQ(3u, writer_->frame_count());
+  } else {
+    EXPECT_EQ(2u, writer_->frame_count());
+  }
+  EXPECT_EQ(1u, writer_->stream_frames().size());
+  EXPECT_FALSE(writer_->ack_frames().empty());
 
   // But an ack with no missing packets will not send an ack.
-  frame2.received_info.missing_packets.clear();
+  AckPacket(original, &frame2);
   ProcessAckPacket(&frame2);
   ProcessAckPacket(&frame2);
 }
@@ -1097,23 +1212,42 @@ TEST_P(QuicConnectionTest, LeastUnackedLower) {
 
   // Start out saying the least unacked is 2.
   creator_.set_sequence_number(5);
-  QuicAckFrame frame(0, QuicTime::Zero(), 2);
-  ProcessAckPacket(&frame);
+  if (version() > QUIC_VERSION_15) {
+    QuicStopWaitingFrame frame = InitStopWaitingFrame(2);
+    ProcessStopWaitingPacket(&frame);
+  } else {
+    QuicAckFrame frame = InitAckFrame(0, 2);
+    ProcessAckPacket(&frame);
+  }
 
   // Change it to 1, but lower the sequence number to fake out-of-order packets.
   // This should be fine.
   creator_.set_sequence_number(1);
-  QuicAckFrame frame2(0, QuicTime::Zero(), 1);
-  // The scheduler will not process out of order acks.
-  EXPECT_CALL(visitor_, OnCanWrite()).Times(0);
-  ProcessAckPacket(&frame2);
+  // The scheduler will not process out of order acks, but all packet processing
+  // causes the connection to try to write.
+  EXPECT_CALL(visitor_, OnCanWrite());
+  if (version() > QUIC_VERSION_15) {
+    QuicStopWaitingFrame frame2 = InitStopWaitingFrame(1);
+    ProcessStopWaitingPacket(&frame2);
+  } else {
+    QuicAckFrame frame2 = InitAckFrame(0, 1);
+    ProcessAckPacket(&frame2);
+  }
 
   // Now claim it's one, but set the ordering so it was sent "after" the first
   // one.  This should cause a connection error.
-  EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_INVALID_ACK_DATA, false));
   EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _));
   creator_.set_sequence_number(7);
-  ProcessAckPacket(&frame2);
+  if (version() > QUIC_VERSION_15) {
+    EXPECT_CALL(visitor_,
+                OnConnectionClosed(QUIC_INVALID_STOP_WAITING_DATA, false));
+    QuicStopWaitingFrame frame2 = InitStopWaitingFrame(1);
+    ProcessStopWaitingPacket(&frame2);
+  } else {
+    EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_INVALID_ACK_DATA, false));
+    QuicAckFrame frame2 = InitAckFrame(0, 1);
+    ProcessAckPacket(&frame2);
+  }
 }
 
 TEST_P(QuicConnectionTest, LargestObservedLower) {
@@ -1122,19 +1256,17 @@ TEST_P(QuicConnectionTest, LargestObservedLower) {
   SendStreamDataToPeer(1, "foo", 0, !kFin, NULL);
   SendStreamDataToPeer(1, "bar", 3, !kFin, NULL);
   SendStreamDataToPeer(1, "eep", 6, !kFin, NULL);
-  EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(2);
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
 
   // Start out saying the largest observed is 2.
-  QuicAckFrame frame(2, QuicTime::Zero(), 0);
-  frame.received_info.entropy_hash = QuicConnectionPeer::GetSentEntropyHash(
-      &connection_, 2);
-  ProcessAckPacket(&frame);
+  QuicAckFrame frame1 = InitAckFrame(1, 0);
+  QuicAckFrame frame2 = InitAckFrame(2, 0);
+  ProcessAckPacket(&frame2);
 
   // Now change it to 1, and it should cause a connection error.
-  QuicAckFrame frame2(1, QuicTime::Zero(), 0);
   EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_INVALID_ACK_DATA, false));
   EXPECT_CALL(visitor_, OnCanWrite()).Times(0);
-  ProcessAckPacket(&frame2);
+  ProcessAckPacket(&frame1);
 }
 
 TEST_P(QuicConnectionTest, AckUnsentData) {
@@ -1142,7 +1274,7 @@ TEST_P(QuicConnectionTest, AckUnsentData) {
   EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_INVALID_ACK_DATA, false));
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
   EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _));
-  QuicAckFrame frame(1, QuicTime::Zero(), 0);
+  QuicAckFrame frame(MakeAckFrame(1, 0));
   EXPECT_CALL(visitor_, OnCanWrite()).Times(0);
   ProcessAckPacket(&frame);
 }
@@ -1152,63 +1284,60 @@ TEST_P(QuicConnectionTest, AckAll) {
   ProcessPacket(1);
 
   creator_.set_sequence_number(1);
-  QuicAckFrame frame1(0, QuicTime::Zero(), 1);
+  QuicAckFrame frame1 = InitAckFrame(0, 1);
   ProcessAckPacket(&frame1);
 }
 
 TEST_P(QuicConnectionTest, SendingDifferentSequenceNumberLengthsBandwidth) {
-  EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillOnce(Return(
-       QuicBandwidth::FromKBitsPerSecond(1000)));
-
   QuicPacketSequenceNumber last_packet;
   SendStreamDataToPeer(1, "foo", 0, !kFin, &last_packet);
   EXPECT_EQ(1u, last_packet);
   EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
             connection_.options()->send_sequence_number_length);
   EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
-            last_header()->public_header.sequence_number_length);
+            writer_->header().public_header.sequence_number_length);
 
-  EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillOnce(Return(
-       QuicBandwidth::FromKBitsPerSecond(1000 * 256)));
+  EXPECT_CALL(*send_algorithm_, GetCongestionWindow()).WillRepeatedly(
+      Return(kMaxPacketSize * 256));
 
-  SendStreamDataToPeer(1u, "bar", 3, !kFin, &last_packet);
+  SendStreamDataToPeer(1, "bar", 3, !kFin, &last_packet);
   EXPECT_EQ(2u, last_packet);
   EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER,
             connection_.options()->send_sequence_number_length);
   // The 1 packet lag is due to the sequence number length being recalculated in
   // QuicConnection after a packet is sent.
   EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
-            last_header()->public_header.sequence_number_length);
+            writer_->header().public_header.sequence_number_length);
 
-  EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillOnce(Return(
-       QuicBandwidth::FromKBitsPerSecond(1000 * 256 * 256)));
+  EXPECT_CALL(*send_algorithm_, GetCongestionWindow()).WillRepeatedly(
+      Return(kMaxPacketSize * 256 * 256));
 
   SendStreamDataToPeer(1, "foo", 6, !kFin, &last_packet);
   EXPECT_EQ(3u, last_packet);
   EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
             connection_.options()->send_sequence_number_length);
   EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER,
-            last_header()->public_header.sequence_number_length);
+            writer_->header().public_header.sequence_number_length);
 
-  EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillOnce(Return(
-       QuicBandwidth::FromKBitsPerSecond(1000ll * 256 * 256 * 256)));
+  EXPECT_CALL(*send_algorithm_, GetCongestionWindow()).WillRepeatedly(
+      Return(kMaxPacketSize * 256 * 256 * 256));
 
-  SendStreamDataToPeer(1u, "bar", 9, !kFin, &last_packet);
+  SendStreamDataToPeer(1, "bar", 9, !kFin, &last_packet);
   EXPECT_EQ(4u, last_packet);
   EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
             connection_.options()->send_sequence_number_length);
   EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
-            last_header()->public_header.sequence_number_length);
+            writer_->header().public_header.sequence_number_length);
 
-  EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillOnce(Return(
-      QuicBandwidth::FromKBitsPerSecond(1000ll * 256 * 256 * 256 * 256)));
+  EXPECT_CALL(*send_algorithm_, GetCongestionWindow()).WillRepeatedly(
+      Return(kMaxPacketSize * 256 * 256 * 256 * 256));
 
-  SendStreamDataToPeer(1u, "foo", 12, !kFin, &last_packet);
+  SendStreamDataToPeer(1, "foo", 12, !kFin, &last_packet);
   EXPECT_EQ(5u, last_packet);
   EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER,
             connection_.options()->send_sequence_number_length);
   EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
-            last_header()->public_header.sequence_number_length);
+            writer_->header().public_header.sequence_number_length);
 }
 
 TEST_P(QuicConnectionTest, SendingDifferentSequenceNumberLengthsUnackedDelta) {
@@ -1218,15 +1347,15 @@ TEST_P(QuicConnectionTest, SendingDifferentSequenceNumberLengthsUnackedDelta) {
   EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
             connection_.options()->send_sequence_number_length);
   EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
-            last_header()->public_header.sequence_number_length);
+            writer_->header().public_header.sequence_number_length);
 
   QuicConnectionPeer::GetPacketCreator(&connection_)->set_sequence_number(100);
 
-  SendStreamDataToPeer(1u, "bar", 3, !kFin, &last_packet);
+  SendStreamDataToPeer(1, "bar", 3, !kFin, &last_packet);
   EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER,
             connection_.options()->send_sequence_number_length);
   EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER,
-            last_header()->public_header.sequence_number_length);
+            writer_->header().public_header.sequence_number_length);
 
   QuicConnectionPeer::GetPacketCreator(&connection_)->set_sequence_number(
       100 * 256);
@@ -1235,77 +1364,76 @@ TEST_P(QuicConnectionTest, SendingDifferentSequenceNumberLengthsUnackedDelta) {
   EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
             connection_.options()->send_sequence_number_length);
   EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER,
-            last_header()->public_header.sequence_number_length);
+            writer_->header().public_header.sequence_number_length);
 
   QuicConnectionPeer::GetPacketCreator(&connection_)->set_sequence_number(
       100 * 256 * 256);
 
-  SendStreamDataToPeer(1u, "bar", 9, !kFin, &last_packet);
+  SendStreamDataToPeer(1, "bar", 9, !kFin, &last_packet);
   EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
             connection_.options()->send_sequence_number_length);
   EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
-            last_header()->public_header.sequence_number_length);
+            writer_->header().public_header.sequence_number_length);
 
   QuicConnectionPeer::GetPacketCreator(&connection_)->set_sequence_number(
       100 * 256 * 256 * 256);
 
-  SendStreamDataToPeer(1u, "foo", 12, !kFin, &last_packet);
+  SendStreamDataToPeer(1, "foo", 12, !kFin, &last_packet);
   EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER,
             connection_.options()->send_sequence_number_length);
   EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER,
-            last_header()->public_header.sequence_number_length);
+            writer_->header().public_header.sequence_number_length);
 }
 
 TEST_P(QuicConnectionTest, BasicSending) {
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
-  EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(6);
   QuicPacketSequenceNumber last_packet;
   SendStreamDataToPeer(1, "foo", 0, !kFin, &last_packet);  // Packet 1
   EXPECT_EQ(1u, last_packet);
   SendAckPacketToPeer();  // Packet 2
 
-  EXPECT_EQ(1u, last_ack()->sent_info.least_unacked);
+  EXPECT_EQ(1u, least_unacked());
 
   SendAckPacketToPeer();  // Packet 3
-  EXPECT_EQ(1u, last_ack()->sent_info.least_unacked);
+  EXPECT_EQ(1u, least_unacked());
 
-  SendStreamDataToPeer(1u, "bar", 3, !kFin, &last_packet);  // Packet 4
+  SendStreamDataToPeer(1, "bar", 3, !kFin, &last_packet);  // Packet 4
   EXPECT_EQ(4u, last_packet);
   SendAckPacketToPeer();  // Packet 5
-  EXPECT_EQ(1u, last_ack()->sent_info.least_unacked);
+  EXPECT_EQ(1u, least_unacked());
+
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
 
   // Peer acks up to packet 3.
-  QuicAckFrame frame(3, QuicTime::Zero(), 0);
-  frame.received_info.entropy_hash =
-      QuicConnectionPeer::GetSentEntropyHash(&connection_, 3);
+  QuicAckFrame frame = InitAckFrame(3, 0);
   ProcessAckPacket(&frame);
   SendAckPacketToPeer();  // Packet 6
 
   // As soon as we've acked one, we skip ack packets 2 and 3 and note lack of
   // ack for 4.
-  EXPECT_EQ(4u, last_ack()->sent_info.least_unacked);
+  EXPECT_EQ(4u, least_unacked());
+
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
 
   // Peer acks up to packet 4, the last packet.
-  QuicAckFrame frame2(6, QuicTime::Zero(), 0);
-  frame2.received_info.entropy_hash =
-      QuicConnectionPeer::GetSentEntropyHash(&connection_, 6);
+  QuicAckFrame frame2 = InitAckFrame(6, 0);
   ProcessAckPacket(&frame2);  // Acks don't instigate acks.
 
   // Verify that we did not send an ack.
-  EXPECT_EQ(6u, last_header()->packet_sequence_number);
+  EXPECT_EQ(6u, writer_->header().packet_sequence_number);
 
   // So the last ack has not changed.
-  EXPECT_EQ(4u, last_ack()->sent_info.least_unacked);
+  EXPECT_EQ(4u, least_unacked());
 
   // If we force an ack, we shouldn't change our retransmit state.
   SendAckPacketToPeer();  // Packet 7
-  EXPECT_EQ(7u, last_ack()->sent_info.least_unacked);
+  EXPECT_EQ(7u, least_unacked());
 
   // But if we send more data it should.
   SendStreamDataToPeer(1, "eep", 6, !kFin, &last_packet);  // Packet 8
   EXPECT_EQ(8u, last_packet);
   SendAckPacketToPeer();  // Packet 9
-  EXPECT_EQ(8u, last_ack()->sent_info.least_unacked);
+  EXPECT_EQ(7u, least_unacked());
 }
 
 TEST_P(QuicConnectionTest, FECSending) {
@@ -1322,7 +1450,7 @@ TEST_P(QuicConnectionTest, FECSending) {
   EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(6);
   // The first stream frame will consume 2 fewer bytes than the other three.
   const string payload(payload_length * 4 - 6, 'a');
-  connection_.SendStreamDataWithString(1, payload, 0, !kFin);
+  connection_.SendStreamDataWithString(1, payload, 0, !kFin, NULL);
   // Expect the FEC group to be closed after SendStreamDataWithString.
   EXPECT_FALSE(creator_.ShouldSendFec(true));
 }
@@ -1338,9 +1466,9 @@ TEST_P(QuicConnectionTest, FECQueueing) {
   connection_.options()->max_packets_per_fec_group = 2;
 
   EXPECT_EQ(0u, connection_.NumQueuedPackets());
-  writer_->set_blocked(true);
+  BlockOnNextWrite();
   const string payload(payload_length, 'a');
-  connection_.SendStreamDataWithString(1, payload, 0, !kFin);
+  connection_.SendStreamDataWithString(1, payload, 0, !kFin, NULL);
   EXPECT_FALSE(creator_.ShouldSendFec(true));
   // Expect the first data packet and the fec packet to be queued.
   EXPECT_EQ(2u, connection_.NumQueuedPackets());
@@ -1350,47 +1478,81 @@ TEST_P(QuicConnectionTest, AbandonFECFromCongestionWindow) {
   connection_.options()->max_packets_per_fec_group = 1;
   // 1 Data and 1 FEC packet.
   EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2);
-  connection_.SendStreamDataWithString(1, "foo", 0, !kFin);
+  connection_.SendStreamDataWithString(3, "foo", 0, !kFin, NULL);
 
   const QuicTime::Delta retransmission_time =
       QuicTime::Delta::FromMilliseconds(5000);
   clock_.AdvanceTime(retransmission_time);
 
-  // Abandon FEC packet.
-  EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(_, _)).Times(1);
+  // Abandon FEC packet and data packet.
+  EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1);
   EXPECT_CALL(visitor_, OnCanWrite());
-  connection_.OnAbandonFecTimeout();
+  connection_.OnRetransmissionTimeout();
 }
 
 TEST_P(QuicConnectionTest, DontAbandonAckedFEC) {
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
   connection_.options()->max_packets_per_fec_group = 1;
-  const QuicPacketSequenceNumber sequence_number =
-      QuicConnectionPeer::GetPacketCreator(&connection_)->sequence_number() + 1;
 
   // 1 Data and 1 FEC packet.
-  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2);
-  connection_.SendStreamDataWithString(1, "foo", 0, !kFin);
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(6);
+  connection_.SendStreamDataWithString(3, "foo", 0, !kFin, NULL);
+  // Send some more data afterwards to ensure early retransmit doesn't trigger.
+  connection_.SendStreamDataWithString(3, "foo", 3, !kFin, NULL);
+  connection_.SendStreamDataWithString(3, "foo", 6, !kFin, NULL);
 
-  QuicAckFrame ack_fec(2, QuicTime::Zero(), 1);
+  QuicAckFrame ack_fec = InitAckFrame(2, 1);
   // Data packet missing.
-  ack_fec.received_info.missing_packets.insert(1);
-  ack_fec.received_info.entropy_hash =
-      QuicConnectionPeer::GetSentEntropyHash(&connection_, 2) ^
-      QuicConnectionPeer::GetSentEntropyHash(&connection_, 1);
+  // TODO(ianswett): Note that this is not a sensible ack, since if the FEC was
+  // received, it would cause the covered packet to be acked as well.
+  NackPacket(1, &ack_fec);
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
+  ProcessAckPacket(&ack_fec);
+  clock_.AdvanceTime(DefaultRetransmissionTime());
+
+  // Don't abandon the acked FEC packet, but it will abandon 2 the subsequent
+  // FEC packets.
+  EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(3);
+  connection_.GetRetransmissionAlarm()->Fire();
+}
 
-  EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(1);
-  EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(1);
+TEST_P(QuicConnectionTest, AbandonAllFEC) {
+  EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
+  connection_.options()->max_packets_per_fec_group = 1;
 
+  // 1 Data and 1 FEC packet.
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(6);
+  connection_.SendStreamDataWithString(3, "foo", 0, !kFin, NULL);
+  // Send some more data afterwards to ensure early retransmit doesn't trigger.
+  connection_.SendStreamDataWithString(3, "foo", 3, !kFin, NULL);
+  // Advance the time so not all the FEC packets are abandoned.
+  clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1));
+  connection_.SendStreamDataWithString(3, "foo", 6, !kFin, NULL);
+
+  QuicAckFrame ack_fec = InitAckFrame(5, 1);
+  // Ack all data packets, but no fec packets.
+  NackPacket(2, &ack_fec);
+  NackPacket(4, &ack_fec);
+
+  // Lose the first FEC packet and ack the three data packets.
+  SequenceNumberSet lost_packets;
+  lost_packets.insert(2);
+  EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
+      .WillOnce(Return(lost_packets));
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
   ProcessAckPacket(&ack_fec);
 
-  clock_.AdvanceTime(DefaultRetransmissionTime());
+  clock_.AdvanceTime(DefaultRetransmissionTime().Subtract(
+      QuicTime::Delta::FromMilliseconds(1)));
 
-  // Abandon only data packet, FEC has been acked.
-  EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(sequence_number, _)).Times(1);
-  // Send only data packet.
-  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1);
+  // Abandon all packets
+  EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(false));
   connection_.GetRetransmissionAlarm()->Fire();
+
+  // Ensure the alarm is not set since all packets have been abandoned.
+  EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet());
 }
 
 TEST_P(QuicConnectionTest, FramePacking) {
@@ -1404,12 +1566,9 @@ TEST_P(QuicConnectionTest, FramePacking) {
       IgnoreResult(InvokeWithoutArgs(&connection_,
                                      &TestConnection::SendStreamData3)),
       IgnoreResult(InvokeWithoutArgs(&connection_,
-                                     &TestConnection::SendStreamData5)),
-      Return(true)));
+                                     &TestConnection::SendStreamData5))));
 
-  EXPECT_CALL(*send_algorithm_,
-              OnPacketSent(_, _, _, NOT_RETRANSMISSION, _))
-      .Times(1);
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1);
   // Unblock the connection.
   connection_.GetSendAlarm()->Fire();
   EXPECT_EQ(0u, connection_.NumQueuedPackets());
@@ -1417,11 +1576,16 @@ TEST_P(QuicConnectionTest, FramePacking) {
 
   // Parse the last packet and ensure it's an ack and two stream frames from
   // two different streams.
-  EXPECT_EQ(3u, writer_->frame_count());
-  EXPECT_TRUE(writer_->ack());
-  EXPECT_EQ(2u, writer_->stream_frames()->size());
-  EXPECT_EQ(kStreamId3, (*writer_->stream_frames())[0].stream_id);
-  EXPECT_EQ(kStreamId5, (*writer_->stream_frames())[1].stream_id);
+  if (version() > QUIC_VERSION_15) {
+    EXPECT_EQ(4u, writer_->frame_count());
+    EXPECT_FALSE(writer_->stop_waiting_frames().empty());
+  } else {
+    EXPECT_EQ(3u, writer_->frame_count());
+  }
+  EXPECT_FALSE(writer_->ack_frames().empty());
+  EXPECT_EQ(2u, writer_->stream_frames().size());
+  EXPECT_EQ(kStreamId3, writer_->stream_frames()[0].stream_id);
+  EXPECT_EQ(kStreamId5, writer_->stream_frames()[1].stream_id);
 }
 
 TEST_P(QuicConnectionTest, FramePackingNonCryptoThenCrypto) {
@@ -1436,12 +1600,9 @@ TEST_P(QuicConnectionTest, FramePackingNonCryptoThenCrypto) {
       IgnoreResult(InvokeWithoutArgs(&connection_,
                                      &TestConnection::SendStreamData3)),
       IgnoreResult(InvokeWithoutArgs(&connection_,
-                                     &TestConnection::SendCryptoStreamData)),
-      Return(true)));
+                                     &TestConnection::SendCryptoStreamData))));
 
-  EXPECT_CALL(*send_algorithm_,
-              OnPacketSent(_, _, _, NOT_RETRANSMISSION, _))
-      .Times(2);
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2);
   // Unblock the connection.
   connection_.GetSendAlarm()->Fire();
   EXPECT_EQ(0u, connection_.NumQueuedPackets());
@@ -1449,9 +1610,8 @@ TEST_P(QuicConnectionTest, FramePackingNonCryptoThenCrypto) {
 
   // Parse the last packet and ensure it's the crypto stream frame.
   EXPECT_EQ(1u, writer_->frame_count());
-  EXPECT_TRUE(writer_->ack());
-  EXPECT_EQ(1u, writer_->stream_frames()->size());
-  EXPECT_EQ(kCryptoStreamId, (*writer_->stream_frames())[0].stream_id);
+  EXPECT_EQ(1u, writer_->stream_frames().size());
+  EXPECT_EQ(kCryptoStreamId, writer_->stream_frames()[0].stream_id);
 }
 
 TEST_P(QuicConnectionTest, FramePackingCryptoThenNonCrypto) {
@@ -1466,12 +1626,9 @@ TEST_P(QuicConnectionTest, FramePackingCryptoThenNonCrypto) {
       IgnoreResult(InvokeWithoutArgs(&connection_,
                                      &TestConnection::SendCryptoStreamData)),
       IgnoreResult(InvokeWithoutArgs(&connection_,
-                                     &TestConnection::SendStreamData3)),
-      Return(true)));
+                                     &TestConnection::SendStreamData3))));
 
-  EXPECT_CALL(*send_algorithm_,
-              OnPacketSent(_, _, _, NOT_RETRANSMISSION, _))
-      .Times(3);
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(3);
   // Unblock the connection.
   connection_.GetSendAlarm()->Fire();
   EXPECT_EQ(0u, connection_.NumQueuedPackets());
@@ -1479,12 +1636,14 @@ TEST_P(QuicConnectionTest, FramePackingCryptoThenNonCrypto) {
 
   // Parse the last packet and ensure it's the stream frame from stream 3.
   EXPECT_EQ(1u, writer_->frame_count());
-  EXPECT_TRUE(writer_->ack());
-  EXPECT_EQ(1u, writer_->stream_frames()->size());
-  EXPECT_EQ(kStreamId3, (*writer_->stream_frames())[0].stream_id);
+  EXPECT_EQ(1u, writer_->stream_frames().size());
+  EXPECT_EQ(kStreamId3, writer_->stream_frames()[0].stream_id);
 }
 
 TEST_P(QuicConnectionTest, FramePackingFEC) {
+  if (version() < QUIC_VERSION_15) {
+    return;
+  }
   // Enable fec.
   connection_.options()->max_packets_per_fec_group = 6;
   // Block the connection.
@@ -1497,46 +1656,36 @@ TEST_P(QuicConnectionTest, FramePackingFEC) {
       IgnoreResult(InvokeWithoutArgs(&connection_,
                                      &TestConnection::SendStreamData3)),
       IgnoreResult(InvokeWithoutArgs(&connection_,
-                                     &TestConnection::SendStreamData5)),
-      Return(true)));
+                                     &TestConnection::SendStreamData5))));
 
-  EXPECT_CALL(*send_algorithm_,
-              OnPacketSent(_, _, _, NOT_RETRANSMISSION, _)).Times(2);
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(2);
   // Unblock the connection.
   connection_.GetSendAlarm()->Fire();
   EXPECT_EQ(0u, connection_.NumQueuedPackets());
   EXPECT_FALSE(connection_.HasQueuedData());
 
   // Parse the last packet and ensure it's in an fec group.
-  EXPECT_EQ(1u, writer_->header()->fec_group);
+  EXPECT_EQ(1u, writer_->header().fec_group);
   EXPECT_EQ(0u, writer_->frame_count());
 }
 
 TEST_P(QuicConnectionTest, FramePackingAckResponse) {
-  if (!GetParam()) {
-    // This test depends on BundleAckWithPacket being true.
-    return;
-  }
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
-
   // Process a data packet to queue up a pending ack.
-  EXPECT_CALL(visitor_, OnStreamFrames(_)).WillOnce(Return(true));
+  EXPECT_CALL(visitor_, OnStreamFrames(_)).Times(1);
   ProcessDataPacket(1, 1, kEntropyFlag);
 
   EXPECT_CALL(visitor_, OnCanWrite()).WillOnce(DoAll(
       IgnoreResult(InvokeWithoutArgs(&connection_,
                                      &TestConnection::SendStreamData3)),
       IgnoreResult(InvokeWithoutArgs(&connection_,
-                                     &TestConnection::SendStreamData5)),
-      Return(true)));
+                                     &TestConnection::SendStreamData5))));
 
-  EXPECT_CALL(*send_algorithm_,
-              OnPacketSent(_, _, _, NOT_RETRANSMISSION, _))
-      .Times(1);
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1);
 
   // Process an ack to cause the visitor's OnCanWrite to be invoked.
   creator_.set_sequence_number(2);
-  QuicAckFrame ack_one(0, QuicTime::Zero(), 0);
+  QuicAckFrame ack_one = InitAckFrame(0, 0);
   ProcessAckPacket(&ack_one);
 
   EXPECT_EQ(0u, connection_.NumQueuedPackets());
@@ -1544,152 +1693,136 @@ TEST_P(QuicConnectionTest, FramePackingAckResponse) {
 
   // Parse the last packet and ensure it's an ack and two stream frames from
   // two different streams.
-  EXPECT_EQ(3u, writer_->frame_count());
-  EXPECT_TRUE(writer_->ack());
-  ASSERT_EQ(2u, writer_->stream_frames()->size());
-  EXPECT_EQ(kStreamId3, (*writer_->stream_frames())[0].stream_id);
-  EXPECT_EQ(kStreamId5, (*writer_->stream_frames())[1].stream_id);
+  if (version() > QUIC_VERSION_15) {
+    EXPECT_EQ(4u, writer_->frame_count());
+    EXPECT_FALSE(writer_->stop_waiting_frames().empty());
+  } else {
+    EXPECT_EQ(3u, writer_->frame_count());
+  }
+  EXPECT_FALSE(writer_->ack_frames().empty());
+  ASSERT_EQ(2u, writer_->stream_frames().size());
+  EXPECT_EQ(kStreamId3, writer_->stream_frames()[0].stream_id);
+  EXPECT_EQ(kStreamId5, writer_->stream_frames()[1].stream_id);
 }
 
 TEST_P(QuicConnectionTest, FramePackingSendv) {
-  // Send two stream frames in 1 packet by using writev.
-  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, NOT_RETRANSMISSION, _));
+  // Send data in 1 packet by writing multiple blocks in a single iovector
+  // using writev.
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _));
 
   char data[] = "ABCD";
   IOVector data_iov;
   data_iov.AppendNoCoalesce(data, 2);
   data_iov.AppendNoCoalesce(data + 2, 2);
-  connection_.SendStreamData(1, data_iov, 0, !kFin);
+  connection_.SendStreamData(1, data_iov, 0, !kFin, NULL);
 
   EXPECT_EQ(0u, connection_.NumQueuedPackets());
   EXPECT_FALSE(connection_.HasQueuedData());
 
-  // Parse the last packet and ensure it's two stream frames from one stream.
-  // TODO(ianswett): Ideally this would arrive in one frame in the future.
-  EXPECT_EQ(2u, writer_->frame_count());
-  EXPECT_EQ(2u, writer_->stream_frames()->size());
-  EXPECT_EQ(1u, (*writer_->stream_frames())[0].stream_id);
-  EXPECT_EQ(1u, (*writer_->stream_frames())[1].stream_id);
-  EXPECT_EQ("AB", (*writer_->stream_frames())[0].data.as_string());
-  EXPECT_EQ("CD", (*writer_->stream_frames())[1].data.as_string());
+  // Parse the last packet and ensure multiple iovector blocks have
+  // been packed into a single stream frame from one stream.
+  EXPECT_EQ(1u, writer_->frame_count());
+  EXPECT_EQ(1u, writer_->stream_frames().size());
+  QuicStreamFrame frame = writer_->stream_frames()[0];
+  EXPECT_EQ(1u, frame.stream_id);
+  EXPECT_EQ("ABCD", string(static_cast<char*>
+                           (frame.data.iovec()[0].iov_base),
+                           (frame.data.iovec()[0].iov_len)));
 }
 
 TEST_P(QuicConnectionTest, FramePackingSendvQueued) {
   // Try to send two stream frames in 1 packet by using writev.
-  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, NOT_RETRANSMISSION, _));
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _));
 
-  writer_->set_blocked(true);
+  BlockOnNextWrite();
   char data[] = "ABCD";
   IOVector data_iov;
   data_iov.AppendNoCoalesce(data, 2);
   data_iov.AppendNoCoalesce(data + 2, 2);
-  connection_.SendStreamData(1, data_iov, 0, !kFin);
+  connection_.SendStreamData(1, data_iov, 0, !kFin, NULL);
 
   EXPECT_EQ(1u, connection_.NumQueuedPackets());
   EXPECT_TRUE(connection_.HasQueuedData());
 
-  // Attempt to send all packets, but since we're actually still
-  // blocked, they should all remain queued.
-  EXPECT_FALSE(connection_.OnCanWrite());
-  EXPECT_EQ(1u, connection_.NumQueuedPackets());
-
   // Unblock the writes and actually send.
-  writer_->set_blocked(false);
-  EXPECT_TRUE(connection_.OnCanWrite());
+  writer_->SetWritable();
+  connection_.OnCanWrite();
   EXPECT_EQ(0u, connection_.NumQueuedPackets());
 
-  // Parse the last packet and ensure it's two stream frames from one stream.
-  // TODO(ianswett): Ideally this would arrive in one frame in the future.
-  EXPECT_EQ(2u, writer_->frame_count());
-  EXPECT_EQ(2u, writer_->stream_frames()->size());
-  EXPECT_EQ(1u, (*writer_->stream_frames())[0].stream_id);
-  EXPECT_EQ(1u, (*writer_->stream_frames())[1].stream_id);
+  // Parse the last packet and ensure it's one stream frame from one stream.
+  EXPECT_EQ(1u, writer_->frame_count());
+  EXPECT_EQ(1u, writer_->stream_frames().size());
+  EXPECT_EQ(1u, writer_->stream_frames()[0].stream_id);
 }
 
 TEST_P(QuicConnectionTest, SendingZeroBytes) {
   // Send a zero byte write with a fin using writev.
-  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, NOT_RETRANSMISSION, _));
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _));
   IOVector empty_iov;
-  connection_.SendStreamData(1, empty_iov, 0, kFin);
+  connection_.SendStreamData(1, empty_iov, 0, kFin, NULL);
 
   EXPECT_EQ(0u, connection_.NumQueuedPackets());
   EXPECT_FALSE(connection_.HasQueuedData());
 
-  // Parse the last packet and ensure it's two stream frames from one stream.
-  // TODO(ianswett): Ideally this would arrive in one frame in the future.
+  // Parse the last packet and ensure it's one stream frame from one stream.
   EXPECT_EQ(1u, writer_->frame_count());
-  EXPECT_EQ(1u, writer_->stream_frames()->size());
-  EXPECT_EQ(1u, (*writer_->stream_frames())[0].stream_id);
-  EXPECT_TRUE((*writer_->stream_frames())[0].fin);
+  EXPECT_EQ(1u, writer_->stream_frames().size());
+  EXPECT_EQ(1u, writer_->stream_frames()[0].stream_id);
+  EXPECT_TRUE(writer_->stream_frames()[0].fin);
 }
 
 TEST_P(QuicConnectionTest, OnCanWrite) {
-  // Visitor's OnCanWill send data, but will return false.
+  // Visitor's OnCanWrite will send data, but will have more pending writes.
   EXPECT_CALL(visitor_, OnCanWrite()).WillOnce(DoAll(
       IgnoreResult(InvokeWithoutArgs(&connection_,
                                      &TestConnection::SendStreamData3)),
       IgnoreResult(InvokeWithoutArgs(&connection_,
-                                     &TestConnection::SendStreamData5)),
-      Return(false)));
-
+                                     &TestConnection::SendStreamData5))));
+  EXPECT_CALL(visitor_, HasPendingWrites()).WillOnce(Return(true));
   EXPECT_CALL(*send_algorithm_,
-              TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillRepeatedly(
+              TimeUntilSend(_, _, _)).WillRepeatedly(
                   testing::Return(QuicTime::Delta::Zero()));
 
-  // Unblock the connection.
   connection_.OnCanWrite();
+
   // Parse the last packet and ensure it's the two stream frames from
   // two different streams.
   EXPECT_EQ(2u, writer_->frame_count());
-  EXPECT_EQ(2u, writer_->stream_frames()->size());
-  EXPECT_EQ(kStreamId3, (*writer_->stream_frames())[0].stream_id);
-  EXPECT_EQ(kStreamId5, (*writer_->stream_frames())[1].stream_id);
+  EXPECT_EQ(2u, writer_->stream_frames().size());
+  EXPECT_EQ(kStreamId3, writer_->stream_frames()[0].stream_id);
+  EXPECT_EQ(kStreamId5, writer_->stream_frames()[1].stream_id);
 }
 
 TEST_P(QuicConnectionTest, RetransmitOnNack) {
-  EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(2);
-  EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(1);
-  EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(2, _)).Times(1);
   QuicPacketSequenceNumber last_packet;
   QuicByteCount second_packet_size;
-  SendStreamDataToPeer(1, "foo", 0, !kFin, &last_packet);  // Packet 1
+  SendStreamDataToPeer(3, "foo", 0, !kFin, &last_packet);  // Packet 1
   second_packet_size =
-      SendStreamDataToPeer(1, "foos", 3, !kFin, &last_packet);  // Packet 2
-  SendStreamDataToPeer(1, "fooos", 7, !kFin, &last_packet);  // Packet 3
+      SendStreamDataToPeer(3, "foos", 3, !kFin, &last_packet);  // Packet 2
+  SendStreamDataToPeer(3, "fooos", 7, !kFin, &last_packet);  // Packet 3
 
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
 
-  // Peer acks one but not two or three.  Right now we only retransmit on
-  // explicit nack, so it should not trigger a retransimission.
-  QuicAckFrame ack_one(1, QuicTime::Zero(), 0);
-  ack_one.received_info.entropy_hash =
-      QuicConnectionPeer::GetSentEntropyHash(&connection_, 1);
-  ProcessAckPacket(&ack_one);
-  ProcessAckPacket(&ack_one);
+  // Don't lose a packet on an ack, and nothing is retransmitted.
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
+  QuicAckFrame ack_one = InitAckFrame(1, 0);
   ProcessAckPacket(&ack_one);
 
-  // Peer acks up to 3 with two explicitly missing.  Two nacks should cause no
-  // change.
-  QuicAckFrame nack_two(3, QuicTime::Zero(), 0);
-  nack_two.received_info.missing_packets.insert(2);
-  nack_two.received_info.entropy_hash =
-      QuicConnectionPeer::GetSentEntropyHash(&connection_, 3) ^
-      QuicConnectionPeer::GetSentEntropyHash(&connection_, 2) ^
-      QuicConnectionPeer::GetSentEntropyHash(&connection_, 1);
-  ProcessAckPacket(&nack_two);
-  ProcessAckPacket(&nack_two);
-
-  // The third nack should trigger a retransimission.
+  // Lose a packet and ensure it triggers retransmission.
+  QuicAckFrame nack_two = InitAckFrame(3, 0);
+  NackPacket(2, &nack_two);
+  SequenceNumberSet lost_packets;
+  lost_packets.insert(2);
+  EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
+      .WillOnce(Return(lost_packets));
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
   EXPECT_CALL(*send_algorithm_,
-              OnPacketSent(_, _, second_packet_size - kQuicVersionSize,
-                         NACK_RETRANSMISSION, _)).Times(1);
+              OnPacketSent(_, _, _, second_packet_size - kQuicVersionSize, _)).
+                  Times(1);
   ProcessAckPacket(&nack_two);
 }
 
 TEST_P(QuicConnectionTest, DiscardRetransmit) {
-  EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(2);
-  EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(1);
-  EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(2, _)).Times(1);
   QuicPacketSequenceNumber last_packet;
   SendStreamDataToPeer(1, "foo", 0, !kFin, &last_packet);  // Packet 1
   SendStreamDataToPeer(1, "foos", 3, !kFin, &last_packet);  // Packet 2
@@ -1697,37 +1830,24 @@ TEST_P(QuicConnectionTest, DiscardRetransmit) {
 
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
 
-  // Peer acks one but not two or three.  Right now we only retransmit on
-  // explicit nack, so it should not trigger a retransimission.
-  QuicAckFrame ack_one(1, QuicTime::Zero(), 0);
-  ack_one.received_info.entropy_hash =
-      QuicConnectionPeer::GetSentEntropyHash(&connection_, 1);
-  ProcessAckPacket(&ack_one);
-  ProcessAckPacket(&ack_one);
-  ProcessAckPacket(&ack_one);
-
-  // Peer acks up to 3 with two explicitly missing.  Two nacks should cause no
-  // change.
-  QuicAckFrame nack_two(3, QuicTime::Zero(), 0);
-  nack_two.received_info.missing_packets.insert(2);
-  nack_two.received_info.entropy_hash =
-      QuicConnectionPeer::GetSentEntropyHash(&connection_, 3) ^
-      QuicConnectionPeer::GetSentEntropyHash(&connection_, 2) ^
-      QuicConnectionPeer::GetSentEntropyHash(&connection_, 1);
-  ProcessAckPacket(&nack_two);
-  ProcessAckPacket(&nack_two);
-
-  // The third nack should trigger a retransimission, but we'll be
+  // Instigate a loss with an ack.
+  QuicAckFrame nack_two = InitAckFrame(3, 0);
+  NackPacket(2, &nack_two);
+  // The first nack should trigger a fast retransmission, but we'll be
   // write blocked, so the packet will be queued.
-  writer_->set_blocked(true);
-
+  BlockOnNextWrite();
+  SequenceNumberSet lost_packets;
+  lost_packets.insert(2);
+  EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
+      .WillOnce(Return(lost_packets));
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
   ProcessAckPacket(&nack_two);
   EXPECT_EQ(1u, connection_.NumQueuedPackets());
 
   // Now, ack the previous transmission.
-  QuicAckFrame ack_all(3, QuicTime::Zero(), 0);
-  nack_two.received_info.entropy_hash =
-      QuicConnectionPeer::GetSentEntropyHash(&connection_, 3);
+  EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
+      .WillOnce(Return(SequenceNumberSet()));
+  QuicAckFrame ack_all = InitAckFrame(3, 0);
   ProcessAckPacket(&ack_all);
 
   // Unblock the socket and attempt to send the queued packets.  However,
@@ -1736,7 +1856,7 @@ TEST_P(QuicConnectionTest, DiscardRetransmit) {
   EXPECT_CALL(*send_algorithm_,
               OnPacketSent(_, _, _, _, _)).Times(0);
 
-  writer_->set_blocked(false);
+  writer_->SetWritable();
   connection_.OnCanWrite();
 
   EXPECT_EQ(0u, connection_.NumQueuedPackets());
@@ -1744,43 +1864,42 @@ TEST_P(QuicConnectionTest, DiscardRetransmit) {
 
 TEST_P(QuicConnectionTest, RetransmitNackedLargestObserved) {
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
-  EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(1);
   QuicPacketSequenceNumber largest_observed;
   QuicByteCount packet_size;
-  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, NOT_RETRANSMISSION, _))
-      .WillOnce(DoAll(SaveArg<1>(&largest_observed), SaveArg<2>(&packet_size),
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _))
+      .WillOnce(DoAll(SaveArg<2>(&largest_observed), SaveArg<3>(&packet_size),
                       Return(true)));
-  EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(1, _)).Times(1);
-  connection_.SendStreamDataWithString(1, "foo", 0, !kFin);
-  QuicAckFrame frame(1, QuicTime::Zero(), largest_observed);
-  frame.received_info.missing_packets.insert(largest_observed);
-  frame.received_info.entropy_hash = QuicConnectionPeer::GetSentEntropyHash(
-      &connection_, largest_observed - 1);
-  ProcessAckPacket(&frame);
-  ProcessAckPacket(&frame);
-  // Third nack should retransmit the largest observed packet.
+  connection_.SendStreamDataWithString(3, "foo", 0, !kFin, NULL);
+
+  QuicAckFrame frame = InitAckFrame(1, largest_observed);
+  NackPacket(largest_observed, &frame);
+  // The first nack should retransmit the largest observed packet.
+  SequenceNumberSet lost_packets;
+  lost_packets.insert(1);
+  EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
+      .WillOnce(Return(lost_packets));
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
   EXPECT_CALL(*send_algorithm_,
-              OnPacketSent(_, _, packet_size - kQuicVersionSize,
-                           NACK_RETRANSMISSION, _));
+              OnPacketSent(_, _, _, packet_size - kQuicVersionSize, _));
   ProcessAckPacket(&frame);
 }
 
 TEST_P(QuicConnectionTest, QueueAfterTwoRTOs) {
   for (int i = 0; i < 10; ++i) {
     EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1);
-    connection_.SendStreamDataWithString(1, "foo", i * 3, !kFin);
+    connection_.SendStreamDataWithString(3, "foo", i * 3, !kFin, NULL);
   }
 
   // Block the congestion window and ensure they're queued.
-  writer_->set_blocked(true);
+  BlockOnNextWrite();
   clock_.AdvanceTime(DefaultRetransmissionTime());
   // Only one packet should be retransmitted.
-  EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(_, _)).Times(10);
+  EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
   connection_.GetRetransmissionAlarm()->Fire();
   EXPECT_TRUE(connection_.HasQueuedData());
 
   // Unblock the congestion window.
-  writer_->set_blocked(false);
+  writer_->SetWritable();
   clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds(
       2 * DefaultRetransmissionTime().ToMicroseconds()));
   // Retransmit already retransmitted packets event though the sequence number
@@ -1791,10 +1910,9 @@ TEST_P(QuicConnectionTest, QueueAfterTwoRTOs) {
 }
 
 TEST_P(QuicConnectionTest, WriteBlockedThenSent) {
-  writer_->set_blocked(true);
-
+  BlockOnNextWrite();
   writer_->set_is_write_blocked_data_buffered(true);
-  connection_.SendStreamDataWithString(1, "foo", 0, !kFin);
+  connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL);
   EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet());
 
   EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1);
@@ -1802,119 +1920,182 @@ TEST_P(QuicConnectionTest, WriteBlockedThenSent) {
   EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet());
 }
 
-TEST_P(QuicConnectionTest, ResumptionAlarmThenWriteBlocked) {
-  // Set the send and resumption alarm, then block the connection.
+TEST_P(QuicConnectionTest, WriteBlockedAckedThenSent) {
+  EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
+  BlockOnNextWrite();
+  writer_->set_is_write_blocked_data_buffered(true);
+  connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL);
+  EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet());
+
+  // Ack the sent packet before the callback returns, which happens in
+  // rare circumstances with write blocked sockets.
+  QuicAckFrame ack = InitAckFrame(1, 0);
+  ProcessAckPacket(&ack);
+
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0);
+  connection_.OnPacketSent(WriteResult(WRITE_STATUS_OK, 0));
+  EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet());
+}
+
+TEST_P(QuicConnectionTest, RetransmitWriteBlockedAckedOriginalThenSent) {
+  EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
+  connection_.SendStreamDataWithString(3, "foo", 0, !kFin, NULL);
+  EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet());
+
+  BlockOnNextWrite();
+  writer_->set_is_write_blocked_data_buffered(true);
+  // Simulate the retransmission alarm firing.
+  EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(_));
+  clock_.AdvanceTime(DefaultRetransmissionTime());
+  connection_.GetRetransmissionAlarm()->Fire();
+
+  // Ack the sent packet before the callback returns, which happens in
+  // rare circumstances with write blocked sockets.
+  QuicAckFrame ack = InitAckFrame(1, 0);
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
+  ProcessAckPacket(&ack);
+
+  connection_.OnPacketSent(WriteResult(WRITE_STATUS_OK, 0));
+  // The retransmission alarm should not be set because there are
+  // no unacked packets.
+  EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet());
+}
+
+TEST_P(QuicConnectionTest, AlarmsWhenWriteBlocked) {
+  // Block the connection.
+  BlockOnNextWrite();
+  connection_.SendStreamDataWithString(3, "foo", 0, !kFin, NULL);
+  EXPECT_EQ(1u, writer_->packets_write_attempts());
+  EXPECT_TRUE(writer_->IsWriteBlocked());
+
+  // Set the send and resumption alarms. Fire the alarms and ensure they don't
+  // attempt to write.
   connection_.GetResumeWritesAlarm()->Set(clock_.ApproximateNow());
   connection_.GetSendAlarm()->Set(clock_.ApproximateNow());
-  QuicConnectionPeer::SetIsWriteBlocked(&connection_, true);
-
-  // Fire the alarms and ensure the connection is still write blocked.
   connection_.GetResumeWritesAlarm()->Fire();
   connection_.GetSendAlarm()->Fire();
-  EXPECT_TRUE(QuicConnectionPeer::IsWriteBlocked(&connection_));
+  EXPECT_TRUE(writer_->IsWriteBlocked());
+  EXPECT_EQ(1u, writer_->packets_write_attempts());
 }
 
-TEST_P(QuicConnectionTest, LimitPacketsPerNack) {
+TEST_P(QuicConnectionTest, NoLimitPacketsPerNack) {
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
-  EXPECT_CALL(*send_algorithm_, OnIncomingAck(12, _, _)).Times(1);
-  EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(1);
-  EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(_, _)).Times(11);
   int offset = 0;
-  // Send packets 1 to 12.
-  for (int i = 0; i < 12; ++i) {
+  // Send packets 1 to 15.
+  for (int i = 0; i < 15; ++i) {
     SendStreamDataToPeer(1, "foo", offset, !kFin, NULL);
     offset += 3;
   }
 
-  // Ack 12, nack 1-11.
-  QuicAckFrame nack(12, QuicTime::Zero(), 0);
-  for (int i = 1; i < 12; ++i) {
-    nack.received_info.missing_packets.insert(i);
+  // Ack 15, nack 1-14.
+  SequenceNumberSet lost_packets;
+  QuicAckFrame nack = InitAckFrame(15, 0);
+  for (int i = 1; i < 15; ++i) {
+    NackPacket(i, &nack);
+    lost_packets.insert(i);
   }
 
-  nack.received_info.entropy_hash =
-      QuicConnectionPeer::GetSentEntropyHash(&connection_, 12) ^
-      QuicConnectionPeer::GetSentEntropyHash(&connection_, 11);
-
-  // Nack three times.
-  ProcessAckPacket(&nack);
-  ProcessAckPacket(&nack);
-  // The third call should trigger retransmitting 10 packets.
-  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(10);
-  ProcessAckPacket(&nack);
-
-  // The fourth call should trigger retransmitting the 11th packet.
-  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1);
+  // 14 packets have been NACK'd and lost.  In TCP cubic, PRR limits
+  // the retransmission rate in the case of burst losses.
+  EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
+      .WillOnce(Return(lost_packets));
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(14);
   ProcessAckPacket(&nack);
 }
 
 // Test sending multiple acks from the connection to the session.
 TEST_P(QuicConnectionTest, MultipleAcks) {
-  EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(6);
-  EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(1);
   QuicPacketSequenceNumber last_packet;
-  SendStreamDataToPeer(1u, "foo", 0, !kFin, &last_packet);  // Packet 1
+  SendStreamDataToPeer(1, "foo", 0, !kFin, &last_packet);  // Packet 1
   EXPECT_EQ(1u, last_packet);
-  SendStreamDataToPeer(3u, "foo", 0, !kFin, &last_packet);  // Packet 2
+  SendStreamDataToPeer(3, "foo", 0, !kFin, &last_packet);  // Packet 2
   EXPECT_EQ(2u, last_packet);
   SendAckPacketToPeer();  // Packet 3
-  SendStreamDataToPeer(5u, "foo", 0, !kFin, &last_packet);  // Packet 4
+  SendStreamDataToPeer(5, "foo", 0, !kFin, &last_packet);  // Packet 4
   EXPECT_EQ(4u, last_packet);
-  SendStreamDataToPeer(1u, "foo", 3, !kFin, &last_packet);  // Packet 5
+  SendStreamDataToPeer(1, "foo", 3, !kFin, &last_packet);  // Packet 5
   EXPECT_EQ(5u, last_packet);
-  SendStreamDataToPeer(3u, "foo", 3, !kFin, &last_packet);  // Packet 6
+  SendStreamDataToPeer(3, "foo", 3, !kFin, &last_packet);  // Packet 6
   EXPECT_EQ(6u, last_packet);
 
-  // Client will ack packets 1, [!2], 3, 4, 5.
-  QuicAckFrame frame1(5, QuicTime::Zero(), 0);
-  frame1.received_info.missing_packets.insert(2);
-  frame1.received_info.entropy_hash =
-      QuicConnectionPeer::GetSentEntropyHash(&connection_, 5) ^
-      QuicConnectionPeer::GetSentEntropyHash(&connection_, 2) ^
-      QuicConnectionPeer::GetSentEntropyHash(&connection_, 1);
-
+  // Client will ack packets 1, 2, [!3], 4, 5.
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
+  QuicAckFrame frame1 = InitAckFrame(5, 0);
+  NackPacket(3, &frame1);
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
-
   ProcessAckPacket(&frame1);
 
-  // Now the client implicitly acks 2, and explicitly acks 6.
-  QuicAckFrame frame2(6, QuicTime::Zero(), 0);
-  frame2.received_info.entropy_hash =
-      QuicConnectionPeer::GetSentEntropyHash(&connection_, 6);
-
+  // Now the client implicitly acks 3, and explicitly acks 6.
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
+  QuicAckFrame frame2 = InitAckFrame(6, 0);
   ProcessAckPacket(&frame2);
 }
 
 TEST_P(QuicConnectionTest, DontLatchUnackedPacket) {
-  EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(1);
   SendStreamDataToPeer(1, "foo", 0, !kFin, NULL);  // Packet 1;
+  // From now on, we send acks, so the send algorithm won't mark them pending.
+  ON_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _))
+              .WillByDefault(Return(false));
   SendAckPacketToPeer();  // Packet 2
 
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
-  QuicAckFrame frame(1, QuicTime::Zero(), 0);
-  frame.received_info.entropy_hash = QuicConnectionPeer::GetSentEntropyHash(
-      &connection_, 1);
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
+  QuicAckFrame frame = InitAckFrame(1, 0);
   ProcessAckPacket(&frame);
 
-  // Verify that our internal state has least-unacked as 3.
+  // Verify that our internal state has least-unacked as 2, because we're still
+  // waiting for a potential ack for 2.
+  EXPECT_EQ(2u, outgoing_ack()->sent_info.least_unacked);
+
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
+  frame = InitAckFrame(2, 0);
+  ProcessAckPacket(&frame);
   EXPECT_EQ(3u, outgoing_ack()->sent_info.least_unacked);
 
   // When we send an ack, we make sure our least-unacked makes sense.  In this
   // case since we're not waiting on an ack for 2 and all packets are acked, we
   // set it to 3.
   SendAckPacketToPeer();  // Packet 3
-  // Since this was an ack packet, we set least_unacked to 4.
-  EXPECT_EQ(4u, outgoing_ack()->sent_info.least_unacked);
+  // Least_unacked remains at 3 until another ack is received.
+  EXPECT_EQ(3u, outgoing_ack()->sent_info.least_unacked);
   // Check that the outgoing ack had its sequence number as least_unacked.
-  EXPECT_EQ(3u, last_ack()->sent_info.least_unacked);
+  EXPECT_EQ(3u, least_unacked());
 
+  // Ack the ack, which updates the rtt and raises the least unacked.
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
+  frame = InitAckFrame(3, 0);
+  ProcessAckPacket(&frame);
+
+  ON_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _))
+              .WillByDefault(Return(true));
   SendStreamDataToPeer(1, "bar", 3, false, NULL);  // Packet 4
   EXPECT_EQ(4u, outgoing_ack()->sent_info.least_unacked);
+  ON_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _))
+              .WillByDefault(Return(false));
   SendAckPacketToPeer();  // Packet 5
-  EXPECT_EQ(4u, last_ack()->sent_info.least_unacked);
+  EXPECT_EQ(4u, least_unacked());
+
+  // Send two data packets at the end, and ensure if the last one is acked,
+  // the least unacked is raised above the ack packets.
+  ON_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _))
+              .WillByDefault(Return(true));
+  SendStreamDataToPeer(1, "bar", 6, false, NULL);  // Packet 6
+  SendStreamDataToPeer(1, "bar", 9, false, NULL);  // Packet 7
+
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
+  frame = InitAckFrame(7, 0);
+  NackPacket(5, &frame);
+  NackPacket(6, &frame);
+  ProcessAckPacket(&frame);
+
+  EXPECT_EQ(6u, outgoing_ack()->sent_info.least_unacked);
 }
 
 TEST_P(QuicConnectionTest, ReviveMissingPacketAfterFecPacket) {
+  if (version() < QUIC_VERSION_15) {
+    return;
+  }
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
 
   // Don't send missing packet 1.
@@ -1923,7 +2104,74 @@ TEST_P(QuicConnectionTest, ReviveMissingPacketAfterFecPacket) {
   EXPECT_EQ(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_, 2));
 }
 
+TEST_P(QuicConnectionTest, ReviveMissingPacketWithVaryingSeqNumLengths) {
+  if (version() < QUIC_VERSION_15) {
+    return;
+  }
+  EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
+
+  // Set up a debug visitor to the connection.
+  scoped_ptr<FecQuicConnectionDebugVisitor>
+      fec_visitor(new FecQuicConnectionDebugVisitor);
+  connection_.set_debug_visitor(fec_visitor.get());
+
+  QuicPacketSequenceNumber fec_packet = 0;
+  QuicSequenceNumberLength lengths[] = {PACKET_6BYTE_SEQUENCE_NUMBER,
+                                        PACKET_4BYTE_SEQUENCE_NUMBER,
+                                        PACKET_2BYTE_SEQUENCE_NUMBER,
+                                        PACKET_1BYTE_SEQUENCE_NUMBER};
+  // For each sequence number length size, revive a packet and check sequence
+  // number length in the revived packet.
+  for (size_t i = 0; i < arraysize(lengths); ++i) {
+    // Set sequence_number_length_ (for data and FEC packets).
+    sequence_number_length_ = lengths[i];
+    fec_packet += 2;
+    // Don't send missing packet, but send fec packet right after it.
+    ProcessFecPacket(/*seq_num=*/fec_packet, /*fec_group=*/fec_packet - 1,
+                     true, !kEntropyFlag, NULL);
+    // Sequence number length in the revived header should be the same as
+    // in the original data/fec packet headers.
+    EXPECT_EQ(sequence_number_length_, fec_visitor->revived_header().
+                                       public_header.sequence_number_length);
+  }
+}
+
+TEST_P(QuicConnectionTest, ReviveMissingPacketWithVaryingConnectionIdLengths) {
+  if (version() < QUIC_VERSION_15) {
+    return;
+  }
+  EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
+
+  // Set up a debug visitor to the connection.
+  scoped_ptr<FecQuicConnectionDebugVisitor>
+      fec_visitor(new FecQuicConnectionDebugVisitor);
+  connection_.set_debug_visitor(fec_visitor.get());
+
+  QuicPacketSequenceNumber fec_packet = 0;
+  QuicConnectionIdLength lengths[] = {PACKET_8BYTE_CONNECTION_ID,
+                                      PACKET_4BYTE_CONNECTION_ID,
+                                      PACKET_1BYTE_CONNECTION_ID,
+                                      PACKET_0BYTE_CONNECTION_ID};
+  // For each connection id length size, revive a packet and check connection
+  // id length in the revived packet.
+  for (size_t i = 0; i < arraysize(lengths); ++i) {
+    // Set connection id length (for data and FEC packets).
+    connection_id_length_ = lengths[i];
+    fec_packet += 2;
+    // Don't send missing packet, but send fec packet right after it.
+    ProcessFecPacket(/*seq_num=*/fec_packet, /*fec_group=*/fec_packet - 1,
+                     true, !kEntropyFlag, NULL);
+    // Connection id length in the revived header should be the same as
+    // in the original data/fec packet headers.
+    EXPECT_EQ(connection_id_length_,
+              fec_visitor->revived_header().public_header.connection_id_length);
+  }
+}
+
 TEST_P(QuicConnectionTest, ReviveMissingPacketAfterDataPacketThenFecPacket) {
+  if (version() < QUIC_VERSION_15) {
+    return;
+  }
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
 
   ProcessFecProtectedPacket(1, false, kEntropyFlag);
@@ -1934,17 +2182,24 @@ TEST_P(QuicConnectionTest, ReviveMissingPacketAfterDataPacketThenFecPacket) {
 }
 
 TEST_P(QuicConnectionTest, ReviveMissingPacketAfterDataPacketsThenFecPacket) {
+  if (version() < QUIC_VERSION_15) {
+    return;
+  }
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
 
   ProcessFecProtectedPacket(1, false, !kEntropyFlag);
   // Don't send missing packet 2.
   ProcessFecProtectedPacket(3, false, !kEntropyFlag);
   ProcessFecPacket(4, 1, true, kEntropyFlag, NULL);
-  // Entropy flag should be true, so entropy should not be 0.
-  EXPECT_NE(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_, 2));
+  // Ensure QUIC no longer revives entropy for lost packets.
+  EXPECT_EQ(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_, 2));
+  EXPECT_NE(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_, 4));
 }
 
 TEST_P(QuicConnectionTest, ReviveMissingPacketAfterDataPacket) {
+  if (version() < QUIC_VERSION_15) {
+    return;
+  }
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
 
   // Don't send missing packet 1.
@@ -1956,6 +2211,9 @@ TEST_P(QuicConnectionTest, ReviveMissingPacketAfterDataPacket) {
 }
 
 TEST_P(QuicConnectionTest, ReviveMissingPacketAfterDataPackets) {
+  if (version() < QUIC_VERSION_15) {
+    return;
+  }
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
 
   ProcessFecProtectedPacket(1, false, !kEntropyFlag);
@@ -1964,30 +2222,31 @@ TEST_P(QuicConnectionTest, ReviveMissingPacketAfterDataPackets) {
   ProcessFecProtectedPacket(3, false, kEntropyFlag);
   ProcessFecProtectedPacket(4, false, kEntropyFlag);
   ProcessFecProtectedPacket(5, true, !kEntropyFlag);
-  // Entropy flag should be true, so entropy should be 0.
-  EXPECT_NE(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_, 2));
+  // Ensure entropy is not revived for the missing packet.
+  EXPECT_EQ(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_, 2));
+  EXPECT_NE(0u, QuicConnectionPeer::ReceivedEntropyHash(&connection_, 3));
 }
 
-TEST_P(QuicConnectionTest, TestRetransmit) {
+TEST_P(QuicConnectionTest, RTO) {
   QuicTime default_retransmission_time = clock_.ApproximateNow().Add(
       DefaultRetransmissionTime());
-  SendStreamDataToPeer(1, "foo", 0, !kFin, NULL);
+  SendStreamDataToPeer(3, "foo", 0, !kFin, NULL);
   EXPECT_EQ(1u, outgoing_ack()->sent_info.least_unacked);
 
-  EXPECT_EQ(1u, last_header()->packet_sequence_number);
+  EXPECT_EQ(1u, writer_->header().packet_sequence_number);
   EXPECT_EQ(default_retransmission_time,
             connection_.GetRetransmissionAlarm()->deadline());
-  // Simulate the retransimission alarm firing.
+  // Simulate the retransmission alarm firing.
   clock_.AdvanceTime(DefaultRetransmissionTime());
-  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _));
-  EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(1, _)).Times(1);
-  connection_.RetransmitPacket(1, RTO_RETRANSMISSION);
-  EXPECT_EQ(2u, last_header()->packet_sequence_number);
+  EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, 2u, _, _));
+  connection_.GetRetransmissionAlarm()->Fire();
+  EXPECT_EQ(2u, writer_->header().packet_sequence_number);
   // We do not raise the high water mark yet.
   EXPECT_EQ(1u, outgoing_ack()->sent_info.least_unacked);
 }
 
-TEST_P(QuicConnectionTest, RetransmitWithSameEncryptionLevel) {
+TEST_P(QuicConnectionTest, RTOWithSameEncryptionLevel) {
   QuicTime default_retransmission_time = clock_.ApproximateNow().Add(
       DefaultRetransmissionTime());
   use_tagging_decrypter();
@@ -1995,29 +2254,32 @@ TEST_P(QuicConnectionTest, RetransmitWithSameEncryptionLevel) {
   // A TaggingEncrypter puts kTagSize copies of the given byte (0x01 here) at
   // the end of the packet. We can test this to check which encrypter was used.
   connection_.SetEncrypter(ENCRYPTION_NONE, new TaggingEncrypter(0x01));
-  SendStreamDataToPeer(1, "foo", 0, !kFin, NULL);
-  EXPECT_EQ(0x01010101u, final_bytes_of_last_packet());
+  SendStreamDataToPeer(3, "foo", 0, !kFin, NULL);
+  EXPECT_EQ(0x01010101u, writer_->final_bytes_of_last_packet());
 
   connection_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(0x02));
   connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL);
-  SendStreamDataToPeer(1, "foo", 0, !kFin, NULL);
-  EXPECT_EQ(0x02020202u, final_bytes_of_last_packet());
+  SendStreamDataToPeer(3, "foo", 0, !kFin, NULL);
+  EXPECT_EQ(0x02020202u, writer_->final_bytes_of_last_packet());
 
   EXPECT_EQ(default_retransmission_time,
             connection_.GetRetransmissionAlarm()->deadline());
-  // Simulate the retransimission alarm firing.
+  {
+    InSequence s;
+    EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
+    EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, 3, _, _));
+    EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, 4, _, _));
+  }
+
+  // Simulate the retransmission alarm firing.
   clock_.AdvanceTime(DefaultRetransmissionTime());
-  EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(_, _)).Times(2);
+  connection_.GetRetransmissionAlarm()->Fire();
 
-  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _));
-  connection_.RetransmitPacket(1, RTO_RETRANSMISSION);
   // Packet should have been sent with ENCRYPTION_NONE.
-  EXPECT_EQ(0x01010101u, final_bytes_of_last_packet());
+  EXPECT_EQ(0x01010101u, writer_->final_bytes_of_previous_packet());
 
-  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _));
-  connection_.RetransmitPacket(2, RTO_RETRANSMISSION);
   // Packet should have been sent with ENCRYPTION_INITIAL.
-  EXPECT_EQ(0x02020202u, final_bytes_of_last_packet());
+  EXPECT_EQ(0x02020202u, writer_->final_bytes_of_last_packet());
 }
 
 TEST_P(QuicConnectionTest, SendHandshakeMessages) {
@@ -2026,12 +2288,12 @@ TEST_P(QuicConnectionTest, SendHandshakeMessages) {
   // the end of the packet. We can test this to check which encrypter was used.
   connection_.SetEncrypter(ENCRYPTION_NONE, new TaggingEncrypter(0x01));
 
-  // Attempt to send a handshake message while the congestion manager
-  // does not permit sending.
+  // Attempt to send a handshake message and have the socket block.
   EXPECT_CALL(*send_algorithm_,
-              TimeUntilSend(_, _, _, IS_HANDSHAKE)).WillRepeatedly(
-                  testing::Return(QuicTime::Delta::Infinite()));
-  connection_.SendStreamDataWithString(1, "foo", 0, !kFin);
+              TimeUntilSend(_, _, _)).WillRepeatedly(
+                  testing::Return(QuicTime::Delta::Zero()));
+  BlockOnNextWrite();
+  connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL);
   // The packet should be serialized, but not queued.
   EXPECT_EQ(1u, connection_.NumQueuedPackets());
 
@@ -2040,15 +2302,13 @@ TEST_P(QuicConnectionTest, SendHandshakeMessages) {
   connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL);
 
   // Now become writeable and flush the packets.
-  EXPECT_CALL(*send_algorithm_,
-              TimeUntilSend(_, _, _, IS_HANDSHAKE)).WillRepeatedly(
-                  testing::Return(QuicTime::Delta::Zero()));
+  writer_->SetWritable();
   EXPECT_CALL(visitor_, OnCanWrite());
   connection_.OnCanWrite();
   EXPECT_EQ(0u, connection_.NumQueuedPackets());
 
   // Verify that the handshake packet went out at the null encryption.
-  EXPECT_EQ(0x01010101u, final_bytes_of_last_packet());
+  EXPECT_EQ(0x01010101u, writer_->final_bytes_of_last_packet());
 }
 
 TEST_P(QuicConnectionTest,
@@ -2056,21 +2316,21 @@ TEST_P(QuicConnectionTest,
   use_tagging_decrypter();
   connection_.SetEncrypter(ENCRYPTION_NONE, new TaggingEncrypter(0x01));
   QuicPacketSequenceNumber sequence_number;
-  SendStreamDataToPeer(1, "foo", 0, !kFin, &sequence_number);
+  SendStreamDataToPeer(3, "foo", 0, !kFin, &sequence_number);
 
   connection_.SetEncrypter(ENCRYPTION_FORWARD_SECURE,
                            new TaggingEncrypter(0x02));
   connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE);
 
+  EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
   EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0);
-  EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(sequence_number, _)).Times(1);
 
   QuicTime default_retransmission_time = clock_.ApproximateNow().Add(
       DefaultRetransmissionTime());
 
   EXPECT_EQ(default_retransmission_time,
             connection_.GetRetransmissionAlarm()->deadline());
-  // Simulate the retransimission alarm firing.
+  // Simulate the retransmission alarm firing.
   clock_.AdvanceTime(DefaultRetransmissionTime());
   connection_.GetRetransmissionAlarm()->Fire();
 }
@@ -2086,11 +2346,9 @@ TEST_P(QuicConnectionTest, RetransmitPacketsWithInitialEncryption) {
   connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL);
 
   SendStreamDataToPeer(2, "bar", 0, !kFin, NULL);
-
   EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1);
-  EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(_, _)).Times(1);
 
-  connection_.RetransmitUnackedPackets(QuicConnection::INITIAL_ENCRYPTION_ONLY);
+  connection_.RetransmitUnackedPackets(INITIAL_ENCRYPTION_ONLY);
 }
 
 TEST_P(QuicConnectionTest, BufferNonDecryptablePackets) {
@@ -2102,153 +2360,152 @@ TEST_P(QuicConnectionTest, BufferNonDecryptablePackets) {
 
   // Process an encrypted packet which can not yet be decrypted
   // which should result in the packet being buffered.
-  ProcessDataPacketAtLevel(1, false, kEntropyFlag, ENCRYPTION_INITIAL);
+  ProcessDataPacketAtLevel(1, 0, kEntropyFlag, ENCRYPTION_INITIAL);
 
   // Transition to the new encryption state and process another
   // encrypted packet which should result in the original packet being
   // processed.
-  connection_.SetDecrypter(new StrictTaggingDecrypter(tag));
+  connection_.SetDecrypter(new StrictTaggingDecrypter(tag),
+                           ENCRYPTION_INITIAL);
   connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL);
   connection_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(tag));
-  EXPECT_CALL(visitor_, OnStreamFrames(_)).Times(2).WillRepeatedly(
-      Return(true));
-  ProcessDataPacketAtLevel(2, false, kEntropyFlag, ENCRYPTION_INITIAL);
+  EXPECT_CALL(visitor_, OnStreamFrames(_)).Times(2);
+  ProcessDataPacketAtLevel(2, 0, kEntropyFlag, ENCRYPTION_INITIAL);
 
   // Finally, process a third packet and note that we do not
   // reprocess the buffered packet.
-  EXPECT_CALL(visitor_, OnStreamFrames(_)).WillOnce(Return(true));
-  ProcessDataPacketAtLevel(3, false, kEntropyFlag, ENCRYPTION_INITIAL);
+  EXPECT_CALL(visitor_, OnStreamFrames(_)).Times(1);
+  ProcessDataPacketAtLevel(3, 0, kEntropyFlag, ENCRYPTION_INITIAL);
 }
 
 TEST_P(QuicConnectionTest, TestRetransmitOrder) {
   QuicByteCount first_packet_size;
   EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).WillOnce(
-      DoAll(SaveArg<2>(&first_packet_size), Return(true)));
+      DoAll(SaveArg<3>(&first_packet_size), Return(true)));
 
-  connection_.SendStreamDataWithString(1, "first_packet", 0, !kFin);
+  connection_.SendStreamDataWithString(3, "first_packet", 0, !kFin, NULL);
   QuicByteCount second_packet_size;
   EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).WillOnce(
-      DoAll(SaveArg<2>(&second_packet_size), Return(true)));
-  connection_.SendStreamDataWithString(1, "second_packet", 12, !kFin);
+      DoAll(SaveArg<3>(&second_packet_size), Return(true)));
+  connection_.SendStreamDataWithString(3, "second_packet", 12, !kFin, NULL);
   EXPECT_NE(first_packet_size, second_packet_size);
   // Advance the clock by huge time to make sure packets will be retransmitted.
   clock_.AdvanceTime(QuicTime::Delta::FromSeconds(10));
-  EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(_, _)).Times(2);
+  EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
   {
     InSequence s;
     EXPECT_CALL(*send_algorithm_,
-                OnPacketSent(_, _, first_packet_size, _, _));
+                OnPacketSent(_, _, _, first_packet_size, _));
     EXPECT_CALL(*send_algorithm_,
-                OnPacketSent(_, _, second_packet_size, _, _));
+                OnPacketSent(_, _, _, second_packet_size, _));
   }
   connection_.GetRetransmissionAlarm()->Fire();
 
   // Advance again and expect the packets to be sent again in the same order.
   clock_.AdvanceTime(QuicTime::Delta::FromSeconds(20));
-  EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(_, _)).Times(2);
+  EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
   {
     InSequence s;
     EXPECT_CALL(*send_algorithm_,
-                OnPacketSent(_, _, first_packet_size, _, _));
+                OnPacketSent(_, _, _, first_packet_size, _));
     EXPECT_CALL(*send_algorithm_,
-                OnPacketSent(_, _, second_packet_size, _, _));
+                OnPacketSent(_, _, _, second_packet_size, _));
   }
   connection_.GetRetransmissionAlarm()->Fire();
 }
 
-TEST_P(QuicConnectionTest, TestRetransmissionCountCalculation) {
+TEST_P(QuicConnectionTest, RetransmissionCountCalculation) {
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
   QuicPacketSequenceNumber original_sequence_number;
-  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, NOT_RETRANSMISSION, _))
-      .WillOnce(DoAll(SaveArg<1>(&original_sequence_number), Return(true)));
-  connection_.SendStreamDataWithString(1, "foo", 0, !kFin);
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _))
+      .WillOnce(DoAll(SaveArg<2>(&original_sequence_number), Return(true)));
+  connection_.SendStreamDataWithString(3, "foo", 0, !kFin, NULL);
 
   EXPECT_TRUE(QuicConnectionPeer::IsSavedForRetransmission(
       &connection_, original_sequence_number));
-  EXPECT_EQ(0u, QuicConnectionPeer::GetRetransmissionCount(
+  EXPECT_FALSE(QuicConnectionPeer::IsRetransmission(
       &connection_, original_sequence_number));
   // Force retransmission due to RTO.
   clock_.AdvanceTime(QuicTime::Delta::FromSeconds(10));
-  EXPECT_CALL(*send_algorithm_,
-              OnPacketAbandoned(original_sequence_number, _)).Times(1);
+  EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
   QuicPacketSequenceNumber rto_sequence_number;
-  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, RTO_RETRANSMISSION, _))
-      .WillOnce(DoAll(SaveArg<1>(&rto_sequence_number), Return(true)));
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _))
+      .WillOnce(DoAll(SaveArg<2>(&rto_sequence_number), Return(true)));
   connection_.GetRetransmissionAlarm()->Fire();
   EXPECT_FALSE(QuicConnectionPeer::IsSavedForRetransmission(
       &connection_, original_sequence_number));
   ASSERT_TRUE(QuicConnectionPeer::IsSavedForRetransmission(
       &connection_, rto_sequence_number));
-  EXPECT_EQ(1u, QuicConnectionPeer::GetRetransmissionCount(
+  EXPECT_TRUE(QuicConnectionPeer::IsRetransmission(
       &connection_, rto_sequence_number));
   // Once by explicit nack.
-  EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(1);
-  EXPECT_CALL(*send_algorithm_,
-              OnPacketAbandoned(rto_sequence_number, _)).Times(1);
+  SequenceNumberSet lost_packets;
+  lost_packets.insert(rto_sequence_number);
+  EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
+      .WillOnce(Return(lost_packets));
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
   QuicPacketSequenceNumber nack_sequence_number = 0;
   // Ack packets might generate some other packets, which are not
   // retransmissions. (More ack packets).
-  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, NOT_RETRANSMISSION, _))
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _))
       .Times(AnyNumber());
-  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, NACK_RETRANSMISSION, _))
-      .WillOnce(DoAll(SaveArg<1>(&nack_sequence_number), Return(true)));
-  QuicAckFrame ack(rto_sequence_number, QuicTime::Zero(), 0);
-  // Ack the retransmitted packet.
-  ack.received_info.missing_packets.insert(original_sequence_number);
-  ack.received_info.missing_packets.insert(rto_sequence_number);
-  ack.received_info.entropy_hash = QuicConnectionPeer::GetSentEntropyHash(
-      &connection_, rto_sequence_number - 1);
-  for (int i = 0; i < 3; i++) {
-    ProcessAckPacket(&ack);
-  }
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _))
+      .WillOnce(DoAll(SaveArg<2>(&nack_sequence_number), Return(true)));
+  QuicAckFrame ack = InitAckFrame(rto_sequence_number, 0);
+  // Nack the retransmitted packet.
+  NackPacket(original_sequence_number, &ack);
+  NackPacket(rto_sequence_number, &ack);
+  ProcessAckPacket(&ack);
+
   ASSERT_NE(0u, nack_sequence_number);
   EXPECT_FALSE(QuicConnectionPeer::IsSavedForRetransmission(
       &connection_, rto_sequence_number));
   ASSERT_TRUE(QuicConnectionPeer::IsSavedForRetransmission(
       &connection_, nack_sequence_number));
-  EXPECT_EQ(2u, QuicConnectionPeer::GetRetransmissionCount(
+  EXPECT_TRUE(QuicConnectionPeer::IsRetransmission(
       &connection_, nack_sequence_number));
 }
 
 TEST_P(QuicConnectionTest, SetRTOAfterWritingToSocket) {
-  writer_->set_blocked(true);
-  connection_.SendStreamDataWithString(1, "foo", 0, !kFin);
+  BlockOnNextWrite();
+  connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL);
   // Make sure that RTO is not started when the packet is queued.
   EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet());
 
   // Test that RTO is started once we write to the socket.
-  writer_->set_blocked(false);
+  writer_->SetWritable();
   connection_.OnCanWrite();
   EXPECT_TRUE(connection_.GetRetransmissionAlarm()->IsSet());
 }
 
 TEST_P(QuicConnectionTest, DelayRTOWithAckReceipt) {
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
-  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, NOT_RETRANSMISSION, _))
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _))
       .Times(2);
-  connection_.SendStreamDataWithString(1, "foo", 0, !kFin);
-  connection_.SendStreamDataWithString(2, "bar", 0, !kFin);
+  connection_.SendStreamDataWithString(2, "foo", 0, !kFin, NULL);
+  connection_.SendStreamDataWithString(3, "bar", 0, !kFin, NULL);
   QuicAlarm* retransmission_alarm = connection_.GetRetransmissionAlarm();
   EXPECT_TRUE(retransmission_alarm->IsSet());
+  EXPECT_EQ(clock_.Now().Add(DefaultRetransmissionTime()),
+            retransmission_alarm->deadline());
 
   // Advance the time right before the RTO, then receive an ack for the first
   // packet to delay the RTO.
   clock_.AdvanceTime(DefaultRetransmissionTime());
-  EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(1);
-  QuicAckFrame ack(1, QuicTime::Zero(), 0);
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
+  QuicAckFrame ack = InitAckFrame(1, 0);
   ProcessAckPacket(&ack);
   EXPECT_TRUE(retransmission_alarm->IsSet());
+  EXPECT_GT(retransmission_alarm->deadline(), clock_.Now());
 
   // Move forward past the original RTO and ensure the RTO is still pending.
-  clock_.AdvanceTime(DefaultRetransmissionTime());
+  clock_.AdvanceTime(DefaultRetransmissionTime().Multiply(2));
 
   // Ensure the second packet gets retransmitted when it finally fires.
   EXPECT_TRUE(retransmission_alarm->IsSet());
-  EXPECT_GE(retransmission_alarm->deadline(), clock_.ApproximateNow());
-  clock_.AdvanceTime(DefaultRetransmissionTime());
   EXPECT_LT(retransmission_alarm->deadline(), clock_.ApproximateNow());
-  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, RTO_RETRANSMISSION, _));
-  EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(_, _));
+  EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _));
   // Manually cancel the alarm to simulate a real test.
   connection_.GetRetransmissionAlarm()->Fire();
 
@@ -2256,25 +2513,20 @@ TEST_P(QuicConnectionTest, DelayRTOWithAckReceipt) {
   // than previously.
   EXPECT_TRUE(retransmission_alarm->IsSet());
   QuicTime next_rto_time = retransmission_alarm->deadline();
-  QuicTime::Delta expected_rto = QuicConnectionPeer::GetCongestionManager(
-      &connection_)->GetRetransmissionDelay(1, 1);
-  EXPECT_EQ(next_rto_time, clock_.ApproximateNow().Add(expected_rto));
+  QuicTime expected_rto_time =
+      connection_.sent_packet_manager().GetRetransmissionTime();
+  EXPECT_EQ(next_rto_time, expected_rto_time);
 }
 
 TEST_P(QuicConnectionTest, TestQueued) {
   EXPECT_EQ(0u, connection_.NumQueuedPackets());
-  writer_->set_blocked(true);
-  connection_.SendStreamDataWithString(1, "foo", 0, !kFin);
-  EXPECT_EQ(1u, connection_.NumQueuedPackets());
-
-  // Attempt to send all packets, but since we're actually still
-  // blocked, they should all remain queued.
-  EXPECT_FALSE(connection_.OnCanWrite());
+  BlockOnNextWrite();
+  connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL);
   EXPECT_EQ(1u, connection_.NumQueuedPackets());
 
   // Unblock the writes and actually send.
-  writer_->set_blocked(false);
-  EXPECT_TRUE(connection_.OnCanWrite());
+  writer_->SetWritable();
+  connection_.OnCanWrite();
   EXPECT_EQ(0u, connection_.NumQueuedPackets());
 }
 
@@ -2287,15 +2539,20 @@ TEST_P(QuicConnectionTest, CloseFecGroup) {
   ASSERT_EQ(1u, connection_.NumFecGroups());
 
   // Now send non-fec protected ack packet and close the group.
-  QuicAckFrame frame(0, QuicTime::Zero(), 5);
   creator_.set_sequence_number(4);
-  ProcessAckPacket(&frame);
+  if (version() > QUIC_VERSION_15) {
+    QuicStopWaitingFrame frame = InitStopWaitingFrame(5);
+    ProcessStopWaitingPacket(&frame);
+  } else {
+    QuicAckFrame frame = InitAckFrame(0, 5);
+    ProcessAckPacket(&frame);
+  }
   ASSERT_EQ(0u, connection_.NumFecGroups());
 }
 
 TEST_P(QuicConnectionTest, NoQuicCongestionFeedbackFrame) {
   SendAckPacketToPeer();
-  EXPECT_TRUE(last_feedback() == NULL);
+  EXPECT_TRUE(writer_->feedback_frames().empty());
 }
 
 TEST_P(QuicConnectionTest, WithQuicCongestionFeedbackFrame) {
@@ -2305,23 +2562,28 @@ TEST_P(QuicConnectionTest, WithQuicCongestionFeedbackFrame) {
   SetFeedback(&info);
 
   SendAckPacketToPeer();
-  EXPECT_EQ(kFixRate, last_feedback()->type);
-  EXPECT_EQ(info.fix_rate.bitrate, last_feedback()->fix_rate.bitrate);
+  ASSERT_FALSE(writer_->feedback_frames().empty());
+  ASSERT_EQ(kFixRate, writer_->feedback_frames()[0].type);
+  ASSERT_EQ(info.fix_rate.bitrate,
+            writer_->feedback_frames()[0].fix_rate.bitrate);
 }
 
 TEST_P(QuicConnectionTest, UpdateQuicCongestionFeedbackFrame) {
   SendAckPacketToPeer();
-  EXPECT_CALL(*receive_algorithm_, RecordIncomingPacket(_, _, _, _));
+  EXPECT_CALL(*receive_algorithm_, RecordIncomingPacket(_, _, _));
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
   ProcessPacket(1);
 }
 
 TEST_P(QuicConnectionTest, DontUpdateQuicCongestionFeedbackFrameForRevived) {
+  if (version() < QUIC_VERSION_15) {
+    return;
+  }
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
   SendAckPacketToPeer();
   // Process an FEC packet, and revive the missing data packet
   // but only contact the receive_algorithm once.
-  EXPECT_CALL(*receive_algorithm_, RecordIncomingPacket(_, _, _, _));
+  EXPECT_CALL(*receive_algorithm_, RecordIncomingPacket(_, _, _));
   ProcessFecPacket(2, 1, true, !kEntropyFlag, NULL);
 }
 
@@ -2342,12 +2604,58 @@ TEST_P(QuicConnectionTest, InitialTimeout) {
   EXPECT_FALSE(connection_.connected());
 
   EXPECT_FALSE(connection_.GetAckAlarm()->IsSet());
+  EXPECT_FALSE(connection_.GetPingAlarm()->IsSet());
   EXPECT_FALSE(connection_.GetResumeWritesAlarm()->IsSet());
   EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet());
   EXPECT_FALSE(connection_.GetSendAlarm()->IsSet());
   EXPECT_FALSE(connection_.GetTimeoutAlarm()->IsSet());
 }
 
+TEST_P(QuicConnectionTest, PingAfterSend) {
+  EXPECT_TRUE(connection_.connected());
+  EXPECT_CALL(visitor_, HasOpenDataStreams()).WillRepeatedly(Return(true));
+  EXPECT_FALSE(connection_.GetPingAlarm()->IsSet());
+
+  // Advance to 5ms, and send a packet to the peer, which will set
+  // the ping alarm.
+  clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
+  EXPECT_FALSE(connection_.GetRetransmissionAlarm()->IsSet());
+  SendStreamDataToPeer(1, "GET /", 0, kFin, NULL);
+  EXPECT_TRUE(connection_.GetPingAlarm()->IsSet());
+  EXPECT_EQ(clock_.ApproximateNow().Add(QuicTime::Delta::FromSeconds(15)),
+            connection_.GetPingAlarm()->deadline());
+
+  // Now recevie and ACK of the previous packet, which will move the
+  // ping alarm forward.
+  clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
+  QuicAckFrame frame = InitAckFrame(1, 0);
+  EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
+  ProcessAckPacket(&frame);
+  EXPECT_TRUE(connection_.GetPingAlarm()->IsSet());
+  EXPECT_EQ(clock_.ApproximateNow().Add(QuicTime::Delta::FromSeconds(15)),
+            connection_.GetPingAlarm()->deadline());
+
+  writer_->Reset();
+  clock_.AdvanceTime(QuicTime::Delta::FromSeconds(15));
+  connection_.GetPingAlarm()->Fire();
+  EXPECT_EQ(1u, writer_->frame_count());
+  if (version() > QUIC_VERSION_17) {
+    ASSERT_EQ(1u, writer_->ping_frames().size());
+  } else {
+    ASSERT_EQ(1u, writer_->stream_frames().size());
+    EXPECT_EQ(kCryptoStreamId, writer_->stream_frames()[0].stream_id);
+    EXPECT_EQ(0u, writer_->stream_frames()[0].offset);
+  }
+  writer_->Reset();
+
+  EXPECT_CALL(visitor_, HasOpenDataStreams()).WillRepeatedly(Return(false));
+  clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
+  SendAckPacketToPeer();
+
+  EXPECT_FALSE(connection_.GetPingAlarm()->IsSet());
+}
+
 TEST_P(QuicConnectionTest, TimeoutAfterSend) {
   EXPECT_TRUE(connection_.connected());
 
@@ -2358,7 +2666,7 @@ TEST_P(QuicConnectionTest, TimeoutAfterSend) {
   // kDefaultInitialTimeoutSecs.
   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
 
-  // Send an ack so we don't set the retransimission alarm.
+  // Send an ack so we don't set the retransmission alarm.
   SendAckPacketToPeer();
   EXPECT_EQ(default_timeout, connection_.GetTimeoutAlarm()->deadline());
 
@@ -2384,12 +2692,11 @@ TEST_P(QuicConnectionTest, TimeoutAfterSend) {
   EXPECT_FALSE(connection_.connected());
 }
 
-// TODO(ianswett): Add scheduler tests when should_retransmit is false.
 TEST_P(QuicConnectionTest, SendScheduler) {
   // Test that if we send a packet without delay, it is not queued.
   QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag);
   EXPECT_CALL(*send_algorithm_,
-              TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce(
+              TimeUntilSend(_, _, _)).WillOnce(
                   testing::Return(QuicTime::Delta::Zero()));
   EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _));
   connection_.SendPacket(
@@ -2401,33 +2708,21 @@ TEST_P(QuicConnectionTest, SendSchedulerDelay) {
   // Test that if we send a packet with a delay, it ends up queued.
   QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag);
   EXPECT_CALL(*send_algorithm_,
-              TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce(
+              TimeUntilSend(_, _, _)).WillOnce(
                   testing::Return(QuicTime::Delta::FromMicroseconds(1)));
-  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, 1, _, _, _)).Times(0);
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, 1, _, _)).Times(0);
   connection_.SendPacket(
       ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
   EXPECT_EQ(1u, connection_.NumQueuedPackets());
 }
 
-TEST_P(QuicConnectionTest, SendSchedulerForce) {
-  // Test that if we force send a packet, it is not queued.
-  QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag);
-  EXPECT_CALL(*send_algorithm_,
-              TimeUntilSend(_, NACK_RETRANSMISSION, _, _)).Times(0);
-  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _));
-  connection_.SendPacket(
-      ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
-  // XXX: fixme.  was:  connection_.SendPacket(1, packet, kForce);
-  EXPECT_EQ(0u, connection_.NumQueuedPackets());
-}
-
 TEST_P(QuicConnectionTest, SendSchedulerEAGAIN) {
   QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag);
-  writer_->set_blocked(true);
+  BlockOnNextWrite();
   EXPECT_CALL(*send_algorithm_,
-              TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce(
+              TimeUntilSend(_, _, _)).WillOnce(
                   testing::Return(QuicTime::Delta::Zero()));
-  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, 1, _, _, _)).Times(0);
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, 1, _, _)).Times(0);
   connection_.SendPacket(
       ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
   EXPECT_EQ(1u, connection_.NumQueuedPackets());
@@ -2437,7 +2732,7 @@ TEST_P(QuicConnectionTest, SendSchedulerDelayThenSend) {
   // Test that if we send a packet with a delay, it ends up queued.
   QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag);
   EXPECT_CALL(*send_algorithm_,
-              TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce(
+              TimeUntilSend(_, _, _)).WillOnce(
                   testing::Return(QuicTime::Delta::FromMicroseconds(1)));
   connection_.SendPacket(
        ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
@@ -2446,7 +2741,7 @@ TEST_P(QuicConnectionTest, SendSchedulerDelayThenSend) {
   // Advance the clock to fire the alarm, and configure the scheduler
   // to permit the packet to be sent.
   EXPECT_CALL(*send_algorithm_,
-              TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillRepeatedly(
+              TimeUntilSend(_, _, _)).WillRepeatedly(
                   testing::Return(QuicTime::Delta::Zero()));
   clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds(1));
   EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _));
@@ -2455,19 +2750,18 @@ TEST_P(QuicConnectionTest, SendSchedulerDelayThenSend) {
 }
 
 TEST_P(QuicConnectionTest, SendSchedulerDelayThenRetransmit) {
-  EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, NOT_RETRANSMISSION, _, _))
+  EXPECT_CALL(*send_algorithm_, TimeUntilSend(_, _, _))
       .WillRepeatedly(testing::Return(QuicTime::Delta::Zero()));
-  EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(1, _)).Times(1);
-  EXPECT_CALL(*send_algorithm_,
-              OnPacketSent(_, 1, _, NOT_RETRANSMISSION, _));
-  connection_.SendStreamDataWithString(1, "foo", 0, !kFin);
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, 1, _, _));
+  connection_.SendStreamDataWithString(3, "foo", 0, !kFin, NULL);
   EXPECT_EQ(0u, connection_.NumQueuedPackets());
   // Advance the time for retransmission of lost packet.
   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(501));
   // Test that if we send a retransmit with a delay, it ends up queued in the
   // sent packet manager, but not yet serialized.
+  EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
   EXPECT_CALL(*send_algorithm_,
-              TimeUntilSend(_, RTO_RETRANSMISSION, _, _)).WillOnce(
+              TimeUntilSend(_, _, _)).WillOnce(
                   testing::Return(QuicTime::Delta::FromMicroseconds(1)));
   connection_.GetRetransmissionAlarm()->Fire();
   EXPECT_EQ(0u, connection_.NumQueuedPackets());
@@ -2475,12 +2769,11 @@ TEST_P(QuicConnectionTest, SendSchedulerDelayThenRetransmit) {
   // Advance the clock to fire the alarm, and configure the scheduler
   // to permit the packet to be sent.
   EXPECT_CALL(*send_algorithm_,
-              TimeUntilSend(_, RTO_RETRANSMISSION, _, _)).Times(2).
+              TimeUntilSend(_, _, _)).Times(3).
                   WillRepeatedly(testing::Return(QuicTime::Delta::Zero()));
 
   // Ensure the scheduler is notified this is a retransmit.
-  EXPECT_CALL(*send_algorithm_,
-              OnPacketSent(_, _, _, RTO_RETRANSMISSION, _));
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _));
   clock_.AdvanceTime(QuicTime::Delta::FromMicroseconds(1));
   connection_.GetSendAlarm()->Fire();
   EXPECT_EQ(0u, connection_.NumQueuedPackets());
@@ -2489,7 +2782,7 @@ TEST_P(QuicConnectionTest, SendSchedulerDelayThenRetransmit) {
 TEST_P(QuicConnectionTest, SendSchedulerDelayAndQueue) {
   QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag);
   EXPECT_CALL(*send_algorithm_,
-              TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce(
+              TimeUntilSend(_, _, _)).WillOnce(
                   testing::Return(QuicTime::Delta::FromMicroseconds(1)));
   connection_.SendPacket(
       ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
@@ -2506,7 +2799,7 @@ TEST_P(QuicConnectionTest, SendSchedulerDelayThenAckAndSend) {
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
   QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag);
   EXPECT_CALL(*send_algorithm_,
-              TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce(
+              TimeUntilSend(_, _, _)).WillOnce(
                   testing::Return(QuicTime::Delta::FromMicroseconds(10)));
   connection_.SendPacket(
       ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
@@ -2514,9 +2807,9 @@ TEST_P(QuicConnectionTest, SendSchedulerDelayThenAckAndSend) {
 
   // Now send non-retransmitting information, that we're not going to
   // retransmit 3. The far end should stop waiting for it.
-  QuicAckFrame frame(0, QuicTime::Zero(), 1);
+  QuicAckFrame frame = InitAckFrame(0, 1);
   EXPECT_CALL(*send_algorithm_,
-              TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillRepeatedly(
+              TimeUntilSend(_, _, _)).WillRepeatedly(
                   testing::Return(QuicTime::Delta::Zero()));
   EXPECT_CALL(*send_algorithm_,
               OnPacketSent(_, _, _, _, _));
@@ -2531,7 +2824,7 @@ TEST_P(QuicConnectionTest, SendSchedulerDelayThenAckAndHold) {
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
   QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag);
   EXPECT_CALL(*send_algorithm_,
-              TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce(
+              TimeUntilSend(_, _, _)).WillOnce(
                   testing::Return(QuicTime::Delta::FromMicroseconds(10)));
   connection_.SendPacket(
       ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
@@ -2539,9 +2832,9 @@ TEST_P(QuicConnectionTest, SendSchedulerDelayThenAckAndHold) {
 
   // Now send non-retransmitting information, that we're not going to
   // retransmit 3.  The far end should stop waiting for it.
-  QuicAckFrame frame(0, QuicTime::Zero(), 1);
+  QuicAckFrame frame = InitAckFrame(0, 1);
   EXPECT_CALL(*send_algorithm_,
-              TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce(
+              TimeUntilSend(_, _, _)).WillOnce(
                   testing::Return(QuicTime::Delta::FromMicroseconds(1)));
   ProcessAckPacket(&frame);
 
@@ -2549,18 +2842,20 @@ TEST_P(QuicConnectionTest, SendSchedulerDelayThenAckAndHold) {
 }
 
 TEST_P(QuicConnectionTest, SendSchedulerDelayThenOnCanWrite) {
+  // TODO(ianswett): This test is unrealistic, because we would not serialize
+  // new data if the send algorithm said not to.
   QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag);
   EXPECT_CALL(*send_algorithm_,
-              TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce(
+              TimeUntilSend(_, _, _)).WillOnce(
                   testing::Return(QuicTime::Delta::FromMicroseconds(10)));
   connection_.SendPacket(
       ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
   EXPECT_EQ(1u, connection_.NumQueuedPackets());
 
-  // OnCanWrite should not send the packet (because of the delay)
-  // but should still return true.
-  EXPECT_TRUE(connection_.OnCanWrite());
-  EXPECT_EQ(1u, connection_.NumQueuedPackets());
+  // OnCanWrite should send the packet, because it won't consult the send
+  // algorithm for queued packets.
+  connection_.OnCanWrite();
+  EXPECT_EQ(0u, connection_.NumQueuedPackets());
 }
 
 TEST_P(QuicConnectionTest, TestQueueLimitsOnSendStreamData) {
@@ -2573,12 +2868,12 @@ TEST_P(QuicConnectionTest, TestQueueLimitsOnSendStreamData) {
 
   // Queue the first packet.
   EXPECT_CALL(*send_algorithm_,
-              TimeUntilSend(_, NOT_RETRANSMISSION, _, _)).WillOnce(
+              TimeUntilSend(_, _, _)).WillOnce(
                   testing::Return(QuicTime::Delta::FromMicroseconds(10)));
   const string payload(payload_length, 'a');
   EXPECT_EQ(0u,
             connection_.SendStreamDataWithString(3, payload, 0,
-                                                 !kFin).bytes_consumed);
+                                                 !kFin, NULL).bytes_consumed);
   EXPECT_EQ(0u, connection_.NumQueuedPackets());
 }
 
@@ -2596,13 +2891,46 @@ TEST_P(QuicConnectionTest, LoopThroughSendingPackets) {
   const string payload(payload_length * 7 - 12, 'a');
   EXPECT_EQ(payload.size(),
             connection_.SendStreamDataWithString(1, payload, 0,
-                                                 !kFin).bytes_consumed);
+                                                 !kFin, NULL).bytes_consumed);
 }
 
-TEST_P(QuicConnectionTest, SendDelayedAckOnTimer) {
+TEST_P(QuicConnectionTest, SendDelayedAck) {
   QuicTime ack_time = clock_.ApproximateNow().Add(DefaultDelayedAckTime());
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
   EXPECT_FALSE(connection_.GetAckAlarm()->IsSet());
+  const uint8 tag = 0x07;
+  connection_.SetDecrypter(new StrictTaggingDecrypter(tag),
+                           ENCRYPTION_INITIAL);
+  framer_.SetEncrypter(ENCRYPTION_INITIAL, new TaggingEncrypter(tag));
+  // Process a packet from the non-crypto stream.
+  frame1_.stream_id = 3;
+
+  // The same as ProcessPacket(1) except that ENCRYPTION_INITIAL is used
+  // instead of ENCRYPTION_NONE.
+  EXPECT_CALL(visitor_, OnStreamFrames(_)).Times(1);
+  ProcessDataPacketAtLevel(1, 0, !kEntropyFlag, ENCRYPTION_INITIAL);
+
+  // Check if delayed ack timer is running for the expected interval.
+  EXPECT_TRUE(connection_.GetAckAlarm()->IsSet());
+  EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline());
+  // Simulate delayed ack alarm firing.
+  connection_.GetAckAlarm()->Fire();
+  // Check that ack is sent and that delayed ack alarm is reset.
+  if (version() > QUIC_VERSION_15) {
+    EXPECT_EQ(2u, writer_->frame_count());
+    EXPECT_FALSE(writer_->stop_waiting_frames().empty());
+  } else {
+    EXPECT_EQ(1u, writer_->frame_count());
+  }
+  EXPECT_FALSE(writer_->ack_frames().empty());
+  EXPECT_FALSE(connection_.GetAckAlarm()->IsSet());
+}
+
+TEST_P(QuicConnectionTest, SendEarlyDelayedAckForCrypto) {
+  QuicTime ack_time = clock_.ApproximateNow();
+  EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
+  EXPECT_FALSE(connection_.GetAckAlarm()->IsSet());
+  // Process a packet from the crypto stream, which is frame1_'s default.
   ProcessPacket(1);
   // Check if delayed ack timer is running for the expected interval.
   EXPECT_TRUE(connection_.GetAckAlarm()->IsSet());
@@ -2610,8 +2938,13 @@ TEST_P(QuicConnectionTest, SendDelayedAckOnTimer) {
   // Simulate delayed ack alarm firing.
   connection_.GetAckAlarm()->Fire();
   // Check that ack is sent and that delayed ack alarm is reset.
-  EXPECT_EQ(1u, writer_->frame_count());
-  EXPECT_TRUE(writer_->ack());
+  if (version() > QUIC_VERSION_15) {
+    EXPECT_EQ(2u, writer_->frame_count());
+    EXPECT_FALSE(writer_->stop_waiting_frames().empty());
+  } else {
+    EXPECT_EQ(1u, writer_->frame_count());
+  }
+  EXPECT_FALSE(writer_->ack_frames().empty());
   EXPECT_FALSE(connection_.GetAckAlarm()->IsSet());
 }
 
@@ -2620,30 +2953,126 @@ TEST_P(QuicConnectionTest, SendDelayedAckOnSecondPacket) {
   ProcessPacket(1);
   ProcessPacket(2);
   // Check that ack is sent and that delayed ack alarm is reset.
-  EXPECT_EQ(1u, writer_->frame_count());
-  EXPECT_TRUE(writer_->ack());
+  if (version() > QUIC_VERSION_15) {
+    EXPECT_EQ(2u, writer_->frame_count());
+    EXPECT_FALSE(writer_->stop_waiting_frames().empty());
+  } else {
+    EXPECT_EQ(1u, writer_->frame_count());
+  }
+  EXPECT_FALSE(writer_->ack_frames().empty());
   EXPECT_FALSE(connection_.GetAckAlarm()->IsSet());
 }
 
+TEST_P(QuicConnectionTest, NoAckOnOldNacks) {
+  EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
+  // Drop one packet, triggering a sequence of acks.
+  ProcessPacket(2);
+  size_t frames_per_ack = version() > QUIC_VERSION_15 ? 2 : 1;
+  EXPECT_EQ(frames_per_ack, writer_->frame_count());
+  EXPECT_FALSE(writer_->ack_frames().empty());
+  writer_->Reset();
+  ProcessPacket(3);
+  EXPECT_EQ(frames_per_ack, writer_->frame_count());
+  EXPECT_FALSE(writer_->ack_frames().empty());
+  writer_->Reset();
+  ProcessPacket(4);
+  EXPECT_EQ(frames_per_ack, writer_->frame_count());
+  EXPECT_FALSE(writer_->ack_frames().empty());
+  writer_->Reset();
+  ProcessPacket(5);
+  EXPECT_EQ(frames_per_ack, writer_->frame_count());
+  EXPECT_FALSE(writer_->ack_frames().empty());
+  writer_->Reset();
+  // Now only set the timer on the 6th packet, instead of sending another ack.
+  ProcessPacket(6);
+  EXPECT_EQ(0u, writer_->frame_count());
+  EXPECT_TRUE(connection_.GetAckAlarm()->IsSet());
+}
+
 TEST_P(QuicConnectionTest, SendDelayedAckOnOutgoingPacket) {
-  if (!FLAGS_bundle_ack_with_outgoing_packet) {
-    // This test specifically tests ack bundling behavior.
-    return;
-  }
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
   ProcessPacket(1);
-  connection_.SendStreamDataWithString(1, "foo", 0, !kFin);
+  connection_.SendStreamDataWithString(kStreamId3, "foo", 0, !kFin, NULL);
   // Check that ack is bundled with outgoing data and that delayed ack
   // alarm is reset.
-  EXPECT_EQ(2u, writer_->frame_count());
-  EXPECT_TRUE(writer_->ack());
+  if (version() > QUIC_VERSION_15) {
+    EXPECT_EQ(3u, writer_->frame_count());
+    EXPECT_FALSE(writer_->stop_waiting_frames().empty());
+  } else {
+    EXPECT_EQ(2u, writer_->frame_count());
+  }
+  EXPECT_FALSE(writer_->ack_frames().empty());
   EXPECT_FALSE(connection_.GetAckAlarm()->IsSet());
 }
 
-TEST_P(QuicConnectionTest, NoAckForClose) {
+TEST_P(QuicConnectionTest, SendDelayedAckOnOutgoingCryptoPacket) {
+  EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
+  ProcessPacket(1);
+  connection_.SendStreamDataWithString(kCryptoStreamId, "foo", 0, !kFin, NULL);
+  // Check that ack is bundled with outgoing crypto data.
+  EXPECT_EQ(version() <= QUIC_VERSION_15 ? 2u : 3u, writer_->frame_count());
+  EXPECT_FALSE(writer_->ack_frames().empty());
+  EXPECT_FALSE(connection_.GetAckAlarm()->IsSet());
+}
+
+TEST_P(QuicConnectionTest, BundleAckWithDataOnIncomingAck) {
+  EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
+  connection_.SendStreamDataWithString(kStreamId3, "foo", 0, !kFin, NULL);
+  connection_.SendStreamDataWithString(kStreamId3, "foo", 3, !kFin, NULL);
+  // Ack the second packet, which will retransmit the first packet.
+  QuicAckFrame ack = InitAckFrame(2, 0);
+  NackPacket(1, &ack);
+  SequenceNumberSet lost_packets;
+  lost_packets.insert(1);
+  EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
+      .WillOnce(Return(lost_packets));
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
+  ProcessAckPacket(&ack);
+  EXPECT_EQ(1u, writer_->frame_count());
+  EXPECT_EQ(1u, writer_->stream_frames().size());
+  writer_->Reset();
+
+  // Now ack the retransmission, which will both raise the high water mark
+  // and see if there is more data to send.
+  ack = InitAckFrame(3, 0);
+  NackPacket(1, &ack);
+  EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
+      .WillOnce(Return(SequenceNumberSet()));
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
+  ProcessAckPacket(&ack);
+
+  // Check that no packet is sent and the ack alarm isn't set.
+  EXPECT_EQ(0u, writer_->frame_count());
+  EXPECT_FALSE(connection_.GetAckAlarm()->IsSet());
+  writer_->Reset();
+
+  // Send the same ack, but send both data and an ack together.
+  ack = InitAckFrame(3, 0);
+  NackPacket(1, &ack);
+  EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
+      .WillOnce(Return(SequenceNumberSet()));
+  EXPECT_CALL(visitor_, OnCanWrite()).WillOnce(
+      IgnoreResult(InvokeWithoutArgs(
+          &connection_,
+          &TestConnection::EnsureWritableAndSendStreamData5)));
+  ProcessAckPacket(&ack);
+
+  // Check that ack is bundled with outgoing data and the delayed ack
+  // alarm is reset.
+  if (version() > QUIC_VERSION_15) {
+    EXPECT_EQ(3u, writer_->frame_count());
+    EXPECT_FALSE(writer_->stop_waiting_frames().empty());
+  } else {
+    EXPECT_EQ(2u, writer_->frame_count());
+  }
+  EXPECT_FALSE(writer_->ack_frames().empty());
+  EXPECT_EQ(1u, writer_->stream_frames().size());
+  EXPECT_FALSE(connection_.GetAckAlarm()->IsSet());
+}
+
+TEST_P(QuicConnectionTest, NoAckSentForClose) {
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
   ProcessPacket(1);
-  EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(0);
   EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_PEER_GOING_AWAY, true));
   EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(0);
   ProcessClosePacket(2, 0);
@@ -2655,14 +3084,14 @@ TEST_P(QuicConnectionTest, SendWhenDisconnected) {
   connection_.CloseConnection(QUIC_PEER_GOING_AWAY, false);
   EXPECT_FALSE(connection_.connected());
   QuicPacket* packet = ConstructDataPacket(1, 0, !kEntropyFlag);
-  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, 1, _, _, _)).Times(0);
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, 1, _, _)).Times(0);
   connection_.SendPacket(
       ENCRYPTION_NONE, 1, packet, kTestEntropyHash, HAS_RETRANSMITTABLE_DATA);
 }
 
 TEST_P(QuicConnectionTest, PublicReset) {
   QuicPublicResetPacket header;
-  header.public_header.guid = guid_;
+  header.public_header.connection_id = connection_id_;
   header.public_header.reset_flag = true;
   header.public_header.version_flag = false;
   header.rejected_sequence_number = 10101;
@@ -2683,17 +3112,52 @@ TEST_P(QuicConnectionTest, GoAway) {
   ProcessGoAwayPacket(&goaway);
 }
 
+TEST_P(QuicConnectionTest, WindowUpdate) {
+  EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
+
+  QuicWindowUpdateFrame window_update;
+  window_update.stream_id = 3;
+  window_update.byte_offset = 1234;
+  EXPECT_CALL(visitor_, OnWindowUpdateFrames(_));
+  ProcessFramePacket(QuicFrame(&window_update));
+}
+
+TEST_P(QuicConnectionTest, Blocked) {
+  EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
+
+  QuicBlockedFrame blocked;
+  blocked.stream_id = 3;
+  EXPECT_CALL(visitor_, OnBlockedFrames(_));
+  ProcessFramePacket(QuicFrame(&blocked));
+}
+
+TEST_P(QuicConnectionTest, InvalidPacket) {
+  EXPECT_CALL(visitor_,
+              OnConnectionClosed(QUIC_INVALID_PACKET_HEADER, false));
+  QuicEncryptedPacket encrypted(NULL, 0);
+  connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), encrypted);
+  // The connection close packet should have error details.
+  ASSERT_FALSE(writer_->connection_close_frames().empty());
+  EXPECT_EQ("Unable to read public flags.",
+            writer_->connection_close_frames()[0].error_details);
+}
+
 TEST_P(QuicConnectionTest, MissingPacketsBeforeLeastUnacked) {
-  QuicAckFrame ack(0, QuicTime::Zero(), 4);
   // Set the sequence number of the ack packet to be least unacked (4).
   creator_.set_sequence_number(3);
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
-  ProcessAckPacket(&ack);
+  if (version() > QUIC_VERSION_15) {
+    QuicStopWaitingFrame frame = InitStopWaitingFrame(4);
+    ProcessStopWaitingPacket(&frame);
+  } else {
+    QuicAckFrame ack = InitAckFrame(0, 4);
+    ProcessAckPacket(&ack);
+  }
   EXPECT_TRUE(outgoing_ack()->received_info.missing_packets.empty());
 }
 
 TEST_P(QuicConnectionTest, ReceivedEntropyHashCalculation) {
-  EXPECT_CALL(visitor_, OnStreamFrames(_)).WillRepeatedly(Return(true));
+  EXPECT_CALL(visitor_, OnStreamFrames(_)).Times(AtLeast(1));
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
   ProcessDataPacket(1, 1, kEntropyFlag);
   ProcessDataPacket(4, 1, kEntropyFlag);
@@ -2702,21 +3166,43 @@ TEST_P(QuicConnectionTest, ReceivedEntropyHashCalculation) {
   EXPECT_EQ(146u, outgoing_ack()->received_info.entropy_hash);
 }
 
+TEST_P(QuicConnectionTest, ReceivedEntropyHashCalculationHalfFEC) {
+  if (version() < QUIC_VERSION_15) {
+    return;
+  }
+  // FEC packets should not change the entropy hash calculation.
+  EXPECT_CALL(visitor_, OnStreamFrames(_)).Times(AtLeast(1));
+  EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
+  ProcessDataPacket(1, 1, kEntropyFlag);
+  ProcessFecPacket(4, 1, false, kEntropyFlag, NULL);
+  ProcessDataPacket(3, 3, !kEntropyFlag);
+  ProcessFecPacket(7, 3, false, kEntropyFlag, NULL);
+  EXPECT_EQ(146u, outgoing_ack()->received_info.entropy_hash);
+}
+
 TEST_P(QuicConnectionTest, UpdateEntropyForReceivedPackets) {
-  EXPECT_CALL(visitor_, OnStreamFrames(_)).WillRepeatedly(Return(true));
+  EXPECT_CALL(visitor_, OnStreamFrames(_)).Times(AtLeast(1));
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
   ProcessDataPacket(1, 1, kEntropyFlag);
   ProcessDataPacket(5, 1, kEntropyFlag);
   ProcessDataPacket(4, 1, !kEntropyFlag);
   EXPECT_EQ(34u, outgoing_ack()->received_info.entropy_hash);
   // Make 4th packet my least unacked, and update entropy for 2, 3 packets.
-  QuicAckFrame ack(0, QuicTime::Zero(), 4);
-  QuicPacketEntropyHash kRandomEntropyHash = 129u;
-  ack.sent_info.entropy_hash = kRandomEntropyHash;
   creator_.set_sequence_number(5);
   QuicPacketEntropyHash six_packet_entropy_hash = 0;
-  if (ProcessAckPacket(&ack)) {
-    six_packet_entropy_hash = 1 << 6;
+  QuicPacketEntropyHash kRandomEntropyHash = 129u;
+  if (version() > QUIC_VERSION_15) {
+    QuicStopWaitingFrame frame = InitStopWaitingFrame(4);
+    frame.entropy_hash = kRandomEntropyHash;
+    if (ProcessStopWaitingPacket(&frame)) {
+      six_packet_entropy_hash = 1 << 6;
+    }
+  } else {
+    QuicAckFrame ack = InitAckFrame(0, 4);
+    ack.sent_info.entropy_hash = kRandomEntropyHash;
+    if (ProcessAckPacket(&ack)) {
+      six_packet_entropy_hash = 1 << 6;
+    }
   }
 
   EXPECT_EQ((kRandomEntropyHash + (1 << 5) + six_packet_entropy_hash),
@@ -2724,7 +3210,7 @@ TEST_P(QuicConnectionTest, UpdateEntropyForReceivedPackets) {
 }
 
 TEST_P(QuicConnectionTest, UpdateEntropyHashUptoCurrentPacket) {
-  EXPECT_CALL(visitor_, OnStreamFrames(_)).WillRepeatedly(Return(true));
+  EXPECT_CALL(visitor_, OnStreamFrames(_)).Times(AtLeast(1));
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
   ProcessDataPacket(1, 1, kEntropyFlag);
   ProcessDataPacket(5, 1, !kEntropyFlag);
@@ -2733,9 +3219,16 @@ TEST_P(QuicConnectionTest, UpdateEntropyHashUptoCurrentPacket) {
   creator_.set_sequence_number(22);
   QuicPacketEntropyHash kRandomEntropyHash = 85u;
   // Current packet is the least unacked packet.
-  QuicAckFrame ack(0, QuicTime::Zero(), 23);
-  ack.sent_info.entropy_hash = kRandomEntropyHash;
-  QuicPacketEntropyHash ack_entropy_hash =  ProcessAckPacket(&ack);
+  QuicPacketEntropyHash ack_entropy_hash;
+  if (version() > QUIC_VERSION_15) {
+    QuicStopWaitingFrame frame = InitStopWaitingFrame(23);
+    frame.entropy_hash = kRandomEntropyHash;
+    ack_entropy_hash = ProcessStopWaitingPacket(&frame);
+  } else {
+    QuicAckFrame ack = InitAckFrame(0, 23);
+    ack.sent_info.entropy_hash = kRandomEntropyHash;
+    ack_entropy_hash = ProcessAckPacket(&ack);
+  }
   EXPECT_EQ((kRandomEntropyHash + ack_entropy_hash),
             outgoing_ack()->received_info.entropy_hash);
   ProcessDataPacket(25, 1, kEntropyFlag);
@@ -2744,7 +3237,7 @@ TEST_P(QuicConnectionTest, UpdateEntropyHashUptoCurrentPacket) {
 }
 
 TEST_P(QuicConnectionTest, EntropyCalculationForTruncatedAck) {
-  EXPECT_CALL(visitor_, OnStreamFrames(_)).WillRepeatedly(Return(true));
+  EXPECT_CALL(visitor_, OnStreamFrames(_)).Times(AtLeast(1));
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
   QuicPacketEntropyHash entropy[51];
   entropy[0] = 0;
@@ -2799,10 +3292,11 @@ TEST_P(QuicConnectionTest, CheckSentEntropyHash) {
 }
 
 TEST_P(QuicConnectionTest, ServerSendsVersionNegotiationPacket) {
+  connection_.SetSupportedVersions(QuicSupportedVersions());
   framer_.set_version_for_tests(QUIC_VERSION_UNSUPPORTED);
 
   QuicPacketHeader header;
-  header.public_header.guid = guid_;
+  header.public_header.connection_id = connection_id_;
   header.public_header.reset_flag = false;
   header.public_header.version_flag = true;
   header.entropy_flag = false;
@@ -2818,13 +3312,13 @@ TEST_P(QuicConnectionTest, ServerSendsVersionNegotiationPacket) {
   scoped_ptr<QuicEncryptedPacket> encrypted(
       framer_.EncryptPacket(ENCRYPTION_NONE, 12, *packet));
 
-  framer_.set_version(QuicVersionMax());
+  framer_.set_version(version());
   connection_.set_is_server(true);
   connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted);
   EXPECT_TRUE(writer_->version_negotiation_packet() != NULL);
 
   size_t num_versions = arraysize(kSupportedQuicVersions);
-  EXPECT_EQ(num_versions,
+  ASSERT_EQ(num_versions,
             writer_->version_negotiation_packet()->versions.size());
 
   // We expect all versions in kSupportedQuicVersions to be
@@ -2836,10 +3330,11 @@ TEST_P(QuicConnectionTest, ServerSendsVersionNegotiationPacket) {
 }
 
 TEST_P(QuicConnectionTest, ServerSendsVersionNegotiationPacketSocketBlocked) {
+  connection_.SetSupportedVersions(QuicSupportedVersions());
   framer_.set_version_for_tests(QUIC_VERSION_UNSUPPORTED);
 
   QuicPacketHeader header;
-  header.public_header.guid = guid_;
+  header.public_header.connection_id = connection_id_;
   header.public_header.reset_flag = false;
   header.public_header.version_flag = true;
   header.entropy_flag = false;
@@ -2855,20 +3350,19 @@ TEST_P(QuicConnectionTest, ServerSendsVersionNegotiationPacketSocketBlocked) {
   scoped_ptr<QuicEncryptedPacket> encrypted(
       framer_.EncryptPacket(ENCRYPTION_NONE, 12, *packet));
 
-  framer_.set_version(QuicVersionMax());
+  framer_.set_version(version());
   connection_.set_is_server(true);
-  writer_->set_blocked(true);
+  BlockOnNextWrite();
   connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted);
   EXPECT_EQ(0u, writer_->last_packet_size());
   EXPECT_TRUE(connection_.HasQueuedData());
-  EXPECT_TRUE(QuicConnectionPeer::IsWriteBlocked(&connection_));
 
-  writer_->set_blocked(false);
+  writer_->SetWritable();
   connection_.OnCanWrite();
   EXPECT_TRUE(writer_->version_negotiation_packet() != NULL);
 
   size_t num_versions = arraysize(kSupportedQuicVersions);
-  EXPECT_EQ(num_versions,
+  ASSERT_EQ(num_versions,
             writer_->version_negotiation_packet()->versions.size());
 
   // We expect all versions in kSupportedQuicVersions to be
@@ -2881,10 +3375,11 @@ TEST_P(QuicConnectionTest, ServerSendsVersionNegotiationPacketSocketBlocked) {
 
 TEST_P(QuicConnectionTest,
        ServerSendsVersionNegotiationPacketSocketBlockedDataBuffered) {
+  connection_.SetSupportedVersions(QuicSupportedVersions());
   framer_.set_version_for_tests(QUIC_VERSION_UNSUPPORTED);
 
   QuicPacketHeader header;
-  header.public_header.guid = guid_;
+  header.public_header.connection_id = connection_id_;
   header.public_header.reset_flag = false;
   header.public_header.version_flag = true;
   header.entropy_flag = false;
@@ -2900,14 +3395,13 @@ TEST_P(QuicConnectionTest,
   scoped_ptr<QuicEncryptedPacket> encrypted(
       framer_.EncryptPacket(ENCRYPTION_NONE, 12, *packet));
 
-  framer_.set_version(QuicVersionMax());
+  framer_.set_version(version());
   connection_.set_is_server(true);
-  writer_->set_blocked(true);
+  BlockOnNextWrite();
   writer_->set_is_write_blocked_data_buffered(true);
   connection_.ProcessUdpPacket(IPEndPoint(), IPEndPoint(), *encrypted);
   EXPECT_EQ(0u, writer_->last_packet_size());
   EXPECT_FALSE(connection_.HasQueuedData());
-  EXPECT_TRUE(QuicConnectionPeer::IsWriteBlocked(&connection_));
 }
 
 TEST_P(QuicConnectionTest, ClientHandlesVersionNegotiation) {
@@ -2916,7 +3410,7 @@ TEST_P(QuicConnectionTest, ClientHandlesVersionNegotiation) {
       QUIC_VERSION_UNSUPPORTED);
 
   QuicPacketHeader header;
-  header.public_header.guid = guid_;
+  header.public_header.connection_id = connection_id_;
   header.public_header.reset_flag = false;
   header.public_header.version_flag = true;
   header.entropy_flag = false;
@@ -2954,7 +3448,7 @@ TEST_P(QuicConnectionTest, ClientHandlesVersionNegotiation) {
 
 TEST_P(QuicConnectionTest, BadVersionNegotiation) {
   QuicPacketHeader header;
-  header.public_header.guid = guid_;
+  header.public_header.connection_id = connection_id_;
   header.public_header.reset_flag = false;
   header.public_header.version_flag = true;
   header.entropy_flag = false;
@@ -2979,49 +3473,36 @@ TEST_P(QuicConnectionTest, BadVersionNegotiation) {
 }
 
 TEST_P(QuicConnectionTest, CheckSendStats) {
-  EXPECT_CALL(*send_algorithm_,
-              OnPacketSent(_, _, _, NOT_RETRANSMISSION, _));
-  connection_.SendStreamDataWithString(1u, "first", 0, !kFin);
-  size_t first_packet_size = last_sent_packet_size();
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _));
+  connection_.SendStreamDataWithString(3, "first", 0, !kFin, NULL);
+  size_t first_packet_size = writer_->last_packet_size();
 
-  EXPECT_CALL(*send_algorithm_,
-              OnPacketSent(_, _, _, NOT_RETRANSMISSION, _));
-  connection_.SendStreamDataWithString(1u, "second", 0, !kFin);
-  size_t second_packet_size = last_sent_packet_size();
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _));
+  connection_.SendStreamDataWithString(5, "second", 0, !kFin, NULL);
+  size_t second_packet_size = writer_->last_packet_size();
 
   // 2 retransmissions due to rto, 1 due to explicit nack.
-  EXPECT_CALL(*send_algorithm_,
-              OnPacketSent(_, _, _, RTO_RETRANSMISSION, _)).Times(2);
-  EXPECT_CALL(*send_algorithm_,
-              OnPacketSent(_, _, _, NACK_RETRANSMISSION, _));
-  EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(_, _)).Times(3);
-  EXPECT_CALL(visitor_, OnCanWrite()).Times(2).WillRepeatedly(Return(true));
+  EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(3);
 
   // Retransmit due to RTO.
   clock_.AdvanceTime(QuicTime::Delta::FromSeconds(10));
   connection_.GetRetransmissionAlarm()->Fire();
 
   // Retransmit due to explicit nacks.
-  QuicAckFrame nack_three(4, QuicTime::Zero(), 0);
-  nack_three.received_info.missing_packets.insert(3);
-  nack_three.received_info.missing_packets.insert(1);
-  nack_three.received_info.entropy_hash =
-      QuicConnectionPeer::GetSentEntropyHash(&connection_, 4) ^
-      QuicConnectionPeer::GetSentEntropyHash(&connection_, 3) ^
-      QuicConnectionPeer::GetSentEntropyHash(&connection_, 2) ^
-      QuicConnectionPeer::GetSentEntropyHash(&connection_, 1);
-  QuicFrame frame(&nack_three);
-  EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(1);
-  EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(1);
-  EXPECT_CALL(visitor_, OnCanWrite()).Times(4).WillRepeatedly(Return(true));
-  EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
-
-  ProcessFramePacket(frame);
-  ProcessFramePacket(frame);
-  ProcessFramePacket(frame);
-
-  EXPECT_CALL(*send_algorithm_, SmoothedRtt()).WillOnce(
-      Return(QuicTime::Delta::Zero()));
+  QuicAckFrame nack_three = InitAckFrame(4, 0);
+  NackPacket(3, &nack_three);
+  NackPacket(1, &nack_three);
+  SequenceNumberSet lost_packets;
+  lost_packets.insert(1);
+  lost_packets.insert(3);
+  EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
+      .WillOnce(Return(lost_packets));
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
+  EXPECT_CALL(visitor_, OnCanWrite()).Times(2);
+  EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
+  ProcessAckPacket(&nack_three);
+
   EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillOnce(
       Return(QuicBandwidth::Zero()));
 
@@ -3036,6 +3517,9 @@ TEST_P(QuicConnectionTest, CheckSendStats) {
 }
 
 TEST_P(QuicConnectionTest, CheckReceiveStats) {
+  if (version() < QUIC_VERSION_15) {
+    return;
+  }
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
 
   size_t received_bytes = 0;
@@ -3045,8 +3529,6 @@ TEST_P(QuicConnectionTest, CheckReceiveStats) {
   received_bytes += ProcessDataPacket(3, 1, !kEntropyFlag);
   received_bytes += ProcessFecPacket(4, 1, true, !kEntropyFlag, NULL);
 
-  EXPECT_CALL(*send_algorithm_, SmoothedRtt()).WillOnce(
-      Return(QuicTime::Delta::Zero()));
   EXPECT_CALL(*send_algorithm_, BandwidthEstimate()).WillOnce(
       Return(QuicBandwidth::Zero()));
 
@@ -3082,7 +3564,7 @@ TEST_P(QuicConnectionTest, TestFecGroupLimits) {
 
 TEST_P(QuicConnectionTest, ProcessFramesIfPacketClosedConnection) {
   // Construct a packet with stream frame and connection close frame.
-  header_.public_header.guid = guid_;
+  header_.public_header.connection_id = connection_id_;
   header_.packet_sequence_number = 1;
   header_.public_header.reset_flag = false;
   header_.public_header.version_flag = false;
@@ -3092,7 +3574,6 @@ TEST_P(QuicConnectionTest, ProcessFramesIfPacketClosedConnection) {
 
   QuicConnectionCloseFrame qccf;
   qccf.error_code = QUIC_PEER_GOING_AWAY;
-  qccf.ack_frame = QuicAckFrame(0, QuicTime::Zero(), 1);
   QuicFrame close_frame(&qccf);
   QuicFrame stream_frame(&frame1_);
 
@@ -3113,9 +3594,10 @@ TEST_P(QuicConnectionTest, ProcessFramesIfPacketClosedConnection) {
 }
 
 TEST_P(QuicConnectionTest, SelectMutualVersion) {
+  connection_.SetSupportedVersions(QuicSupportedVersions());
   // Set the connection to speak the lowest quic version.
-  connection_.set_version(test::QuicVersionMin());
-  EXPECT_EQ(test::QuicVersionMin(), connection_.version());
+  connection_.set_version(QuicVersionMin());
+  EXPECT_EQ(QuicVersionMin(), connection_.version());
 
   // Pass in available versions which includes a higher mutually supported
   // version.  The higher mutually supported version should be selected.
@@ -3129,11 +3611,11 @@ TEST_P(QuicConnectionTest, SelectMutualVersion) {
   // Expect that the lowest version is selected.
   // Ensure the lowest supported version is less than the max, unless they're
   // the same.
-  EXPECT_LE(test::QuicVersionMin(), QuicVersionMax());
+  EXPECT_LE(QuicVersionMin(), QuicVersionMax());
   QuicVersionVector lowest_version_vector;
-  lowest_version_vector.push_back(test::QuicVersionMin());
+  lowest_version_vector.push_back(QuicVersionMin());
   EXPECT_TRUE(connection_.SelectMutualVersion(lowest_version_vector));
-  EXPECT_EQ(test::QuicVersionMin(), connection_.version());
+  EXPECT_EQ(QuicVersionMin(), connection_.version());
 
   // Shouldn't be able to find a mutually supported version.
   QuicVersionVector unsupported_version;
@@ -3141,41 +3623,32 @@ TEST_P(QuicConnectionTest, SelectMutualVersion) {
   EXPECT_FALSE(connection_.SelectMutualVersion(unsupported_version));
 }
 
-TEST_P(QuicConnectionTest, ConnectionCloseWhenNotWriteBlocked) {
-  writer_->set_blocked(false);  // Already default.
+TEST_P(QuicConnectionTest, ConnectionCloseWhenWritable) {
+  EXPECT_FALSE(writer_->IsWriteBlocked());
 
-  // Send a packet (but write will not block).
-  connection_.SendStreamDataWithString(1, "foo", 0, !kFin);
+  // Send a packet.
+  connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL);
   EXPECT_EQ(0u, connection_.NumQueuedPackets());
   EXPECT_EQ(1u, writer_->packets_write_attempts());
 
-  // Send an erroneous packet to close the connection.
-  EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_INVALID_PACKET_HEADER, false));
-  ProcessDataPacket(6000, 0, !kEntropyFlag);
+  TriggerConnectionClose();
   EXPECT_EQ(2u, writer_->packets_write_attempts());
 }
 
-TEST_P(QuicConnectionTest, ConnectionCloseWhenWriteBlocked) {
-  EXPECT_EQ(0u, connection_.NumQueuedPackets());
-  writer_->set_blocked(true);
-
-  // Send a packet to so that write will really block.
-  connection_.SendStreamDataWithString(1, "foo", 0, !kFin);
-  EXPECT_EQ(1u, connection_.NumQueuedPackets());
-  EXPECT_EQ(1u, writer_->packets_write_attempts());
-
-  // Send an erroneous packet to close the connection.
-  EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_INVALID_PACKET_HEADER, false));
-  ProcessDataPacket(6000, 0, !kEntropyFlag);
+TEST_P(QuicConnectionTest, ConnectionCloseGettingWriteBlocked) {
+  BlockOnNextWrite();
+  TriggerConnectionClose();
   EXPECT_EQ(1u, writer_->packets_write_attempts());
+  EXPECT_TRUE(writer_->IsWriteBlocked());
 }
 
-TEST_P(QuicConnectionTest, ConnectionCloseWhenNothingPending) {
-  writer_->set_blocked(true);
-
-  // Send an erroneous packet to close the connection.
-  EXPECT_CALL(visitor_, OnConnectionClosed(QUIC_INVALID_PACKET_HEADER, false));
-  ProcessDataPacket(6000, 0, !kEntropyFlag);
+TEST_P(QuicConnectionTest, ConnectionCloseWhenWriteBlocked) {
+  BlockOnNextWrite();
+  connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL);
+  EXPECT_EQ(1u, connection_.NumQueuedPackets());
+  EXPECT_EQ(1u, writer_->packets_write_attempts());
+  EXPECT_TRUE(writer_->IsWriteBlocked());
+  TriggerConnectionClose();
   EXPECT_EQ(1u, writer_->packets_write_attempts());
 }
 
@@ -3183,16 +3656,15 @@ TEST_P(QuicConnectionTest, AckNotifierTriggerCallback) {
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
 
   // Create a delegate which we expect to be called.
-  MockAckNotifierDelegate delegate;
-  EXPECT_CALL(delegate, OnAckNotification()).Times(1);;
+  scoped_refptr<MockAckNotifierDelegate> delegate(new MockAckNotifierDelegate);
+  EXPECT_CALL(*delegate, OnAckNotification(_, _, _, _, _)).Times(1);
 
   // Send some data, which will register the delegate to be notified.
-  connection_.SendStreamDataWithStringAndNotifyWhenAcked(1, "foo", 0, !kFin,
-                                                         &delegate);
+  connection_.SendStreamDataWithString(1, "foo", 0, !kFin, delegate.get());
 
   // Process an ACK from the server which should trigger the callback.
-  EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(1);
-  QuicAckFrame frame(1, QuicTime::Zero(), 0);
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
+  QuicAckFrame frame = InitAckFrame(1, 0);
   ProcessAckPacket(&frame);
 }
 
@@ -3200,25 +3672,26 @@ TEST_P(QuicConnectionTest, AckNotifierFailToTriggerCallback) {
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
 
   // Create a delegate which we don't expect to be called.
-  MockAckNotifierDelegate delegate;
-  EXPECT_CALL(delegate, OnAckNotification()).Times(0);;
-
-  EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(2);
-  EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(1);
+  scoped_refptr<MockAckNotifierDelegate> delegate(new MockAckNotifierDelegate);
+  EXPECT_CALL(*delegate, OnAckNotification(_, _, _, _, _)).Times(0);
 
   // Send some data, which will register the delegate to be notified. This will
   // not be ACKed and so the delegate should never be called.
-  connection_.SendStreamDataWithStringAndNotifyWhenAcked(1, "foo", 0, !kFin,
-                                                         &delegate);
+  connection_.SendStreamDataWithString(1, "foo", 0, !kFin, delegate.get());
 
   // Send some other data which we will ACK.
-  connection_.SendStreamDataWithString(1, "foo", 0, !kFin);
-  connection_.SendStreamDataWithString(1, "bar", 0, !kFin);
+  connection_.SendStreamDataWithString(1, "foo", 0, !kFin, NULL);
+  connection_.SendStreamDataWithString(1, "bar", 0, !kFin, NULL);
 
   // Now we receive ACK for packets 2 and 3, but importantly missing packet 1
   // which we registered to be notified about.
-  QuicAckFrame frame(3, QuicTime::Zero(), 0);
-  frame.received_info.missing_packets.insert(1);
+  QuicAckFrame frame = InitAckFrame(3, 0);
+  NackPacket(1, &frame);
+  SequenceNumberSet lost_packets;
+  lost_packets.insert(1);
+  EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
+      .WillOnce(Return(lost_packets));
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
   ProcessAckPacket(&frame);
 }
 
@@ -3226,70 +3699,169 @@ TEST_P(QuicConnectionTest, AckNotifierCallbackAfterRetransmission) {
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
 
   // Create a delegate which we expect to be called.
-  MockAckNotifierDelegate delegate;
-  EXPECT_CALL(delegate, OnAckNotification()).Times(1);;
-
-  // In total expect ACKs for all 4 packets.
-  EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(4);
-
-  // We will lose the second packet.
-  EXPECT_CALL(*send_algorithm_, OnIncomingLoss(_)).Times(1);
+  scoped_refptr<MockAckNotifierDelegate> delegate(new MockAckNotifierDelegate);
+  EXPECT_CALL(*delegate, OnAckNotification(_, _, _, _, _)).Times(1);
 
   // Send four packets, and register to be notified on ACK of packet 2.
-  connection_.SendStreamDataWithString(1, "foo", 0, !kFin);
-  connection_.SendStreamDataWithStringAndNotifyWhenAcked(1, "bar", 0, !kFin,
-                                                         &delegate);
-  connection_.SendStreamDataWithString(1, "baz", 0, !kFin);
-  connection_.SendStreamDataWithString(1, "qux", 0, !kFin);
-
-  // Now we receive ACK for packets 1, 3, and 4.
-  QuicAckFrame frame(4, QuicTime::Zero(), 0);
-  frame.received_info.missing_packets.insert(2);
+  connection_.SendStreamDataWithString(3, "foo", 0, !kFin, NULL);
+  connection_.SendStreamDataWithString(3, "bar", 0, !kFin, delegate.get());
+  connection_.SendStreamDataWithString(3, "baz", 0, !kFin, NULL);
+  connection_.SendStreamDataWithString(3, "qux", 0, !kFin, NULL);
+
+  // Now we receive ACK for packets 1, 3, and 4 and lose 2.
+  QuicAckFrame frame = InitAckFrame(4, 0);
+  NackPacket(2, &frame);
+  SequenceNumberSet lost_packets;
+  lost_packets.insert(2);
+  EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
+      .WillOnce(Return(lost_packets));
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _));
   ProcessAckPacket(&frame);
 
-  // Advance time to trigger RTO, for packet 2 (which should be retransmitted as
-  // packet 5).
-  EXPECT_CALL(*send_algorithm_, OnPacketAbandoned(2, _)).Times(1);
-  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(1);
+  // Now we get an ACK for packet 5 (retransmitted packet 2), which should
+  // trigger the callback.
+  EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
+      .WillRepeatedly(Return(SequenceNumberSet()));
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
+  QuicAckFrame second_ack_frame = InitAckFrame(5, 0);
+  ProcessAckPacket(&second_ack_frame);
+}
+
+// AckNotifierCallback is triggered by the ack of a packet that timed
+// out and was retransmitted, even though the retransmission has a
+// different sequence number.
+TEST_P(QuicConnectionTest, AckNotifierCallbackForAckAfterRTO) {
+  InSequence s;
+
+  // Create a delegate which we expect to be called.
+  scoped_refptr<MockAckNotifierDelegate> delegate(
+      new StrictMock<MockAckNotifierDelegate>);
 
+  QuicTime default_retransmission_time = clock_.ApproximateNow().Add(
+      DefaultRetransmissionTime());
+  connection_.SendStreamDataWithString(3, "foo", 0, !kFin, delegate.get());
+  EXPECT_EQ(1u, outgoing_ack()->sent_info.least_unacked);
+
+  EXPECT_EQ(1u, writer_->header().packet_sequence_number);
+  EXPECT_EQ(default_retransmission_time,
+            connection_.GetRetransmissionAlarm()->deadline());
+  // Simulate the retransmission alarm firing.
   clock_.AdvanceTime(DefaultRetransmissionTime());
+  EXPECT_CALL(*send_algorithm_, OnRetransmissionTimeout(true));
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, 2u, _, _));
   connection_.GetRetransmissionAlarm()->Fire();
+  EXPECT_EQ(2u, writer_->header().packet_sequence_number);
+  // We do not raise the high water mark yet.
+  EXPECT_EQ(1u, outgoing_ack()->sent_info.least_unacked);
 
-  // Now we get an ACK for packet 5 (retransmitted packet 2), which should
-  // trigger the callback.
-  QuicAckFrame second_ack_frame(5, QuicTime::Zero(), 0);
+  // Ack the original packet.
+  EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
+  EXPECT_CALL(*delegate, OnAckNotification(1, _, 1, _, _));
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
+  QuicAckFrame ack_frame = InitAckFrame(1, 0);
+  ProcessAckPacket(&ack_frame);
+
+  // Delegate is not notified again when the retransmit is acked.
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
+  QuicAckFrame second_ack_frame = InitAckFrame(2, 0);
+  ProcessAckPacket(&second_ack_frame);
+}
+
+// AckNotifierCallback is triggered by the ack of a packet that was
+// previously nacked, even though the retransmission has a different
+// sequence number.
+TEST_P(QuicConnectionTest, AckNotifierCallbackForAckOfNackedPacket) {
+  InSequence s;
+
+  // Create a delegate which we expect to be called.
+  scoped_refptr<MockAckNotifierDelegate> delegate(
+      new StrictMock<MockAckNotifierDelegate>);
+
+  // Send four packets, and register to be notified on ACK of packet 2.
+  connection_.SendStreamDataWithString(3, "foo", 0, !kFin, NULL);
+  connection_.SendStreamDataWithString(3, "bar", 0, !kFin, delegate.get());
+  connection_.SendStreamDataWithString(3, "baz", 0, !kFin, NULL);
+  connection_.SendStreamDataWithString(3, "qux", 0, !kFin, NULL);
+
+  // Now we receive ACK for packets 1, 3, and 4 and lose 2.
+  QuicAckFrame frame = InitAckFrame(4, 0);
+  NackPacket(2, &frame);
+  SequenceNumberSet lost_packets;
+  lost_packets.insert(2);
+  EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
+  EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
+      .WillOnce(Return(lost_packets));
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
+  EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _));
+  ProcessAckPacket(&frame);
+
+  // Now we get an ACK for packet 2, which was previously nacked.
+  SequenceNumberSet no_lost_packets;
+  EXPECT_CALL(*delegate, OnAckNotification(1, _, 1, _, _));
+  EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
+      .WillOnce(Return(no_lost_packets));
+  QuicAckFrame second_ack_frame = InitAckFrame(4, 0);
   ProcessAckPacket(&second_ack_frame);
+
+  // Verify that the delegate is not notified again when the
+  // retransmit is acked.
+  EXPECT_CALL(*loss_algorithm_, DetectLostPackets(_, _, _, _))
+      .WillOnce(Return(no_lost_packets));
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
+  QuicAckFrame third_ack_frame = InitAckFrame(5, 0);
+  ProcessAckPacket(&third_ack_frame);
+}
+
+TEST_P(QuicConnectionTest, AckNotifierFECTriggerCallback) {
+  EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
+
+  // Create a delegate which we expect to be called.
+  scoped_refptr<MockAckNotifierDelegate> delegate(
+      new MockAckNotifierDelegate);
+  EXPECT_CALL(*delegate, OnAckNotification(_, _, _, _, _)).Times(1);
+
+  // Send some data, which will register the delegate to be notified.
+  connection_.SendStreamDataWithString(1, "foo", 0, !kFin, delegate.get());
+  connection_.SendStreamDataWithString(2, "bar", 0, !kFin, NULL);
+
+  // Process an ACK from the server with a revived packet, which should trigger
+  // the callback.
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
+  QuicAckFrame frame = InitAckFrame(2, 0);
+  NackPacket(1, &frame);
+  frame.received_info.revived_packets.insert(1);
+  ProcessAckPacket(&frame);
+  // If the ack is processed again, the notifier should not be called again.
+  ProcessAckPacket(&frame);
 }
 
-// TODO(rjshade): Add a similar test that FEC recovery on peer (and resulting
-//                ACK) triggers notification on our end.
 TEST_P(QuicConnectionTest, AckNotifierCallbackAfterFECRecovery) {
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
-  EXPECT_CALL(visitor_, OnCanWrite()).Times(1).WillOnce(Return(true));
+  EXPECT_CALL(visitor_, OnCanWrite());
 
   // Create a delegate which we expect to be called.
-  MockAckNotifierDelegate delegate;
-  EXPECT_CALL(delegate, OnAckNotification()).Times(1);;
+  scoped_refptr<MockAckNotifierDelegate> delegate(new MockAckNotifierDelegate);
+  EXPECT_CALL(*delegate, OnAckNotification(_, _, _, _, _)).Times(1);
 
   // Expect ACKs for 1 packet.
-  EXPECT_CALL(*send_algorithm_, OnIncomingAck(_, _, _)).Times(1);
+  EXPECT_CALL(*send_algorithm_, OnCongestionEvent(true, _, _, _));
 
   // Send one packet, and register to be notified on ACK.
-  connection_.SendStreamDataWithStringAndNotifyWhenAcked(1, "foo", 0, !kFin,
-                                                         &delegate);
+  connection_.SendStreamDataWithString(1, "foo", 0, !kFin, delegate.get());
 
   // Ack packet gets dropped, but we receive an FEC packet that covers it.
   // Should recover the Ack packet and trigger the notification callback.
   QuicFrames frames;
 
-  QuicAckFrame ack_frame(1, QuicTime::Zero(), 0);
+  QuicAckFrame ack_frame = InitAckFrame(1, 0);
   frames.push_back(QuicFrame(&ack_frame));
 
   // Dummy stream frame to satisfy expectations set elsewhere.
   frames.push_back(QuicFrame(&frame1_));
 
   QuicPacketHeader ack_header;
-  ack_header.public_header.guid = guid_;
+  ack_header.public_header.connection_id = connection_id_;
   ack_header.public_header.reset_flag = false;
   ack_header.public_header.version_flag = false;
   ack_header.entropy_flag = !kEntropyFlag;
@@ -3312,9 +3884,10 @@ class MockQuicConnectionDebugVisitor
   MOCK_METHOD1(OnFrameAddedToPacket,
                void(const QuicFrame&));
 
-  MOCK_METHOD4(OnPacketSent,
+  MOCK_METHOD5(OnPacketSent,
                void(QuicPacketSequenceNumber,
                     EncryptionLevel,
+                    TransmissionType,
                     const QuicEncryptedPacket&,
                     WriteResult));
 
@@ -3342,6 +3915,9 @@ class MockQuicConnectionDebugVisitor
   MOCK_METHOD1(OnCongestionFeedbackFrame,
                void(const QuicCongestionFeedbackFrame&));
 
+  MOCK_METHOD1(OnStopWaitingFrame,
+               void(const QuicStopWaitingFrame&));
+
   MOCK_METHOD1(OnRstStreamFrame,
                void(const QuicRstStreamFrame&));
 
@@ -3368,6 +3944,53 @@ TEST_P(QuicConnectionTest, OnPacketHeaderDebugVisitor) {
   connection_.OnPacketHeader(header);
 }
 
+TEST_P(QuicConnectionTest, Pacing) {
+  ValueRestore<bool> old_flag(&FLAGS_enable_quic_pacing, true);
+
+  TestConnection server(connection_id_, IPEndPoint(), helper_.get(),
+                        writer_.get(), true, version(),
+                        kDefaultFlowControlSendWindow);
+  TestConnection client(connection_id_, IPEndPoint(), helper_.get(),
+                        writer_.get(), false, version(),
+                        kDefaultFlowControlSendWindow);
+  EXPECT_TRUE(client.sent_packet_manager().using_pacing());
+  EXPECT_FALSE(server.sent_packet_manager().using_pacing());
+}
+
+TEST_P(QuicConnectionTest, ControlFramesInstigateAcks) {
+  EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
+
+  // Send a WINDOW_UPDATE frame.
+  QuicWindowUpdateFrame window_update;
+  window_update.stream_id = 3;
+  window_update.byte_offset = 1234;
+  EXPECT_CALL(visitor_, OnWindowUpdateFrames(_));
+  ProcessFramePacket(QuicFrame(&window_update));
+
+  // Ensure that this has caused the ACK alarm to be set.
+  QuicAlarm* ack_alarm = QuicConnectionPeer::GetAckAlarm(&connection_);
+  EXPECT_TRUE(ack_alarm->IsSet());
+
+  // Cancel alarm, and try again with BLOCKED frame.
+  ack_alarm->Cancel();
+  QuicBlockedFrame blocked;
+  blocked.stream_id = 3;
+  EXPECT_CALL(visitor_, OnBlockedFrames(_));
+  ProcessFramePacket(QuicFrame(&blocked));
+  EXPECT_TRUE(ack_alarm->IsSet());
+}
+
+TEST_P(QuicConnectionTest, InvalidFlowControlWindow) {
+  ValueRestore<bool> old_flag(&FLAGS_enable_quic_pacing, true);
+
+  const uint32 kSmallerFlowControlWindow = kDefaultFlowControlSendWindow - 1;
+  TestConnection connection(connection_id_, IPEndPoint(), helper_.get(),
+                            writer_.get(), true, version(),
+                            kSmallerFlowControlWindow);
+  EXPECT_EQ(kDefaultFlowControlSendWindow,
+            connection.max_flow_control_receive_window_bytes());
+}
+
 }  // namespace
 }  // namespace test
 }  // namespace net