mirror of
https://github.com/phishingclub/phishingclub.git
synced 2026-12-06 01:53:42 +01:00
152 lines
5.4 KiB
Go
152 lines
5.4 KiB
Go
package service
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// TestProgramResultsCacheBounds checks the size bound and that a result
|
|
// computed across an invalidation is not kept
|
|
func TestProgramResultsCacheBounds(t *testing.T) {
|
|
c := &programResultsCache{}
|
|
now := time.Now()
|
|
for i := 0; i < programResultsCacheSize+10; i++ {
|
|
key := newProgramResultsKey(uuid.New(), nil, nil)
|
|
c.store(key, 0, &programResultsEntry{results: &ProgramResults{}, expires: now.Add(time.Hour), used: now.Add(time.Duration(i))}, false)
|
|
}
|
|
if len(c.entries) != programResultsCacheSize {
|
|
t.Fatalf("entries %d, want %d", len(c.entries), programResultsCacheSize)
|
|
}
|
|
id := uuid.New()
|
|
key := newProgramResultsKey(id, nil, nil)
|
|
gen := c.generationOf(id)
|
|
c.invalidate(id)
|
|
c.store(key, gen, &programResultsEntry{results: &ProgramResults{}, expires: now.Add(time.Hour)}, false)
|
|
if _, ok := c.entries[key]; ok {
|
|
t.Fatal("a result computed across an invalidation was kept")
|
|
}
|
|
}
|
|
|
|
// TestProgramResultsCacheBackground checks results of background work never
|
|
// push out an entry and only replace one of the same key, keeping its use
|
|
func TestProgramResultsCacheBackground(t *testing.T) {
|
|
c := &programResultsCache{}
|
|
now := time.Now()
|
|
keys := []programResultsKey{}
|
|
for i := 0; i < programResultsCacheSize; i++ {
|
|
key := newProgramResultsKey(uuid.New(), nil, nil)
|
|
keys = append(keys, key)
|
|
c.store(key, 0, &programResultsEntry{results: &ProgramResults{}, expires: now.Add(time.Hour), used: now.Add(time.Duration(i))}, false)
|
|
}
|
|
c.store(newProgramResultsKey(uuid.New(), nil, nil), 0, &programResultsEntry{results: &ProgramResults{}, expires: now.Add(time.Hour), used: now.Add(time.Hour)}, true)
|
|
for _, k := range keys {
|
|
if _, ok := c.entries[k]; !ok {
|
|
t.Fatal("background results pushed out an entry")
|
|
}
|
|
}
|
|
if len(c.entries) != programResultsCacheSize {
|
|
t.Fatalf("entries %d, want %d", len(c.entries), programResultsCacheSize)
|
|
}
|
|
fresh := &programResultsEntry{results: &ProgramResults{}, expires: now.Add(time.Hour), used: now.Add(time.Hour)}
|
|
c.store(keys[0], 0, fresh, true)
|
|
if c.entries[keys[0]] != fresh || !fresh.used.Equal(now) {
|
|
t.Fatalf("background results must replace the entry and keep its use: %v", c.entries[keys[0]].used)
|
|
}
|
|
}
|
|
|
|
// TestProgramResultsCacheInvalidateDuringLoad checks a facts store read
|
|
// across a results invalidation is checked again when it is kept
|
|
func TestProgramResultsCacheInvalidateDuringLoad(t *testing.T) {
|
|
c := &programResultsCache{}
|
|
id := uuid.New()
|
|
st := newProgramFactsStore(id, false)
|
|
st.resultsGen = c.generationOf(id)
|
|
// the program changes while the store is read
|
|
c.invalidateResults(id)
|
|
c.keepFacts(st, time.Now(), false)
|
|
if !st.recheck.Load() {
|
|
t.Fatal("a store read across an invalidation must be checked again")
|
|
}
|
|
st.recheck.Store(false)
|
|
c.keepFacts(st, time.Now(), false)
|
|
if st.recheck.Load() {
|
|
t.Fatal("a store that saw the invalidation must not be checked again")
|
|
}
|
|
c.invalidateResults(id)
|
|
if !st.recheck.Load() {
|
|
t.Fatal("a kept store must be checked again after an invalidation")
|
|
}
|
|
}
|
|
|
|
// TestProgramFactsKeepUsesKeptSizes checks keepFacts does not read the size
|
|
// of the other kept stores, so a store under its write lock for a long
|
|
// refresh does not hold up keeping another
|
|
func TestProgramFactsKeepUsesKeptSizes(t *testing.T) {
|
|
c := &programResultsCache{}
|
|
now := time.Now()
|
|
busy := newProgramFactsStore(uuid.New(), false)
|
|
busy.sends = make([]programFactSend, 10)
|
|
c.keepFacts(busy, now, false)
|
|
if busy.keptSize != 10 {
|
|
t.Fatalf("kept size %d, want 10", busy.keptSize)
|
|
}
|
|
busy.mu.Lock()
|
|
defer busy.mu.Unlock()
|
|
done := make(chan struct{})
|
|
go func() {
|
|
other := newProgramFactsStore(uuid.New(), false)
|
|
other.sends = make([]programFactSend, 5)
|
|
c.keepFacts(other, now.Add(time.Second), false)
|
|
close(done)
|
|
}()
|
|
select {
|
|
case <-done:
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("keeping facts waited for the lock of another store")
|
|
}
|
|
if len(c.facts) != 2 {
|
|
t.Fatalf("kept %d, want 2", len(c.facts))
|
|
}
|
|
}
|
|
|
|
// TestProgramFactsCacheBounds checks the facts of too many programs or too
|
|
// many sends make room by the least recently used, and facts read across an
|
|
// invalidation are not kept
|
|
func TestProgramFactsCacheBounds(t *testing.T) {
|
|
c := &programResultsCache{}
|
|
now := time.Now()
|
|
stores := []*programFactsStore{}
|
|
for i := 0; i < programFactsCacheSize+3; i++ {
|
|
st := newProgramFactsStore(uuid.New(), false)
|
|
st.sends = make([]programFactSend, 10)
|
|
c.keepFacts(st, now.Add(time.Duration(i)*time.Second), false)
|
|
stores = append(stores, st)
|
|
}
|
|
if len(c.facts) != programFactsCacheSize {
|
|
t.Fatalf("kept %d, want %d", len(c.facts), programFactsCacheSize)
|
|
}
|
|
if _, ok := c.facts[stores[0].program]; ok {
|
|
t.Fatal("the least recently used facts must make room")
|
|
}
|
|
big := newProgramFactsStore(uuid.New(), false)
|
|
big.sends = make([]programFactSend, programFactsMaxSends-5)
|
|
c.keepFacts(big, now.Add(time.Hour), false)
|
|
if c.facts[big.program] != big || len(c.facts) != 1 {
|
|
t.Fatalf("a large program must push out the others, kept %d", len(c.facts))
|
|
}
|
|
huge := newProgramFactsStore(uuid.New(), false)
|
|
huge.sends = make([]programFactSend, programFactsMaxSends+1)
|
|
c.keepFacts(huge, now.Add(2*time.Hour), false)
|
|
if _, ok := c.facts[huge.program]; ok {
|
|
t.Fatal("facts over the bound must not be kept")
|
|
}
|
|
st := newProgramFactsStore(uuid.New(), false)
|
|
c.invalidate(st.program)
|
|
c.keepFacts(st, now, false)
|
|
if _, ok := c.facts[st.program]; ok {
|
|
t.Fatal("facts read across an invalidation were kept")
|
|
}
|
|
}
|