Skip to content

Commit

Permalink
fix: flakyness for removal test fixed by returning second uuid for re…
Browse files Browse the repository at this point in the history
…moval
  • Loading branch information
schaermu committed Apr 12, 2024
1 parent 711b6e7 commit 0d7e30e
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions pkg/collectors/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
promtest "github.com/prometheus/client_golang/prometheus/testutil"
"github.com/schaermu/changedetection.io-exporter/internal/testutil"
"github.com/schaermu/changedetection.io-exporter/pkg/data"
"golang.org/x/exp/maps"
)

var (
Expand All @@ -35,17 +34,18 @@ func expectMetricCount(t *testing.T, c prometheus.Collector, expected int) {
}
}

func createCollectorTestDb() map[string]*data.WatchItem {
// createCollectorTestDb creates a test database with two watch items and returns the UUID of the second item and the database.
func createCollectorTestDb() (string, map[string]*data.WatchItem) {
watchDb := testutil.NewWatchDb(0)
uuid1, watch1 := testutil.NewTestItem("Item 1", 100, "USD")
uuid2, watch2 := testutil.NewTestItem("Item 2", 200, "USD")
watchDb[uuid1] = watch1
watchDb[uuid2] = watch2
return watchDb
return uuid2, watchDb
}

func TestPriceCollector(t *testing.T) {
watchDb := createCollectorTestDb()
_, watchDb := createCollectorTestDb()
server := testutil.CreateTestApiServer(t, watchDb)
defer server.Close()

Expand All @@ -58,8 +58,8 @@ func TestPriceCollector(t *testing.T) {
expectMetrics(t, c, "price_metrics.prom")
}

func TestAutoUnregisterCollector(t *testing.T) {
watchDb := createCollectorTestDb()
func TestPriceCollector_RemoveWatchDuringRuntime(t *testing.T) {
keyToRemove, watchDb := createCollectorTestDb()
server := testutil.CreateTestApiServer(t, watchDb)
defer server.Close()

Expand All @@ -69,15 +69,14 @@ func TestAutoUnregisterCollector(t *testing.T) {
}
expectMetricCount(t, c, 2)

keyToRemove := maps.Keys(watchDb)[len(watchDb)-1]
delete(watchDb, keyToRemove)

expectMetricCount(t, c, 1)
expectMetrics(t, c, "price_metrics_autounregister.prom")
}

func TestPriceCollector_NewWatchDuringRuntime(t *testing.T) {
watchDb := createCollectorTestDb()
_, watchDb := createCollectorTestDb()
server := testutil.CreateTestApiServer(t, watchDb)
defer server.Close()

Expand Down

0 comments on commit 0d7e30e

Please sign in to comment.