Exemplo n.º 1
0
func TestRoundRobinStrategy(t *testing.T) {
	lib, err := dns.NewDefaultLookupLib()
	assert.Nil(t, err)

	//given
	c := lb.NewGeneric(&lb.Config{
		Dns:      lib,
		Strategy: lb.RoundRobinStrategy,
	})

	// when
	srvName := "foo.service.fligl.io"
	add1, err1 := c.Next(srvName)
	add2, err2 := c.Next(srvName)

	// then
	assert.Nil(t, err1)
	assert.Nil(t, err2)

	adds := []dns.Address{add1, add2}
	sort.Sort(ByConnectionString(adds))

	expected := []dns.Address{dns.Address{Address: "0.1.2.3", Port: 8001}, dns.Address{Address: "4.5.6.7", Port: 8002}}
	assert.Equal(t, expected, adds, "unexpected results")
}
Exemplo n.º 2
0
func DefaultConfig() *Config {
	var dnsLib dns.Lookup

	if dnsHost := os.Getenv("SRVLB_HOST"); dnsHost != "" {
		dnsLib = dns.NewLookupLib(dnsHost)
	} else {
		dnsLib = dns.NewDefaultLookupLib()
	}

	return &Config{
		Dns:      dnsLib,
		Strategy: RoundRobinStrategy,
	}
}
Exemplo n.º 3
0
func TestRandomStrategy(t *testing.T) {
	lib, err := dns.NewDefaultLookupLib()
	assert.Nil(t, err)

	//given
	c := lb.NewGeneric(&lb.Config{
		Dns:      lib,
		Strategy: random.RandomStrategy,
	})

	// when
	srvName := "foo.service.fligl.io"
	_, err = c.Next(srvName)

	// then
	assert.Nil(t, err)
}
Exemplo n.º 4
0
func DefaultConfig() (*Config, error) {
	var dnsLib dns.Lookup

	if dnsHost := os.Getenv("SRVLB_HOST"); dnsHost != "" {
		dnsLib = dns.NewLookupLib(dnsHost)
	} else {
		lib, err := dns.NewDefaultLookupLib()
		if err != nil {
			return nil, err
		}
		dnsLib = lib
	}

	return &Config{
		Dns:      dnsLib,
		Strategy: RoundRobinStrategy,
	}, nil
}
Exemplo n.º 5
0
func TestRoundRobinLookup(t *testing.T) {
	// given
	srvName := "foo.service.fligl.io"
	lib := dns.NewDefaultLookupLib()
	c := NewRoundRobinStrategy(lib)

	// when
	address, err := c.Next(srvName)

	// then
	assert.Nil(t, err)

	if address.Port == 8001 && address.Address == "0.1.2.3" {
		return
	} else if address.Port == 8002 && address.Address == "4.5.6.7" {
		return
	} else {
		t.Errorf("port '%d' not expected with address: '%s'", address.Port, address.Address)
	}

}