forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
urlcaching_test.go
94 lines (82 loc) · 2.54 KB
/
urlcaching_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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package avatars
import (
"testing"
"time"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/clockwork"
"github.com/stretchr/testify/require"
)
type apiHandlerFn func(libkb.APIArg, libkb.APIResponseWrapper) error
type avatarMockAPI struct {
libkb.API
handler apiHandlerFn
}
func (a *avatarMockAPI) GetDecode(arg libkb.APIArg, res libkb.APIResponseWrapper) error {
return a.handler(arg, res)
}
func newAvatarMockAPI(f apiHandlerFn) *avatarMockAPI {
return &avatarMockAPI{handler: f}
}
func makeHandler(url string, cb chan struct{}) apiHandlerFn {
return func(arg libkb.APIArg, res libkb.APIResponseWrapper) error {
m := make(map[keybase1.AvatarFormat]keybase1.AvatarUrl)
m["square"] = keybase1.MakeAvatarURL(url)
res.(*apiAvatarRes).Pictures = []map[keybase1.AvatarFormat]keybase1.AvatarUrl{m}
cb <- struct{}{}
return nil
}
}
func TestAvatarsURLCaching(t *testing.T) {
tc := libkb.SetupTest(t, "TestAvatarsURLCaching", 1)
defer tc.Cleanup()
clock := clockwork.NewFakeClock()
tc.G.SetClock(clock)
cb := make(chan struct{}, 5)
tc.G.API = newAvatarMockAPI(makeHandler("url", cb))
source := NewURLCachingSource(time.Hour, 10)
t.Logf("API server fetch")
m := libkb.NewMetaContextForTest(tc)
res, err := source.LoadUsers(m, []string{"mike"}, []keybase1.AvatarFormat{"square"})
require.NoError(t, err)
require.Equal(t, "url", res.Picmap["mike"]["square"].String())
select {
case <-cb:
case <-time.After(20 * time.Second):
require.Fail(t, "no API call")
}
t.Logf("cache fetch")
res, err = source.LoadUsers(m, []string{"mike"}, []keybase1.AvatarFormat{"square"})
require.NoError(t, err)
require.Equal(t, "url", res.Picmap["mike"]["square"].String())
select {
case <-cb:
require.Fail(t, "no API call")
default:
}
t.Logf("stale")
source.staleFetchCh = make(chan struct{}, 5)
clock.Advance(2 * time.Hour)
tc.G.API = newAvatarMockAPI(makeHandler("url2", cb))
res, err = source.LoadUsers(m, []string{"mike"}, []keybase1.AvatarFormat{"square"})
require.NoError(t, err)
require.Equal(t, "url", res.Picmap["mike"]["square"].String())
select {
case <-cb:
case <-time.After(20 * time.Second):
require.Fail(t, "no API call")
}
select {
case <-source.staleFetchCh:
case <-time.After(20 * time.Second):
require.Fail(t, "no stale fetch")
}
res, err = source.LoadUsers(m, []string{"mike"}, []keybase1.AvatarFormat{"square"})
require.NoError(t, err)
require.Equal(t, "url2", res.Picmap["mike"]["square"].String())
select {
case <-cb:
require.Fail(t, "no API call")
default:
}
}