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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
package gmeasure_test
import (
"fmt"
"os"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gmeasure"
)
var _ = Describe("Cache", func() {
var path string
var cache gmeasure.ExperimentCache
var e1, e2 *gmeasure.Experiment
BeforeEach(func() {
var err error
path = fmt.Sprintf("./cache-%d", GinkgoParallelProcess())
cache, err = gmeasure.NewExperimentCache(path)
Ω(err).ShouldNot(HaveOccurred())
e1 = gmeasure.NewExperiment("Experiment-1")
e1.RecordValue("foo", 32)
e2 = gmeasure.NewExperiment("Experiment-2")
e2.RecordValue("bar", 64)
})
AfterEach(func() {
Ω(os.RemoveAll(path)).Should(Succeed())
})
Describe("when creating a cache that points to a file", func() {
It("errors", func() {
f, err := os.Create("cache-temp-file")
Ω(err).ShouldNot(HaveOccurred())
f.Close()
cache, err := gmeasure.NewExperimentCache("cache-temp-file")
Ω(err).Should(MatchError("cache-temp-file is not a directory"))
Ω(cache).Should(BeZero())
Ω(os.RemoveAll("cache-temp-file")).Should(Succeed())
})
})
Describe("the happy path", func() {
It("can save, load, list, delete, and clear the cache", func() {
Ω(cache.Save("e1", 1, e1)).Should(Succeed())
Ω(cache.Save("e2", 7, e2)).Should(Succeed())
Ω(cache.Load("e1", 1)).Should(Equal(e1))
Ω(cache.Load("e2", 7)).Should(Equal(e2))
Ω(cache.List()).Should(ConsistOf(
gmeasure.CachedExperimentHeader{"e1", 1},
gmeasure.CachedExperimentHeader{"e2", 7},
))
Ω(cache.Delete("e2")).Should(Succeed())
Ω(cache.Load("e1", 1)).Should(Equal(e1))
Ω(cache.Load("e2", 7)).Should(BeNil())
Ω(cache.List()).Should(ConsistOf(
gmeasure.CachedExperimentHeader{"e1", 1},
))
Ω(cache.Clear()).Should(Succeed())
Ω(cache.List()).Should(BeEmpty())
Ω(cache.Load("e1", 1)).Should(BeNil())
Ω(cache.Load("e2", 7)).Should(BeNil())
})
})
Context("with an empty cache", func() {
It("should list nothing", func() {
Ω(cache.List()).Should(BeEmpty())
})
It("should not error when clearing", func() {
Ω(cache.Clear()).Should(Succeed())
})
It("returs nil when loading a non-existing experiment", func() {
Ω(cache.Load("floop", 17)).Should(BeNil())
})
})
Describe("version management", func() {
BeforeEach(func() {
Ω(cache.Save("e1", 7, e1)).Should(Succeed())
})
Context("when the cached version is older than the requested version", func() {
It("returns nil", func() {
Ω(cache.Load("e1", 8)).Should(BeNil())
})
})
Context("when the cached version equals the requested version", func() {
It("returns the cached version", func() {
Ω(cache.Load("e1", 7)).Should(Equal(e1))
})
})
Context("when the cached version is newer than the requested version", func() {
It("returns the cached version", func() {
Ω(cache.Load("e1", 6)).Should(Equal(e1))
})
})
})
})
|