-
-
Notifications
You must be signed in to change notification settings - Fork 341
Expand file tree
/
Copy pathtopicselector_test.go
More file actions
64 lines (49 loc) · 2.1 KB
/
topicselector_test.go
File metadata and controls
64 lines (49 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package mercure
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMatchCache(t *testing.T) {
t.Parallel()
tss, err := NewTopicSelectorStore(DefaultTopicSelectorStoreCacheSize)
require.NoError(t, err)
assert.False(t, tss.match("foo", "bar"))
assert.True(t, tss.match("https://example.com/foo/bar", "https://example.com/{foo}/bar"))
// Template should be cached
_, found := tss.templateCache.GetIfPresent("https://example.com/{foo}/bar")
assert.True(t, found)
// Match result should be cached (struct key, no collision possible)
_, found = tss.matchCache.GetIfPresent(matchCacheKey{
topicSelector: "https://example.com/{foo}/bar",
topic: "https://example.com/foo/bar",
})
assert.True(t, found)
assert.True(t, tss.match("https://example.com/foo/bar", "https://example.com/{foo}/bar"))
assert.False(t, tss.match("https://example.com/foo/bar/baz", "https://example.com/{foo}/bar"))
assert.True(t, tss.match("https://example.com/kevin/dunglas", "https://example.com/{fistname}/{lastname}"))
assert.True(t, tss.match("https://example.com/foo/bar", "*"))
assert.True(t, tss.match("https://example.com/foo/bar", "https://example.com/foo/bar"))
assert.True(t, tss.match("foo", "foo"))
assert.False(t, tss.match("foo", "bar"))
}
func TestMatchCacheKeyNoCollision(t *testing.T) {
t.Parallel()
tss, err := NewTopicSelectorStore(DefaultTopicSelectorStoreCacheSize)
require.NoError(t, err)
// With struct keys, these are naturally distinct — no encoding tricks needed.
// Use URI templates to ensure results get cached.
assert.True(t, tss.match("https://example.com/a", "https://example.com/{x}"))
assert.False(t, tss.match("https://other.com/a", "https://example.com/{x}"))
// Verify independent cache entries exist
_, found := tss.matchCache.GetIfPresent(matchCacheKey{
topicSelector: "https://example.com/{x}",
topic: "https://example.com/a",
})
assert.True(t, found)
_, found = tss.matchCache.GetIfPresent(matchCacheKey{
topicSelector: "https://example.com/{x}",
topic: "https://other.com/a",
})
assert.True(t, found)
}