MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / TestCache

Function TestCache

pkg/auth/rights/cache_test.go:35–127  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

33}
34
35func TestCache(t *testing.T) {
36 rights.OverrideNowForTest(func() time.Time {
37 return currentTime
38 })
39
40 a := assertions.New(t)
41
42 mockErr := errors.New("mock")
43
44 mockFetcher := &mockFetcher{}
45
46 c, ok := rights.NewInMemoryCache(mockFetcher, 5*time.Minute, time.Minute).(*rights.InMemoryCache)
47 if !ok {
48 t.Fatal("Failed to create in-memory cache")
49 }
50
51 mockFetcher.authInfoError = mockErr
52 mockFetcher.applicationError = mockErr
53 mockFetcher.clientError = mockErr
54 mockFetcher.gatewayError = mockErr
55 mockFetcher.organizationError = mockErr
56 mockFetcher.userError = mockErr
57
58 ctxA := context.WithValue(test.Context(), struct{}{}, "A")
59 _, authInfoErr := fetchAuthInfo(ctxA, c)
60 a.So(mockFetcher.authInfoCtx, should.Equal, ctxA)
61 a.So(authInfoErr, should.Resemble, mockFetcher.authInfoError)
62 entityRes := fetchEntityRights(ctxA, "foo", c)
63 a.So(mockFetcher.applicationCtx, should.Equal, ctxA)
64 a.So(mockFetcher.gatewayCtx, should.Equal, ctxA)
65 a.So(mockFetcher.organizationCtx, should.Equal, ctxA)
66 a.So(entityRes.AppErr, should.Resemble, mockFetcher.applicationError)
67 a.So(entityRes.GtwErr, should.Resemble, mockFetcher.gatewayError)
68 a.So(entityRes.OrgErr, should.Resemble, mockFetcher.organizationError)
69
70 timeTravel(31 * time.Second) // Error responses should be cached for 1 minute.
71
72 ctxB := context.WithValue(test.Context(), struct{}{}, "B")
73 _, _ = fetchAuthInfo(ctxB, c)
74 a.So(mockFetcher.authInfoCtx, should.Equal, ctxA)
75 _ = fetchEntityRights(ctxB, "foo", c)
76 a.So(mockFetcher.applicationCtx, should.Equal, ctxA)
77 a.So(mockFetcher.gatewayCtx, should.Equal, ctxA)
78 a.So(mockFetcher.organizationCtx, should.Equal, ctxA)
79
80 timeTravel(31 * time.Second) // Error responses should be expired after 1 minute.
81
82 _, _ = fetchAuthInfo(ctxB, c)
83 a.So(mockFetcher.authInfoCtx, should.Equal, ctxB)
84 _ = fetchEntityRights(ctxB, "foo", c)
85 a.So(mockFetcher.applicationCtx, should.Equal, ctxB)
86 a.So(mockFetcher.gatewayCtx, should.Equal, ctxB)
87 a.So(mockFetcher.organizationCtx, should.Equal, ctxB)
88
89 timeTravel(61 * time.Second)
90
91 mockFetcher.authInfoError = nil
92 mockFetcher.applicationError, mockFetcher.gatewayError, mockFetcher.organizationError = nil, nil, nil

Callers

nothing calls this directly

Calls 14

OverrideNowForTestFunction · 0.92
NewFunction · 0.92
NewInMemoryCacheFunction · 0.92
ContextFunction · 0.92
fetchAuthInfoFunction · 0.85
fetchEntityRightsFunction · 0.85
timeTravelFunction · 0.85
MaybeCleanupTestMethod · 0.80
AuthInfoTestMethod · 0.80
ApplicationRightsTestMethod · 0.80
GatewayRightsTestMethod · 0.80

Tested by

no test coverage detected