Skip to content

Commit 749dea1

Browse files
committed
test(cache): cycle-safe BuildGraph (A→B→A does not infinite-loop)
Signed-off-by: Sebastian Mendel <info@sebastianmendel.de>
1 parent d277f4c commit 749dea1

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

internal/ldap_cache/graph_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"math"
66
"sort"
77
"testing"
8+
"time"
89

910
ldap "github.com/netresearch/simple-ldap-go"
1011
)
@@ -323,6 +324,35 @@ func TestAssignConcentric_Deterministic(t *testing.T) {
323324
}
324325
}
325326

327+
func TestBuildGraph_CycleSafe(t *testing.T) {
328+
manager := New(&mockLDAPClient{})
329+
manager.Groups.setAll([]ldap.Group{
330+
newGroupWithDN("cn=A,ou=Groups,dc=ex,dc=com", "A", []string{"cn=B,ou=Groups,dc=ex,dc=com"}),
331+
newGroupWithDN("cn=B,ou=Groups,dc=ex,dc=com", "B", []string{"cn=A,ou=Groups,dc=ex,dc=com"}),
332+
})
333+
334+
var data *GraphData
335+
336+
done := make(chan struct{})
337+
go func() {
338+
data, _ = manager.BuildGraph("cn=A,ou=Groups,dc=ex,dc=com", 3)
339+
close(done)
340+
}()
341+
342+
select {
343+
case <-done:
344+
if data == nil {
345+
t.Fatal("BuildGraph returned nil graph")
346+
}
347+
348+
if len(data.Nodes) < 2 {
349+
t.Errorf("expected at least 2 nodes (A, B), got %d", len(data.Nodes))
350+
}
351+
case <-time.After(500 * time.Millisecond):
352+
t.Fatal("BuildGraph cycled forever")
353+
}
354+
}
355+
326356
func TestAssignConcentric_EvenDistribution(t *testing.T) {
327357
m := graphFixture(t)
328358
data, _ := m.BuildGraph("cn=bob,ou=Engineering,dc=ex,dc=com", 1)

0 commit comments

Comments
 (0)