Skip to content

Commit

Permalink
fix breaking tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aranjans committed Dec 19, 2024
1 parent 101e23d commit f2a9f2a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
11 changes: 5 additions & 6 deletions balancer/rls/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package rls

import (
"google.golang.org/grpc/stats/opentelemetry"
"testing"
"time"

Expand Down Expand Up @@ -121,7 +120,7 @@ func (s) TestLRU_BasicOperations(t *testing.T) {

func (s) TestDataCache_BasicOperations(t *testing.T) {
initCacheEntries()
dc := newDataCache(5, nil, &opentelemetry.NoopMetricsRecorder{}, "")
dc := newDataCache(5, nil, &stats.NoopMetricsRecorder{}, "")
for i, k := range cacheKeys {
dc.addEntry(k, cacheEntries[i])
}
Expand All @@ -135,7 +134,7 @@ func (s) TestDataCache_BasicOperations(t *testing.T) {

func (s) TestDataCache_AddForcesResize(t *testing.T) {
initCacheEntries()
dc := newDataCache(1, nil, &opentelemetry.NoopMetricsRecorder{}, "")
dc := newDataCache(1, nil, &stats.NoopMetricsRecorder{}, "")

// The first entry in cacheEntries has a minimum expiry time in the future.
// This entry would stop the resize operation since we do not evict entries
Expand Down Expand Up @@ -164,7 +163,7 @@ func (s) TestDataCache_AddForcesResize(t *testing.T) {

func (s) TestDataCache_Resize(t *testing.T) {
initCacheEntries()
dc := newDataCache(5, nil, &opentelemetry.NoopMetricsRecorder{}, "")
dc := newDataCache(5, nil, &stats.NoopMetricsRecorder{}, "")
for i, k := range cacheKeys {
dc.addEntry(k, cacheEntries[i])
}
Expand Down Expand Up @@ -195,7 +194,7 @@ func (s) TestDataCache_Resize(t *testing.T) {

func (s) TestDataCache_EvictExpiredEntries(t *testing.T) {
initCacheEntries()
dc := newDataCache(5, nil, &opentelemetry.NoopMetricsRecorder{}, "")
dc := newDataCache(5, nil, &stats.NoopMetricsRecorder{}, "")
for i, k := range cacheKeys {
dc.addEntry(k, cacheEntries[i])
}
Expand All @@ -222,7 +221,7 @@ func (s) TestDataCache_ResetBackoffState(t *testing.T) {
}

initCacheEntries()
dc := newDataCache(5, nil, &opentelemetry.NoopMetricsRecorder{}, "")
dc := newDataCache(5, nil, &stats.NoopMetricsRecorder{}, "")
for i, k := range cacheKeys {
dc.addEntry(k, cacheEntries[i])
}
Expand Down
19 changes: 19 additions & 0 deletions internal/testutils/stats/test_metrics_recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,22 @@ func (r *TestMetricsRecorder) TagConn(ctx context.Context, _ *stats.ConnTagInfo)

// HandleConn is TestMetricsRecorder's implementation of HandleConn.
func (r *TestMetricsRecorder) HandleConn(context.Context, stats.ConnStats) {}

// NoopMetricsRecorder is a noop MetricsRecorder to be used in tests to prevent
// nil panics.
type NoopMetricsRecorder struct{}

// RecordInt64Count is a noop implementation of RecordInt64Count.
func (r *NoopMetricsRecorder) RecordInt64Count(*estats.Int64CountHandle, int64, ...string) {}

// RecordFloat64Count is a noop implementation of RecordFloat64Count.
func (r *NoopMetricsRecorder) RecordFloat64Count(*estats.Float64CountHandle, float64, ...string) {}

// RecordInt64Histo is a noop implementation of RecordInt64Histo.
func (r *NoopMetricsRecorder) RecordInt64Histo(*estats.Int64HistoHandle, int64, ...string) {}

// RecordFloat64Histo is a noop implementation of RecordFloat64Histo.
func (r *NoopMetricsRecorder) RecordFloat64Histo(*estats.Float64HistoHandle, float64, ...string) {}

// RecordInt64Gauge is a noop implementation of RecordInt64Gauge.
func (r *NoopMetricsRecorder) RecordInt64Gauge(*estats.Int64GaugeHandle, int64, ...string) {}
2 changes: 1 addition & 1 deletion stats/opentelemetry/client_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (h *clientStatsHandler) initializeMetrics() {
// Will set no metrics to record, logically making this stats handler a
// no-op.
if h.options.MetricsOptions.MeterProvider == nil {
h.MetricsRecorder = &NoopMetricsRecorder{}
h.MetricsRecorder = &OtelNoopMetricsRecorder{}
h.options.MetricsOptions.MeterProvider = metric.NewMeterProvider()
h.setClientMetrics()
return
Expand Down
16 changes: 9 additions & 7 deletions stats/opentelemetry/opentelemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,21 +395,23 @@ func DefaultMetrics() *stats.MetricSet {
return defaultPerCallMetrics.Join(estats.DefaultMetrics)
}

// NoopMetricsRecorder is a noop MetricsRecorder to be used in tests to prevent
// OtelNoopMetricsRecorder is a noop MetricsRecorder to be used to prevent
// nil panics.
type NoopMetricsRecorder struct{}
type OtelNoopMetricsRecorder struct{}

// RecordInt64Count is a noop implementation of RecordInt64Count.
func (r *NoopMetricsRecorder) RecordInt64Count(*estats.Int64CountHandle, int64, ...string) {}
func (r *OtelNoopMetricsRecorder) RecordInt64Count(*estats.Int64CountHandle, int64, ...string) {}

// RecordFloat64Count is a noop implementation of RecordFloat64Count.
func (r *NoopMetricsRecorder) RecordFloat64Count(*estats.Float64CountHandle, float64, ...string) {}
func (r *OtelNoopMetricsRecorder) RecordFloat64Count(*estats.Float64CountHandle, float64, ...string) {
}

// RecordInt64Histo is a noop implementation of RecordInt64Histo.
func (r *NoopMetricsRecorder) RecordInt64Histo(*estats.Int64HistoHandle, int64, ...string) {}
func (r *OtelNoopMetricsRecorder) RecordInt64Histo(*estats.Int64HistoHandle, int64, ...string) {}

// RecordFloat64Histo is a noop implementation of RecordFloat64Histo.
func (r *NoopMetricsRecorder) RecordFloat64Histo(*estats.Float64HistoHandle, float64, ...string) {}
func (r *OtelNoopMetricsRecorder) RecordFloat64Histo(*estats.Float64HistoHandle, float64, ...string) {
}

// RecordInt64Gauge is a noop implementation of RecordInt64Gauge.
func (r *NoopMetricsRecorder) RecordInt64Gauge(*estats.Int64GaugeHandle, int64, ...string) {}
func (r *OtelNoopMetricsRecorder) RecordInt64Gauge(*estats.Int64GaugeHandle, int64, ...string) {}
2 changes: 1 addition & 1 deletion stats/opentelemetry/server_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (h *serverStatsHandler) initializeMetrics() {
// Will set no metrics to record, logically making this stats handler a
// no-op.
if h.options.MetricsOptions.MeterProvider == nil {
h.MetricsRecorder = &NoopMetricsRecorder{}
h.MetricsRecorder = &OtelNoopMetricsRecorder{}
h.options.MetricsOptions.MeterProvider = metric.NewMeterProvider()
h.setServerMetrics()
return
Expand Down
2 changes: 1 addition & 1 deletion test/creds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func (s) TestWaitForReadyRPCErrorOnBadCertificates(t *testing.T) {
defer te.tearDown()

opts := []grpc.DialOption{grpc.WithTransportCredentials(clientAlwaysFailCred{})}
cc, err := grpc.NewClient(te.srvAddr, opts...)
cc, err := grpc.Dial(te.srvAddr, opts...)
if err != nil {
t.Fatalf("NewClient(_) = %v, want %v", err, nil)
}
Expand Down

0 comments on commit f2a9f2a

Please sign in to comment.