0
0
mirror of https://github.com/go-gitea/gitea.git synced 2026-04-25 15:36:18 +02:00
gitea/services/actions/matrix_metrics_test.go
Pascal Zimmermann a1c60ac854 feat: Add support for dynamic matrix strategies with job outputs
Signed-off-by: Pascal Zimmermann <pascal.zimmermann@theiotstudio.com>

# Conflicts:
#	models/actions/run_job.go
#	models/migrations/migrations.go
#	models/migrations/v1_26/v326.go
#	services/actions/run.go
2026-04-01 08:14:51 +02:00

83 lines
2.2 KiB
Go

// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package actions
import (
"testing"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
)
// Essential Prometheus Collector Tests
func TestNewMatrixMetricsCollector(t *testing.T) {
collector := NewMatrixMetricsCollector()
assert.NotNil(t, collector)
assert.NotNil(t, collector.totalReevaluations)
assert.NotNil(t, collector.successRate)
}
func TestMatrixMetricsCollectorDescribe(t *testing.T) {
collector := NewMatrixMetricsCollector()
ch := make(chan *prometheus.Desc, 100)
collector.Describe(ch)
assert.NotEmpty(t, ch)
}
func TestMatrixMetricsCollectorCollect(t *testing.T) {
matrixMetricsInstance = nil
metrics := GetMatrixMetrics()
metrics.RecordReevaluation(10*time.Millisecond, true, 5)
metrics.RecordParseTime(8 * time.Millisecond)
collector := NewMatrixMetricsCollector()
ch := make(chan prometheus.Metric, 100)
collector.Collect(ch)
assert.NotEmpty(t, ch)
matrixMetricsInstance = nil
}
func TestMatrixMetricsGetStats(t *testing.T) {
metrics := &MatrixMetrics{
ReevaluationTimes: make([]time.Duration, 0, 1000),
ParseTimes: make([]time.Duration, 0, 1000),
InsertTimes: make([]time.Duration, 0, 1000),
}
metrics.RecordReevaluation(10*time.Millisecond, true, 3)
metrics.RecordReevaluation(15*time.Millisecond, true, 2)
metrics.RecordReevaluation(5*time.Millisecond, false, 0)
stats := metrics.GetStats()
assert.Equal(t, int64(3), stats["total_reevaluations"])
assert.Equal(t, int64(2), stats["successful_reevaluations"])
assert.Equal(t, int64(1), stats["failed_reevaluations"])
assert.Greater(t, stats["success_rate_percent"].(float64), 60.0)
}
func BenchmarkMatrixMetricsCollectorCollect(b *testing.B) {
metrics := &MatrixMetrics{
ReevaluationTimes: make([]time.Duration, 0, 1000),
ParseTimes: make([]time.Duration, 0, 1000),
InsertTimes: make([]time.Duration, 0, 1000),
}
matrixMetricsInstance = metrics
for range 100 {
metrics.RecordReevaluation(10*time.Millisecond, true, 5)
metrics.RecordParseTime(5 * time.Millisecond)
}
collector := NewMatrixMetricsCollector()
ch := make(chan prometheus.Metric, 100)
b.ResetTimer()
for b.Loop() {
collector.Collect(ch)
}
}