<commit_before>package gomcketama

import (
	"fmt"
	"testing"

	mc "github.com/bradfitz/gomemcache/memcache"
)

func TestNodeKeys(t *testing.T) {
	for h, nKeys := range nodeKeys {
		_, keys, err := prepServer(h)
		if err != nil {
			t.Fatalf("prep server failed: %s", err)
		}

		if len(keys) != len(nKeys) {
			t.Fatalf("generated keys length==%d, expected %d",
				len(keys),
				len(nKeys))
		}

		for i := 0; i < len(keys); i++ {
			if keys[i] != nKeys[i] {
				t.Fatalf("invalid key: got %s, expected %s",
					keys[i],
					nKeys[i])
			}
		}
	}
}

func TestKVToNode(t *testing.T) {
	ks := &KetamaServerSelector{}

	for h := range nodeKeys {
		ks.AddServer(h)
	}

	for k, h := range kvToNode {
		a, err := ks.PickServer(k)
		if err != nil {
			t.Fatalf("got error instead of server: %s", err)
		}

		ae, err := lookup(h)
		if err != nil {
			t.Fatalf("expected server lookup failed: %s", err)
		}

		if a.String() != ae.String() {
			t.Fatalf("for key %s, got server %s, expected %s: %s",
				k,
				a.String(),
				ae.String())
		}
	}
}

func TestMemcache(t *testing.T) {
	m := New("localhost:11211", "localhost:11212")

	for i := 0; i < 100; i++ {
		err := m.Set(&mc.Item{
			Key:   fmt.Sprintf("%d", i),
			Value: []byte(fmt.Sprintf("%d", i+1)),
		})

		if err != nil {
			t.Fatalf("failed to set key: %s", err)
		}
	}

	for i := 0; i < 100; i++ {
		k := fmt.Sprintf("%d", i)
		it, err := m.Get(k)
		if err != nil {
			t.Fatalf("failed to get key %s: %s", k, err)
		}

		ev := fmt.Sprintf("%d", i+1)<commit_msg>Make tests parallel and cache net.Addrs<commit_after>package gomcketama

import (
	"fmt"
	"net"
	"testing"

	mc "github.com/bradfitz/gomemcache/memcache"
)

func TestNodeKeys(t *testing.T) {
	t.Parallel()

	for h, nKeys := range nodeKeys {
		_, keys, err := prepServer(h)
		if err != nil {
			t.Fatalf("prep server failed: %s", err)
		}

		if len(keys) != len(nKeys) {
			t.Fatalf("generated keys length==%d, expected %d",
				len(keys),
				len(nKeys))
		}

		for i := 0; i < len(keys); i++ {
			if keys[i] != nKeys[i] {
				t.Fatalf("invalid key: got %s, expected %s",
					keys[i],
					nKeys[i])
			}
		}
	}
}

func TestKVToNode(t *testing.T) {
	t.Parallel()

	ks := &KetamaServerSelector{}

	for h := range nodeKeys {
		ks.AddServer(h)
	}

	hostToAddr := make(map[string]net.Addr)
	for k, h := range kvToNode {
		a, err := ks.PickServer(k)
		if err != nil {
			t.Fatalf("got error instead of server: %s", err)
		}

		ae, ok := hostToAddr[h]
		if !ok {
			ae, err = lookup(h)
			if err != nil {
				t.Fatalf("expected server lookup failed: %s", err)
			}

			hostToAddr[h] = ae
		}

		if a.String() != ae.String() {
			t.Fatalf("for key %s, got server %s, expected %s: %s",
				k,
				a.String(),
				ae.String())
		}
	}
}

func TestMemcache(t *testing.T) {
	t.Parallel()

	m := New("localhost:11211", "localhost:11212")

	for i := 0; i < 100; i++ {
		err := m.Set(&mc.Item{
			Key:   fmt.Sprintf("%d", i),
			Value: []byte(fmt.Sprintf("%d", i+1)),
		})

		if err != nil {
			t.Fatalf("failed to set key: %s", err)
		}
	}

	for i := 0; i < 100; i++ {
		k := fmt.Sprintf("%d", i)
		it, err := m.Get(k)
		if err != nil {
			t.Fatalf("failed to get key %s: %s", k, err)
		}

		ev := fmt.Sprintf("%d", i+1)
{'fraction_non_alphanumeric': 0.19931804091754496, 'fraction_numerical': 0.013019218846869188, 'mean_word_length': 2.792009400705053, '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': '6766760', 'n_tokens_mistral': 1555, 'n_tokens_neox': 1355, 'n_words': 343}