Path: blob/main/component/prometheus/globalrefmap_test.go
4093 views
package prometheus12import (3"testing"4"time"56"github.com/prometheus/prometheus/model/labels"7"github.com/stretchr/testify/require"8)910func TestAddingMarker(t *testing.T) {11mapping := newGlobalRefMap()12l := labels.Labels{}13l = append(l, labels.Label{14Name: "__name__",15Value: "test",16})17globalID := mapping.GetOrAddGlobalRefID(l)18shouldBeSameGlobalID := mapping.GetOrAddGlobalRefID(l)19require.True(t, globalID == shouldBeSameGlobalID)20require.Len(t, mapping.labelsHashToGlobal, 1)21}2223func TestAddingDifferentMarkers(t *testing.T) {24mapping := newGlobalRefMap()25l := labels.Labels{}26l = append(l, labels.Label{27Name: "__name__",28Value: "test",29})30l2 := labels.Labels{}31l2 = append(l2, labels.Label{32Name: "__name__",33Value: "roar",34})35globalID := mapping.GetOrAddGlobalRefID(l)36shouldBeDifferentID := mapping.GetOrAddGlobalRefID(l2)37require.True(t, globalID != shouldBeDifferentID)38require.Len(t, mapping.labelsHashToGlobal, 2)39}4041func TestAddingLocalMapping(t *testing.T) {42mapping := newGlobalRefMap()43l := labels.Labels{}44l = append(l, labels.Label{45Name: "__name__",46Value: "test",47})4849globalID := mapping.GetOrAddGlobalRefID(l)50shouldBeSameGlobalID := mapping.GetOrAddLink("1", 1, l)51require.True(t, globalID == shouldBeSameGlobalID)52require.Len(t, mapping.labelsHashToGlobal, 1)53require.Len(t, mapping.mappings, 1)54require.True(t, mapping.mappings["1"].RemoteWriteID == "1")55require.True(t, mapping.mappings["1"].globalToLocal[shouldBeSameGlobalID] == 1)56require.True(t, mapping.mappings["1"].localToGlobal[1] == shouldBeSameGlobalID)57}5859func TestAddingLocalMappings(t *testing.T) {60mapping := newGlobalRefMap()61l := labels.Labels{}62l = append(l, labels.Label{63Name: "__name__",64Value: "test",65})6667globalID := mapping.GetOrAddGlobalRefID(l)68shouldBeSameGlobalID := mapping.GetOrAddLink("1", 1, l)69shouldBeSameGlobalID2 := mapping.GetOrAddLink("2", 1, l)70require.True(t, globalID == shouldBeSameGlobalID)71require.True(t, globalID == shouldBeSameGlobalID2)72require.Len(t, mapping.labelsHashToGlobal, 1)73require.Len(t, mapping.mappings, 2)7475require.True(t, mapping.mappings["1"].RemoteWriteID == "1")76require.True(t, mapping.mappings["1"].globalToLocal[shouldBeSameGlobalID] == 1)77require.True(t, mapping.mappings["1"].localToGlobal[1] == shouldBeSameGlobalID)7879require.True(t, mapping.mappings["2"].RemoteWriteID == "2")80require.True(t, mapping.mappings["2"].globalToLocal[shouldBeSameGlobalID2] == 1)81require.True(t, mapping.mappings["2"].localToGlobal[1] == shouldBeSameGlobalID2)82}8384func TestAddingLocalMappingsWithoutCreatingGlobalUpfront(t *testing.T) {85mapping := newGlobalRefMap()86l := labels.Labels{}87l = append(l, labels.Label{88Name: "__name__",89Value: "test",90})9192shouldBeSameGlobalID := mapping.GetOrAddLink("1", 1, l)93shouldBeSameGlobalID2 := mapping.GetOrAddLink("2", 1, l)94require.True(t, shouldBeSameGlobalID2 == shouldBeSameGlobalID)95require.Len(t, mapping.labelsHashToGlobal, 1)96require.Len(t, mapping.mappings, 2)9798require.True(t, mapping.mappings["1"].RemoteWriteID == "1")99require.True(t, mapping.mappings["1"].globalToLocal[shouldBeSameGlobalID] == 1)100require.True(t, mapping.mappings["1"].localToGlobal[1] == shouldBeSameGlobalID)101102require.True(t, mapping.mappings["2"].RemoteWriteID == "2")103require.True(t, mapping.mappings["2"].globalToLocal[shouldBeSameGlobalID2] == 1)104require.True(t, mapping.mappings["2"].localToGlobal[1] == shouldBeSameGlobalID2)105}106107func TestStaleness(t *testing.T) {108mapping := newGlobalRefMap()109l := labels.Labels{}110l = append(l, labels.Label{111Name: "__name__",112Value: "test",113})114l2 := labels.Labels{}115l2 = append(l2, labels.Label{116Name: "__name__",117Value: "test2",118})119120global1 := mapping.GetOrAddLink("1", 1, l)121_ = mapping.GetOrAddLink("2", 1, l2)122mapping.AddStaleMarker(global1, l)123require.Len(t, mapping.staleGlobals, 1)124require.Len(t, mapping.labelsHashToGlobal, 2)125staleDuration = 1 * time.Millisecond126time.Sleep(10 * time.Millisecond)127mapping.CheckStaleMarkers()128require.Len(t, mapping.staleGlobals, 0)129require.Len(t, mapping.labelsHashToGlobal, 1)130}131132func TestRemovingStaleness(t *testing.T) {133mapping := newGlobalRefMap()134l := labels.Labels{}135l = append(l, labels.Label{136Name: "__name__",137Value: "test",138})139140global1 := mapping.GetOrAddLink("1", 1, l)141mapping.AddStaleMarker(global1, l)142require.Len(t, mapping.staleGlobals, 1)143mapping.RemoveStaleMarker(global1)144require.Len(t, mapping.staleGlobals, 0)145}146147148