<commit_before>package io.termd.core.term;

import io.termd.core.Handler;
import io.termd.core.Provider;
import io.termd.core.readline.RequestContext;
import io.termd.core.telnet.TelnetConnection;
import io.termd.core.telnet.TelnetHandler;
import io.termd.core.telnet.TelnetTestBase;
import io.termd.core.telnet.vertx.VertxTermConnection;
import org.apache.commons.net.telnet.EchoOptionHandler;
import org.apache.commons.net.telnet.TelnetClient;
import org.junit.Test;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class ReadlineTermTest extends TelnetTestBase {

  @Test
  public void testGetPrompt() throws Exception {
    final AtomicInteger connectionCount = new AtomicInteger();
    final AtomicInteger requestCount = new AtomicInteger();
    server(new Provider<TelnetHandler>() {
      @Override
      public TelnetHandler provide() {
        connectionCount.incrementAndGet();
        return new VertxTermConnection() {
          @Override
          protected void onOpen(TelnetConnection conn) {
            super.onOpen(conn);
            new ReadlineTerm(this, new Handler<RequestContext>() {
              @Override
              public void handle(RequestContext event) {
                requestCount.incrementAndGet();
                event.end();
              }
            });
          }
        };
      }
    });
    client = new TelnetClient();
    client.addOptionHandler(new EchoOptionHandler(false, false, true, true));
    client.connect("localhost", 4000);
    byte[] bytes = new byte[100];
    assertEquals(2, client.getInputStream().read(bytes));
    assertEquals("% ", new String(bytes, 0, 2));
    assertEquals(1, connectionCount.get());
    assertEquals(0, requestCount.get());
  }

  @Test
  public void testRequestWrite() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    server(new Provider<TelnetHandler>() {
      @Override
      public TelnetHandler provide() {
        return new VertxTermConnection() {
          @Override
          protected void onOpen(TelnetConnection conn) {
            super.onOpen(conn);
            new ReadlineTerm(this, new Handler<RequestContext>() {
              @Override
              public void handle(RequestContext event) {
                event.write("hello");
                event.end();
                latch.countDown();
              }
            });
          }
        };
      }
    });
    client = new TelnetClient();
    client.addOptionHandler(new EchoOptionHandler(false, false, true, true));
    client.connect("localhost", 4000);
    byte[] bytes = new byte[100];
    assertEquals(2, client.getInputStream().read(bytes));
    assertEquals("% ", new String(bytes, 0, 2));
    client.getOutputStream().write('\r');
    client.getOutputStream().flush();
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(9, client.getInputStream().read(bytes));
    assertEquals("\r\nhello% ", new String(bytes, 0, 9));
  }

  @Test
  public void testAsyncEndRequest() throws Exception {
    final ArrayBlockingQueue<RequestContext> requestContextWait = new ArrayBlockingQueue<>(1);
    server(new Provider<TelnetHandler>() {
      @Override
      public TelnetHandler provide() {
        return new VertxTermConnection() {
          @Override
          protected void onOpen(TelnetConnection conn) {
            super.onOpen(conn);
            new ReadlineTerm(this, new Handler<RequestContext>() {
              @Override
              public void handle(RequestContext event) {
                requestContextWait.add(event);
              }
            });
          }
        };
      }
    });
    client = new TelnetClient();
    client.addOptionHandler(new EchoOptionHandler(false, false, true, true));
    client.connect("localhost", 4000);
    byte[] bytes = new byte[100];
    assertEquals(2, client.getInputStream().read(bytes));
    assertEquals("% ", new String(bytes, 0, 2));
    client.getOutputStream().write('\r');
    client.getOutputStream().flush();
    RequestContext requestContext = assertNotNull(requestContextWait.poll(10, TimeUnit.SECONDS));
    assertEquals(2, client.getInputStream().read(bytes));
    assertEquals("\r\n", new String(bytes, 0, 2));
    requestContext.end();
    assertEquals(2, client.getInputStream().read(bytes));
    assertEquals("% ", new String(bytes, 0, 2));
  }
}<commit_msg>Refactor a bit ReadlineTermTest<commit_after>package io.termd.core.term;

