From 89ebf692161077771401339141ec8acf3a81dc3e Mon Sep 17 00:00:00 2001 From: Matt Johnstone Date: Fri, 14 Jun 2024 21:31:45 +0200 Subject: [PATCH] reduced indentation (for jeff) --- cmd/solana_exporter/dynamic_test.go | 10 ++---- cmd/solana_exporter/static_test.go | 36 ++++++-------------- cmd/solana_exporter/utils_test.go | 51 +++++++---------------------- 3 files changed, 23 insertions(+), 74 deletions(-) diff --git a/cmd/solana_exporter/dynamic_test.go b/cmd/solana_exporter/dynamic_test.go index 95e69f5..0bcdc09 100644 --- a/cmd/solana_exporter/dynamic_test.go +++ b/cmd/solana_exporter/dynamic_test.go @@ -11,10 +11,7 @@ import ( func TestSolanaCollector_Collect_Dynamic(t *testing.T) { client := newDynamicRPCClient() - collector := createSolanaCollector( - client, - slotPacerSchedule, - ) + collector := createSolanaCollector(client, slotPacerSchedule) prometheus.NewPedanticRegistry().MustRegister(collector) // start off by testing initial state: @@ -155,10 +152,7 @@ func TestSolanaCollector_WatchSlots_Dynamic(t *testing.T) { // create clients: client := newDynamicRPCClient() - collector := createSolanaCollector( - client, - 300*time.Millisecond, - ) + collector := createSolanaCollector(client, 300*time.Millisecond) prometheus.NewPedanticRegistry().MustRegister(collector) // start client/collector and wait a bit: diff --git a/cmd/solana_exporter/static_test.go b/cmd/solana_exporter/static_test.go index da487c4..09f0906 100644 --- a/cmd/solana_exporter/static_test.go +++ b/cmd/solana_exporter/static_test.go @@ -94,40 +94,24 @@ func TestSolanaCollector_WatchSlots_Static(t *testing.T) { go collector.WatchSlots(ctx) time.Sleep(1 * time.Second) + firstSlot := staticEpochInfo.AbsoluteSlot - staticEpochInfo.SlotIndex + lastSlot := firstSlot + staticEpochInfo.SlotsInEpoch tests := []struct { expectedValue float64 metric prometheus.Gauge }{ - { - expectedValue: float64(staticEpochInfo.AbsoluteSlot), - metric: confirmedSlotHeight, - }, - { - expectedValue: float64(staticEpochInfo.TransactionCount), - metric: totalTransactionsTotal, - }, - { - expectedValue: float64(staticEpochInfo.Epoch), - metric: currentEpochNumber, - }, - { - expectedValue: float64(staticEpochInfo.AbsoluteSlot - staticEpochInfo.SlotIndex), - metric: epochFirstSlot, - }, - { - expectedValue: float64(staticEpochInfo.AbsoluteSlot - staticEpochInfo.SlotIndex + staticEpochInfo.SlotsInEpoch), - metric: epochLastSlot, - }, + {expectedValue: float64(staticEpochInfo.AbsoluteSlot), metric: confirmedSlotHeight}, + {expectedValue: float64(staticEpochInfo.TransactionCount), metric: totalTransactionsTotal}, + {expectedValue: float64(staticEpochInfo.Epoch), metric: currentEpochNumber}, + {expectedValue: float64(firstSlot), metric: epochFirstSlot}, + {expectedValue: float64(lastSlot), metric: epochLastSlot}, } for _, testCase := range tests { name := extractName(testCase.metric.Desc()) - t.Run( - name, - func(t *testing.T) { - assert.Equal(t, testCase.expectedValue, testutil.ToFloat64(testCase.metric)) - }, - ) + t.Run(name, func(t *testing.T) { + assert.Equal(t, testCase.expectedValue, testutil.ToFloat64(testCase.metric)) + }) } metrics := map[string]*prometheus.CounterVec{ diff --git a/cmd/solana_exporter/utils_test.go b/cmd/solana_exporter/utils_test.go index be0fdfc..03d5486 100644 --- a/cmd/solana_exporter/utils_test.go +++ b/cmd/solana_exporter/utils_test.go @@ -6,6 +6,7 @@ import ( "github.com/certusone/solana_exporter/pkg/rpc" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/testutil" + "github.com/stretchr/testify/assert" "math/rand" "regexp" "testing" @@ -39,16 +40,8 @@ type ( ) var ( - identities = []string{ - "aaa", - "bbb", - "ccc", - } - identityVotes = map[string]string{ - "aaa": "AAA", - "bbb": "BBB", - "ccc": "CCC", - } + identities = []string{"aaa", "bbb", "ccc"} + identityVotes = map[string]string{"aaa": "AAA", "bbb": "BBB", "ccc": "CCC"} nv = len(identities) staticEpochInfo = rpc.EpochInfo{ AbsoluteSlot: 166598, @@ -62,18 +55,9 @@ var ( FirstSlot: 100000000, LastSlot: 200000000, Hosts: map[string]rpc.BlockProductionPerHost{ - "bbb": { - LeaderSlots: 40000000, - BlocksProduced: 36000000, - }, - "ccc": { - LeaderSlots: 30000000, - BlocksProduced: 29600000, - }, - "aaa": { - LeaderSlots: 30000000, - BlocksProduced: 10000000, - }, + "bbb": {LeaderSlots: 40000000, BlocksProduced: 36000000}, + "ccc": {LeaderSlots: 30000000, BlocksProduced: 29600000}, + "aaa": {LeaderSlots: 30000000, BlocksProduced: 10000000}, }, } staticVoteAccounts = rpc.VoteAccounts{ @@ -307,10 +291,7 @@ func (c *dynamicRPCClient) GetVoteAccounts( currentVoteAccounts = append(currentVoteAccounts, voteAccount) } } - return &rpc.VoteAccounts{ - Current: currentVoteAccounts, - Delinquent: delinquentVoteAccounts, - }, nil + return &rpc.VoteAccounts{Current: currentVoteAccounts, Delinquent: delinquentVoteAccounts}, nil } //goland:noinspection GoUnusedParameter @@ -347,10 +328,8 @@ func (c *dynamicRPCClient) GetBlockProduction( func extractName(desc *prometheus.Desc) string { // Get the string representation of the descriptor descString := desc.String() - // Use regex to extract the metric name and help message from the descriptor string reName := regexp.MustCompile(`fqName: "([^"]+)"`) - nameMatch := reName.FindStringSubmatch(descString) var name string @@ -368,17 +347,9 @@ type collectionTest struct { func runCollectionTests(t *testing.T, collector prometheus.Collector, testCases []collectionTest) { for _, test := range testCases { - t.Run( - test.Name, - func(t *testing.T) { - if err := testutil.CollectAndCompare( - collector, - bytes.NewBufferString(test.ExpectedResponse), - test.Name, - ); err != nil { - t.Errorf("unexpected collecting result for %s: \n%s", test.Name, err) - } - }, - ) + t.Run(test.Name, func(t *testing.T) { + err := testutil.CollectAndCompare(collector, bytes.NewBufferString(test.ExpectedResponse), test.Name) + assert.Nilf(t, "unexpected collecting result for %s: \n%s", test.Name, err) + }) } }