<commit_before>package measured

import (
	"net"
	"net/http"
	"runtime"
	"sync"
	"sync/atomic"
	"testing"
	"time"

	"github.com/getlantern/testify/assert"
)

func TestReportError(t *testing.T) {
	md, nr := startWithMockReporter()
	defer md.Stop()
	d := md.Dialer(net.Dial, 10*time.Second)
	_, _ = d("tcp", "localhost:9999")
	_, _ = d("tcp", "localhost:9998")
	time.Sleep(100 * time.Millisecond)
	nr.Lock()
	defer nr.Unlock()
	if assert.Equal(t, 2, len(nr.error)) {
		assert.Equal(t, 1, nr.error[Error{"localhost:9999", "connection refused", "dial"}])
		assert.Equal(t, 1, nr.error[Error{"localhost:9998", "connection refused", "dial"}])
	}
}

func TestReportStats(t *testing.T) {
	md, nr := startWithMockReporter()
	defer md.Stop()
	var bytesIn, bytesOut uint64
	var RemoteAddr string

	// start server with byte counting
	l, err := net.Listen("tcp", "127.0.0.1:0")<commit_msg>add test to cover stopped measured instance<commit_after>package measured

import (
	"net"
	"net/http"
	"runtime"
	"sync"
	"sync/atomic"
	"testing"
	"time"

	"github.com/getlantern/testify/assert"
)

func TestReportError(t *testing.T) {
	md, nr := startWithMockReporter()
	defer md.Stop()
	d := md.Dialer(net.Dial, 10*time.Second)
	_, _ = d("tcp", "localhost:9999")
	_, _ = d("tcp", "localhost:9998")
	time.Sleep(100 * time.Millisecond)
	nr.Lock()
	defer nr.Unlock()
	if assert.Equal(t, 2, len(nr.error)) {
		assert.Equal(t, 1, nr.error[Error{"localhost:9999", "connection refused", "dial"}])
		assert.Equal(t, 1, nr.error[Error{"localhost:9998", "connection refused", "dial"}])
	}
}

func TestStop(t *testing.T) {
	md, nr := startWithMockReporter()
	md.Stop()
	d := md.Dialer(net.Dial, 10*time.Second)
	_, _ = d("tcp", "localhost:9999")
	_, _ = d("tcp", "localhost:9998")
	time.Sleep(100 * time.Millisecond)
	nr.Lock()
	defer nr.Unlock()
	assert.Equal(t, 0, len(nr.error), "stopped measured should not submit any metrics")
}

func TestReportStats(t *testing.T) {
	md, nr := startWithMockReporter()
	defer md.Stop()
	var bytesIn, bytesOut uint64
	var RemoteAddr string

	// start server with byte counting
	l, err := net.Listen("tcp", "127.0.0.1:0")
{'fraction_non_alphanumeric': 0.1810912511759172, 'fraction_numerical': 0.03762935089369708, 'mean_word_length': 4.748648648648649, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '181918', 'n_tokens_mistral': 887, 'n_tokens_neox': 821, 'n_words': 176}