added TestCountVoteTransactions

This commit is contained in:
Matt Johnstone 2024-10-29 00:38:48 +02:00
parent d06edc078e
commit cb8422c563
No known key found for this signature in database
GPG Key ID: BE985FBB9BE7D3BB
5 changed files with 62 additions and 58 deletions

View File

@ -5,12 +5,18 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
) )
type GaugeDesc struct { type (
Desc *prometheus.Desc GaugeDesc struct {
Name string Desc *prometheus.Desc
Help string Name string
VariableLabels []string Help string
} VariableLabels []string
}
collectionTest struct {
Name string
ExpectedResponse string
}
)
func NewGaugeDesc(name string, description string, variableLabels ...string) *GaugeDesc { func NewGaugeDesc(name string, description string, variableLabels ...string) *GaugeDesc {
return &GaugeDesc{ return &GaugeDesc{

View File

@ -10,7 +10,6 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"math" "math"
"math/rand" "math/rand"
"regexp"
"slices" "slices"
"strings" "strings"
"testing" "testing"
@ -37,10 +36,6 @@ type (
} }
) )
func voteTx(nodekey string) []string {
return []string{nodekey, strings.ToUpper(nodekey), VoteProgram}
}
func NewSimulator(t *testing.T, slot int) (*Simulator, *rpc.Client) { func NewSimulator(t *testing.T, slot int) (*Simulator, *rpc.Client) {
nodekeys := []string{"aaa", "bbb", "ccc"} nodekeys := []string{"aaa", "bbb", "ccc"}
votekeys := []string{"AAA", "BBB", "CCC"} votekeys := []string{"AAA", "BBB", "CCC"}
@ -144,7 +139,7 @@ func (c *Simulator) PopulateSlot(slot int) {
} }
// assume all validators voted // assume all validators voted
for _, nodekey := range c.Nodekeys { for _, nodekey := range c.Nodekeys {
transactions = append(transactions, voteTx(nodekey)) transactions = append(transactions, []string{nodekey, strings.ToUpper(nodekey), VoteProgram})
info := c.Server.GetValidatorInfo(nodekey) info := c.Server.GetValidatorInfo(nodekey)
info.LastVote = slot info.LastVote = slot
c.Server.SetOpt(rpc.ValidatorInfoOpt, nodekey, info) c.Server.SetOpt(rpc.ValidatorInfoOpt, nodekey, info)
@ -187,40 +182,6 @@ func (c *Simulator) PopulateSlot(slot int) {
) )
} }
/*
===== OTHER TEST UTILITIES =====:
*/
// extractName takes a Prometheus descriptor and returns its name
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
if len(nameMatch) > 1 {
name = nameMatch[1]
}
return name
}
type collectionTest struct {
Name string
ExpectedResponse string
}
func runCollectionTests(t *testing.T, collector prometheus.Collector, testCases []collectionTest) {
for _, test := range testCases {
t.Run(test.Name, func(t *testing.T) {
err := testutil.CollectAndCompare(collector, bytes.NewBufferString(test.ExpectedResponse), test.Name)
assert.NoErrorf(t, err, "unexpected collecting result for %s: \n%s", test.Name, err)
})
}
}
func newTestConfig(simulator *Simulator, fast bool) *ExporterConfig { func newTestConfig(simulator *Simulator, fast bool) *ExporterConfig {
pace := time.Duration(100) * time.Second pace := time.Duration(100) * time.Second
if fast { if fast {
@ -292,5 +253,10 @@ func TestSolanaCollector(t *testing.T) {
), ),
} }
runCollectionTests(t, collector, testCases) for _, test := range testCases {
t.Run(test.Name, func(t *testing.T) {
err := testutil.CollectAndCompare(collector, bytes.NewBufferString(test.ExpectedResponse), test.Name)
assert.NoErrorf(t, err, "unexpected collecting result for %s: \n%s", test.Name, err)
})
}
} }

View File

@ -7,6 +7,7 @@ import (
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil" "github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"regexp"
"testing" "testing"
"time" "time"
) )
@ -20,6 +21,22 @@ type slotMetricValues struct {
BlockHeight float64 BlockHeight float64
} }
// extractName takes a Prometheus descriptor and returns its name
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
if len(nameMatch) > 1 {
name = nameMatch[1]
}
return name
}
func getSlotMetricValues(watcher *SlotWatcher) slotMetricValues { func getSlotMetricValues(watcher *SlotWatcher) slotMetricValues {
return slotMetricValues{ return slotMetricValues{
SlotHeight: testutil.ToFloat64(watcher.SlotHeightMetric), SlotHeight: testutil.ToFloat64(watcher.SlotHeightMetric),
@ -74,8 +91,7 @@ func assertSlotMetricsChangeCorrectly(t *testing.T, initial slotMetricValues, fi
func TestSlotWatcher_WatchSlots_Static(t *testing.T) { func TestSlotWatcher_WatchSlots_Static(t *testing.T) {
// TODO: is this test necessary? If not - remove, else, could definitely do with a clean. // TODO: is this test necessary? If not - remove, else, could definitely do with a clean.
ctx, cancel := context.WithCancel(context.Background()) ctx := context.Background()
defer cancel()
simulator, client := NewSimulator(t, 35) simulator, client := NewSimulator(t, 35)
watcher := NewSlotWatcher(client, newTestConfig(simulator, true)) watcher := NewSlotWatcher(client, newTestConfig(simulator, true))

File diff suppressed because one or more lines are too long

View File

@ -2,19 +2,13 @@ package main
import ( import (
"context" "context"
_ "embed"
"encoding/json"
"github.com/asymmetric-research/solana_exporter/pkg/rpc" "github.com/asymmetric-research/solana_exporter/pkg/rpc"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"testing" "testing"
) )
var (
rawLeaderSchedule = map[string]any{
"aaa": []int{0, 3, 6, 9, 12},
"bbb": []int{1, 4, 7, 10, 13},
"ccc": []int{2, 5, 8, 11, 14},
}
)
func TestSelectFromSchedule(t *testing.T) { func TestSelectFromSchedule(t *testing.T) {
selected := SelectFromSchedule( selected := SelectFromSchedule(
map[string][]int64{ map[string][]int64{
@ -33,7 +27,14 @@ func TestSelectFromSchedule(t *testing.T) {
func TestGetTrimmedLeaderSchedule(t *testing.T) { func TestGetTrimmedLeaderSchedule(t *testing.T) {
_, client := rpc.NewMockClient(t, _, client := rpc.NewMockClient(t,
map[string]any{"getLeaderSchedule": rawLeaderSchedule}, nil, nil, nil, nil, map[string]any{
"getLeaderSchedule": map[string]any{
"aaa": []int{0, 3, 6, 9, 12},
"bbb": []int{1, 4, 7, 10, 13},
"ccc": []int{2, 5, 8, 11, 14},
},
},
nil, nil, nil, nil,
) )
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
@ -87,3 +88,17 @@ func TestGetEpochBounds(t *testing.T) {
assert.Equal(t, int64(20), first) assert.Equal(t, int64(20), first)
assert.Equal(t, int64(29), last) assert.Equal(t, int64(29), last)
} }
//go:embed testdata/block-297609329.json
var blockJson []byte
func TestCountVoteTransactions(t *testing.T) {
var block rpc.Block
err := json.Unmarshal(blockJson, &block)
assert.NoError(t, err)
voteCount, err := CountVoteTransactions(&block)
assert.NoError(t, err)
// https://explorer.solana.com/block/297609329
assert.Equal(t, 1048, voteCount)
}