<commit_before>package cluster

import (
	"crypto/rand"
	"encoding/hex"
	. "testing"
	"time"

	"github.com/levenlabs/golib/testutil"
	radix "github.com/mediocregopher/radix.v2"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

// slotKeys contains a random key for every slot. Unfortunately I haven't come
// up with a better way to do this than brute force. It takes like 5 seconds on
// my laptop, which isn't terrible.
var slotKeys = func() [numSlots]string {
	var a [numSlots]string
	for {
		k := testutil.RandStr()
		a[Slot(k)] = k

		var notFull bool
		for _, k := range a {
			if k == "" {
				notFull = true
				break
			}
		}

		if !notFull {
			return a
		}
	}
}()

func randStr() string {
	b := make([]byte, 16)
	if _, err := rand.Read(b); err != nil {
		panic(err)
	}
	return hex.EncodeToString(b)
}

func newTestCluster() (*Cluster, *stubCluster) {
	scl := newStubCluster(testTopo)
	c, err := NewCluster(scl.poolFunc(), scl.addrs()...)
	if err != nil {
		panic(err)
	}
	return c, scl
}

func TestClusterSync(t *T) {
	c, scl := newTestCluster()
	assertClusterState := func() {
		assert.Len(t, c.pools, len(testTopo))<commit_msg>make sure to lock in tests, might fix a random panic I was seeing<commit_after>package cluster

import (
	"crypto/rand"
	"encoding/hex"
	. "testing"
	"time"

	"github.com/levenlabs/golib/testutil"
	radix "github.com/mediocregopher/radix.v2"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

// slotKeys contains a random key for every slot. Unfortunately I haven't come
// up with a better way to do this than brute force. It takes like 5 seconds on
// my laptop, which isn't terrible.
var slotKeys = func() [numSlots]string {
	var a [numSlots]string
	for {
		k := testutil.RandStr()
		a[Slot(k)] = k

		var notFull bool
		for _, k := range a {
			if k == "" {
				notFull = true
				break
			}
		}

		if !notFull {
			return a
		}
	}
}()

func randStr() string {
	b := make([]byte, 16)
	if _, err := rand.Read(b); err != nil {
		panic(err)
	}
	return hex.EncodeToString(b)
}

func newTestCluster() (*Cluster, *stubCluster) {
	scl := newStubCluster(testTopo)
	c, err := NewCluster(scl.poolFunc(), scl.addrs()...)
	if err != nil {
		panic(err)
	}
	return c, scl
}

func TestClusterSync(t *T) {
	c, scl := newTestCluster()
	assertClusterState := func() {
		c.RLock()
		defer c.RUnlock()
		assert.Len(t, c.pools, len(testTopo))
{'fraction_non_alphanumeric': 0.14645214521452146, 'fraction_numerical': 0.0033003300330033004, 'mean_word_length': 3.4171220400728597, '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': '2231558', 'n_tokens_mistral': 984, 'n_tokens_neox': 925, 'n_words': 247}