package sbalance

import (
	"fmt"
	"testing"
)

func deplete(conn Connection) int {
	allcount := 0
	for ptr := conn.Next(Start); ptr != nil; ptr = conn.Next(Start) {
		allcount++
		(*ptr.(*int))++
	}
	return allcount
}

func TestSequential(t *testing.T) {
	N := 6
	R := 10

	sb := Service{Retries: R, FailCost: 100, SoftFailCost: 10, Strat: StratSeq}

	counts := make([]int, N)
	for i := range counts {
		sb.AddNode(&counts[i], 10)
	}

	conn := sb.NewConn(nil)
	defer conn.Close()

	allcount := deplete(conn)

	if allcount != N*(R+1) {
		t.Errorf("Expected allcount %v, got %v", N*(R+1), allcount)
	}
	for i, c := range counts {
		if c != R+1 {
			t.Errorf("index %v: Expected count %v, got %v", i, R+1, c)
		}
	}

	fmt.Println("Sequential, equal cost")
	fmt.Printf("allcount: %d\n", allcount)
	fmt.Printf("counts: %v\n", counts)
}

func TestRandom(t *testing.T) {
	N := 6
	R := 10
	rounds := 1000

	sb := Service{Retries: R, FailCost: 100, SoftFailCost: 10, Strat: StratRandom}

	counts := make([]int, N)
	for i := range counts {
		sb.AddNode(&counts[i], 10)
	}

	allcount := 0
	for i := 0; i < rounds; i++ {
		conn := sb.NewConn(nil)
		allcount += deplete(conn)
		conn.Close()
	}

	if allcount != N*R*rounds+rounds {
		t.Errorf("Expected allcount %v, got %v", N*R*rounds+rounds, allcount)
	}
	for i, c := range counts {

		if c < R*rounds {
			t.Errorf("index %v: Expected count >= %v, got %v", i, R*rounds, c)
		}
	}

	fmt.Println("Random, equal cost")
	fmt.Printf("allcount: %d\n", allcount)
	fmt.Printf("counts: %v\n", counts)
}

