本文整理匯總了Golang中github.com/flynn/flynn/discoverd/testutil.SetupDiscoverd函數的典型用法代碼示例。如果您正苦於以下問題:Golang SetupDiscoverd函數的具體用法?Golang SetupDiscoverd怎麽用?Golang SetupDiscoverd使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了SetupDiscoverd函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestDefaultClient
func TestDefaultClient(t *testing.T) {
_, cleanup := testutil.SetupDiscoverd(t)
defer cleanup()
serviceName := "defaultClientTest"
assert(discoverd.Register(serviceName, ":1111"), t)
assert(discoverd.Register(serviceName, ":2222"), t)
assert(discoverd.Register(serviceName, ":3333"), t)
services, err := discoverd.Services(serviceName, 1)
assert(err, t)
if len(services) != 3 {
t.Fatal("Wrong number of services")
}
assert(discoverd.UnregisterAll(), t)
set, err := discoverd.NewServiceSet(serviceName)
assert(err, t)
if len(set.Services()) != 0 {
t.Fatal("There should be no services")
}
assert(set.Close(), t)
discoverd.DefaultClient.Close()
discoverd.DefaultClient = nil
}
示例2: TestUnregisterAll
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)
}
示例3: TestRegisterWithSetLeaderSelf
func TestRegisterWithSetLeaderSelf(t *testing.T) {
client, cleanup := testutil.SetupDiscoverd(t)
defer cleanup()
serviceName := "registerWithSetLeaderSelfTest"
assert(client.Register(serviceName, ":1111"), t)
set, err := client.RegisterWithSet(serviceName, ":2222", nil)
assert(err, t)
leader := make(chan *discoverd.Service, 2)
go func() {
leaders := set.Leaders()
for {
leader <- <-leaders
}
}()
assert(client.Register(serviceName, ":3333"), t)
if (<-leader).Addr != "127.0.0.1:1111" {
t.Fatal("Incorrect leader")
}
assert(client.Unregister(serviceName, ":1111"), t)
if (<-leader).Addr != set.SelfAddr() {
t.Fatal("Incorrect leader", leader)
}
assert(set.Close(), t)
}
示例4: TestServiceAge
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")
}
示例5: TestWatch
func TestWatch(t *testing.T) {
client, cleanup := testutil.SetupDiscoverd(t)
defer cleanup()
serviceName := "watchTest"
assert(client.Register(serviceName, ":1111"), t)
assert(client.Register(serviceName, ":2222"), t)
set, err := client.NewServiceSet(serviceName)
assert(err, t)
updates := set.Watch(true)
assert(client.Register(serviceName, ":3333"), t)
for i := 0; i < 3; i++ {
var update *agent.ServiceUpdate
select {
case update = <-updates:
case <-time.After(3 * time.Second):
t.Fatal("Timeout exceeded", i)
}
if update.Online != true {
t.Fatal("Service update of unexected status: ", update, i)
}
if update.Name != serviceName {
t.Fatal("Service update of unexected name: ", update, i)
}
}
assert(set.Close(), t)
}
示例6: TestBasicRegisterAndServiceSet
func TestBasicRegisterAndServiceSet(t *testing.T) {
client, cleanup := testutil.SetupDiscoverd(t)
defer cleanup()
serviceName := "basicTest"
assert(client.RegisterWithAttributes(serviceName, ":1111", map[string]string{"foo": "bar"}), t)
assert(client.Register(serviceName, ":2222"), t)
set, err := client.NewServiceSet(serviceName)
assert(err, t)
waitUpdates(t, set, true, 2)()
if len(set.Services()) < 2 || len(set.Addrs()) < 2 {
t.Fatal("Registered services not online")
}
wait := waitUpdates(t, set, false, 1)
assert(client.Unregister(serviceName, ":2222"), t)
wait()
if len(set.Services()) != 1 || len(set.Addrs()) != 1 {
t.Fatal("Only 1 registered service should be left")
}
if set.Services()[0].Attrs["foo"] != "bar" {
t.Fatal("Attribute not set on service as 'bar'")
}
assert(set.Close(), t)
}
示例7: TestRegisterWithSet
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)
}
}
示例8: TestHeartbeat
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")
}
}
示例9: TestNoServices
func TestNoServices(t *testing.T) {
client, cleanup := testutil.SetupDiscoverd(t)
defer cleanup()
set, err := client.NewServiceSet("nonexistent")
assert(err, t)
if len(set.Services()) != 0 {
t.Fatal("There should be no services")
}
assert(set.Close(), t)
}
示例10: TestServices
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)
}
}
示例11: TestLeaderChannel
func TestLeaderChannel(t *testing.T) {
client, cleanup := testutil.SetupDiscoverd(t)
defer cleanup()
serviceName := "leadersTest"
assert(client.Register(serviceName, ":1111"), t)
set, err := client.NewServiceSet(serviceName)
assert(err, t)
leader := make(chan *discoverd.Service, 3)
go func() {
leaders := set.Leaders()
for {
leader <- <-leaders
}
}()
if (<-leader).Addr != "127.0.0.1:1111" {
t.Fatal("Incorrect leader")
}
assert(client.Unregister(serviceName, ":1111"), t)
if (<-leader) != nil {
t.Fatal("Incorrect leader")
}
assert(client.Register(serviceName, ":2222"), t)
assert(client.Register(serviceName, ":3333"), t)
if (<-leader).Addr != "127.0.0.1:2222" {
t.Fatal("Incorrect leader", leader)
}
assert(client.Unregister(serviceName, ":2222"), t)
if (<-leader).Addr != "127.0.0.1:3333" {
t.Fatal("Incorrect leader")
}
assert(set.Close(), t)
}
示例12: TestSelecting
func TestSelecting(t *testing.T) {
client, cleanup := testutil.SetupDiscoverd(t)
defer cleanup()
serviceName := "selectTest"
set, err := client.NewServiceSet(serviceName)
assert(err, t)
assert(client.Register(serviceName, ":1111"), t)
assert(client.RegisterWithAttributes(serviceName, ":2222", map[string]string{"foo": "qux", "id": "2"}), t)
assert(client.RegisterWithAttributes(serviceName, ":3333", map[string]string{"foo": "qux", "id": "3"}), t)
waitUpdates(t, set, true, 3)()
if s := set.Select(map[string]string{"id": "3"}); len(s) != 1 {
t.Fatalf("Expected one service, got: %#v", s)
}
assert(set.Close(), t)
}
示例13: TestRegisterAndStandby
func TestRegisterAndStandby(t *testing.T) {
client, cleanup := testutil.SetupDiscoverd(t)
defer cleanup()
serviceName := "registerAndStandbyTest"
assert(client.Register(serviceName, ":1111"), t)
standbyCh, err := client.RegisterAndStandby(serviceName, ":2222", nil)
assert(err, t)
assert(client.Register(serviceName, ":3333"), t)
assert(client.Unregister(serviceName, ":3333"), t)
assert(client.Unregister(serviceName, ":1111"), t)
leader := <-standbyCh
if leader.Addr != "127.0.0.1:2222" {
t.Fatal("Incorrect leader", leader)
}
}
示例14: TestNewAttributes
func TestNewAttributes(t *testing.T) {
client, cleanup := testutil.SetupDiscoverd(t)
defer cleanup()
serviceName := "attributeTest"
set, err := client.NewServiceSet(serviceName)
assert(err, t)
assert(client.RegisterWithAttributes(serviceName, ":1111", map[string]string{"foo": "bar"}), t)
waitUpdates(t, set, true, 1)()
wait := waitUpdates(t, set, false, 1)
assert(client.RegisterWithAttributes(serviceName, ":1111", map[string]string{"foo": "baz"}), t)
wait()
if s := set.Services()[0]; s.Attrs["foo"] != "baz" {
t.Fatalf(`Expected attribute set on re-registered service to be "baz", not %q`, s.Attrs["foo"])
}
assert(set.Close(), t)
}
示例15: TestHTTPClient
func TestHTTPClient(t *testing.T) {
client, cleanup := testutil.SetupDiscoverd(t)
defer cleanup()
hc := dialer.NewHTTPClient(client)
_, err := hc.Get("http://httpclient/")
if ue, ok := err.(*url.Error); !ok || ue.Err != balancer.ErrNoServices {
t.Error("Expected err to be ErrNoServices, got", ue.Err)
}
s := httptest.NewServer(nil)
defer s.Close()
client.Register("httpclient", s.URL[7:])
set, _ := discoverd.NewServiceSet("httpclient")
waitUpdates(t, set, true, 1)()
set.Close()
_, err = hc.Get("http://httpclient/")
if err != nil {
t.Error("Unexpected error during request:", err)
}
}