forked from getlantern/fronted
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_test.go
80 lines (68 loc) · 2.59 KB
/
cache_test.go
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
package fronted
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCaching(t *testing.T) {
dir, err := os.MkdirTemp("", "direct_test")
if !assert.NoError(t, err, "Unable to create temp dir") {
return
}
defer os.RemoveAll(dir)
cacheFile := filepath.Join(dir, "cachefile.1")
cloudsackID := "cloudsack"
providers := map[string]*Provider{
testProviderID: NewProvider(nil, "", nil, nil, nil, nil, nil),
cloudsackID: NewProvider(nil, "", nil, nil, nil, nil, nil),
}
makeDirect := func() *fronted {
d := &fronted{
masquerades: make(sortedMasquerades, 0, 1000),
maxAllowedCachedAge: 250 * time.Millisecond,
maxCacheSize: 4,
cacheSaveInterval: 50 * time.Millisecond,
cacheDirty: make(chan interface{}, 1),
cacheClosed: make(chan interface{}),
providers: providers,
defaultProviderID: cloudsackID,
}
go d.maintainCache(cacheFile)
return d
}
now := time.Now()
mb := &masquerade{Masquerade: Masquerade{Domain: "b", IpAddress: "2"}, LastSucceeded: now, ProviderID: testProviderID}
mc := &masquerade{Masquerade: Masquerade{Domain: "c", IpAddress: "3"}, LastSucceeded: now, ProviderID: ""} // defaulted
md := &masquerade{Masquerade: Masquerade{Domain: "d", IpAddress: "4"}, LastSucceeded: now, ProviderID: "sadcloud"} // skipped
d := makeDirect()
d.masquerades = append(d.masquerades, mb, mc, md)
readCached := func() []*masquerade {
var result []*masquerade
b, err := os.ReadFile(cacheFile)
require.NoError(t, err, "Unable to read cache file")
err = json.Unmarshal(b, &result)
require.NoError(t, err, "Unable to unmarshal cache file")
return result
}
// Save the cache
d.markCacheDirty()
time.Sleep(d.cacheSaveInterval * 2)
d.closeCache()
time.Sleep(50 * time.Millisecond)
// Reopen cache file and make sure right data was in there
d = makeDirect()
d.prepopulateMasquerades(cacheFile)
masquerades := readCached()
require.Len(t, masquerades, 3, "Wrong number of masquerades read")
for i, expected := range []*masquerade{mb, mc, md} {
require.Equal(t, expected.Domain, masquerades[i].Domain, "Wrong masquerade at position %d", i)
require.Equal(t, expected.IpAddress, masquerades[i].IpAddress, "Masquerade at position %d has wrong IpAddress", 0)
require.Equal(t, expected.ProviderID, masquerades[i].ProviderID, "Masquerade at position %d has wrong ProviderID", 0)
require.Equal(t, now.Unix(), masquerades[i].LastSucceeded.Unix(), "Masquerade at position %d has wrong LastSucceeded", 0)
}
d.closeCache()
}