var testAddrs = [...]string{
	"78.69.46.145", "81.232.62.225", "90.227.132.92", "81.227.242.105", "213.64.74.191", "195.84.114.14", "213.100.171.123", "81.227.201.204",
	"217.210.73.159", "81.224.108.84", "83.250.143.28", "83.181.14.38", "85.226.144.78", "78.69.253.251", "81.200.170.173", "83.178.0.149", "83.216.98.24", "83.166.15.229",
	"78.69.0.5", "213.67.70.9", "83.249.99.45", "85.228.200.56", "217.210.16.9", "85.226.220.247", "83.249.149.64", "81.230.37.112", "90.229.160.57", "213.113.217.112",
	"84.217.47.195", "213.112.174.220", "80.216.5.241", "83.251.245.116", "217.208.137.47", "90.235.7.84", "195.216.51.161", "83.209.43.2", "83.227.64.21", "92.32.93.156",
	"89.233.192.90", "90.230.69.157", "81.234.253.231", "83.227.236.95", "81.216.117.125", "90.231.145.253", "74.6.22.106", "79.136.49.72", "217.78.24.167", "90.231.226.186",
	"81.231.58.224", "74.6.22.106", "81.230.68.155", "78.156.209.26", "90.229.194.145", "83.188.201.157", "83.253.100.14", "85.224.56.134", "79.138.214.90", "83.233.110.85",
	"84.217.10.198", "79.103.196.187", "81.226.252.161", "85.225.52.55", "85.224.87.96", "130.227.120.189", "217.210.200.197", "81.233.118.141", "81.216.200.116", "83.188.195.163",
	"81.233.35.90", "212.105.79.76", "90.229.135.210", "79.133.24.247", "84.218.35.150", "66.249.72.71", "90.230.49.217", "90.231.40.88", "84.217.52.102", "81.233.178.88",
	"85.224.232.194", "81.225.20.210", "80.251.192.2", "213.112.93.42", "217.210.185.104", "79.138.191.247", "66.249.72.71", "83.176.234.132", "85.30.137.113", "194.237.160.53",
	"213.65.165.115", "81.8.176.198", "62.20.103.179", "83.226.10.34", "79.138.155.10", "83.233.59.123", "78.69.129.153", "84.217.0.67", "78.69.54.147", "83.188.218.244",
	"195.162.73.77", "193.10.63.101", "83.248.122.105", "85.230.7.254", "80.217.41.63", "81.234.103.3", "81.166.200.217", "213.64.158.10", "85.228.183.69", "84.217.50.77",
	"83.251.252.76", "83.249.116.50", "66.249.72.71", "92.32.81.85", "78.70.129.215", "90.235.9.93", "144.95.32.9", "83.233.57.98", "213.113.94.217", "81.229.51.32",
	"85.228.83.146", "79.102.112.49", "89.233.241.96", "213.66.216.212", "67.195.37.98", "213.101.56.160", "83.178.156.105", "83.233.111.131", "195.216.42.202", "78.69.82.196",
	"212.27.2.62", "81.226.116.93", "83.252.163.53", "81.234.220.56", "83.255.3.17", "90.235.19.150", "81.235.47.30", "90.231.191.180", "77.110.0.160", "92.254.232.252",
	"213.65.216.117", "74.6.22.106", "81.224.170.181", "89.236.0.50", "90.235.140.164", "81.233.152.113", "79.138.156.74", "83.254.21.193", "80.217.184.160", "80.88.118.145",
	"83.254.20.211", "217.213.123.141", "88.88.61.47", "213.112.93.42", "90.239.92.96", "80.88.118.60", "85.224.13.14", "217.208.108.135", "81.236.19.60", "81.216.183.196",
	"85.225.138.209", "85.224.112.240", "78.82.197.43", "79.136.28.131", "213.114.188.190", "213.66.114.34", "213.112.119.103", "83.251.242.7", "79.138.190.216", "79.102.90.54",
	"78.69.48.91", "213.89.32.35", "83.255.20.159", "85.226.187.40", "81.226.88.184", "84.55.95.94", "83.252.56.154", "213.100.172.99", "80.217.202.250", "85.197.137.150",
	"144.95.32.9", "66.249.72.71", "81.233.202.237", "91.95.194.33", "81.16.170.101", "78.69.155.77", "90.227.184.181", "82.196.162.172", "90.239.65.22", "217.73.105.44",
	"213.112.68.189", "83.249.211.25", "80.217.141.75", "85.228.134.243", "217.211.76.114", "84.217.41.13", "81.231.4.31", "67.195.37.98", "85.226.144.78", "78.69.128.254",
	"81.233.107.184", "213.89.235.210", "83.254.183.156", "213.112.120.234", "213.66.219.59", "81.233.51.38", "90.235.136.37", "84.244.211.124", "83.254.246.10", "84.217.29.32",
	"83.166.24.20", "88.215.73.193", "82.209.144.168", "213.199.83.39", "80.217.170.16", "78.82.251.15", "83.178.30.169", "81.216.192.134", "83.255.130.61", "62.20.136.94",
	"83.188.198.154", "82.209.172.84", "74.6.22.106", "85.225.183.49", "92.241.192.115", "85.226.74.181", "81.231.155.166", "90.231.234.234", "81.233.147.74", "77.53.9.123",
	"81.227.27.235", "81.229.68.76", "81.230.36.23", "81.227.152.163", "81.225.22.228", "213.113.120.177", "80.251.207.129", "194.132.137.34", "90.227.6.41", "164.9.102.4",
	"81.235.16.185", "81.234.248.238", "84.217.39.249", "80.85.124.227", "84.218.38.122", "83.183.231.16", "84.219.14.41", "213.64.241.123", "212.251.203.125", "81.229.9.159",
	"83.226.246.114", "92.254.176.158", "213.100.99.100", "90.239.65.145", "85.226.5.197", "66.249.72.71", "74.6.22.106", "79.138.129.71", "84.216.58.36", "83.223.9.100",
	"78.69.156.51", "91.149.56.47", "81.227.42.90", "85.225.213.125", "81.235.139.7", "213.66.225.242", "83.188.195.53", "217.213.54.117", "81.234.153.37", "213.100.96.112",
	"83.219.193.49", "81.234.163.43", "79.138.135.189", "81.225.114.14", "90.239.69.52", "83.252.74.253", "84.217.53.19", "81.229.190.70", "92.32.93.83", "90.231.7.147",
	"85.225.221.157", "81.229.243.217", "83.188.208.120", "213.67.75.156", "83.248.231.106", "74.6.22.106", "81.231.32.157", "78.69.155.183", "83.188.215.226", "81.237.222.137",
	"85.167.81.239", "213.67.249.107", "83.249.114.75", "213.101.44.118", "85.119.130.132", "81.8.174.73", "80.217.206.164", "81.225.76.57", "81.234.37.87", "89.233.231.184",
	"90.227.60.212", "81.234.188.184", "81.225.12.102", "90.235.133.102", "213.66.200.168", "81.226.187.112", "83.255.86.106", "81.232.148.77", "66.249.72.71", "83.181.17.57",
	"217.211.57.149", "81.235.28.243", "83.249.109.93", "90.224.66.21", "217.210.90.2", "82.183.161.130", "81.216.191.150", "78.70.126.197", "83.253.234.124", "85.230.171.57",
	"85.226.144.78", "213.89.244.66", "129.16.128.109", "85.224.100.23", "90.231.125.198", "83.251.50.200", "84.217.22.35", "87.59.135.97", "80.216.128.166", "82.209.147.156",
	"81.228.248.226", "82.182.182.228", "83.226.91.2", "217.208.135.210", "81.231.8.11", "81.233.57.192", "79.102.109.254", "82.96.37.13", "213.67.39.230", "81.228.199.108",
	"192.176.230.1", "62.20.218.236", "217.209.199.227", "87.57.182.211", "213.66.186.80", "62.108.212.221", "77.53.47.181", "85.225.233.113", "81.233.97.175", "62.108.200.159",
	"217.208.138.152", "83.254.47.72", "81.229.2.154", "90.229.222.211", "81.227.19.58", "83.183.27.183", "90.224.211.254", "90.224.96.63", "90.235.130.73", "79.138.196.146",
	"81.236.134.13", "83.250.153.65", "83.241.200.123", "83.248.9.5", "81.231.75.4", "194.192.81.89", "81.227.102.248", "83.253.21.117", "81.94.95.227", "83.251.56.200",
	"83.183.182.11", "90.224.24.63", "81.225.53.119", "81.233.49.69", "74.6.22.106", "83.251.252.254", "78.69.39.228", "83.227.40.80", "83.251.50.200", "83.216.98.24",
	"217.211.159.218", "77.53.46.249", "81.225.90.218", "90.231.107.55", "83.254.145.195", "62.108.216.22", "90.227.255.191", "85.226.191.34", "81.227.49.123", "85.226.205.97",
	"81.236.12.165", "194.71.123.2", "83.255.142.39", "83.249.116.106", "81.170.226.24", "92.241.199.78", "81.235.217.160", "69.147.112.168", "89.233.237.107", "83.249.109.8",
	"81.232.164.12", "90.235.10.18", "62.212.200.100", "79.138.131.121", "83.249.128.142", "217.151.60.50", "194.23.140.254", "213.226.88.171", "85.230.172.17", "83.254.18.32",
	"83.142.0.139", "213.67.38.173", "90.225.100.135", "79.136.49.118", "81.231.101.48", "81.227.69.168", "203.209.79.3", "91.149.45.127", "83.249.241.53", "90.229.184.84",
	"85.224.169.136", "213.101.1.247", "85.229.107.28", "85.224.215.168", "91.126.54.145", "84.217.151.44", "90.239.74.131", "74.6.22.106", "85.228.204.128", "85.225.1.63",
	"85.224.168.193", "81.227.158.205", "78.82.191.123", "212.27.7.19", "83.183.237.152", "83.188.214.153", "83.249.34.57", "79.136.104.52", "82.182.146.204", "81.233.179.252",
	"213.89.195.178", "85.229.16.159", "81.233.195.79", "90.230.55.156", "85.224.164.33", "90.230.67.8", "213.113.120.202", "85.228.140.251", "90.227.222.70", "85.228.160.135",
	"81.228.50.90", "81.235.207.156", "81.225.48.35", "81.8.199.125", "85.157.226.247", "213.112.101.122", "85.231.204.248", "81.234.149.151", "217.67.89.151", "217.209.71.84",
	"81.216.244.204", "66.249.72.71", "81.227.208.69", "81.216.117.39", "212.181.128.16", "83.254.225.255", "85.228.200.59", "85.226.179.157", "217.208.56.238", "217.198.145.16",
	"77.241.128.90", "213.65.146.159", "82.197.237.141", "83.188.218.38", "212.181.138.137", "213.114.114.134", "81.156.238.248", "83.254.97.189", "79.102.74.133", "84.208.98.21",
	"85.228.156.52", "90.228.219.115", "90.235.3.196", "84.244.234.44", "81.226.240.243", "90.235.0.201", "213.114.64.34", "83.178.151.22", "85.224.225.45", "90.230.203.246",
	"212.27.8.225", "83.241.200.123", "84.217.39.249", "81.233.248.247", "212.112.39.227", "78.69.48.91", "83.241.200.123", "81.234.246.67", "83.216.98.24", "85.225.225.107",
	"212.181.175.242", "195.252.57.181", "90.230.10.249", "212.214.188.94", "90.239.66.47", "81.225.165.8", "213.67.203.246", "213.66.218.27", "81.233.180.162", "83.252.74.253",
	"213.112.96.158", "85.224.5.7", "217.67.93.10", "217.208.111.10", "79.102.97.83", "90.229.245.69", "85.226.188.71", "83.183.210.247", "81.235.196.40", "74.6.22.106",
	"90.229.145.110", "80.217.136.122", "90.230.27.158", "213.113.174.249", "79.142.218.71", "80.70.145.208", "90.224.211.45", "85.226.144.78", "81.231.233.167", "81.225.90.218",
	"74.6.22.106", "90.230.159.217", "78.82.205.164", "80.213.18.209", "66.249.72.71", "92.32.94.117", "82.197.237.103", "217.10.120.77", "217.67.93.67", "83.253.96.133",
	"212.73.172.70", "90.224.51.118", "81.216.158.175", "90.224.186.127", "212.181.158.92", "213.113.229.76", "85.194.136.69", "81.236.5.119", "81.233.228.243", "81.237.222.181",
	"90.184.73.203", "84.217.151.44", "81.230.91.103", "85.224.168.53", "83.140.166.35", "81.232.122.212", "81.229.144.89", "74.6.22.106", "83.183.17.201", "85.24.134.209",
	"81.234.143.12", "85.225.252.123", "81.228.159.22", "212.116.91.78", "213.89.254.13", "83.241.200.123", "217.211.240.249", "81.236.219.138", "82.209.142.141", "74.6.22.106",
	"213.112.221.119", "195.216.42.202", "66.249.72.71", "90.227.217.131", "85.228.159.98", "90.224.1.152", "213.64.1.128", "87.227.117.246", "81.233.20.12", "83.254.156.253",
	"84.217.53.19", "83.227.51.31", "83.209.99.203", "85.165.55.20", "90.231.1.59", "77.53.6.20", "90.230.117.54", "80.217.77.97", "83.226.68.73", "83.241.200.123",
	"213.112.162.85", "83.227.72.69", "90.230.129.220", "85.230.4.226", "90.229.208.125", "84.217.151.44", "90.230.132.176", "213.89.128.191", "79.102.35.48", "85.224.70.181",
	"79.138.142.229", "81.233.118.177", "81.231.34.241", "213.64.134.103", "83.209.46.70", "91.149.34.78", "85.30.188.233", "79.138.182.105", "85.8.38.218", "77.74.134.243",
	"87.227.1.47", "83.252.90.137", "194.71.181.8", "84.217.139.238", "85.226.85.129", "72.14.193.165", "213.113.174.249", "90.231.212.28", "83.233.129.192", "90.229.145.239",
	"83.255.66.68", "83.227.204.141", "83.145.56.53", "90.227.8.175", "62.20.136.94", "79.138.197.42", "85.228.105.61", "90.224.176.33", "85.224.208.249", "90.235.16.208",
	"194.192.81.89", "83.252.94.46", "90.231.193.135", "74.6.22.106", "87.227.6.106", "80.216.154.10", "194.117.188.249", "84.216.46.174", "217.209.46.170", "83.223.29.52",
	"85.225.211.20", "81.224.218.150", "90.231.192.129", "81.230.24.194", "195.252.60.161", "83.250.36.226", "90.229.250.107", "66.249.72.71", "80.217.11.227", "87.227.114.41",
	"81.236.241.124", "85.224.246.172", "90.231.63.129", "194.192.81.89", "83.140.193.203", "81.216.185.133", "217.213.143.7", "87.227.0.42", "85.230.168.162", "81.233.162.206",
	"213.100.246.204", "66.249.72.71", "212.27.11.4", "213.112.93.42", "90.231.189.147", "74.6.22.106", "83.254.163.35", "213.114.114.176", "81.231.233.167", "81.231.233.167",
	"90.231.210.62", "78.70.217.189", "83.248.53.42", "81.233.26.50", "83.241.200.123", "90.230.96.115", "81.236.134.240", "85.226.7.93", "195.67.214.205", "83.183.81.96",
	"85.227.144.60", "85.228.211.102", "90.229.208.125", "78.70.146.82", "217.213.132.221", "217.209.40.246", "81.233.245.110", "82.99.42.124", "85.224.146.229", "83.249.133.153",
	"83.253.231.59", "90.227.51.245", "90.230.60.109", "85.231.55.104", "90.227.195.52", "78.70.139.125", "78.69.144.195", "81.226.37.235", "213.66.21.253", "217.27.177.36",
	"83.226.111.117", "213.67.81.93", "85.226.161.7", "90.235.139.43", "90.228.250.160", "83.183.182.11", "85.194.148.28", "66.249.72.71", "85.30.172.61", "81.226.218.153",
	"81.229.225.241", "83.181.15.182", "85.197.139.92", "213.226.119.199", "84.217.157.251", "90.235.136.37", "83.249.99.45", "79.102.100.161", "85.224.81.181", "83.183.48.1",
	"85.89.19.99", "83.252.55.95", "81.229.58.83", "81.228.255.230", "90.228.206.235", "81.235.205.141", "213.66.176.97", "81.230.253.153", "83.233.173.163", "90.235.134.251",
	"78.70.150.149", "83.254.67.72", "79.136.41.14", "194.17.16.46", "81.226.26.86", "213.64.27.160", "212.37.119.92", "217.210.100.245", "81.226.179.55", "83.241.200.123",
	"81.227.124.44", "83.188.203.70", "78.156.198.168", "83.252.216.23", "90.227.66.148", "85.11.14.58", "90.227.157.117", "81.224.30.80", "85.226.99.247", "83.251.26.238",
	"83.255.129.19", "66.249.72.71", "83.188.206.110", "194.16.88.66", "90.224.63.183", "66.249.72.71", "81.230.117.89", "81.229.64.59", "144.95.32.9", "213.114.147.122",
	"89.160.72.148", "67.195.37.98", "81.231.38.135", "83.253.23.223", "90.227.6.41", "83.216.98.24", "83.248.225.240", "81.228.153.228", "85.226.167.178", "81.216.78.86",
	"131.116.254.200", "85.224.17.8", "81.216.113.80", "213.113.41.23", "83.252.72.97", "81.236.143.200", "83.241.200.123", "81.230.95.7", "85.225.161.63", "90.227.163.212",
	"78.69.209.186", "90.231.207.179", "81.216.146.149", "85.228.167.68", "147.186.254.241", "84.218.38.122", "213.141.85.120", "89.233.241.214", "85.226.144.78", "195.165.240.129",
	"79.102.118.26", "81.227.236.144", "213.226.93.57", "79.185.88.49", "85.230.55.89", "85.226.144.78", "90.224.96.28", "83.251.191.152", "83.241.200.123", "85.225.108.58",
	"90.235.136.46", "217.210.90.2", "213.113.17.24", "90.229.173.192", "78.70.112.58", "88.206.143.132", "83.241.200.123", "79.133.18.237", "83.250.154.14", "81.233.21.77",
	"91.149.43.93", "91.95.202.134", "83.188.198.154", "90.230.198.117", "81.226.196.67", "83.150.152.114", "84.217.255.139", "83.241.200.123", "83.188.210.111", "213.112.69.243",
	"83.183.106.239", "81.236.226.217", "81.229.25.118", "81.235.1.51", "83.254.160.226", "213.100.91.123", "83.253.89.4", "81.225.165.8", "84.217.151.84", "78.64.4.62",
	"90.228.245.236", "90.239.66.61", "85.226.183.242", "81.227.27.92", "85.228.119.45", "81.233.18.221", "62.108.223.157", "83.251.187.64", "213.89.128.93", "85.231.203.120",
	"87.227.107.65", "90.224.89.251", "213.199.78.169", "79.136.34.59", "81.234.252.175", "195.38.2.106", "83.241.200.123", "66.249.72.71", "130.244.202.210", "212.247.210.127",
	"83.178.16.33", "192.121.192.22", "83.255.2.255", "84.217.46.86", "91.95.208.67", "74.6.22.106", "212.214.30.20", "79.102.128.74", "213.114.233.104", "81.236.5.38",
	"83.188.214.251", "90.235.1.176", "81.232.150.157", "79.138.184.247", "81.8.244.154", "78.156.196.204", "148.160.187.113", "213.65.32.36", "81.235.200.160", "81.229.94.18",
	"90.227.67.134", "90.230.49.217", "217.211.230.53", "81.224.85.95", "83.241.200.123", "213.67.80.194", "83.250.76.75", "88.131.85.203", "85.226.210.185", "85.224.227.38",
	"90.231.232.222", "217.210.128.245", "85.229.79.68", "213.67.22.179", "83.216.98.24", "217.208.119.147", "83.254.52.191", "83.188.205.121", "92.32.104.8", "83.233.200.151",
	"81.224.84.204", "90.231.232.200", "83.233.219.134", "90.230.127.108", "90.239.87.17", "193.221.47.232", "90.224.246.28", "90.229.205.99", "213.100.98.251", "81.233.36.165",
	"83.176.248.70", "90.235.13.65", "81.233.236.55", "85.224.3.162", "81.224.219.142", "213.89.236.122", "85.228.202.15", "83.188.208.164", "83.250.147.82", "74.6.22.106",
	"212.242.145.158", "74.6.22.106", "85.226.144.78", "85.230.13.221", "81.170.241.47", "81.233.94.74", "83.251.51.88", "78.69.44.138", "85.225.248.221", "83.209.129.247",
	"85.225.28.127", "81.236.176.35", "82.182.62.83", "148.160.65.125", "82.197.245.192", "213.114.82.156", "90.239.74.160", "85.30.174.96", "74.6.22.106", "90.224.184.35",
	"83.248.100.54", "85.236.95.225", "81.233.123.41", "78.69.147.206", "193.221.74.5", "81.233.99.27", "81.225.6.30", "90.231.234.168", "74.6.22.106", "91.95.8.6",
	"69.147.112.168", "69.147.112.168", "78.69.159.222", "69.147.112.168", "69.147.112.168", "74.6.22.106", "81.235.202.241", "80.222.133.172", "88.83.44.79", "81.229.118.186",
	"83.253.234.124", "81.232.173.251", "217.73.105.44", "83.188.195.83", "213.113.65.156", "80.216.48.179", "81.227.10.99", "213.112.123.22", "81.230.80.185", "62.20.171.220",
	"77.53.6.20", "217.209.166.24", "74.6.22.106", "82.182.146.204", "84.216.93.227", "85.11.55.126", "82.196.104.90", "83.176.248.52", "84.217.32.220", "90.231.46.195",
	"83.176.248.70", "84.216.50.92", "85.225.183.177", "83.183.231.16", "213.101.34.6", "81.233.54.133", "84.217.21.102", "85.228.186.175", "85.226.197.92", "81.170.237.175",
	"62.63.222.219", "80.216.152.204", "81.8.215.240", "82.182.100.165", "85.228.145.124", "212.181.162.140", "85.229.63.178", "194.17.178.160", "83.183.8.165", "193.210.65.69",
	"213.89.72.82", "78.82.241.231", "81.229.113.67", "81.191.85.227", "85.225.31.237", "66.249.72.71", "81.226.250.130", "78.82.192.201", "194.237.160.252", "83.253.132.147",
	"217.151.56.58", "69.147.112.168",
}

