<commit_before>package test

import (
	"io"
	"net"
	"testing"
	"time"

	"github.com/nbusy/devastator"
	"github.com/nbusy/neptulon/jsonrpc"
)

// client cert for testing
var (
	// host = client.127.0.0.1, cn = client.127.0.0.1, org = devastator
	clientCert = `-----BEGIN CER<KEY>`

	// RSA 3248 bits
	clientKey = `-----<KEY>END RSA PRIVATE KEY-----`

	clientCertBytes = []byte(clientCert)
	clientKeyBytes  = []byte(clientKey)
)

// ClientHelper is a JSON-RPC client wrapper with built-in error logging for testing.
type ClientHelper struct {
	client    *jsonrpc.Client
	testing   *testing.T
	cert, key []byte
}

// NewClientHelper creates a new JSON-RPC client wrapper which has built-in error logging for testing.
func NewClientHelper(t *testing.T) *ClientHelper {
	if testing.Short() {
		t.Skip("Skipping integration test in short testing mode")
	}

	return &ClientHelper{testing: t}
}

// DefaultCert attaches default test client certificate to the connection.
func (c *ClientHelper) DefaultCert() *ClientHelper {
	c.cert = clientCertBytes
	c.key = clientKeyBytes
	return c
}

// Cert attaches given PEM encoded client certificate to the connection.
func (c *ClientHelper) Cert(cert, key []byte) *ClientHelper {
	c.cert = cert
	c.key = key
	return c
}

// Dial initiates a connection.
func (c *ClientHelper) Dial() *ClientHelper {
	addr := "127.0.0.1:" + devastator.Conf.App.Port

	// retry connect in case we're operating on a very slow machine
	for i := 0; i <= 5; i++ {
		client, err := jsonrpc.Dial(addr, caCertBytes, c.cert, c.key, false) // no need for debug mode on client conn
		if err != nil {
			if operr, ok := err.(*net.OpError); ok && operr.Op == "dial" && operr.Err.Error() == "connection refused" {
				time.Sleep(time.Millisecond * 50)
				continue
			} else if i == 5 {
				c.testing.Fatalf("Cannot connect to server address %v after 5 retries, with error: %v", addr, err)
			}
			c.testing.Fatalf("Cannot connect to server address %v with error: %v", addr, err)
		}

		if i != 0 {
			c.testing.Logf("WARNING: it took %v retries to connect to the server, which might indicate code issues or slow machine.", i)
		}

		c.client = client
		return c
	}

	return c
}

// WriteRequest writes a request to a client connection with error logging for testing.
func (c *ClientHelper) WriteRequest(method string, params interface{}) (reqID string) {
	id, err := c.client.WriteRequest(method, params)
	if err != nil {
		c.testing.Fatal("Failed to write request to client connection:", err)
	}
	return id
}

// ReadMsg reads a JSON-RPC message from a client connection with error logging for testing.
func (c *ClientHelper) ReadMsg() (req *jsonrpc.Request, res *jsonrpc.Response, not *jsonrpc.Notification) {
	req, res, not, err := c.client.ReadMsg()
	if err == io.EOF {
		return nil, nil, nil
	}
	if err != nil {
		c.testing.Fatal("Failed to read message from client connection:", err)
	}

	return
}

// Close closes a client connection.
func (c *ClientHelper) Close() {
	if err := c.client.Close(); err != nil {
		c.testing.Fatal("Failed to close client connection:", err)
	}
}
<commit_msg>add conn close note<commit_after>package test

import (
	"io"
	"net"
	"testing"
	"time"

	"github.com/nbusy/devastator"
	"github.com/nbusy/neptulon/jsonrpc"
)

// client cert for testing
var (
	// host = client.127.0.0.1, cn = client.127.0.0.1, org = devastator
	clientCert = `-----<KEY>
-----END CERTIFICATE-----`

	// RSA 3248 bits
	clientKey = `-----<KEY>`

	clientCertBytes = []byte(clientCert)
	clientKeyBytes  = []byte(clientKey)
)

// ClientHelper is a JSON-RPC client wrapper with built-in error logging for testing.
type ClientHelper struct {
	client    *jsonrpc.Client
	testing   *testing.T
	cert, key []byte
}

// NewClientHelper creates a new JSON-RPC client wrapper which has built-in error logging for testing.
func NewClientHelper(t *testing.T) *ClientHelper {
	if testing.Short() {
		t.Skip("Skipping integration test in short testing mode")
	}

	return &ClientHelper{testing: t}
}

// DefaultCert attaches default test client certificate to the connection.
func (c *ClientHelper) DefaultCert() *ClientHelper {
	c.cert = clientCertBytes
	c.key = clientKeyBytes
	return c
}

// Cert attaches given PEM encoded client certificate to the connection.
func (c *ClientHelper) Cert(cert, key []byte) *ClientHelper {
	c.cert = cert
	c.key = key
	return c
}

// Dial initiates a connection.
func (c *ClientHelper) Dial() *ClientHelper {
	addr := "127.0.0.1:" + devastator.Conf.App.Port

	// retry connect in case we're operating on a very slow machine
	for i := 0; i <= 5; i++ {
		client, err := jsonrpc.Dial(addr, caCertBytes, c.cert, c.key, false) // no need for debug mode on client conn
		if err != nil {
			if operr, ok := err.(*net.OpError); ok && operr.Op == "dial" && operr.Err.Error() == "connection refused" {
				time.Sleep(time.Millisecond * 50)
				continue
			} else if i == 5 {
				c.testing.Fatalf("Cannot connect to server address %v after 5 retries, with error: %v", addr, err)
			}
			c.testing.Fatalf("Cannot connect to server address %v with error: %v", addr, err)
		}

		if i != 0 {
			c.testing.Logf("WARNING: it took %v retries to connect to the server, which might indicate code issues or slow machine.", i)
		}

		c.client = client
		return c
	}

	return c
}

// WriteRequest writes a request to a client connection with error logging for testing.
func (c *ClientHelper) WriteRequest(method string, params interface{}) (reqID string) {
	id, err := c.client.WriteRequest(method, params)
	if err != nil {
		c.testing.Fatal("Failed to write request to client connection:", err)
	}
	return id
}

// ReadMsg reads a JSON-RPC message from a client connection with error logging for testing.
// Returns nil if connection was closed.
func (c *ClientHelper) ReadMsg() (req *jsonrpc.Request, res *jsonrpc.Response, not *jsonrpc.Notification) {
	req, res, not, err := c.client.ReadMsg()
	if err == io.EOF {
		return nil, nil, nil
	}
	if err != nil {
		c.testing.Fatal("Failed to read message from client connection:", err)
	}

	return
}

// Close closes a client connection.
func (c *ClientHelper) Close() {
	if err := c.client.Close(); err != nil {
		c.testing.Fatal("Failed to close client connection:", err)
	}
}

{'fraction_non_alphanumeric': 0.12860808210391275, 'fraction_numerical': 0.009300833867864015, 'mean_word_length': 3.562545720555962, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, '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': '1511587', 'n_tokens_mistral': 2151, 'n_tokens_neox': 2021, 'n_words': 715}