<commit_before>package mockirc

import (
	"bufio"
	"fmt"
	"net"
	"strings"
)

type MockIRC struct {
	Port       int
	ServerName string
	Socket     net.Listener
	ctl        chan bool
	running    bool
	events     map[string]*WhenEvent
	io         *bufio.ReadWriter
}

func New(server string, port int) *MockIRC {
	irc := &MockIRC{}
	if port == 0 {
		irc.Port = 6661
	} else {
		irc.Port = port
	}
	irc.ServerName = server
	irc.events = make(map[string]*WhenEvent)
	return irc
}

func (irc *MockIRC) Start() (chan bool, error) {
	wait := make(chan bool, 1)
	sock, err := net.Listen("tcp", fmt.Sprintf(":%d", irc.Port))
	if err != nil {
		return nil, err
	}
	irc.Socket = sock
	irc.ctl = make(chan bool, 1)
	go func() {
		wait <- true
		close(wait)
		for {
			conn, err := irc.Socket.Accept()
			if err != nil {
				fmt.Println(err)
				return
			}
			go irc.connectionWorker(conn)
		}
	}()
	irc.running = true
	return wait, nil
}

func (irc *MockIRC) Stop() {
	irc.Socket.Close()
}

func (irc *MockIRC) connectionWorker(conn net.Conn) {
	irc.io = bufio.NewReadWriter(
		bufio.NewReader(conn),
		bufio.NewWriter(conn))
	defer conn.Close()
	for {
		msg, err := irc.io.ReadString('\n')
		if err != nil {
			fmt.Println(err)
		}
		irc.handleMessage(msg)
	}
}

func (irc *MockIRC) handleMessage(msg string) {
	msg = strings.TrimSuffix(msg, "\r\n")
	var err error
	if val, ok := irc.events[msg]; ok {
		for _, response := range val.responses {
			_, err = irc.io.WriteString(response + "\r\n")
			if err != nil {
				fmt.Println(err)
				return
			}
			err = irc.io.Flush()
			if err != nil {<commit_msg>Added stupid timeout to see if it fixes tests in 1.3<commit_after>package mockirc

import (
	"bufio"
	"fmt"
	"net"
	"strings"
	"time"
)

type MockIRC struct {
	Port       int
	ServerName string
	Socket     net.Listener
	ctl        chan bool
	running    bool
	events     map[string]*WhenEvent
	io         *bufio.ReadWriter
}

func New(server string, port int) *MockIRC {
	irc := &MockIRC{}
	if port == 0 {
		irc.Port = 6661
	} else {
		irc.Port = port
	}
	irc.ServerName = server
	irc.events = make(map[string]*WhenEvent)
	return irc
}

func (irc *MockIRC) Start() (chan bool, error) {
	wait := make(chan bool, 1)
	sock, err := net.Listen("tcp", fmt.Sprintf(":%d", irc.Port))
	if err != nil {
		return nil, err
	}
	irc.Socket = sock
	irc.ctl = make(chan bool, 1)
	go func() {
		wait <- true
		close(wait)
		for {
			conn, err := irc.Socket.Accept()
			if err != nil {
				fmt.Println(err)
				return
			}
			go irc.connectionWorker(conn)
		}
	}()
	irc.running = true
	return wait, nil
}

func (irc *MockIRC) Stop() {
	irc.Socket.Close()
	<-time.After(time.Second)
}

func (irc *MockIRC) connectionWorker(conn net.Conn) {
	irc.io = bufio.NewReadWriter(
		bufio.NewReader(conn),
		bufio.NewWriter(conn))
	defer conn.Close()
	for {
		msg, err := irc.io.ReadString('\n')
		if err != nil {
			fmt.Println(err)
		}
		irc.handleMessage(msg)
	}
}

func (irc *MockIRC) handleMessage(msg string) {
	msg = strings.TrimSuffix(msg, "\r\n")
	var err error
	if val, ok := irc.events[msg]; ok {
		for _, response := range val.responses {
			_, err = irc.io.WriteString(response + "\r\n")
			if err != nil {
				fmt.Println(err)
				return
			}
			err = irc.io.Flush()
			if err != nil {
{'fraction_non_alphanumeric': 0.1485906862745098, 'fraction_numerical': 0.004901960784313725, 'mean_word_length': 2.9290012033694346, 'pattern_counts': {'":': 2, '<': 6, '<?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': '6018264', 'n_tokens_mistral': 1420, 'n_tokens_neox': 1278, 'n_words': 320}