<commit_before>package ccache

import (
	"sort"
	"strconv"
	"sync/atomic"
	"testing"
	"time"

	. "github.com/karlseguin/expect"
)

type CacheTests struct{}

func Test_Cache(t *testing.T) {
	Expectify(new(CacheTests), t)
}

func (_ CacheTests) DeletesAValue() {
	cache := New(Configure())
	defer cache.Stop()
	Expect(cache.ItemCount()).To.Equal(0)

	cache.Set("spice", "flow", time.Minute)
	cache.Set("worm", "sand", time.Minute)
	Expect(cache.ItemCount()).To.Equal(2)

	cache.Delete("spice")
	Expect(cache.Get("spice")).To.Equal(nil)
	Expect(cache.Get("worm").Value()).To.Equal("sand")
	Expect(cache.ItemCount()).To.Equal(1)
}

func (_ CacheTests) DeletesAPrefix() {
	cache := New(Configure())
	defer cache.Stop()
	Expect(cache.ItemCount()).To.Equal(0)

	cache.Set("aaa", "1", time.Minute)
	cache.Set("aab", "2", time.Minute)
	cache.Set("aac", "3", time.Minute)
	cache.Set("ac", "4", time.Minute)
	cache.Set("z5", "7", time.Minute)
	Expect(cache.ItemCount()).To.Equal(5)

	Expect(cache.DeletePrefix("9a")).To.Equal(0)
	Expect(cache.ItemCount()).To.Equal(5)

	Expect(cache.DeletePrefix("aa")).To.Equal(3)
	Expect(cache.Get("aaa")).To.Equal(nil)
	Expect(cache.Get("aab")).To.Equal(nil)
	Expect(cache.Get("aac")).To.Equal(nil)
	Expect(cache.Get("ac").Value()).To.Equal("4")
	Expect(cache.Get("z5").Value()).To.Equal("7")
	Expect(cache.ItemCount()).To.Equal(2)
}

func (_ CacheTests) DeletesAFunc() {
	cache := New(Configure())
	defer cache.Stop()
	Expect(cache.ItemCount()).To.Equal(0)

	cache.Set("a", 1, time.Minute)
	cache.Set("b", 2, time.Minute)
	cache.Set("c", 3, time.Minute)
	cache.Set("d", 4, time.Minute)
	cache.Set("e", 5, time.Minute)
	cache.Set("f", 6, time.Minute)
	Expect(cache.ItemCount()).To.Equal(6)

	Expect(cache.DeleteFunc(func(key string, item *Item) bool {
		return false
	})).To.Equal(0)
	Expect(cache.ItemCount()).To.Equal(6)

	Expect(cache.DeleteFunc(func(key string, item *Item) bool {
		return item.Value().(int) < 4
	})).To.Equal(3)
	Expect(cache.ItemCount()).To.Equal(3)

	Expect(cache.DeleteFunc(func(key string, item *Item) bool {
		return key == "d"
	})).To.Equal(1)
	Expect(cache.ItemCount()).To.Equal(2)

}

func (_ CacheTests) OnDeleteCallbackCalled() {
	onDeleteFnCalled := int32(0)
	onDeleteFn := func(item *Item) {
		if item.key == "spice" {
			atomic.AddInt32(&onDeleteFnCalled, 1)
		}
	}

	cache := New(Configure().OnDelete(onDeleteFn))
	defer cache.Stop()
	cache.Set("spice", "flow", time.Minute)
	cache.Set("worm", "sand", time.Minute)

	cache.SyncUpdates() // wait for worker to pick up preceding updates

	cache.Delete("spice")
	cache.SyncUpdates()

	Expect(cache.Get("spice")).To.Equal(nil)
	Expect(cache.Get("worm").Value()).To.Equal("sand")
	Expect(atomic.LoadInt32(&onDeleteFnCalled)).To.Eql(1)
}