func TestHashFirst(t *testing.T) {
	N := 6
	R := 0

	sb := Service{Retries: R, FailCost: 100, SoftFailCost: 100, Strat: StratHash}

	counts := make([]int, N)
	for i := range counts {
		sb.AddNode(&counts[i], 10)
	}

	allcount := 0
	for _, a := range testAddrs {
		conn := sb.NewConn([]byte(a))
		allcount += deplete(conn)
		conn.Close()
	}

	if allcount != len(testAddrs) {
		t.Errorf("Expected allcount %v, got %v\n", len(testAddrs), allcount)
	}

	for i, c := range counts {
		if c < 140 || c > 200 {
			t.Errorf("Index %v, expected between 140 and 200, got %v", i, c)
		}
	}

	fmt.Println("Hash, equal cost")
	fmt.Printf("allcount: %d\n", allcount)
	fmt.Printf("counts: %v\n", counts)
}

func TestHashReinit(t *testing.T) {
	N := 6
	R := 0

	counts := make([]int, N)

	allcount := 0
	for _, a := range testAddrs {
		sb := Service{Retries: R, FailCost: 100, SoftFailCost: 100, Strat: StratHash}
		for i := range counts {
			sb.AddNode(&counts[i], 10)
		}

		conn := sb.NewConn([]byte(a))
		allcount += deplete(conn)
		conn.Close()
	}

	if allcount != len(testAddrs) {
		t.Errorf("Expected allcount %v, got %v\n", len(testAddrs), allcount)
	}

	for i, c := range counts {
		if c < 140 || c > 200 {
			t.Errorf("Index %v, expected between 140 and 200, got %v", i, c)
		}
	}

	fmt.Println("Hash, equal cost, reinited")
	fmt.Printf("allcount: %d\n", allcount)
	fmt.Printf("counts: %v\n", counts)
}

