Skip to content
This repository has been archived by the owner on Aug 30, 2019. It is now read-only.

{statsd,writer}: keep fixtures private and limited to tests #373

Merged
merged 1 commit into from
Feb 26, 2018
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
2 changes: 1 addition & 1 deletion statsd/fixtures.go → fixtures/statsd.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package statsd
package fixtures

import (
"math"
Expand Down
72 changes: 36 additions & 36 deletions writer/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
"github.com/DataDog/datadog-trace-agent/fixtures"
)

// PayloadConstructedHandlerArgs encodes the arguments passed to a PayloadConstructedHandler call.
type PayloadConstructedHandlerArgs struct {
// payloadConstructedHandlerArgs encodes the arguments passed to a PayloadConstructedHandler call.
type payloadConstructedHandlerArgs struct {
payload *Payload
stats interface{}
}

// TestEndpoint represents a mocked endpoint that replies with a configurable error and records successful and failed
// testEndpoint represents a mocked endpoint that replies with a configurable error and records successful and failed
// payloads.
type TestEndpoint struct {
type testEndpoint struct {
sync.RWMutex
err error
successPayloads []Payload
Expand All @@ -26,7 +26,7 @@ type TestEndpoint struct {

// Write mocks the writing of a payload to a remote endpoint, recording it and replying with the configured error (or
// success in its absence).
func (e *TestEndpoint) Write(payload *Payload) error {
func (e *testEndpoint) Write(payload *Payload) error {
e.Lock()
defer e.Unlock()
if e.err != nil {
Expand All @@ -37,35 +37,35 @@ func (e *TestEndpoint) Write(payload *Payload) error {
return e.err
}

func (e *TestEndpoint) Error() error {
func (e *testEndpoint) Error() error {
e.RLock()
defer e.RUnlock()
return e.err
}

// ErrorPayloads returns all the error payloads registered with the test endpoint.
func (e *TestEndpoint) ErrorPayloads() []Payload {
func (e *testEndpoint) ErrorPayloads() []Payload {
e.RLock()
defer e.RUnlock()
return e.errorPayloads
}

// SuccessPayloads returns all the success payloads registered with the test endpoint.
func (e *TestEndpoint) SuccessPayloads() []Payload {
func (e *testEndpoint) SuccessPayloads() []Payload {
e.RLock()
defer e.RUnlock()
return e.successPayloads
}

// SetError sets the passed error on the endpoint.
func (e *TestEndpoint) SetError(err error) {
func (e *testEndpoint) SetError(err error) {
e.Lock()
defer e.Unlock()
e.err = err
}

func (e *TestEndpoint) String() string {
return "TestEndpoint"
func (e *testEndpoint) String() string {
return "testEndpoint"
}

// RandomPayload creates a new payload instance using random data and up to 32 bytes.
Expand All @@ -78,28 +78,28 @@ func RandomSizedPayload(size int) *Payload {
return NewPayload(fixtures.RandomSizedBytes(size), fixtures.RandomStringMap())
}

// TestPayloadSender is a PayloadSender that is connected to a TestEndpoint, used for testing.
type TestPayloadSender struct {
testEndpoint *TestEndpoint
// testPayloadSender is a PayloadSender that is connected to a testEndpoint, used for testing.
type testPayloadSender struct {
testEndpoint *testEndpoint
BasePayloadSender
}

// NewTestPayloadSender creates a new instance of a TestPayloadSender.
func NewTestPayloadSender() *TestPayloadSender {
testEndpoint := &TestEndpoint{}
return &TestPayloadSender{
// newTestPayloadSender creates a new instance of a testPayloadSender.
func newTestPayloadSender() *testPayloadSender {
testEndpoint := &testEndpoint{}
return &testPayloadSender{
testEndpoint: testEndpoint,
BasePayloadSender: *NewBasePayloadSender(testEndpoint),
}
}

// Start asynchronously starts this payload sender.
func (c *TestPayloadSender) Start() {
func (c *testPayloadSender) Start() {
go c.Run()
}

// Run executes the core loop of this sender.
func (c *TestPayloadSender) Run() {
func (c *testPayloadSender) Run() {
c.exitWG.Add(1)
defer c.exitWG.Done()

Expand All @@ -120,21 +120,21 @@ func (c *TestPayloadSender) Run() {
}

// Payloads allows access to all payloads recorded as being successfully sent by this sender.
func (c *TestPayloadSender) Payloads() []Payload {
func (c *testPayloadSender) Payloads() []Payload {
return c.testEndpoint.SuccessPayloads()
}

// Endpoint allows access to the underlying TestEndpoint.
func (c *TestPayloadSender) Endpoint() *TestEndpoint {
// Endpoint allows access to the underlying testEndpoint.
func (c *testPayloadSender) Endpoint() *testEndpoint {
return c.testEndpoint
}

func (c *TestPayloadSender) setEndpoint(endpoint Endpoint) {
c.testEndpoint = endpoint.(*TestEndpoint)
func (c *testPayloadSender) setEndpoint(endpoint Endpoint) {
c.testEndpoint = endpoint.(*testEndpoint)
}

// TestPayloadSenderMonitor monitors a PayloadSender and stores all events
type TestPayloadSenderMonitor struct {
// testPayloadSenderMonitor monitors a PayloadSender and stores all events
type testPayloadSenderMonitor struct {
SuccessEvents []SenderSuccessEvent
FailureEvents []SenderFailureEvent
RetryEvents []SenderRetryEvent
Expand All @@ -145,21 +145,21 @@ type TestPayloadSenderMonitor struct {
exitWG sync.WaitGroup
}

// NewTestPayloadSenderMonitor creates a new TestPayloadSenderMonitor monitoring the specified sender.
func NewTestPayloadSenderMonitor(sender PayloadSender) *TestPayloadSenderMonitor {
return &TestPayloadSenderMonitor{
// newTestPayloadSenderMonitor creates a new testPayloadSenderMonitor monitoring the specified sender.
func newTestPayloadSenderMonitor(sender PayloadSender) *testPayloadSenderMonitor {
return &testPayloadSenderMonitor{
sender: sender,
exit: make(chan struct{}),
}
}

// Start asynchronously starts this payload monitor.
func (m *TestPayloadSenderMonitor) Start() {
func (m *testPayloadSenderMonitor) Start() {
go m.Run()
}

// Run executes the core loop of this monitor.
func (m *TestPayloadSenderMonitor) Run() {
func (m *testPayloadSenderMonitor) Run() {
m.exitWG.Add(1)
defer m.exitWG.Done()

Expand Down Expand Up @@ -187,13 +187,13 @@ func (m *TestPayloadSenderMonitor) Run() {
}

// Stop stops this payload monitor and waits for it to stop.
func (m *TestPayloadSenderMonitor) Stop() {
func (m *testPayloadSenderMonitor) Stop() {
close(m.exit)
m.exitWG.Wait()
}

// SuccessPayloads returns a slice containing all successful payloads.
func (m *TestPayloadSenderMonitor) SuccessPayloads() []Payload {
func (m *testPayloadSenderMonitor) SuccessPayloads() []Payload {
result := make([]Payload, len(m.SuccessEvents))

for i, successEvent := range m.SuccessEvents {
Expand All @@ -204,7 +204,7 @@ func (m *TestPayloadSenderMonitor) SuccessPayloads() []Payload {
}

// FailurePayloads returns a slice containing all failed payloads.
func (m *TestPayloadSenderMonitor) FailurePayloads() []Payload {
func (m *testPayloadSenderMonitor) FailurePayloads() []Payload {
result := make([]Payload, len(m.FailureEvents))

for i, successEvent := range m.FailureEvents {
Expand All @@ -215,7 +215,7 @@ func (m *TestPayloadSenderMonitor) FailurePayloads() []Payload {
}

// RetryPayloads returns a slice containing all failed payloads.
func (m *TestPayloadSenderMonitor) RetryPayloads() []Payload {
func (m *testPayloadSenderMonitor) RetryPayloads() []Payload {
result := make([]Payload, len(m.RetryEvents))

for i, successEvent := range m.RetryEvents {
Expand Down
28 changes: 14 additions & 14 deletions writer/payload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ func TestQueuablePayloadSender_WorkingEndpoint(t *testing.T) {
assert := assert.New(t)

// Given an endpoint that doesn't fail
workingEndpoint := &TestEndpoint{}
workingEndpoint := &testEndpoint{}

// And a queuable sender using that endpoint
queuableSender := NewQueuablePayloadSender(workingEndpoint)

// And a test monitor for that sender
monitor := NewTestPayloadSenderMonitor(queuableSender)
monitor := newTestPayloadSenderMonitor(queuableSender)

// When we start the sender
monitor.Start()
Expand Down Expand Up @@ -64,7 +64,7 @@ func TestQueuablePayloadSender_FlakyEndpoint(t *testing.T) {
assert := assert.New(t)

// Given an endpoint that initially works ok
flakyEndpoint := &TestEndpoint{}
flakyEndpoint := &testEndpoint{}

// And a test backoff timer that can be triggered on-demand
testBackoffTimer := fixtures.NewTestBackoffTimer()
Expand All @@ -77,7 +77,7 @@ func TestQueuablePayloadSender_FlakyEndpoint(t *testing.T) {
queuableSender.syncBarrier = syncBarrier

// And a test monitor for that sender
monitor := NewTestPayloadSenderMonitor(queuableSender)
monitor := newTestPayloadSenderMonitor(queuableSender)

monitor.Start()
queuableSender.Start()
Expand Down Expand Up @@ -162,7 +162,7 @@ func TestQueuablePayloadSender_MaxQueuedPayloads(t *testing.T) {
assert := assert.New(t)

// Given an endpoint that continuously throws out retriable errors
flakyEndpoint := &TestEndpoint{}
flakyEndpoint := &testEndpoint{}
flakyEndpoint.SetError(&RetriableError{err: fmt.Errorf("bleh"), endpoint: flakyEndpoint})

// And a test backoff timer that can be triggered on-demand
Expand All @@ -177,7 +177,7 @@ func TestQueuablePayloadSender_MaxQueuedPayloads(t *testing.T) {
queuableSender.syncBarrier = syncBarrier

// And a test monitor for that sender
monitor := NewTestPayloadSenderMonitor(queuableSender)
monitor := newTestPayloadSenderMonitor(queuableSender)

monitor.Start()
queuableSender.Start()
Expand Down Expand Up @@ -232,7 +232,7 @@ func TestQueuablePayloadSender_MaxQueuedBytes(t *testing.T) {
assert := assert.New(t)

// Given an endpoint that continuously throws out retriable errors
flakyEndpoint := &TestEndpoint{}
flakyEndpoint := &testEndpoint{}
flakyEndpoint.SetError(&RetriableError{err: fmt.Errorf("bleh"), endpoint: flakyEndpoint})

// And a test backoff timer that can be triggered on-demand
Expand All @@ -247,7 +247,7 @@ func TestQueuablePayloadSender_MaxQueuedBytes(t *testing.T) {
queuableSender.syncBarrier = syncBarrier

// And a test monitor for that sender
monitor := NewTestPayloadSenderMonitor(queuableSender)
monitor := newTestPayloadSenderMonitor(queuableSender)

monitor.Start()
queuableSender.Start()
Expand Down Expand Up @@ -301,7 +301,7 @@ func TestQueuablePayloadSender_DropBigPayloadsOnRetry(t *testing.T) {
assert := assert.New(t)

// Given an endpoint that continuously throws out retriable errors
flakyEndpoint := &TestEndpoint{}
flakyEndpoint := &testEndpoint{}
flakyEndpoint.SetError(&RetriableError{err: fmt.Errorf("bleh"), endpoint: flakyEndpoint})

// And a test backoff timer that can be triggered on-demand
Expand All @@ -316,7 +316,7 @@ func TestQueuablePayloadSender_DropBigPayloadsOnRetry(t *testing.T) {
queuableSender.syncBarrier = syncBarrier

// And a test monitor for that sender
monitor := NewTestPayloadSenderMonitor(queuableSender)
monitor := newTestPayloadSenderMonitor(queuableSender)

monitor.Start()
queuableSender.Start()
Expand Down Expand Up @@ -358,7 +358,7 @@ func TestQueuablePayloadSender_SendBigPayloadsIfNoRetry(t *testing.T) {
assert := assert.New(t)

// Given an endpoint that works
workingEndpoint := &TestEndpoint{}
workingEndpoint := &testEndpoint{}

// And a test backoff timer that can be triggered on-demand
testBackoffTimer := fixtures.NewTestBackoffTimer()
Expand All @@ -372,7 +372,7 @@ func TestQueuablePayloadSender_SendBigPayloadsIfNoRetry(t *testing.T) {
queuableSender.syncBarrier = syncBarrier

// And a test monitor for that sender
monitor := NewTestPayloadSenderMonitor(queuableSender)
monitor := newTestPayloadSenderMonitor(queuableSender)

monitor.Start()
queuableSender.Start()
Expand Down Expand Up @@ -403,7 +403,7 @@ func TestQueuablePayloadSender_MaxAge(t *testing.T) {
assert := assert.New(t)

// Given an endpoint that continuously throws out retriable errors
flakyEndpoint := &TestEndpoint{}
flakyEndpoint := &testEndpoint{}
flakyEndpoint.SetError(&RetriableError{err: fmt.Errorf("bleh"), endpoint: flakyEndpoint})

// And a test backoff timer that can be triggered on-demand
Expand All @@ -418,7 +418,7 @@ func TestQueuablePayloadSender_MaxAge(t *testing.T) {
queuableSender.syncBarrier = syncBarrier

// And a test monitor for that sender
monitor := NewTestPayloadSenderMonitor(queuableSender)
monitor := newTestPayloadSenderMonitor(queuableSender)

monitor.Start()
queuableSender.Start()
Expand Down
7 changes: 3 additions & 4 deletions writer/service_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/DataDog/datadog-trace-agent/fixtures"
"github.com/DataDog/datadog-trace-agent/info"
"github.com/DataDog/datadog-trace-agent/model"
"github.com/DataDog/datadog-trace-agent/statsd"
writerconfig "github.com/DataDog/datadog-trace-agent/writer/config"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -190,15 +189,15 @@ func assertMetadata(assert *assert.Assertions, expectedHeaders map[string]string
assert.Equal(expectedMetadata, servicesMetadata, "Service metadata should match expectation")
}

func testServiceWriter() (*ServiceWriter, chan model.ServicesMetadata, *TestEndpoint, *statsd.TestStatsClient) {
func testServiceWriter() (*ServiceWriter, chan model.ServicesMetadata, *testEndpoint, *fixtures.TestStatsClient) {
serviceChannel := make(chan model.ServicesMetadata)
conf := &config.AgentConfig{
ServiceWriterConfig: writerconfig.DefaultServiceWriterConfig(),
}
serviceWriter := NewServiceWriter(conf, serviceChannel)
testEndpoint := &TestEndpoint{}
testEndpoint := &testEndpoint{}
serviceWriter.BaseWriter.payloadSender.setEndpoint(testEndpoint)
testStatsClient := &statsd.TestStatsClient{}
testStatsClient := &fixtures.TestStatsClient{}
serviceWriter.statsClient = testStatsClient

return serviceWriter, serviceChannel, testEndpoint, testStatsClient
Expand Down
7 changes: 3 additions & 4 deletions writer/stats_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/DataDog/datadog-trace-agent/fixtures"
"github.com/DataDog/datadog-trace-agent/info"
"github.com/DataDog/datadog-trace-agent/model"
"github.com/DataDog/datadog-trace-agent/statsd"
writerconfig "github.com/DataDog/datadog-trace-agent/writer/config"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -200,17 +199,17 @@ func assertStatsPayload(assert *assert.Assertions, headers map[string]string, bu
assert.Equal(buckets, statsPayload.Stats, "Stat buckets should match expectation")
}

func testStatsWriter() (*StatsWriter, chan []model.StatsBucket, *TestEndpoint, *statsd.TestStatsClient) {
func testStatsWriter() (*StatsWriter, chan []model.StatsBucket, *testEndpoint, *fixtures.TestStatsClient) {
statsChannel := make(chan []model.StatsBucket)
conf := &config.AgentConfig{
HostName: testHostName,
DefaultEnv: testEnv,
StatsWriterConfig: writerconfig.DefaultStatsWriterConfig(),
}
statsWriter := NewStatsWriter(conf, statsChannel)
testEndpoint := &TestEndpoint{}
testEndpoint := &testEndpoint{}
statsWriter.BaseWriter.payloadSender.setEndpoint(testEndpoint)
testStatsClient := &statsd.TestStatsClient{}
testStatsClient := &fixtures.TestStatsClient{}
statsWriter.statsClient = testStatsClient

return statsWriter, statsChannel, testEndpoint, testStatsClient
Expand Down
Loading