func (_ CacheTests) FetchesExpiredItems() {
	cache := New(Configure())
	defer cache.Stop()
	fn := func() (interface{}, error) { return "moo-moo", nil }

	cache.Set("beef", "moo", time.Second*-1)
	Expect(cache.Get("beef").Value()).To.Equal("moo")

	out, _ := cache.Fetch("beef", time.Second, fn)
	Expect(out.Value()).To.Equal("moo-moo")
}

func (_ CacheTests) GCsTheOldestItems() {
	cache := New(Configure().ItemsToPrune(10))
	defer cache.Stop()
	for i := 0; i < 500; i++ {
		cache.Set(strconv.Itoa(i), i, time.Minute)
	}
	cache.SyncUpdates()
	gcCache(cache)
	Expect(cache.Get("9")).To.Equal(nil)
	Expect(cache.Get("10").Value()).To.Equal(10)
	Expect(cache.ItemCount()).To.Equal(490)
}

func (_ CacheTests) PromotedItemsDontGetPruned() {
	cache := New(Configure().ItemsToPrune(10).GetsPerPromote(1))
	defer cache.Stop()
	for i := 0; i < 500; i++ {
		cache.Set(strconv.Itoa(i), i, time.Minute)
	}
	cache.SyncUpdates()
	cache.Get("9")
	cache.SyncUpdates()
	gcCache(cache)
	Expect(cache.Get("9").Value()).To.Equal(9)
	Expect(cache.Get("10")).To.Equal(nil)
	Expect(cache.Get("11").Value()).To.Equal(11)
}

