<commit_before>package rdbtools

import (
	"bufio"
	"bytes"
	"encoding/binary"
	"io"
	"math"
	"strings"
	"testing"
	"time"
)

func mustParse(t *testing.T, p *Parser, r io.Reader) {
	err := p.Parse(r)
	if err != nil {
		t.Fatalf("Error while parsing; err=%s", err)
	}
}

func TestReadMagicString(t *testing.T) {
	var buffer bytes.Buffer

	br := bufio.NewWriter(&buffer)
	br.WriteString("REDIS")
	br.Flush()

	err := readMagicString(bufio.NewReader(&buffer))
	ok(t, err)

	// No data
	buffer.Reset()

	err = readMagicString(bufio.NewReader(&buffer))
	equals(t, io.EOF, err)

	// Not enough data
	buffer.Reset()
	br.WriteString("FOO")
	br.Flush()

	err = readMagicString(bufio.NewReader(&buffer))
	equals(t, ErrInvalidMagicString, err)

	// Invalid data<commit_msg>Close the channels if there is an error so that there's no deadlock<commit_after>package rdbtools

import (
	"bufio"
	"bytes"
	"encoding/binary"
	"io"
	"math"
	"strings"
	"testing"
	"time"
)

func mustParse(t *testing.T, p *Parser, r io.Reader) {
	err := p.Parse(r)
	if err != nil {
		p.ctx.closeChannels()
		t.Fatalf("Error while parsing; err=%s", err)
	}
}

func TestReadMagicString(t *testing.T) {
	var buffer bytes.Buffer

	br := bufio.NewWriter(&buffer)
	br.WriteString("REDIS")
	br.Flush()

	err := readMagicString(bufio.NewReader(&buffer))
	ok(t, err)

	// No data
	buffer.Reset()

	err = readMagicString(bufio.NewReader(&buffer))
	equals(t, io.EOF, err)

	// Not enough data
	buffer.Reset()
	br.WriteString("FOO")
	br.Flush()

	err = readMagicString(bufio.NewReader(&buffer))
	equals(t, ErrInvalidMagicString, err)

	// Invalid data
{'fraction_non_alphanumeric': 0.15792759051186017, 'fraction_numerical': 0.0, 'mean_word_length': 4.30794701986755, 'pattern_counts': {'":': 0, '<': 3, '<?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': '601946', 'n_tokens_mistral': 651, 'n_tokens_neox': 622, 'n_words': 138}