func TestHashSameValue(t *testing.T) {
	N := 6
	R := 0

	sb := Service{Retries: R, FailCost: 100, SoftFailCost: 100, Strat: StratHash}

	counts := make([]int, N)
	for i := range counts {
		sb.AddNode(&counts[i], 10)
	}

	allcount := 0
	for i := 0; i < 100; i++ {
		conn := sb.NewConn([]byte("127.0.0.1"))
		allcount += deplete(conn)
		conn.Close()
	}

	if allcount != 100 {
		t.Errorf("Expected allcount %v, got %v\n", 100, allcount)
	}

	buckets := 0
	for _, c := range counts {
		if c > 0 {
			buckets++
		}
	}
	if buckets != 1 {
		t.Errorf("Expected a single bucket used, got %v", buckets)
	}

	fmt.Println("Hash same")
	fmt.Printf("allcount: %d\n", allcount)
	fmt.Printf("counts: %v\n", counts)
}
// Copyright 2018 Schibsted

package sbalance

import (
	"fmt"
	"testing"
)

func deplete(conn Connection) int {
	allcount := 0
	for ptr := conn.Next(Start); ptr != nil; ptr = conn.Next(Start) {
		allcount++
		(*ptr.(*int))++
	}
	return allcount
}

func TestSequential(t *testing.T) {
	N := 6
	R := 10

	sb := Service{Retries: R, FailCost: 100, SoftFailCost: 10, Strat: StratSeq}

	counts := make([]int, N)
	for i := range counts {
		sb.AddNode(&counts[i], 10)
	}

	conn := sb.NewConn(nil)
	defer conn.Close()

	allcount := deplete(conn)

	if allcount != N*(R+1) {
		t.Errorf("Expected allcount %v, got %v", N*(R+1), allcount)
	}
	for i, c := range counts {
		if c != R+1 {
			t.Errorf("index %v: Expected count %v, got %v", i, R+1, c)
		}
	}

	fmt.Println("Sequential, equal cost")
	fmt.Printf("allcount: %d\n", allcount)
	fmt.Printf("counts: %v\n", counts)
}

