Skip to content

Commit

Permalink
Allow setting polling interval for httpClient (#183)
Browse files Browse the repository at this point in the history
Resolves #176
  • Loading branch information
phanidevavarapu authored Jul 7, 2023
1 parent 90b56d9 commit 3127f49
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
5 changes: 5 additions & 0 deletions client/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -113,3 +114,7 @@ func (c *httpClient) runUntilStopped(ctx context.Context) {
c.common.Capabilities,
)
}

func (c *httpClient) SetPollingInterval(duration time.Duration) {
c.sender.SetPollingInterval(duration)
}
40 changes: 38 additions & 2 deletions client/httpclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -105,3 +106,38 @@ 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{}
settings.OpAMPServerURL = "http://" + srv.Endpoint
client := NewHTTP(nil)
client.SetPollingInterval(100 * time.Millisecond)
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)
}

0 comments on commit 3127f49

Please sign in to comment.