import io.termd.core.Handler;
import io.termd.core.Provider;
import io.termd.core.readline.RequestContext;
import io.termd.core.telnet.TelnetConnection;
import io.termd.core.telnet.TelnetHandler;
import io.termd.core.telnet.TelnetTestBase;
import io.termd.core.telnet.vertx.VertxTermConnection;
import org.apache.commons.net.telnet.EchoOptionHandler;
import org.apache.commons.net.telnet.TelnetClient;
import org.junit.Test;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class ReadlineTermTest extends TelnetTestBase {

  protected final void assertConnect() throws Exception {
    client = new TelnetClient();
    client.addOptionHandler(new EchoOptionHandler(false, false, true, true));
    client.connect("localhost", 4000);
  }

  protected final String assertRead(int length) throws Exception {
    byte[] bytes = new byte[length];
    while (length > 0) {
      int i = client.getInputStream().read(bytes, bytes.length - length, length);
      if (i == -1) {
        throw new AssertionError();
      }
      length -= i;
    }
    return new String(bytes, 0, bytes.length, "UTF-8");
  }

  protected final void assertWrite(String s) throws Exception {
    client.getOutputStream().write(s.getBytes("UTF-8"));
  }

  @Test
  public void testGetPrompt() throws Exception {
    final AtomicInteger connectionCount = new AtomicInteger();
    final AtomicInteger requestCount = new AtomicInteger();
    server(new Provider<TelnetHandler>() {
      @Override
      public TelnetHandler provide() {
        connectionCount.incrementAndGet();
        return new VertxTermConnection() {
          @Override
          protected void onOpen(TelnetConnection conn) {
            super.onOpen(conn);
            new ReadlineTerm(this, new Handler<RequestContext>() {
              @Override
              public void handle(RequestContext event) {
                requestCount.incrementAndGet();
                event.end();
              }
            });
          }
        };
      }
    });
    assertConnect();
    assertEquals("% ", assertRead(2));
    assertEquals(1, connectionCount.get());
    assertEquals(0, requestCount.get());
  }

  @Test
  public void testRequestWrite() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    server(new Provider<TelnetHandler>() {
      @Override
      public TelnetHandler provide() {
        return new VertxTermConnection() {
          @Override
          protected void onOpen(TelnetConnection conn) {
            super.onOpen(conn);
            new ReadlineTerm(this, new Handler<RequestContext>() {
              @Override
              public void handle(RequestContext event) {
                event.write("hello");
                event.end();
                latch.countDown();
              }
            });
          }
        };
      }
    });
    assertConnect();
    assertEquals("% ", assertRead(2));
    assertWrite("\r");
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals("\r\nhello% ", assertRead(9));
  }

  @Test
  public void testAsyncEndRequest() throws Exception {
    final ArrayBlockingQueue<RequestContext> requestContextWait = new ArrayBlockingQueue<>(1);
    server(new Provider<TelnetHandler>() {
      @Override
      public TelnetHandler provide() {
        return new VertxTermConnection() {
          @Override
          protected void onOpen(TelnetConnection conn) {
            super.onOpen(conn);
            new ReadlineTerm(this, new Handler<RequestContext>() {
              @Override
              public void handle(RequestContext event) {
                requestContextWait.add(event);
              }
            });
          }
        };
      }
    });
    assertConnect();
    assertEquals("% ", assertRead(2));
    assertWrite("\r");
    RequestContext requestContext = assertNotNull(requestContextWait.poll(10, TimeUnit.SECONDS));
    assertEquals("\r\n", assertRead(2));
    requestContext.end();
    assertEquals("% ", assertRead(2));
  }
}
{'fraction_non_alphanumeric': 0.1202819463392451, 'fraction_numerical': 0.007958162801273306, 'mean_word_length': 2.819800260529744, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 28, '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': '5827450', 'n_tokens_mistral': 2476, 'n_tokens_neox': 2363, 'n_words': 498}