<commit_before>package cmd

import (
	"bytes"
	"context"
	"io"
	"net"
	"strconv"
	"testing"
	"time"
)

func listen(port int, t *testing.T) net.Listener {
	l, err := net.Listen("tcp", "localhost:"+strconv.Itoa(port))
	if err != nil {
		t.Log(err)
		t.Skip()
	}
	return l
}

func connect(port int, t *testing.T) net.Conn {
	conn, err := net.Dial("tcp", "localhost:"+strconv.Itoa(port))
	if err != nil {
		t.Fatal(err)
	}
	return conn
}

func TestServer(t *testing.T) {
	l := listen(15555, t)
	handler := func(ctx context.Context, conn net.Conn) {
		conn.Write([]byte{'h', 'e', 'l', 'l', 'o'})
		<-ctx.Done()

		// uncomment the next line delays the test
		//time.Sleep(2 * time.Second)
	}

	env := NewEnvironment()
	s := &Server{
		Handler: handler,
		Env:     env,
	}
	s.Serve(l)

	conn := connect(15555, t)
	buf := make([]byte, 5)
	_, err := io.ReadFull(conn, buf)
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(buf, []byte{'h', 'e', 'l', 'l', 'o'}) {
		t.Error(`!bytes.Equal(buf, []byte{'h', 'e', 'l', 'l', 'o'})`)
	}

	env.Stop(nil)
	err = env.Wait()
	if err != nil {
		t.Error(err)
	}

	if s.TimedOut() {
		t.Error(`s.TimedOut()`)
	}
}

func TestServerTimeout(t *testing.T) {
	l := listen(15556, t)
	handler := func(ctx context.Context, conn net.Conn) {
		conn.Write([]byte{'h', 'e', 'l', 'l', 'o'})
		<-ctx.Done()
		time.Sleep(1 * time.Second)
	}

	env := NewEnvironment()
	s := &Server{
		Handler:         handler,
		ShutdownTimeout: 100 * time.Millisecond,
		Env:             env,
	}
	s.Serve(l)

	conn := connect(15556, t)
	buf := make([]byte, 5)
	_, err := io.ReadFull(conn, buf)
	if err != nil {<commit_msg>Cosmetic changes.<commit_after>package cmd

import (
	"bytes"
	"context"
	"io"
	"net"
	"strconv"
	"testing"
	"time"
)

func listen(port int, t *testing.T) net.Listener {
	l, err := net.Listen("tcp", "localhost:"+strconv.Itoa(port))
	if err != nil {
		t.Skip(err)
	}
	return l
}

func connect(port int, t *testing.T) net.Conn {
	conn, err := net.Dial("tcp", "localhost:"+strconv.Itoa(port))
	if err != nil {
		t.Fatal(err)
	}
	return conn
}

func TestServer(t *testing.T) {
	t.Parallel()

	l := listen(15555, t)
	handler := func(ctx context.Context, conn net.Conn) {
		conn.Write([]byte{'h', 'e', 'l', 'l', 'o'})
		<-ctx.Done()

		// uncomment the next line delays the test
		//time.Sleep(2 * time.Second)
	}

	env := NewEnvironment()
	s := &Server{
		Handler: handler,
		Env:     env,
	}
	s.Serve(l)

	conn := connect(15555, t)
	buf := make([]byte, 5)
	_, err := io.ReadFull(conn, buf)
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(buf, []byte{'h', 'e', 'l', 'l', 'o'}) {
		t.Error(`!bytes.Equal(buf, []byte{'h', 'e', 'l', 'l', 'o'})`)
	}

	env.Stop(nil)
	err = env.Wait()
	if err != nil {
		t.Error(err)
	}

	if s.TimedOut() {
		t.Error(`s.TimedOut()`)
	}
}

func TestServerTimeout(t *testing.T) {
	t.Parallel()

	l := listen(15556, t)
	handler := func(ctx context.Context, conn net.Conn) {
		conn.Write([]byte{'h', 'e', 'l', 'l', 'o'})
		<-ctx.Done()
		time.Sleep(1 * time.Second)
	}

	env := NewEnvironment()
	s := &Server{
		Handler:         handler,
		ShutdownTimeout: 100 * time.Millisecond,
		Env:             env,
	}
	s.Serve(l)

	conn := connect(15556, t)
	buf := make([]byte, 5)
	_, err := io.ReadFull(conn, buf)
	if err != nil {
{'fraction_non_alphanumeric': 0.21739130434782608, 'fraction_numerical': 0.01653398652786283, 'mean_word_length': 3.2817824377457403, 'pattern_counts': {'":': 0, '<': 7, '<?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': '6836444', 'n_tokens_mistral': 1493, 'n_tokens_neox': 1393, 'n_words': 316}