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

Improve test run #748

Merged
merged 21 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
b8a5038
Improve test run
mantzas Sep 15, 2024
ebd39e3
Improve test run
mantzas Sep 15, 2024
97d590e
Improve test run
mantzas Sep 15, 2024
4083d87
Improve test run
mantzas Sep 15, 2024
2c97ed0
Improve test run
mantzas Sep 15, 2024
9cccc93
Improve test run
mantzas Sep 18, 2024
2da61af
Refactor test setup in component/kafka, client/amqp, options_test, co…
mantzas Sep 18, 2024
afa5243
Refactor mapHeader function in component/kafka/component.go
mantzas Sep 19, 2024
7bb3f3f
Refactor test setup in component/kafka/integration_test.go
mantzas Sep 19, 2024
18c5fea
Refactor test setup in service_test.go
mantzas Sep 20, 2024
0eba5e3
Refactor test setup in client/amqp, client/es, client/grpc, client/ka…
mantzas Sep 20, 2024
94d6897
Refactor test setup in component/amqp, component/kafka, component/sqs…
mantzas Sep 21, 2024
8ee478e
Refactor assertSpan function in component/grpc/component_test.go
mantzas Sep 21, 2024
6c9be1f
Refactor assertSpan function in component/grpc/component_test.go
mantzas Sep 21, 2024
bd8bf63
Refactor test setup in internal/test/observability_test.go, client/es…
mantzas Sep 22, 2024
a132cad
Refactor test setup and metric assertion in grpc_test.go and observab…
mantzas Sep 22, 2024
4ac8965
Refactor test setup in internal/test/observability_test.go
mantzas Sep 22, 2024
07eceb6
Add go.opentelemetry.io/otel to the list of OSS projects in ACKNOWLED…
mantzas Sep 23, 2024
c6748a7
Refactor test setup and metric assertion in component/grpc/component_…
mantzas Sep 23, 2024
abde492
Refactor logging and tracing middleware in component/http/middleware/…
mantzas Sep 23, 2024
c7897ea
Update docker-compose.yml with latest image versions
mantzas Sep 23, 2024
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 .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
uses: actions/setup-go@v5
with:
go-version-file: "go.mod"

- name: Start dependencies
run: make deps-start

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ testint: fmtcheck
testint-nocache: fmtcheck
go test ./... -race -cover -tags=integration -timeout 300s -count=1

ci: fmtcheck
ci:
go test `go list ./... | grep -v -e 'examples' -e 'encoding/protobuf/test'` -race -cover -coverprofile=coverage.txt -covermode=atomic -tags=integration

fmt:
Expand Down
13 changes: 6 additions & 7 deletions cache/lru/lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@ import (
)

