<commit_before><commit_msg>add peer sharing test<commit_after>package gateway

import (
	"testing"

	"github.com/NebulousLabs/Sia/network"
)

func TestPeerSharing(t *testing.T) {
	// create server
	tcps, err := network.NewTCPServer(":9002")
	if err != nil {
		t.Fatal(err)
	}
	myAddr := tcps.Address()
	g := New(tcps, nil, nil)

	// register SharePeers RPC
	tcps.RegisterRPC("SharePeers", g.SharePeers)

	// add a peer
	peer := network.Address("foo:9001")
	g.addPeer(peer)
	// gateway only has one peer, so randomPeer should return peer
	if p, err := g.randomPeer(); err != nil || p != peer {
		t.Fatal("server has bad peer list:", g.Info())
	}

	// ask tcps for peers
	var resp []network.Address
	err = myAddr.RPC("SharePeers", nil, &resp)
	if err != nil {
		t.Fatal(err)
	}
	// resp should be exactly []Address{peer}
	if len(resp) != 1 || resp[0] != peer {
		t.Fatal("server gave bad peer list:", resp)
	}

	// add a couple more peers
	g.addPeer(network.Address("bar:9002"))
	g.addPeer(network.Address("baz:9003"))
	g.addPeer(network.Address("quux:9004"))
	err = myAddr.RPC("SharePeers", nil, &resp)
	if err != nil {
		t.Fatal(err)
	}
	// resp should now contain 4 distinct addresses
	for i := 0; i < len(resp); i++ {
		for j := i + 1; j < len(resp); j++ {
			if resp[i] == resp[j] {
				t.Fatal("resp contains duplicate addresses:", resp)
			}
		}
	}
}
{'fraction_non_alphanumeric': 0.17121771217712178, 'fraction_numerical': 0.01845018450184502, 'mean_word_length': 3.5966101694915253, 'pattern_counts': {'":': 1, '<': 5, '<?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': '1656485', 'n_tokens_mistral': 551, 'n_tokens_neox': 511, 'n_words': 136}