Files
ingot/labels/matchers_test.go
T
david 0356f2e082 Compaction, retention, and soak testing
Block refcounting (atomic refs + condemned flag) lets live queries
survive concurrent compaction and retention. Levelled compactor merges
2h->8h->32h blocks via raw chunk passthrough. Retention drops blocks
older than the configured window. Background goroutine drives
flush/compact/retain cycles; exported RunCompaction/ApplyRetention allow
deterministic test control via injectable clock.

Soak test: 10k series x 48h simulated at 15s intervals (115M samples).
Validates flat memory (158 MiB peak), bounded disk (13 blocks), and zero
query errors during compaction.

All existing tests refactored to table-driven with uniform assertions.
2026-07-04 17:02:57 -04:00

157 lines
4.3 KiB
Go

package labels
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMatcherMatches(t *testing.T) {
tests := []struct {
name string
typ MatchType
pattern string
value string
want bool
}{
// MatchEqual
{name: "equal_match", typ: MatchEqual, pattern: "foo", value: "foo", want: true},
{name: "equal_no_match", typ: MatchEqual, pattern: "foo", value: "bar", want: false},
{name: "equal_empty", typ: MatchEqual, pattern: "", value: "", want: true},
{name: "equal_empty_vs_nonempty", typ: MatchEqual, pattern: "", value: "x", want: false},
// MatchNotEqual
{name: "not_equal_match", typ: MatchNotEqual, pattern: "foo", value: "bar", want: true},
{name: "not_equal_no_match", typ: MatchNotEqual, pattern: "foo", value: "foo", want: false},
{name: "not_equal_empty", typ: MatchNotEqual, pattern: "", value: "x", want: true},
// MatchRegexp
{name: "regexp_exact", typ: MatchRegexp, pattern: "foo", value: "foo", want: true},
{name: "regexp_no_match", typ: MatchRegexp, pattern: "foo", value: "foobar", want: false},
{name: "regexp_alternation", typ: MatchRegexp, pattern: "foo|bar", value: "bar", want: true},
{name: "regexp_alternation_no_match", typ: MatchRegexp, pattern: "foo|bar", value: "baz", want: false},
{name: "regexp_wildcard", typ: MatchRegexp, pattern: "fo.*", value: "foobar", want: true},
{name: "regexp_prefix_anchored", typ: MatchRegexp, pattern: "fo", value: "foo", want: false},
{name: "regexp_dot_plus", typ: MatchRegexp, pattern: ".+", value: "anything", want: true},
{name: "regexp_dot_plus_empty", typ: MatchRegexp, pattern: ".+", value: "", want: false},
// MatchNotRegexp
{name: "not_regexp_match", typ: MatchNotRegexp, pattern: "foo", value: "bar", want: true},
{name: "not_regexp_no_match", typ: MatchNotRegexp, pattern: "foo", value: "foo", want: false},
{name: "not_regexp_alternation", typ: MatchNotRegexp, pattern: "foo|bar", value: "baz", want: true},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
m, err := NewMatcher(tc.typ, "__name__", tc.pattern)
require.NoError(t, err)
assert.Equal(t, tc.want, m.Matches(tc.value))
})
}
}
func TestNewMatcherErrors(t *testing.T) {
tests := []struct {
name string
typ MatchType
pattern string
}{
{name: "bad_regexp_bracket", typ: MatchRegexp, pattern: "[invalid"},
{name: "bad_not_regexp_bracket", typ: MatchNotRegexp, pattern: "(unclosed"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
_, err := NewMatcher(tc.typ, "__name__", tc.pattern)
assert.Error(t, err)
})
}
}
func TestMustNewMatcherPanics(t *testing.T) {
tests := []struct {
name string
typ MatchType
pattern string
}{
{name: "bad_regexp", typ: MatchRegexp, pattern: "[invalid"},
{name: "bad_not_regexp", typ: MatchNotRegexp, pattern: "(unclosed"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
assert.Panics(t, func() {
MustNewMatcher(tc.typ, "__name__", tc.pattern)
})
})
}
}
func TestFromStrings(t *testing.T) {
tests := []struct {
name string
args []string
want []Label
}{
{
name: "single_pair",
args: []string{"__name__", "temp"},
want: []Label{{Name: "__name__", Value: "temp"}},
},
{
name: "multiple_pairs_sorted",
args: []string{"room", "office", "__name__", "temp"},
want: []Label{{Name: "__name__", Value: "temp"}, {Name: "room", Value: "office"}},
},
{
name: "empty",
args: []string{},
want: []Label{},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := FromStrings(tc.args...)
assert.Equal(t, tc.want, got)
})
}
}
func TestFromStringsPanics(t *testing.T) {
tests := []struct {
name string
args []string
}{
{name: "odd_count_one", args: []string{"__name__"}},
{name: "odd_count_three", args: []string{"__name__", "temp", "room"}},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
assert.Panics(t, func() {
FromStrings(tc.args...)
})
})
}
}
func TestMatchTypeString(t *testing.T) {
tests := []struct {
typ MatchType
want string
}{
{typ: MatchEqual, want: "="},
{typ: MatchNotEqual, want: "!="},
{typ: MatchRegexp, want: "=~"},
{typ: MatchNotRegexp, want: "!~"},
}
for _, tc := range tests {
t.Run(tc.want, func(t *testing.T) {
assert.Equal(t, tc.want, tc.typ.String())
})
}
}