Files
coredns/plugin/file/tree/elem_test.go
Ville Vesilehto 70fb03f711 fix(file): fix data race in tree Elem.Name (#7574)
Eagerly set name in newElem and make Name() read-only to avoid
racy lazy writes under concurrent lookups. Add tests for empty-name
comparisons and concurrent access to Less/Name(). In addition,
regression tests to CloudDNS plugin.

Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
2025-09-28 19:49:47 -07:00

42 lines
857 B
Go

package tree
import (
"testing"
"github.com/miekg/dns"
)
// Test that Name() falls back to reading from the stored RRs when the cached name is empty.
func TestElemName_FallbackWhenCachedEmpty(t *testing.T) {
rr, err := dns.NewRR("a.example. 3600 IN A 1.2.3.4")
if err != nil {
t.Fatalf("failed to create RR: %v", err)
}
// Build via newElem to ensure m is populated
e := newElem(rr)
got := e.Name()
want := "a.example."
if got != want {
t.Fatalf("unexpected name; want %q, got %q", want, got)
}
// clear the cached name
e.name = ""
got = e.Name()
want = "a.example."
if got != want {
t.Fatalf("unexpected name; want %q, got %q", want, got)
}
// clear the map
e.m = make(map[uint16][]dns.RR, 0)
got = e.Name()
want = ""
if got != want {
t.Fatalf("unexpected name after clearing RR map; want %q, got %q", want, got)
}
}