func TestRandom(t *testing.T) {
	N := 6
	R := 10
	rounds := 1000

	sb := Service{Retries: R, FailCost: 100, SoftFailCost: 10, Strat: StratRandom}

	counts := make([]int, N)
	for i := range counts {
		sb.AddNode(&counts[i], 10)
	}

	allcount := 0
	for i := 0; i < rounds; i++ {
		conn := sb.NewConn(nil)
		allcount += deplete(conn)
		conn.Close()
	}

	if allcount != N*R*rounds+rounds {
		t.Errorf("Expected allcount %v, got %v", N*R*rounds+rounds, allcount)
	}
	for i, c := range counts {
		// Each bucket should have around (rounds/BUCKETS) extra hits, we don't count that as our randomness margin.
		if c < R*rounds {
			t.Errorf("index %v: Expected count >= %v, got %v", i, R*rounds, c)
		}
	}

	fmt.Println("Random, equal cost")
	fmt.Printf("allcount: %d\n", allcount)
	fmt.Printf("counts: %v\n", counts)
}

var testAddrs = [...]string{
	"78.69.46.145", "81.232.62.225", "90.227.132.92", "81.227.242.105", "213.64.74.191", "195.84.114.14", "213.100.171.123", "81.227.201.204",
	"217.210.73.159", "81.224.108.84", "83.250.143.28", "83.181.14.38", "85.226.144.78", "78.69.253.251", "81.200.170.173", "83.178.0.149", "83.216.98.24", "83.166.15.229",
	"78.69.0.5", "213.67.70.9", "83.249.99.45", "85.228.200.56", "217.210.16.9", "85.226.220.247", "83.249.149.64", "81.230.37.112", "90.229.160.57", "213.113.217.112",
	"84.217.47.195", "213.112.174.220", "80.216.5.241", "83.251.245.116", "217.208.137.47", "90.235.7.84", "195.216.51.161", "83.209.43.2", "83.227.64.21", "92.32.93.156",
	"89.233.192.90", "90.230.69.157", "81.234.253.231", "83.227.236.95", "81.216.117.125", "90.231.145.253", "74.6.22.106", "79.136.49.72", "217.78.24.167", "90.231.226.186",
	"81.231.58.224", "74.6.22.106", "81.230.68.155", "78.156.209.26", "90.229.194.145", "83.188.201.157", "83.253.100.14", "85.224.56.134", "79.138.214.90", "83.233.110.85",
	"84.217.10.198", "79.103.196.187", "81.226.252.161", "85.225.52.55", "85.224.87.96", "130.227.120.189", "217.210.200.197", "81.233.118.141", "81.216.200.116", "83.188.195.163",
	"81.233.35.90", "212.105.79.76", "90.229.135.210", "79.133.24.247", "84.218.35.150", "66.249.72.71", "90.230.49.217", "90.231.40.88", "84.217.52.102", "81.233.178.88",
	"85.224.232.194", "81.225.20.210", "80.251.192.2", "213.112.93.42", "217.210.185.104", "79.138.191.247", "66.249.72.71", "83.176.234.132", "85.30.137.113", "194.237.160.53",
	"213.65.165.115", "81.8.176.198", "62.20.103.179", "83.226.10.34", "79.138.155.10", "83.233.59.123", "78.69.129.153", "84.217.0.67", "78.69.54.147", "83.188.218.244",
	"195.162.73.77", "193.10.63.101", "83.248.122.105", "85.230.7.254", "80.217.41.63", "81.234.103.3", "81.166.200.217", "213.64.158.10", "85.228.183.69", "84.217.50.77",
	"83.251.252.76", "83.249.116.50", "66.249.72.71", "92.32.81.85", "78.70.129.215", "90.235.9.93", "144.95.32.9", "83.233.57.98", "213.113.94.217", "81.229.51.32",
	"85.228.83.146", "79.102.112.49", "89.233.241.96", "213.66.216.212", "67.195.37.98", "213.101.56.160", "83.178.156.105", "83.233.111.131", "195.216.42.202", "78.69.82.196",
	"212.27.2.62", "81.226.116.93", "83.252.163.53", "81.234.220.56", "83.255.3.17", "90.235.19.150", "81.235.47.30", "90.231.191.180", "77.110.0.160", "92.254.232.252",
	"213.65.216.117", "74.6.22.106", "81.224.170.181", "89.236.0.50", "90.235.140.164", "81.233.152.113", "79.138.156.74", "83.254.21.193", "80.217.184.160", "80.88.118.145",
	"83.254.20.211", "217.213.123.141", "88.88.61.47", "213.112.93.42", "90.239.92.96", "80.88.118.60", "85.224.13.14", "217.208.108.135", "81.236.19.60", "81.216.183.196",
	"85.225.138.209", "85.224.112.240", "78.82.197.43", "79.136.28.131", "213.114.188.190", "213.66.114.34", "213.112.119.103", "83.251.242.7", "79.138.190.216", "79.102.90.54",
	"78.69.48.91", "213.89.32.35", "83.255.20.159", "85.226.187.40", "81.226.88.184", "84.55.95.94", "83.252.56.154", "213.100.172.99", "80.217.202.250", "85.197.137.150",
	"144.95.32.9", "66.249.72.71", "81.233.202.237", "91.95.194.33", "81.16.170.101", "78.69.155.77", "90.227.184.181", "82.196.162.172", "90.239.65.22", "217.73.105.44",
	"213.112.68.189", "83.249.211.25", "80.217.141.75", "85.228.134.243", "217.211.76.114", "84.217.41.13", "81.231.4.31", "67.195.37.98", "85.226.144.78", "78.69.128.254",
	"81.233.107.184", "213.89.235.210", "83.254.183.156", "213.112.120.234", "213.66.219.59", "81.233.51.38", "90.235.136.37", "84.244.211.124", "83.254.246.10", "84.217.29.32",
	"83.166.24.20", "88.215.73.193", "82.209.144.168", "213.199.83.39", "80.217.170.16", "78.82.251.15", "83.178.30.169", "81.216.192.134", "83.255.130.61", "62.20.136.94",
	"83.188.198.154", "82.209.172.84", "74.6.22.106", "85.225.183.49", "92.241.192.115", "85.226.74.181", "81.231.155.166", "90.231.234.234", "81.233.147.74", "77.53.9.123",
	"81.227.27.235", "81.229.68.76", "81.230.36.23", "81.227.152.163", "81.225.22.228", "213.113.120.177", "80.251.207.129", "194.132.137.34", "90.227.6.41", "164.9.102.4",
	"81.235.16.185", "81.234.248.238", "84.217.39.249", "80.85.124.227", "84.218.38.122", "83.183.231.16", "84.219.14.41", "213.64.241.123", "212.251.203.125", "81.229.9.159",
	"83.226.246.114", "92.254.176.158", "213.100.99.100", "90.239.65.145", "85.226.5.197", "66.249.72.71", "74.6.22.106", "79.138.129.71", "84.216.58.36", "83.223.9.100",
	"78.69.156.51", "91.149.56.47", "81.227.42.90", "85.225.213.125", "81.235.139.7", "213.66.225.242", "83.188.195.53", "217.213.54.117", "81.234.153.37", "213.100.96.112",
	"83.219.193.49", "81.234.163.43", "79.138.135.189", "81.225.114.14", "90.239.69.52", "83.252.74.253", "84.217.53.19", "81.229.190.70", "92.32.93.83", "90.231.7.147",
	"85.225.221.157", "81.229.243.217", "83.188.208.120", "213.67.75.156", "83.248.231.106", "74.6.22.106", "81.231.32.157", "78.69.155.183", "83.188.215.226", "81.237.222.137",
	"85.167.81.239", "213.67.249.107", "83.249.114.75", "213.101.44.118", "85.119.130.132", "81.8.174.73", "80.217.206.164", "81.225.76.57", "81.234.37.87", "89.233.231.184",
	"90.227.60.212", "81.234.188.184", "81.225.12.102", "90.235.133.102", "213.66.200.168", "81.226.187.112", "83.255.86.106", "81.232.148.77", "66.249.72.71", "83.181.17.57",
	"217.211.57.149", "81.235.28.243", "83.249.109.93", "90.224.66.21", "217.210.90.2", "82.183.161.130", "81.216.191.150", "78.70.126.197", "83.253.234.124", "85.230.171.57",
	"85.226.144.78", "213.89.244.66", "129.16.128.109", "85.224.100.23", "90.231.125.198", "83.251.50.200", "84.217.22.35", "87.59.135.97", "80.216.128.166", "82.209.147.156",
	"81.228.248.226", "82.182.182.228", "83.226.91.2", "217.208.135.210", "81.231.8.11", "81.233.57.192", "79.102.109.254", "82.96.37.13", "213.67.39.230", "81.228.199.108",
	"192.176.230.1", "62.20.218.236", "217.209.199.227", "87.57.182.211", "213.66.186.80", "62.108.212.221", "77.53.47.181", "85.225.233.113", "81.233.97.175", "62.108.200.159",
	"217.208.138.152", "83.254.47.72", "81.229.2.154", "90.229.222.211", "81.227.19.58", "83.183.27.183", "90.224.211.254", "90.224.96.63", "90.235.130.73", "79.138.196.146",
	"81.236.134.13", "83.250.153.65", "83.241.200.123", "83.248.9.5", "81.231.75.4", "194.192.81.89", "81.227.102.248", "83.253.21.117", "81.94.95.227", "83.251.56.200",
	"83.183.182.11", "90.224.24.63", "81.225.53.119", "81.233.49.69", "74.6.22.106", "83.251.252.254", "78.69.39.228", "83.227.40.80", "83.251.50.200", "83.216.98.24",
	"217.211.159.218", "77.53.46.249", "81.225.90.218", "90.231.107.55", "83.254.145.195", "62.108.216.22", "90.227.255.191", "85.226.191.34", "81.227.49.123", "85.226.205.97",
	"81.236.12.165", "194.71.123.2", "83.255.142.39", "83.249.116.106", "81.170.226.24", "92.241.199.78", "81.235.217.160", "69.147.112.168", "89.233.237.107", "83.249.109.8",
	"81.232.164.12", "90.235.10.18", "62.212.200.100", "79.138.131.121", "83.249.128.142", "217.151.60.50", "194.23.140.254", "213.226.88.171", "85.230.172.17", "83.254.18.32",
	"83.142.0.139", "213.67.38.173", "90.225.100.135", "79.136.49.118", "81.231.101.48", "81.227.69.168", "203.209.79.3", "91.149.45.127", "83.249.241.53", "90.229.184.84",
	"85.224.169.136", "213.101.1.247", "85.229.107.28", "85.224.215.168", "91.126.54.145", "84.217.151.44", "90.239.74.131", "74.6.22.106", "85.228.204.128", "85.225.1.63",
	"85.224.168.193", "81.227.158.205", "78.82.191.123", "212.27.7.19", "83.183.237.152", "83.188.214.153", "83.249.34.57", "79.136.104.52", "82.182.146.204", "81.233.179.252",
	"213.89.195.178", "85.229.16.159", "81.233.195.79", "90.230.55.156", "85.224.164.33", "90.230.67.8", "213.113.120.202", "85.228.140.251", "90.227.222.70", "85.228.160.135",
	"81.228.50.90", "81.235.207.156", "81.225.48.35", "81.8.199.125", "85.157.226.247", "213.112.101.122", "85.231.204.248", "81.234.149.151", "217.67.89.151", "217.209.71.84",
	"81.216.244.204", "66.249.72.71", "81.227.208.69", "81.216.117.39", "212.181.128.16", "83.254.225.255", "85.228.200.59", "85.226.179.157", "217.208.56.238", "217.198.145.16",
	"77.241.128.90", "213.65.146.159", "82.197.237.141", "83.188.218.38", "212.181.138.137", "213.114.114.134", "81.156.238.248", "83.254.97.189", "79.102.74.133", "84.208.98.21",
	"85.228.156.52", "90.228.219.115", "90.235.3.196", "84.244.234.44", "81.226.240.243", "90.235.0.201", "213.114.64.34", "83.178.151.22", "85.224.225.45", "90.230.203.246",
	"212.27.8.225", "83.241.200.123", "84.217.39.249", "81.233.248.247", "212.112.39.227", "78.69.48.91", "83.241.200.123", "81.234.246.67", "83.216.98.24", "85.225.225.107",
	"212.181.175.242", "195.252.57.181", "90.230.10.249", "212.214.188.94", "90.239.66.47", "81.225.165.8", "213.67.203.246", "213.66.218.27", "81.233.180.162", "83.252.74.253",
	"213.112.96.158", "85.224.5.7", "217.67.93.10", "217.208.111.10", "79.102.97.83", "90.229.245.69", "85.226.188.71", "83.183.210.247", "81.235.196.40", "74.6.22.106",
	"90.229.145.110", "80.217.136.122", "90.230.27.158", "213.113.174.249", "79.142.218.71", "80.70.145.208", "90.224.211.45", "85.226.144.78", "81.231.233.167", "81.225.90.218",
	"74.6.22.106", "90.230.159.217", "78.82.205.164", "80.213.18.209", "66.249.72.71", "92.32.94.117", "82.197.237.103", "217.10.120.77", "217.67.93.67", "83.253.96.133",
	"212.73.172.70", "90.224.51.118", "81.216.158.175", "90.224.186.127", "212.181.158.92", "213.113.229.76", "85.194.136.69", "81.236.5.119", "81.233.228.243", "81.237.222.181",
	"90.184.73.203", "84.217.151.44", "81.230.91.103", "85.224.168.53", "83.140.166.35", "81.232.122.212", "81.229.144.89", "74.6.22.106", "83.183.17.201", "85.24.134.209",
	"81.234.143.12", "85.225.252.123", "81.228.159.22", "212.116.91.78", "213.89.254.13", "83.241.200.123", "217.211.240.249", "81.236.219.138", "82.209.142.141", "74.6.22.106",
	"213.112.221.119", "195.216.42.202", "66.249.72.71", "90.227.217.131", "85.228.159.98", "90.224.1.152", "213.64.1.128", "87.227.117.246", "81.233.20.12", "83.254.156.253",
	"84.217.53.19", "83.227.51.31", "83.209.99.203", "85.165.55.20", "90.231.1.59", "77.53.6.20", "90.230.117.54", "80.217.77.97", "83.226.68.73", "83.241.200.123",
	"213.112.162.85", "83.227.72.69", "90.230.129.220", "85.230.4.226", "90.229.208.125", "84.217.151.44", "90.230.132.176", "213.89.128.191", "79.102.35.48", "85.224.70.181",
	"79.138.142.229", "81.233.118.177", "81.231.34.241", "213.64.134.103", "83.209.46.70", "91.149.34.78", "85.30.188.233", "79.138.182.105", "85.8.38.218", "77.74.134.243",
	"87.227.1.47", "83.252.90.137", "194.71.181.8", "84.217.139.238", "85.226.85.129", "72.14.193.165", "213.113.174.249", "90.231.212.28", "83.233.129.192", "90.229.145.239",
	"83.255.66.68", "83.227.204.141", "83.145.56.53", "90.227.8.175", "62.20.136.94", "79.138.197.42", "85.228.105.61", "90.224.176.33", "85.224.208.249", "90.235.16.208",
	"194.192.81.89", "83.252.94.46", "90.231.193.135", "74.6.22.106", "87.227.6.106", "80.216.154.10", "194.117.188.249", "84.216.46.174", "217.209.46.170", "83.223.29.52",
	"85.225.211.20", "81.224.218.150", "90.231.192.129", "81.230.24.194", "195.252.60.161", "83.250.36.226", "90.229.250.107", "66.249.72.71", "80.217.11.227", "87.227.114.41",
	"81.236.241.124", "85.224.246.172", "90.231.63.129", "194.192.81.89", "83.140.193.203", "81.216.185.133", "217.213.143.7", "87.227.0.42", "85.230.168.162", "81.233.162.206",
	"213.100.246.204", "66.249.72.71", "212.27.11.4", "213.112.93.42", "90.231.189.147", "74.6.22.106", "83.254.163.35", "213.114.114.176", "81.231.233.167", "81.231.233.167",
	"90.231.210.62", "78.70.217.189", "83.248.53.42", "81.233.26.50", "83.241.200.123", "90.230.96.115", "81.236.134.240", "85.226.7.93", "195.67.214.205", "83.183.81.96",
	"85.227.144.60", "85.228.211.102", "90.229.208.125", "78.70.146.82", "217.213.132.221", "217.209.40.246", "81.233.245.110", "82.99.42.124", "85.224.146.229", "83.249.133.153",
	"83.253.231.59", "90.227.51.245", "90.230.60.109", "85.231.55.104", "90.227.195.52", "78.70.139.125", "78.69.144.195", "81.226.37.235", "213.66.21.253", "217.27.177.36",
	"83.226.111.117", "213.67.81.93", "85.226.161.7", "90.235.139.43", "90.228.250.160", "83.183.182.11", "85.194.148.28", "66.249.72.71", "85.30.172.61", "81.226.218.153",
	"81.229.225.241", "83.181.15.182", "85.197.139.92", "213.226.119.199", "84.217.157.251", "90.235.136.37", "83.249.99.45", "79.102.100.161", "85.224.81.181", "83.183.48.1",
	"85.89.19.99", "83.252.55.95", "81.229.58.83", "81.228.255.230", "90.228.206.235", "81.235.205.141", "213.66.176.97", "81.230.253.153", "83.233.173.163", "90.235.134.251",
	"78.70.150.149", "83.254.67.72", "79.136.41.14", "194.17.16.46", "81.226.26.86", "213.64.27.160", "212.37.119.92", "217.210.100.245", "81.226.179.55", "83.241.200.123",
	"81.227.124.44", "83.188.203.70", "78.156.198.168", "83.252.216.23", "90.227.66.148", "85.11.14.58", "90.227.157.117", "81.224.30.80", "85.226.99.247", "83.251.26.238",
	"83.255.129.19", "66.249.72.71", "83.188.206.110", "194.16.88.66", "90.224.63.183", "66.249.72.71", "81.230.117.89", "81.229.64.59", "144.95.32.9", "213.114.147.122",
	"89.160.72.148", "67.195.37.98", "81.231.38.135", "83.253.23.223", "90.227.6.41", "83.216.98.24", "83.248.225.240", "81.228.153.228", "85.226.167.178", "81.216.78.86",
	"131.116.254.200", "85.224.17.8", "81.216.113.80", "213.113.41.23", "83.252.72.97", "81.236.143.200", "83.241.200.123", "81.230.95.7", "85.225.161.63", "90.227.163.212",
	"78.69.209.186", "90.231.207.179", "81.216.146.149", "85.228.167.68", "147.186.254.241", "84.218.38.122", "213.141.85.120", "89.233.241.214", "85.226.144.78", "195.165.240.129",
	"79.102.118.26", "81.227.236.144", "213.226.93.57", "79.185.88.49", "85.230.55.89", "85.226.144.78", "90.224.96.28", "83.251.191.152", "83.241.200.123", "85.225.108.58",
	"90.235.136.46", "217.210.90.2", "213.113.17.24", "90.229.173.192", "78.70.112.58", "88.206.143.132", "83.241.200.123", "79.133.18.237", "83.250.154.14", "81.233.21.77",
	"91.149.43.93", "91.95.202.134", "83.188.198.154", "90.230.198.117", "81.226.196.67", "83.150.152.114", "84.217.255.139", "83.241.200.123", "83.188.210.111", "213.112.69.243",
	"83.183.106.239", "81.236.226.217", "81.229.25.118", "81.235.1.51", "83.254.160.226", "213.100.91.123", "83.253.89.4", "81.225.165.8", "84.217.151.84", "78.64.4.62",
	"90.228.245.236", "90.239.66.61", "85.226.183.242", "81.227.27.92", "85.228.119.45", "81.233.18.221", "62.108.223.157", "83.251.187.64", "213.89.128.93", "85.231.203.120",
	"87.227.107.65", "90.224.89.251", "213.199.78.169", "79.136.34.59", "81.234.252.175", "195.38.2.106", "83.241.200.123", "66.249.72.71", "130.244.202.210", "212.247.210.127",
	"83.178.16.33", "192.121.192.22", "83.255.2.255", "84.217.46.86", "91.95.208.67", "74.6.22.106", "212.214.30.20", "79.102.128.74", "213.114.233.104", "81.236.5.38",
	"83.188.214.251", "90.235.1.176", "81.232.150.157", "79.138.184.247", "81.8.244.154", "78.156.196.204", "148.160.187.113", "213.65.32.36", "81.235.200.160", "81.229.94.18",
	"90.227.67.134", "90.230.49.217", "217.211.230.53", "81.224.85.95", "83.241.200.123", "213.67.80.194", "83.250.76.75", "88.131.85.203", "85.226.210.185", "85.224.227.38",
	"90.231.232.222", "217.210.128.245", "85.229.79.68", "213.67.22.179", "83.216.98.24", "217.208.119.147", "83.254.52.191", "83.188.205.121", "92.32.104.8", "83.233.200.151",
	"81.224.84.204", "90.231.232.200", "83.233.219.134", "90.230.127.108", "90.239.87.17", "193.221.47.232", "90.224.246.28", "90.229.205.99", "213.100.98.251", "81.233.36.165",
	"83.176.248.70", "90.235.13.65", "81.233.236.55", "85.224.3.162", "81.224.219.142", "213.89.236.122", "85.228.202.15", "83.188.208.164", "83.250.147.82", "74.6.22.106",
	"212.242.145.158", "74.6.22.106", "85.226.144.78", "85.230.13.221", "81.170.241.47", "81.233.94.74", "83.251.51.88", "78.69.44.138", "85.225.248.221", "83.209.129.247",
	"85.225.28.127", "81.236.176.35", "82.182.62.83", "148.160.65.125", "82.197.245.192", "213.114.82.156", "90.239.74.160", "85.30.174.96", "74.6.22.106", "90.224.184.35",
	"83.248.100.54", "85.236.95.225", "81.233.123.41", "78.69.147.206", "193.221.74.5", "81.233.99.27", "81.225.6.30", "90.231.234.168", "74.6.22.106", "91.95.8.6",
	"69.147.112.168", "69.147.112.168", "78.69.159.222", "69.147.112.168", "69.147.112.168", "74.6.22.106", "81.235.202.241", "80.222.133.172", "88.83.44.79", "81.229.118.186",
	"83.253.234.124", "81.232.173.251", "217.73.105.44", "83.188.195.83", "213.113.65.156", "80.216.48.179", "81.227.10.99", "213.112.123.22", "81.230.80.185", "62.20.171.220",
	"77.53.6.20", "217.209.166.24", "74.6.22.106", "82.182.146.204", "84.216.93.227", "85.11.55.126", "82.196.104.90", "83.176.248.52", "84.217.32.220", "90.231.46.195",
	"83.176.248.70", "84.216.50.92", "85.225.183.177", "83.183.231.16", "213.101.34.6", "81.233.54.133", "84.217.21.102", "85.228.186.175", "85.226.197.92", "81.170.237.175",
	"62.63.222.219", "80.216.152.204", "81.8.215.240", "82.182.100.165", "85.228.145.124", "212.181.162.140", "85.229.63.178", "194.17.178.160", "83.183.8.165", "193.210.65.69",
	"213.89.72.82", "78.82.241.231", "81.229.113.67", "81.191.85.227", "85.225.31.237", "66.249.72.71", "81.226.250.130", "78.82.192.201", "194.237.160.252", "83.253.132.147",
	"217.151.56.58", "69.147.112.168",
}

