<commit_before>package org.realityforge.replicant.server.transport;

import org.realityforge.replicant.server.ChangeSet;
import org.testng.annotations.Test;
import static org.testng.Assert.*;

public class PacketQueueTest
{
  @Test
  public void basicFlow()
  {
    final PacketQueue queue = new PacketQueue();
    assertEquals( queue.getLastSequenceAcked(), 0 );
    assertEquals( queue.size(), 0 );

    final Packet p1 = queue.addPacket( null, null, new ChangeSet() );
    final Packet p2 = queue.addPacket( null, null, new ChangeSet() );
    final Packet p3 = queue.addPacket( null, null, new ChangeSet() );
    final Packet p4 = queue.addPacket( null, null, new ChangeSet() );

    assertEquals( queue.size(), 4 );

    assertEquals( queue.getPacket( 1 ), p1 );
    assertEquals( queue.getPacket( 2 ), p2 );
    assertEquals( queue.getPacket( 3 ), p3 );
    assertEquals( queue.getPacket( 4 ), p4 );

    assertEquals( queue.nextPacketToProcess().getSequence(), 1 );
    assertEquals( queue.nextPacketToProcess(), queue.getPacket( 1 ) );
    assertEquals( queue.nextPacketToProcess().getSequence(), 1 );

    queue.ack( 0 );
    assertEquals( queue.nextPacketToProcess().getSequence(), 1 );

    queue.ack( 1 );
    assertEquals( queue.size(), 3 );
    assertEquals( queue.nextPacketToProcess().getSequence(), 2 );
    assertEquals( queue.nextPacketToProcess(), queue.getPacket( 2 ) );

    assertEquals( queue.getPacket( 3 ).getSequence(), 3 );
    assertEquals( queue.getPacket( 4 ).getSequence(), 4 );
  }

  @Test
  public void ack()
  {
    final PacketQueue queue = new PacketQueue();
    assertEquals( queue.getLastSequenceAcked(), 0 );
    assertEquals( queue.size(), 0 );

    queue.addPacket( null, null, new ChangeSet() );
    queue.addPacket( null, null, new ChangeSet() );
    queue.addPacket( null, null, new ChangeSet() );
    queue.addPacket( null, null, new ChangeSet() );

    assertEquals( queue.size(), 4 );

    queue.ack( 0 );
    assertEquals( queue.getLastSequenceAcked(), 0 );
    queue.ack( 1 );
    assertEquals( queue.getLastSequenceAcked(), 1 );
    assertEquals( queue.size(), 3 );
    queue.ack( 0 );
    assertEquals( queue.getLastSequenceAcked(), 1 );
    assertEquals( queue.size(), 3 );
    queue.ack( 4 );
    assertEquals( queue.getLastSequenceAcked(), 4 );
    assertEquals( queue.size(), 0 );

    try
    {
      queue.ack( 5 );
      fail( "Attempted to ack future sequence" );
    }
    catch ( final IllegalStateException ise )
    {
      //Ignored
    }
  }
}
<commit_msg>Simplify test to use requestId to track packets as that is how client code also does it<commit_after>package org.realityforge.replicant.server.transport;

import javax.annotation.Nonnull;
import org.realityforge.replicant.server.ChangeSet;
import org.testng.annotations.Test;
import static org.testng.Assert.*;

public class PacketQueueTest
{
  @Test
  public void basicFlow()
  {
    final PacketQueue queue = new PacketQueue();
    assertEquals( queue.getLastSequenceAcked(), 0 );
    assertEquals( queue.size(), 0 );

    queue.addPacket( 21, null, new ChangeSet() );
    queue.addPacket( 22, null, new ChangeSet() );
    queue.addPacket( 23, null, new ChangeSet() );
    queue.addPacket( 24, null, new ChangeSet() );

    assertEquals( queue.size(), 4 );

    assertEquals( getPacketBySequence( queue, 1 ).getRequestId(), (Integer) 21 );
    assertEquals( getPacketBySequence( queue, 2 ).getRequestId(), (Integer) 22 );
    assertEquals( getPacketBySequence( queue, 3 ).getRequestId(), (Integer) 23 );
    assertEquals( getPacketBySequence( queue, 4 ).getRequestId(), (Integer) 24 );

    assertEquals( queue.nextPacketToProcess(), getPacketBySequence( queue, 1 ) );

    queue.ack( 0 );

    assertEquals( queue.nextPacketToProcess(), getPacketBySequence( queue, 1 ) );

    queue.ack( 1 );
    assertEquals( queue.size(), 3 );
    assertEquals( queue.nextPacketToProcess(), getPacketBySequence( queue, 2 ) );

    assertEquals( getPacketBySequence( queue, 3 ).getSequence(), 3 );
    assertEquals( getPacketBySequence( queue, 4 ).getSequence(), 4 );
  }

  @Nonnull
  private Packet getPacketBySequence( @Nonnull final PacketQueue queue, final int sequence )
  {
    final Packet packet = queue.findPacketBySequence( sequence );
    assertNotNull( packet );
    return packet;
  }

  @Test
  public void ack()
  {
    final PacketQueue queue = new PacketQueue();
    assertEquals( queue.getLastSequenceAcked(), 0 );
    assertEquals( queue.size(), 0 );

    queue.addPacket( null, null, new ChangeSet() );
    queue.addPacket( null, null, new ChangeSet() );
    queue.addPacket( null, null, new ChangeSet() );
    queue.addPacket( null, null, new ChangeSet() );

    assertEquals( queue.size(), 4 );

    queue.ack( 0 );
    assertEquals( queue.getLastSequenceAcked(), 0 );
    queue.ack( 1 );
    assertEquals( queue.getLastSequenceAcked(), 1 );
    assertEquals( queue.size(), 3 );
    queue.ack( 0 );
    assertEquals( queue.getLastSequenceAcked(), 1 );
    assertEquals( queue.size(), 3 );
    queue.ack( 4 );
    assertEquals( queue.getLastSequenceAcked(), 4 );
    assertEquals( queue.size(), 0 );

    try
    {
      queue.ack( 5 );
      fail( "Attempted to ack future sequence" );
    }
    catch ( final IllegalStateException ise )
    {
      //Ignored
    }
  }
}

{'fraction_non_alphanumeric': 0.1355325164938737, 'fraction_numerical': 0.01715362865221489, 'mean_word_length': 3.5428082191780823, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3320334', 'n_tokens_mistral': 1675, 'n_tokens_neox': 1540, 'n_words': 427}