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

Allow setting polling interval for httpClient #183

Merged
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
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 @@ -93,3 +94,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)
}
Loading