Repository navigation
Expand file tree
/
Copy pathcache_test.go
More file actions
103 lines (85 loc) · 2.87 KB
/
Copy pathcache_test.go
File metadata and controls
103 lines (85 loc) · 2.87 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package domainfront
import (
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFileCache(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "cache.json")
cache := &FileCache{Path: path}
// Initially empty
loaded, err := cache.Load()
require.NoError(t, err)
assert.Nil(t, loaded)
// Save some fronts
fronts := []*CachedFront{
{Domain: "a.com", IpAddress: "1.1.1.1", ProviderID: "p1", LastSucceeded: time.Now()},
{Domain: "b.com", IpAddress: "2.2.2.2", ProviderID: "p2", LastSucceeded: time.Now()},
}
require.NoError(t, cache.Save(fronts))
// Load back
loaded, err = cache.Load()
require.NoError(t, err)
require.Len(t, loaded, 2)
assert.Equal(t, "a.com", loaded[0].Domain)
assert.Equal(t, "b.com", loaded[1].Domain)
}
func TestFileCache_MissingFile(t *testing.T) {
cache := &FileCache{Path: "/nonexistent/path/cache.json"}
loaded, err := cache.Load()
assert.NoError(t, err)
assert.Nil(t, loaded)
}
func TestNopCache(t *testing.T) {
cache := NopCache{}
loaded, err := cache.Load()
assert.NoError(t, err)
assert.Nil(t, loaded)
assert.NoError(t, cache.Save([]*CachedFront{}))
}
func TestApplyCachedState(t *testing.T) {
now := time.Now()
fronts := []*front{
newFront(&Masquerade{Domain: "a.com", IpAddress: "1.1.1.1"}, "p1"),
newFront(&Masquerade{Domain: "b.com", IpAddress: "2.2.2.2"}, "p2"),
}
cached := []*CachedFront{
{Domain: "a.com", IpAddress: "1.1.1.1", ProviderID: "p1", LastSucceeded: now.Add(-1 * time.Hour)},
{Domain: "b.com", IpAddress: "2.2.2.2", ProviderID: "p2", LastSucceeded: now.Add(-25 * time.Hour)}, // stale
}
applyCachedState(fronts, cached, 24*time.Hour)
assert.True(t, fronts[0].isSucceeding(), "fresh cached state should be applied")
assert.False(t, fronts[1].isSucceeding(), "stale cached state should not be applied")
}
func TestFrontsToCache(t *testing.T) {
fronts := []*front{
newFront(&Masquerade{Domain: "a.com", IpAddress: "1.1.1.1"}, "p1"),
newFront(&Masquerade{Domain: "b.com", IpAddress: "2.2.2.2"}, "p2"),
newFront(&Masquerade{Domain: "c.com", IpAddress: "3.3.3.3"}, "p3"),
}
// Mark all as succeeded — only succeeded fronts are cached
for _, f := range fronts {
f.markSucceeded()
}
cached := frontsToCache(fronts, 2)
assert.Len(t, cached, 2, "should be limited to maxSize")
// Verify that never-succeeded fronts are skipped
unsucceeded := []*front{
newFront(&Masquerade{Domain: "d.com", IpAddress: "4.4.4.4"}, "p4"),
}
cached = frontsToCache(unsucceeded, 10)
assert.Len(t, cached, 0, "never-succeeded fronts should not be cached")
}
func TestFileCache_CreatesDirs(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "sub", "dir", "cache.json")
cache := &FileCache{Path: path}
fronts := []*CachedFront{{Domain: "a.com"}}
require.NoError(t, cache.Save(fronts))
_, err := os.Stat(path)
assert.NoError(t, err)
}