From 9ca40f25e815435b1a80e6983ec38cb3ae34d7b4 Mon Sep 17 00:00:00 2001 From: Phdevava Date: Thu, 29 Jun 2023 10:35:44 -0700 Subject: [PATCH 1/3] Changes to allow to set polling interval in start settings --- client/httpclient.go | 4 ++++ client/httpclient_test.go | 44 +++++++++++++++++++++++++++++++++-- client/types/startsettings.go | 5 ++++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/client/httpclient.go b/client/httpclient.go index 0fc34aa2..e1d31577 100644 --- a/client/httpclient.go +++ b/client/httpclient.go @@ -52,6 +52,10 @@ func (c *httpClient) Start(ctx context.Context, settings types.StartSettings) er c.sender.EnableCompression() } + if settings.PollingIntervalMs != nil { + c.sender.SetPollingInterval(*settings.PollingIntervalMs) + } + // Prepare the first message to send. err := c.common.PrepareFirstMessage(ctx) if err != nil { diff --git a/client/httpclient_test.go b/client/httpclient_test.go index 4d2f2436..53ba68eb 100644 --- a/client/httpclient_test.go +++ b/client/httpclient_test.go @@ -9,11 +9,12 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" + "google.golang.org/protobuf/proto" + "github.com/open-telemetry/opamp-go/client/internal" "github.com/open-telemetry/opamp-go/client/types" "github.com/open-telemetry/opamp-go/protobufs" - "github.com/stretchr/testify/assert" - "google.golang.org/protobuf/proto" ) func TestHTTPPolling(t *testing.T) { @@ -93,3 +94,42 @@ func TestHTTPClientCompression(t *testing.T) { err := client.Stop(context.Background()) assert.NoError(t, err) } + +func TestHTTPClientSetPollingInterval(t *testing.T) { + // Start a Server. + srv := internal.StartMockServer(t) + var rcvCounter int64 + srv.OnMessage = func(msg *protobufs.AgentToServer) *protobufs.ServerToAgent { + assert.EqualValues(t, rcvCounter, msg.SequenceNum) + if msg != nil { + atomic.AddInt64(&rcvCounter, 1) + } + return nil + } + + // Start a client. + settings := types.StartSettings{ + PollingIntervalMs: func() *time.Duration { + tms := 101 * time.Millisecond + return &tms + }(), + } + settings.OpAMPServerURL = "http://" + srv.Endpoint + client := NewHTTP(nil) + prepareClient(t, &settings, client) + + assert.NoError(t, client.Start(context.Background(), settings)) + + // Verify that status report is delivered. + eventually(t, func() bool { return atomic.LoadInt64(&rcvCounter) == 1 }) + + // Verify that status report is delivered again. no call is made for next 100ms + assert.Eventually(t, func() bool { return atomic.LoadInt64(&rcvCounter) == 2 }, 5*time.Second, 100*time.Millisecond) + + // Shutdown the Server. + srv.Close() + + // Shutdown the client. + err := client.Stop(context.Background()) + assert.NoError(t, err) +} diff --git a/client/types/startsettings.go b/client/types/startsettings.go index b395afdb..e651bb35 100644 --- a/client/types/startsettings.go +++ b/client/types/startsettings.go @@ -3,6 +3,7 @@ package types import ( "crypto/tls" "net/http" + "time" "github.com/open-telemetry/opamp-go/protobufs" ) @@ -48,4 +49,8 @@ type StartSettings struct { // the compression is only effectively enabled if the Server also supports compression. // The data will be compressed in both directions. EnableCompression bool + + // Optional PollingIntervalMs to configure the polling interval for http client + // if nil uses the default polling interval else uses this value. + PollingIntervalMs *time.Duration } From 7937c52ae8356c4b4ddaa8b0f1f2205c8ff66112 Mon Sep 17 00:00:00 2001 From: Phdevava Date: Thu, 6 Jul 2023 08:22:02 -0700 Subject: [PATCH 2/3] moved polling interval setter into http client --- client/httpclient.go | 9 +++++---- client/httpclient_test.go | 8 ++------ client/types/startsettings.go | 5 ----- 3 files changed, 7 insertions(+), 15 deletions(-) diff --git a/client/httpclient.go b/client/httpclient.go index e1d31577..06c71a63 100644 --- a/client/httpclient.go +++ b/client/httpclient.go @@ -2,6 +2,7 @@ package client import ( "context" + "time" "github.com/open-telemetry/opamp-go/client/internal" "github.com/open-telemetry/opamp-go/client/types" @@ -52,10 +53,6 @@ func (c *httpClient) Start(ctx context.Context, settings types.StartSettings) er c.sender.EnableCompression() } - if settings.PollingIntervalMs != nil { - c.sender.SetPollingInterval(*settings.PollingIntervalMs) - } - // Prepare the first message to send. err := c.common.PrepareFirstMessage(ctx) if err != nil { @@ -117,3 +114,7 @@ func (c *httpClient) runUntilStopped(ctx context.Context) { c.common.Capabilities, ) } + +func (c *httpClient) SetPollingInterval(duration time.Duration) { + c.sender.SetPollingInterval(duration) +} diff --git a/client/httpclient_test.go b/client/httpclient_test.go index 53ba68eb..412a6f26 100644 --- a/client/httpclient_test.go +++ b/client/httpclient_test.go @@ -108,14 +108,10 @@ func TestHTTPClientSetPollingInterval(t *testing.T) { } // Start a client. - settings := types.StartSettings{ - PollingIntervalMs: func() *time.Duration { - tms := 101 * time.Millisecond - return &tms - }(), - } + settings := types.StartSettings{} settings.OpAMPServerURL = "http://" + srv.Endpoint client := NewHTTP(nil) + client.SetPollingInterval(101 * time.Millisecond) prepareClient(t, &settings, client) assert.NoError(t, client.Start(context.Background(), settings)) diff --git a/client/types/startsettings.go b/client/types/startsettings.go index e651bb35..b395afdb 100644 --- a/client/types/startsettings.go +++ b/client/types/startsettings.go @@ -3,7 +3,6 @@ package types import ( "crypto/tls" "net/http" - "time" "github.com/open-telemetry/opamp-go/protobufs" ) @@ -49,8 +48,4 @@ type StartSettings struct { // the compression is only effectively enabled if the Server also supports compression. // The data will be compressed in both directions. EnableCompression bool - - // Optional PollingIntervalMs to configure the polling interval for http client - // if nil uses the default polling interval else uses this value. - PollingIntervalMs *time.Duration } From 3f4cd0f95961116689d65ea9b8c1eed5f75d16fd Mon Sep 17 00:00:00 2001 From: Phdevava Date: Thu, 6 Jul 2023 08:23:56 -0700 Subject: [PATCH 3/3] duration change --- client/httpclient_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/httpclient_test.go b/client/httpclient_test.go index 412a6f26..b60f7021 100644 --- a/client/httpclient_test.go +++ b/client/httpclient_test.go @@ -111,7 +111,7 @@ func TestHTTPClientSetPollingInterval(t *testing.T) { settings := types.StartSettings{} settings.OpAMPServerURL = "http://" + srv.Endpoint client := NewHTTP(nil) - client.SetPollingInterval(101 * time.Millisecond) + client.SetPollingInterval(100 * time.Millisecond) prepareClient(t, &settings, client) assert.NoError(t, client.Start(context.Background(), settings))