<commit_before>package cache

import (
	"bytes"
	"encoding/binary"
	"io"
	"net"
	"testing"
	"time"

	"github.com/lomik/go-carbon/logging"
	"github.com/lomik/go-carbon/points"
	"github.com/stretchr/testify/assert"
)

const sampleCacheQuery = "\x00\x00\x00Y\x80\x02}q\x01(U\x06metricq\x02U,carbon.agents.carbon_agent_server.cache.sizeq\x03U\x04typeq\x04U\x0bcache-queryq\x05u."
const sampleCacheQuery2 = "\x00\x00\x00Y\x80\x02}q\x01(U\x06metricq\x02U,carbon.agents.carbon_agent_server.param.sizeq\x03U\x04typeq\x04U\x0bcache-queryq\x05u."

func TestCarbonlinkRead(t *testing.T) {
	assert := assert.New(t)

	reader := bytes.NewReader([]byte(sampleCacheQuery))

	reqData, err := ReadCarbonlinkRequest(reader)
	assert.NoError(err)

	req, err := ParseCarbonlinkRequest(reqData)

	assert.NoError(err)
	assert.NotNil(req)
	assert.Equal("cache-query", req.Type)
	assert.Equal("carbon.agents.carbon_agent_server.cache.size", req.Metric)
}

func TestCarbonlink(t *testing.T) {
	assert := assert.New(t)

	cache := New()
	cache.Start()
	cache.SetOutputChanSize(0)

	msg1 := points.OnePoint(
		"carbon.agents.carbon_agent_server.cache.size",
		42.17,
		1422797285,
	)

	msg2 := points.OnePoint(
		"carbon.agents.carbon_agent_server.param.size",
		-42.14,
		1422797267,
	)

	msg3 := points.OnePoint(
		"carbon.agents.carbon_agent_server.param.size",
		15,
		1422795966,
	)

	cache.In() <- msg1
	cache.In() <- msg2
	cache.In() <- msg3

	defer cache.Stop()

	addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
	assert.NoError(err)
<commit_msg>Small delay in carbonlink test<commit_after>package cache

import (
	"bytes"
	"encoding/binary"
	"io"
	"net"
	"testing"
	"time"

	"github.com/lomik/go-carbon/logging"
	"github.com/lomik/go-carbon/points"
	"github.com/stretchr/testify/assert"
)

const sampleCacheQuery = "\x00\x00\x00Y\x80\x02}q\x01(U\x06metricq\x02U,carbon.agents.carbon_agent_server.cache.sizeq\x03U\x04typeq\x04U\x0bcache-queryq\x05u."
const sampleCacheQuery2 = "\x00\x00\x00Y\x80\x02}q\x01(U\x06metricq\x02U,carbon.agents.carbon_agent_server.param.sizeq\x03U\x04typeq\x04U\x0bcache-queryq\x05u."

func TestCarbonlinkRead(t *testing.T) {
	assert := assert.New(t)

	reader := bytes.NewReader([]byte(sampleCacheQuery))

	reqData, err := ReadCarbonlinkRequest(reader)
	assert.NoError(err)

	req, err := ParseCarbonlinkRequest(reqData)

	assert.NoError(err)
	assert.NotNil(req)
	assert.Equal("cache-query", req.Type)
	assert.Equal("carbon.agents.carbon_agent_server.cache.size", req.Metric)
}

func TestCarbonlink(t *testing.T) {
	assert := assert.New(t)

	cache := New()
	cache.Start()
	cache.SetOutputChanSize(0)

	msg1 := points.OnePoint(
		"carbon.agents.carbon_agent_server.cache.size",
		42.17,
		1422797285,
	)

	msg2 := points.OnePoint(
		"carbon.agents.carbon_agent_server.param.size",
		-42.14,
		1422797267,
	)

	msg3 := points.OnePoint(
		"carbon.agents.carbon_agent_server.param.size",
		15,
		1422795966,
	)

	cache.In() <- msg1
	cache.In() <- msg2
	cache.In() <- msg3

	defer cache.Stop()

	time.Sleep(50 * time.Millisecond)

	addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
	assert.NoError(err)

{'fraction_non_alphanumeric': 0.15846994535519127, 'fraction_numerical': 0.06428801028608165, 'mean_word_length': 4.74169741697417, 'pattern_counts': {'":': 0, '<': 9, '<?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': '2065199', 'n_tokens_mistral': 1474, 'n_tokens_neox': 1290, 'n_words': 156}