Exemple #1
0
func TestUnregisterAll(t *testing.T) {
	client, cleanup := testutil.SetupDiscoverd(t)
	defer cleanup()

	serviceName := "unregisterAllTest"

	assert(client.Register(serviceName, ":1111"), t)
	assert(client.Register(serviceName, ":2222"), t)
	assert(client.Register(serviceName, ":3333"), t)

	services, err := client.Services(serviceName, 1)
	assert(err, t)
	if len(services) != 3 {
		t.Fatal("Wrong number of services")
	}

	assert(client.UnregisterAll(), t)

	set, err := client.NewServiceSet(serviceName)
	assert(err, t)

	if len(set.Services()) != 0 {
		t.Fatal("There should be no services")
	}

	assert(set.Close(), t)

}
Exemple #2
0
func TestRegisterWithSet(t *testing.T) {
	client, cleanup := testutil.SetupDiscoverd(t)
	defer cleanup()

	serviceName := "registerWithSetTest"

	assert(client.Register(serviceName, ":1111"), t)

	set, err := client.RegisterWithSet(serviceName, ":2222", nil)
	assert(err, t)

	if len(set.Services()) != 1 {
		t.Fatal("There should only be one other service")
	}
	if set.Services()[0].Addr != "127.0.0.1:1111" {
		t.Fatal("Set contains the wrong service")
	}

	assert(set.Close(), t)

	services, err := client.Services(serviceName, 1)
	assert(err, t)
	if len(services) != 2 {
		t.Fatal("Not all registered services were returned:", services)
	}
}
Exemple #3
0
func TestServiceAge(t *testing.T) {
	client, cleanup := testutil.SetupDiscoverd(t)
	defer cleanup()

	serviceName := "ageTest"

	checkOldest := func(addr string) {
		services, err := client.Services(serviceName, 1)
		assert(err, t)
		if services[0].Addr != "127.0.0.1"+addr {
			t.Fatal("Oldest service is not first in Services() slice")
		}
	}

	assert(client.Register(serviceName, ":1111"), t)
	checkOldest(":1111")
	assert(client.Register(serviceName, ":2222"), t)
	checkOldest(":1111")
	assert(client.Register(serviceName, ":3333"), t)
	checkOldest(":1111")
	assert(client.Register(serviceName, ":4444"), t)
	checkOldest(":1111")
	assert(client.Unregister(serviceName, ":1111"), t)
	checkOldest(":2222")

}
Exemple #4
0
func TestHeartbeat(t *testing.T) {
	client, cleanup := testutil.SetupDiscoverd(t)
	defer cleanup()

	serviceName := "heartbeatTest"
	assert(client.Register(serviceName, ":1111"), t)
	time.Sleep(12 * time.Second) // wait for one heartbeat
	services, err := client.Services(serviceName, 1)
	assert(err, t)
	if len(services) != 1 {
		t.Fatal("Missing services")
	}
}
Exemple #5
0
func TestServices(t *testing.T) {
	client, cleanup := testutil.SetupDiscoverd(t)
	defer cleanup()

	serviceName := "servicesTest"

	assert(client.Register(serviceName, ":1111"), t)
	assert(client.Register(serviceName, ":2222"), t)

	services, err := client.Services(serviceName, 1)
	assert(err, t)
	if len(services) != 2 {
		t.Fatal("Not all registered services were returned:", services)
	}
}