Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions server/gsl/gsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ func (s *GenericSublist[T]) Remove(subject string, value T) error {

// HasInterestStartingIn is a helper for subject tree intersection.
func (s *GenericSublist[T]) HasInterestStartingIn(subj string) bool {
s.RLock()
defer s.RUnlock()
var _tokens [64]string
tokens := tokenizeSubjectIntoSlice(_tokens[:0], subj)
return hasInterestStartingIn(s.root, tokens)
Expand Down
53 changes: 53 additions & 0 deletions server/gsl/gsl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,59 @@ func TestGenericSublistHasInterestOverlapping(t *testing.T) {
require_True(t, s.HasInterest("stream.A"))
}

// TestGenericSublistHasInterestStartingInRace tests that HasInterestStartingIn
// is safe to call concurrently with modifications to the sublist.
func TestGenericSublistHasInterestStartingInRace(t *testing.T) {
s := NewSublist[int]()

// Pre-populate with some patterns
for i := 0; i < 10; i++ {
s.Insert("foo.bar.baz", i)
s.Insert("foo.*.baz", i+10)
s.Insert("foo.>", i+20)
}

done := make(chan struct{})
const iterations = 1000

// Goroutine 1: repeatedly call HasInterestStartingIn
go func() {
for i := 0; i < iterations; i++ {
s.HasInterestStartingIn("foo")
s.HasInterestStartingIn("foo.bar")
s.HasInterestStartingIn("foo.bar.baz")
s.HasInterestStartingIn("other.subject")
}
done <- struct{}{}
}()

// Goroutine 2: repeatedly modify the sublist
go func() {
for i := 0; i < iterations; i++ {
val := 1000 + i
s.Insert("test.subject."+string(rune('a'+i%26)), val)
s.Insert("foo.*.test", val)
s.Remove("test.subject."+string(rune('a'+i%26)), val)
s.Remove("foo.*.test", val)
}
done <- struct{}{}
}()

// Goroutine 3: also call HasInterest (which does lock)
go func() {
for i := 0; i < iterations; i++ {
s.HasInterest("foo.bar.baz")
s.HasInterest("foo.something.baz")
}
done <- struct{}{}
}()

// Wait for all goroutines
<-done
<-done
<-done
}

func TestGenericSublistNumInterest(t *testing.T) {
s := NewSublist[int]()
require_NoError(t, s.Insert("foo", 11))
Expand Down