From fdae40e1bef0de0f99a15905df08670ca5463112 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Thomas?= Date: Fri, 5 May 2023 10:26:59 +0200 Subject: [PATCH 1/5] add context to get --- service/get.go | 10 ++++++---- service/get_test.go | 10 ++++++---- service/register_test.go | 2 +- service/service_test.go | 41 ++++++++++++++++++++++++---------------- 4 files changed, 38 insertions(+), 25 deletions(-) diff --git a/service/get.go b/service/get.go index 0cbbe75..f8a1cc5 100644 --- a/service/get.go +++ b/service/get.go @@ -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("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 @@ -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) { diff --git a/service/get_test.go b/service/get_test.go index 99b0d68..c45f63e 100644 --- a/service/get_test.go +++ b/service/get_test.go @@ -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) }) @@ -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" @@ -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() { diff --git a/service/register_test.go b/service/register_test.go index abb52a2..6a98497 100644 --- a/service/register_test.go +++ b/service/register_test.go @@ -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) }) diff --git a/service/service_test.go b/service/service_test.go index bc7b181..a853bd4 100644 --- a/service/service_test.go +++ b/service/service_test.go @@ -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() @@ -18,15 +19,16 @@ 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() hosts, err := s.All() So(err, ShouldBeNil) So(len(hosts), ShouldEqual, 2) @@ -41,7 +43,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) @@ -50,11 +53,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) @@ -65,7 +69,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) @@ -74,11 +79,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) @@ -90,13 +96,14 @@ 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) @@ -104,11 +111,12 @@ func TestServiceUrl(t *testing.T) { }) 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) @@ -116,11 +124,12 @@ func TestServiceUrl(t *testing.T) { }) 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) From 9d8eebe0232b70987982c72bc22e521b18529ae1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Thomas?= Date: Fri, 5 May 2023 10:31:49 +0200 Subject: [PATCH 2/5] linting --- service/service_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/service/service_test.go b/service/service_test.go index a853bd4..c1a82de 100644 --- a/service/service_test.go +++ b/service/service_test.go @@ -29,6 +29,7 @@ func TestServiceAll(t *testing.T) { w2.WaitRegistration() s, err := Get(ctx, "test-get-222").Service() + So(err, ShouldBeNil) hosts, err := s.All() So(err, ShouldBeNil) So(len(hosts), ShouldEqual, 2) From c7f76321218f24c489c2d1b722c1a74e2e9a5437 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Thomas?= Date: Fri, 5 May 2023 10:33:26 +0200 Subject: [PATCH 3/5] update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 51d849e..614af44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## To Be Released +* Add a context as parameter to the Get method ## v7.1.2 * fix(host): automatically find scheme From bf4f95d8c5e921070c1ef2617966d733fcee98a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Thomas?= Date: Fri, 5 May 2023 10:35:00 +0200 Subject: [PATCH 4/5] fix: usage in comments --- service/get.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service/get.go b/service/get.go index f8a1cc5..9279d7d 100644 --- a/service/get.go +++ b/service/get.go @@ -10,7 +10,7 @@ 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 From 5a6fe43a32267ee10ae15c293e07ab6738349371 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Thomas?= Date: Fri, 5 May 2023 10:49:16 +0200 Subject: [PATCH 5/5] update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 614af44..8561d9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## To Be Released * Add a context as parameter to the Get method + ## v7.1.2 * fix(host): automatically find scheme