added TestGetEpochBounds

This commit is contained in:
Matt Johnstone 2024-10-18 17:10:42 +02:00
parent 76755da9f3
commit dadf7ec668
No known key found for this signature in database
GPG Key ID: BE985FBB9BE7D3BB
6 changed files with 27 additions and 9 deletions

View File

@ -281,7 +281,6 @@ func main() {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
go slotWatcher.WatchSlots(ctx, collector.slotPace)
go collector.WatchHealth(context.Background())
prometheus.MustRegister(collector)
http.Handle("/metrics", promhttp.Handler())

View File

@ -178,6 +178,12 @@ func (c *staticRPCClient) GetBlock(
return nil, nil
}
//goland:noinspection GoUnusedParameter
func (c *staticRPCClient) GetHealth(ctx context.Context) (*string, error) {
health := "ok"
return &health, nil
}
/*
===== DYNAMIC CLIENT =====:
*/
@ -373,6 +379,12 @@ func (c *dynamicRPCClient) GetBlock(
return nil, nil
}
//goland:noinspection GoUnusedParameter
func (c *dynamicRPCClient) GetHealth(ctx context.Context) (*string, error) {
health := "ok"
return &health, nil
}
/*
===== OTHER TEST UTILITIES =====:
*/

View File

@ -205,7 +205,7 @@ func (c *SlotWatcher) WatchSlots(ctx context.Context, pace time.Duration) {
// and updates the prometheus gauges associated with those metrics.
func (c *SlotWatcher) trackEpoch(ctx context.Context, epoch *rpc.EpochInfo) {
klog.Infof("Tracking epoch %v (from %v)", epoch.Epoch, c.currentEpoch)
firstSlot, lastSlot := getEpochBounds(epoch)
firstSlot, lastSlot := GetEpochBounds(epoch)
// if we haven't yet set c.currentEpoch, that (hopefully) means this is the initial setup,
// and so we can simply store the tracking numbers
if c.currentEpoch == 0 {
@ -393,12 +393,6 @@ func (c *SlotWatcher) fetchAndEmitSingleBlockInfo(
return nil
}
// getEpochBounds returns the first slot and last slot within an [inclusive] Epoch
func getEpochBounds(info *rpc.EpochInfo) (int64, int64) {
firstSlot := info.AbsoluteSlot - info.SlotIndex
return firstSlot, firstSlot + info.SlotsInEpoch - 1
}
// fetchAndEmitInflationRewards fetches and emits the inflation rewards for the configured inflationRewardAddresses
// at the provided epoch
func (c *SlotWatcher) fetchAndEmitInflationRewards(ctx context.Context, epoch int64) error {

View File

@ -106,7 +106,7 @@ func TestSolanaCollector_WatchSlots_Static(t *testing.T) {
assert.NoError(t, err)
time.Sleep(1 * time.Second)
firstSlot, lastSlot := getEpochBounds(&staticEpochInfo)
firstSlot, lastSlot := GetEpochBounds(&staticEpochInfo)
type testCase struct {
expectedValue float64
metric prometheus.Gauge

View File

@ -113,3 +113,9 @@ func CombineUnique[T comparable](args ...[]T) []T {
}
return uniqueItems
}
// GetEpochBounds returns the first slot and last slot within an [inclusive] Epoch
func GetEpochBounds(info *rpc.EpochInfo) (int64, int64) {
firstSlot := info.AbsoluteSlot - info.SlotIndex
return firstSlot, firstSlot + info.SlotsInEpoch - 1
}

View File

@ -56,3 +56,10 @@ func TestGetAssociatedVoteAccounts(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, votekeys, voteAccounts)
}
func TestGetEpochBounds(t *testing.T) {
epoch := rpc.EpochInfo{AbsoluteSlot: 25, SlotIndex: 5, SlotsInEpoch: 10}
first, last := GetEpochBounds(&epoch)
assert.Equal(t, int64(20), first)
assert.Equal(t, int64(29), last)
}