func TestHashFirst(t *testing.T) {
	N := 6
	R := 0

	sb := Service{Retries: R, FailCost: 100, SoftFailCost: 100, Strat: StratHash}

	counts := make([]int, N)
	for i := range counts {
		sb.AddNode(&counts[i], 10)
	}

	allcount := 0
	for _, a := range testAddrs {
		conn := sb.NewConn([]byte(a))
		allcount += deplete(conn)
		conn.Close()
	}

	if allcount != len(testAddrs) {
		t.Errorf("Expected allcount %v, got %v\n", len(testAddrs), allcount)
	}

	for i, c := range counts {
		if c < 140 || c > 200 {
			t.Errorf("Index %v, expected between 140 and 200, got %v", i, c)
		}
	}

	fmt.Println("Hash, equal cost")
	fmt.Printf("allcount: %d\n", allcount)
	fmt.Printf("counts: %v\n", counts)
}

func TestHashReinit(t *testing.T) {
	N := 6
	R := 0

	counts := make([]int, N)

	allcount := 0
	for _, a := range testAddrs {
		sb := Service{Retries: R, FailCost: 100, SoftFailCost: 100, Strat: StratHash}
		for i := range counts {
			sb.AddNode(&counts[i], 10)
		}

		conn := sb.NewConn([]byte(a))
		allcount += deplete(conn)
		conn.Close()
	}

	if allcount != len(testAddrs) {
		t.Errorf("Expected allcount %v, got %v\n", len(testAddrs), allcount)
	}

	for i, c := range counts {
		if c < 140 || c > 200 {
			t.Errorf("Index %v, expected between 140 and 200, got %v", i, c)
		}
	}

	fmt.Println("Hash, equal cost, reinited")
	fmt.Printf("allcount: %d\n", allcount)
	fmt.Printf("counts: %v\n", counts)
}

func TestHashSameValue(t *testing.T) {
	N := 6
	R := 0

	sb := Service{Retries: R, FailCost: 100, SoftFailCost: 100, Strat: StratHash}

	counts := make([]int, N)
	for i := range counts {
		sb.AddNode(&counts[i], 10)
	}

	allcount := 0
	for i := 0; i < 100; i++ {
		conn := sb.NewConn([]byte("127.0.0.1"))
		allcount += deplete(conn)
		conn.Close()
	}

	if allcount != 100 {
		t.Errorf("Expected allcount %v, got %v\n", 100, allcount)
	}

	buckets := 0
	for _, c := range counts {
		if c > 0 {
			buckets++
		}
	}
	if buckets != 1 {
		t.Errorf("Expected a single bucket used, got %v", buckets)
	}

	fmt.Println("Hash same")
	fmt.Printf("allcount: %d\n", allcount)
	fmt.Printf("counts: %v\n", counts)
}