func TestNew(t *testing.T) {
tests := []struct {
name string
tests := map[string]struct {
err string
size int
wantErr bool
}{
{name: "negative size", size: -1, wantErr: true, err: "must provide a positive size"},
{name: "zero size", size: 0, wantErr: true, err: "must provide a positive size"},
{name: "positive size", size: 1024, wantErr: false},
"negative size": {size: -1, wantErr: true, err: "must provide a positive size"},
"zero size": {size: 0, wantErr: true, err: "must provide a positive size"},
"positive size": {size: 1024, wantErr: false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
c, err := New(tt.size, "test")
if tt.wantErr {
assert.Nil(t, c)
Expand Down
1 change: 0 additions & 1 deletion client/amqp/amqp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func TestNew(t *testing.T) {
"fail, missing url": {args: args{}, expectedErr: "url is required"},
}
for name, tt := range tests {
tt := tt
t.Run(name, func(t *testing.T) {
t.Parallel()

Expand Down
23 changes: 4 additions & 19 deletions client/amqp/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ package amqp

import (
"context"
"log"
"testing"

"github.com/beatlabs/patron/internal/test"
"github.com/beatlabs/patron/observability/trace"
amqp "github.com/rabbitmq/amqp091-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
metricsdk "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
)

Expand All @@ -30,17 +27,8 @@ func TestRun(t *testing.T) {
exp := tracetest.NewInMemoryExporter()
tracePublisher := trace.Setup("test", nil, exp)

// Setup metrics
read := metricsdk.NewManualReader()
provider := metricsdk.NewMeterProvider(metricsdk.WithReader(read))
defer func() {
err := provider.Shutdown(context.Background())
if err != nil {
log.Fatal(err)
}
}()

otel.SetMeterProvider(provider)
shutdownProvider, collectMetrics := test.SetupMetrics(ctx, t)
defer shutdownProvider()

require.NoError(t, createQueue(endpoint, queue))

Expand Down Expand Up @@ -70,10 +58,7 @@ func TestRun(t *testing.T) {
assert.Equal(t, expected.Attributes, snaps[0].Attributes())

// Metrics
collectedMetrics := &metricdata.ResourceMetrics{}
require.NoError(t, read.Collect(context.Background(), collectedMetrics))
assert.Len(t, collectedMetrics.ScopeMetrics, 1)
assert.Len(t, collectedMetrics.ScopeMetrics[0].Metrics, 1)
_ = collectMetrics(1)

conn, err := amqp.Dial(endpoint)
require.NoError(t, err)
Expand Down
18 changes: 5 additions & 13 deletions client/es/elasticsearch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,28 @@ package es

import (
"context"
"log"
"net"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/beatlabs/patron/internal/test"
"github.com/beatlabs/patron/observability/trace"
"github.com/elastic/go-elasticsearch/v8"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel"
metricsdk "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
)

func TestNew(t *testing.T) {
exp := tracetest.NewInMemoryExporter()
tracePublisher := trace.Setup("test", nil, exp)

// Setup metrics
read := metricsdk.NewManualReader()
provider := metricsdk.NewMeterProvider(metricsdk.WithReader(read))
defer func() {
err := provider.Shutdown(context.Background())
if err != nil {
log.Fatal(err)
}
}()
otel.SetMeterProvider(provider)
ctx := context.Background()

shutdownProvider, _ := test.SetupMetrics(ctx, t)
defer shutdownProvider()

responseMsg := `[{"acknowledged": true, "shards_acknowledged": true, "index": "test"}]`
ctx, indexName := context.Background(), "test_index"
Expand Down
33 changes: 11 additions & 22 deletions client/grpc/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ import (
"testing"

"github.com/beatlabs/patron/examples"
"github.com/beatlabs/patron/internal/test"
"github.com/beatlabs/patron/observability/trace"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
metricsdk "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -94,7 +92,6 @@ func TestDialContext(t *testing.T) {
},
}
for name, tt := range tests {
tt := tt
t.Run(name, func(t *testing.T) {
t.Parallel()
gotConn, err := DialContext(context.Background(), target, tt.args.opts...)
Expand Down Expand Up @@ -122,13 +119,8 @@ func TestSayHello(t *testing.T) {
tracePublisher := trace.Setup("test", nil, exp)

// Metrics monitoring set up
read := metricsdk.NewManualReader()
provider := metricsdk.NewMeterProvider(metricsdk.WithReader(read))
defer func() {
require.NoError(t, provider.Shutdown(context.Background()))
}()

otel.SetMeterProvider(provider)
shutdownProvider, assertCollectMetrics := test.SetupMetrics(ctx, t)
defer shutdownProvider()

client := examples.NewGreeterClient(conn)

Expand All @@ -155,23 +147,23 @@ func TestSayHello(t *testing.T) {
},
}

for n, tc := range tt {
for n, tt := range tt {
t.Run(n, func(t *testing.T) {
t.Cleanup(func() { exp.Reset() })

res, err := client.SayHello(ctx, tc.req)
if tc.wantErr {
res, err := client.SayHello(ctx, tt.req)
if tt.wantErr {
require.Nil(t, res)
require.Error(t, err)

rpcStatus, ok := status.FromError(err)
require.True(t, ok)
require.Equal(t, tc.wantCode, rpcStatus.Code())
require.Equal(t, tc.wantMsg, rpcStatus.Message())
require.Equal(t, tt.wantCode, rpcStatus.Code())
require.Equal(t, tt.wantMsg, rpcStatus.Message())
} else {
require.NoError(t, err)
require.NotNil(t, res)
require.Equal(t, tc.wantMsg, res.GetMessage())
require.Equal(t, tt.wantMsg, res.GetMessage())
}

require.NoError(t, tracePublisher.ForceFlush(context.Background()))
Expand All @@ -183,13 +175,10 @@ func TestSayHello(t *testing.T) {
assert.Equal(t, attribute.String("rpc.service", "examples.Greeter"), snaps[0].Attributes()[0])
assert.Equal(t, attribute.String("rpc.method", "SayHello"), snaps[0].Attributes()[1])
assert.Equal(t, attribute.String("rpc.system", "grpc"), snaps[0].Attributes()[2])
assert.Equal(t, attribute.Int64("rpc.grpc.status_code", int64(tc.wantCode)), snaps[0].Attributes()[3])
assert.Equal(t, attribute.Int64("rpc.grpc.status_code", int64(tt.wantCode)), snaps[0].Attributes()[3])

// Metrics
collectedMetrics := &metricdata.ResourceMetrics{}
require.NoError(t, read.Collect(context.Background(), collectedMetrics))
assert.Len(t, collectedMetrics.ScopeMetrics, 1)
assert.Positive(t, len(collectedMetrics.ScopeMetrics[0].Metrics))
_ = assertCollectMetrics(4)
})
}
}
1 change: 0 additions & 1 deletion client/http/encoding/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func TestFromResponse(t *testing.T) {
"failure, empty content type": {args: args{contentType: stringPointer(""), payload: buf}, expectedErr: "invalid content type provided: "},
}
for name, tt := range tests {
tt := tt
t.Run(name, func(t *testing.T) {
t.Parallel()

Expand Down
18 changes: 5 additions & 13 deletions client/kafka/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ import (
"testing"

"github.com/IBM/sarama"
"github.com/beatlabs/patron/internal/test"
"github.com/beatlabs/patron/observability/trace"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
metricsdk "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
)
Expand Down Expand Up @@ -60,14 +58,10 @@ func TestNewSyncProducer_Success(t *testing.T) {
func TestAsyncProducer_SendMessage_Close(t *testing.T) {
t.Cleanup(func() { traceExporter.Reset() })

// Metrics monitoring set up
read := metricsdk.NewManualReader()
provider := metricsdk.NewMeterProvider(metricsdk.WithReader(read))
defer func() {
require.NoError(t, provider.Shutdown(context.Background()))
}()
ctx := context.Background()

otel.SetMeterProvider(provider)
shutdownProvider, assertCollectMetrics := test.SetupMetrics(ctx, t)
defer shutdownProvider()

saramaCfg, err := DefaultProducerSaramaConfig("test-consumer", false)
require.NoError(t, err)
Expand Down Expand Up @@ -104,9 +98,7 @@ func TestAsyncProducer_SendMessage_Close(t *testing.T) {
assert.Equal(t, expected.Attributes, snaps[0].Attributes())

// Metrics
collectedMetrics := &metricdata.ResourceMetrics{}
require.NoError(t, read.Collect(context.Background(), collectedMetrics))
assert.Len(t, collectedMetrics.ScopeMetrics, 1)
_ = assertCollectMetrics(1)
}

func TestSyncProducer_SendMessage_Close(t *testing.T) {
Expand Down
2 changes: 0 additions & 2 deletions client/kafka/kafka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func TestBuilder_Create(t *testing.T) {
"missing config": {args: args{brokers: []string{"123"}, cfg: nil}, expectedErr: "no Sarama configuration specified"},
}
for name, tt := range tests {
tt := tt
t.Run(name, func(t *testing.T) {
t.Parallel()
got, err := New(tt.args.brokers, tt.args.cfg).Create()
Expand All @@ -52,7 +51,6 @@ func TestBuilder_CreateAsync(t *testing.T) {
"missing config": {args: args{brokers: []string{"123"}, cfg: nil}, expectedErr: "no Sarama configuration specified"},
}
for name, tt := range tests {
tt := tt
t.Run(name, func(t *testing.T) {
t.Parallel()
got, chErr, err := New(tt.args.brokers, tt.args.cfg).CreateAsync()
Expand Down
26 changes: 6 additions & 20 deletions client/mongo/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import (
"context"
"testing"

"github.com/beatlabs/patron/internal/test"
"github.com/beatlabs/patron/observability/trace"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"
"go.opentelemetry.io/otel"
metricsdk "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
)

Expand All @@ -21,17 +19,11 @@ func TestConnectAndExecute(t *testing.T) {
exp := tracetest.NewInMemoryExporter()
tracePublisher := trace.Setup("test", nil, exp)

// Metrics monitoring set up
read := metricsdk.NewManualReader()
provider := metricsdk.NewMeterProvider(metricsdk.WithReader(read))
defer func() {
require.NoError(t, provider.Shutdown(context.Background()))
}()

otel.SetMeterProvider(provider)

ctx := context.Background()

shutdownProvider, collectAssertMetrics := test.SetupMetrics(ctx, t)
defer shutdownProvider()

client, err := Connect(ctx)
require.NoError(t, err)
assert.NotNil(t, client)
Expand All @@ -43,10 +35,7 @@ func TestConnectAndExecute(t *testing.T) {
require.NoError(t, tracePublisher.ForceFlush(ctx))
assert.Len(t, exp.GetSpans(), 1)
// Metrics
collectedMetrics := &metricdata.ResourceMetrics{}
require.NoError(t, read.Collect(context.Background(), collectedMetrics))
assert.Len(t, collectedMetrics.ScopeMetrics, 1)
assert.Len(t, collectedMetrics.ScopeMetrics[0].Metrics, 1)
_ = collectAssertMetrics(1)
})

t.Run("failure", func(t *testing.T) {
Expand All @@ -57,9 +46,6 @@ func TestConnectAndExecute(t *testing.T) {
require.NoError(t, tracePublisher.ForceFlush(ctx))
assert.Len(t, exp.GetSpans(), 1)
// Metrics
collectedMetrics := &metricdata.ResourceMetrics{}
require.NoError(t, read.Collect(context.Background(), collectedMetrics))
assert.Len(t, collectedMetrics.ScopeMetrics, 1)
assert.Len(t, collectedMetrics.ScopeMetrics[0].Metrics, 1)
_ = collectAssertMetrics(1)
})
}
Loading