예제 #1
0
파일: registry.go 프로젝트: st3v/discovery
func (r *registry) Register(i discovery.Instance, ttl uint64) error {
	_, err := r.client.Set(
		r.storePath(i),
		i.String(),
		ttl,
	)
	return err
}
예제 #2
0
func TestRegister(t *testing.T) {
	var (
		namespace = "namespace"
		machines  = []string{"foo", "bar"}
		ttl       = uint64(1234)
		instance  = discovery.Instance{
			ID:     "id",
			Name:   "name",
			Env:    "env",
			Region: "region",
			Host:   "host",
			Port:   987,
		}
		path   = "namespace/region/env/name/id"
		json   = instance.String()
		client = new(fake.EtcdClient)
	)

	oldEtcdClient := injectClient(client, machines, t)
	defer func() { etcdClient = oldEtcdClient }()

	registry := NewRegistry(machines, namespace)

	err := registry.Register(instance, ttl)
	if want, have := error(nil), err; want != have {
		t.Fatalf("want %v, have %v", want, have)
	}

	if want, have := 1, client.SetCallCount(); want != have {
		t.Fatalf("want %v, have %v", want, have)
	}

	actualPath, actualJSON, actualTTL := client.SetArgsForCall(0)

	if want, have := path, actualPath; want != have {
		t.Fatalf("want %v, have %v", want, have)
	}

	if want, have := json, actualJSON; want != have {
		t.Fatalf("want %v, have %v", want, have)
	}

	if want, have := ttl, actualTTL; want != have {
		t.Fatalf("want %v, have %v", want, have)
	}
}