func (_ CacheTests) TrackerDoesNotCleanupHeldInstance() {
	cache := New(Configure().ItemsToPrune(11).Track())
	item0 := cache.TrackingSet("0", 0, time.Minute)
	for i := 1; i < 11; i++ {
		cache.Set(strconv.Itoa(i), i, time.Minute)
	}
	item1 := cache.TrackingGet("1")
	cache.SyncUpdates()
	gcCache(cache)<commit_msg>revert changes not relevant to the SyncUpdates branch<commit_after>package ccache

import (
	"sort"
	"strconv"
	"sync/atomic"
	"testing"
	"time"

	. "github.com/karlseguin/expect"
)

type CacheTests struct{}

func Test_Cache(t *testing.T) {
	Expectify(new(CacheTests), t)
}

func (_ CacheTests) DeletesAValue() {
	cache := New(Configure())
	Expect(cache.ItemCount()).To.Equal(0)

	cache.Set("spice", "flow", time.Minute)
	cache.Set("worm", "sand", time.Minute)
	Expect(cache.ItemCount()).To.Equal(2)

	cache.Delete("spice")
	Expect(cache.Get("spice")).To.Equal(nil)
	Expect(cache.Get("worm").Value()).To.Equal("sand")
	Expect(cache.ItemCount()).To.Equal(1)
}

func (_ CacheTests) DeletesAPrefix() {
	cache := New(Configure())
	Expect(cache.ItemCount()).To.Equal(0)

	cache.Set("aaa", "1", time.Minute)
	cache.Set("aab", "2", time.Minute)
	cache.Set("aac", "3", time.Minute)
	cache.Set("ac", "4", time.Minute)
	cache.Set("z5", "7", time.Minute)
	Expect(cache.ItemCount()).To.Equal(5)

	Expect(cache.DeletePrefix("9a")).To.Equal(0)
	Expect(cache.ItemCount()).To.Equal(5)

	Expect(cache.DeletePrefix("aa")).To.Equal(3)
	Expect(cache.Get("aaa")).To.Equal(nil)
	Expect(cache.Get("aab")).To.Equal(nil)
	Expect(cache.Get("aac")).To.Equal(nil)
	Expect(cache.Get("ac").Value()).To.Equal("4")
	Expect(cache.Get("z5").Value()).To.Equal("7")
	Expect(cache.ItemCount()).To.Equal(2)
}

func (_ CacheTests) DeletesAFunc() {
	cache := New(Configure())
	Expect(cache.ItemCount()).To.Equal(0)

	cache.Set("a", 1, time.Minute)
	cache.Set("b", 2, time.Minute)
	cache.Set("c", 3, time.Minute)
	cache.Set("d", 4, time.Minute)
	cache.Set("e", 5, time.Minute)
	cache.Set("f", 6, time.Minute)
	Expect(cache.ItemCount()).To.Equal(6)

	Expect(cache.DeleteFunc(func(key string, item *Item) bool {
		return false
	})).To.Equal(0)
	Expect(cache.ItemCount()).To.Equal(6)

	Expect(cache.DeleteFunc(func(key string, item *Item) bool {
		return item.Value().(int) < 4
	})).To.Equal(3)
	Expect(cache.ItemCount()).To.Equal(3)

	Expect(cache.DeleteFunc(func(key string, item *Item) bool {
		return key == "d"
	})).To.Equal(1)
	Expect(cache.ItemCount()).To.Equal(2)

}

func (_ CacheTests) OnDeleteCallbackCalled() {
	onDeleteFnCalled := int32(0)
	onDeleteFn := func(item *Item) {
		if item.key == "spice" {
			atomic.AddInt32(&onDeleteFnCalled, 1)
		}
	}

	cache := New(Configure().OnDelete(onDeleteFn))
	cache.Set("spice", "flow", time.Minute)
	cache.Set("worm", "sand", time.Minute)

	cache.SyncUpdates() // wait for worker to pick up preceding updates

	cache.Delete("spice")
	cache.SyncUpdates()

	Expect(cache.Get("spice")).To.Equal(nil)
	Expect(cache.Get("worm").Value()).To.Equal("sand")
	Expect(atomic.LoadInt32(&onDeleteFnCalled)).To.Eql(1)
}

func (_ CacheTests) FetchesExpiredItems() {
	cache := New(Configure())
	fn := func() (interface{}, error) { return "moo-moo", nil }

	cache.Set("beef", "moo", time.Second*-1)
	Expect(cache.Get("beef").Value()).To.Equal("moo")

	out, _ := cache.Fetch("beef", time.Second, fn)
	Expect(out.Value()).To.Equal("moo-moo")
}

func (_ CacheTests) GCsTheOldestItems() {
	cache := New(Configure().ItemsToPrune(10))
	for i := 0; i < 500; i++ {
		cache.Set(strconv.Itoa(i), i, time.Minute)
	}
	cache.SyncUpdates()
	gcCache(cache)
	Expect(cache.Get("9")).To.Equal(nil)
	Expect(cache.Get("10").Value()).To.Equal(10)
	Expect(cache.ItemCount()).To.Equal(490)
}

func (_ CacheTests) PromotedItemsDontGetPruned() {
	cache := New(Configure().ItemsToPrune(10).GetsPerPromote(1))
	for i := 0; i < 500; i++ {
		cache.Set(strconv.Itoa(i), i, time.Minute)
	}
	cache.SyncUpdates()
	cache.Get("9")
	cache.SyncUpdates()
	gcCache(cache)
	Expect(cache.Get("9").Value()).To.Equal(9)
	Expect(cache.Get("10")).To.Equal(nil)
	Expect(cache.Get("11").Value()).To.Equal(11)
}

func (_ CacheTests) TrackerDoesNotCleanupHeldInstance() {
	cache := New(Configure().ItemsToPrune(11).Track())
	item0 := cache.TrackingSet("0", 0, time.Minute)
	for i := 1; i < 11; i++ {
		cache.Set(strconv.Itoa(i), i, time.Minute)
	}
	item1 := cache.TrackingGet("1")
	cache.SyncUpdates()
	gcCache(cache)
{'fraction_non_alphanumeric': 0.21545012165450123, 'fraction_numerical': 0.020437956204379562, 'mean_word_length': 5.304447852760736, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '173072', 'n_tokens_mistral': 3419, 'n_tokens_neox': 3279, 'n_words': 494}