Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add context to get #96

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## To Be Released

* Add a context as parameter to the Get method

## v7.1.2

* fix(host): automatically find scheme
Expand Down
10 changes: 6 additions & 4 deletions service/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (

// ServiceResponse is the interface used to provide a response to the service.Get() Method.
// This interface provide a standard API used for method chaining like:
// url, err := Get("my-service").First().URL()
//
// url, err := Get(ctx, "my-service").First().URL()
//
// To provide such API, go errors need to be stored and sent at the last moment.
// To do so, each "final" method (like Url or All), will check if the Response is errored, before
Expand All @@ -30,12 +31,13 @@ type ServiceResponse interface {
}

// Get a service by its name. This method does not directly return the Service, but a ServiceResponse. This permit method chaining like:
// url, err := Get("my-service").First().URL()
//
// url, err := Get(ctx, "my-service").First().URL()
//
// If there was an error during the acquisition of the service, this error will be stored in the ServiceResponse. Final methods will check for this error before doing actual logic.
// If the service is not found, we won't render an error, but will return a service with minimal informations. This is done to provide maximal backwerd compatibility since older versions does not register themself to the "/services_infos" directory.
func Get(service string) ServiceResponse {
res, err := KAPI().Get(context.Background(), "/services_infos/"+service, nil)
func Get(ctx context.Context, service string) ServiceResponse {
res, err := KAPI().Get(ctx, "/services_infos/"+service, nil)

if err != nil {
if etcd.IsKeyNotFound(err) {
Expand Down
10 changes: 6 additions & 4 deletions service/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
func TestGetNoHost(t *testing.T) {
Convey("Without any service", t, func() {
Convey("Get should return an empty slice", func() {
hosts, err := Get("test_no_service").All()
ctx := context.Background()
hosts, err := Get(ctx, "test_no_service").All()
So(err, ShouldBeNil)
So(len(hosts), ShouldEqual, 0)
})
Expand All @@ -20,8 +21,9 @@ func TestGetNoHost(t *testing.T) {

func TestGet(t *testing.T) {
Convey("With registred services", t, func() {
ctx1, cancel1 := context.WithCancel(context.Background())
ctx2, cancel2 := context.WithCancel(context.Background())
ctx := context.Background()
ctx1, cancel1 := context.WithCancel(ctx)
ctx2, cancel2 := context.WithCancel(ctx)
host1, host2 := genHost("host1"), genHost("host2")
host1.Name = "test_service_get"
host2.Name = "test_service_get"
Expand All @@ -30,7 +32,7 @@ func TestGet(t *testing.T) {
w1.WaitRegistration()
w2.WaitRegistration()
Convey("We should have 2 hosts", func() {
hosts, err := Get("test_service_get").All()
hosts, err := Get(ctx, "test_service_get").All()
So(err, ShouldBeNil)
So(len(hosts), ShouldEqual, 2)
if hosts[0].UUID == w1.UUID() {
Expand Down
2 changes: 1 addition & 1 deletion service/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestRegister(t *testing.T) {
host.PrivatePorts = Ports{}
w := Register(context.Background(), "hello_world2", host)
w.WaitRegistration()
h, err := Get("hello_world2").First().Host()
h, err := Get(context.Background(), "hello_world2").First().Host()
So(err, ShouldBeNil)
So(len(h.PrivatePorts), ShouldEqual, 1)
})
Expand Down
42 changes: 26 additions & 16 deletions service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (

func TestServiceAll(t *testing.T) {
Convey("With no services", t, func() {
s, err := Get("service-test-get-1").Service()
ctx := context.Background()
s, err := Get(ctx, "service-test-get-1").Service()
So(err, ShouldBeNil)

hosts, err := s.All()
Expand All @@ -18,15 +19,17 @@ func TestServiceAll(t *testing.T) {
})

Convey("With two services", t, func() {
ctx := context.Background()
host1 := genHost("test1")
host2 := genHost("test2")
w1 := Register(context.Background(), "test-get-222", host1)
w2 := Register(context.Background(), "test-get-222", host2)
w1 := Register(ctx, "test-get-222", host1)
w2 := Register(ctx, "test-get-222", host2)

w1.WaitRegistration()
w2.WaitRegistration()

s, err := Get("test-get-222").Service()
s, err := Get(ctx, "test-get-222").Service()
So(err, ShouldBeNil)
hosts, err := s.All()
So(err, ShouldBeNil)
So(len(hosts), ShouldEqual, 2)
Expand All @@ -41,7 +44,8 @@ func TestServiceAll(t *testing.T) {

func TestServiceFirst(t *testing.T) {
Convey("With no services", t, func() {
s, err := Get("service-test-1").Service()
ctx := context.Background()
s, err := Get(ctx, "service-test-1").Service()
So(err, ShouldBeNil)
host, err := s.First()
So(err, ShouldNotBeNil)
Expand All @@ -50,11 +54,12 @@ func TestServiceFirst(t *testing.T) {
})

Convey("With a service", t, func() {
ctx := context.Background()
host1 := genHost("test1")
w := Register(context.Background(), "test-truc", host1)
w := Register(ctx, "test-truc", host1)
w.WaitRegistration()

s, err := Get("test-truc").Service()
s, err := Get(ctx, "test-truc").Service()
So(err, ShouldBeNil)
host, err := s.First()
So(err, ShouldBeNil)
Expand All @@ -65,7 +70,8 @@ func TestServiceFirst(t *testing.T) {

func TestServiceOne(t *testing.T) {
Convey("With no services", t, func() {
s, err := Get("service-test-1").Service()
ctx := context.Background()
s, err := Get(ctx, "service-test-1").Service()
So(err, ShouldBeNil)
host, err := s.One()
So(err, ShouldNotBeNil)
Expand All @@ -74,11 +80,12 @@ func TestServiceOne(t *testing.T) {
})

Convey("With a service", t, func() {
ctx := context.Background()
host1 := genHost("test1")
w := Register(context.Background(), "test-truc", host1)
w := Register(ctx, "test-truc", host1)
w.WaitRegistration()

s, err := Get("test-truc").Service()
s, err := Get(ctx, "test-truc").Service()
So(err, ShouldBeNil)
host, err := s.One()
So(err, ShouldBeNil)
Expand All @@ -90,37 +97,40 @@ func TestServiceOne(t *testing.T) {
func TestServiceUrl(t *testing.T) {
Convey("With a public service", t, func() {
Convey("With a service without any password", func() {
ctx := context.Background()
host := genHost("test")
host.User = ""
host.Password = ""
w := Register(context.Background(), "service-url-1", host)
w := Register(ctx, "service-url-1", host)
w.WaitRegistration()

s, err := Get("service-url-1").Service()
s, err := Get(ctx, "service-url-1").Service()
So(err, ShouldBeNil)
url, err := s.URL("http", "/path")
So(err, ShouldBeNil)
So(url, ShouldEqual, "http://public.dev:10000/path")
})

Convey("With a host with a password", func() {
ctx := context.Background()
host := genHost("test")
w := Register(context.Background(), "service-url-3", host)
w := Register(ctx, "service-url-3", host)
w.WaitRegistration()

s, err := Get("service-url-3").Service()
s, err := Get(ctx, "service-url-3").Service()
So(err, ShouldBeNil)
url, err := s.URL("http", "/path")
So(err, ShouldBeNil)
So(url, ShouldEqual, "http://user:[email protected]:10000/path")
})

Convey("When the port does'nt exists", func() {
ctx := context.Background()
host := genHost("test")
w := Register(context.Background(), "service-url-4", host)
w := Register(ctx, "service-url-4", host)
w.WaitRegistration()

s, err := Get("service-url-4").Service()
s, err := Get(ctx, "service-url-4").Service()
So(err, ShouldBeNil)
url, err := s.URL("htjp", "/path")
So(err, ShouldNotBeNil)
Expand Down