OLD | NEW |
(Empty) | |
| 1 package db |
| 2 |
| 3 import ( |
| 4 "testing" |
| 5 "time" |
| 6 |
| 7 assert "github.com/stretchr/testify/require" |
| 8 "go.skia.org/infra/go/testutils" |
| 9 ) |
| 10 |
| 11 func testGetBuildsForCommits(t *testing.T, c *BuildCache, b *Build) { |
| 12 for _, commit := range b.Commits { |
| 13 builds, err := c.GetBuildsForCommits([]string{commit}) |
| 14 assert.NoError(t, err) |
| 15 testutils.AssertDeepEqual(t, map[string]map[string]*Build{ |
| 16 commit: map[string]*Build{ |
| 17 b.Builder: b, |
| 18 }, |
| 19 }, builds) |
| 20 } |
| 21 } |
| 22 |
| 23 func TestDBCache(t *testing.T) { |
| 24 db := NewInMemoryDB() |
| 25 defer testutils.AssertCloses(t, db) |
| 26 |
| 27 // Pre-load a build into the DB. |
| 28 startTime := time.Now().Add(-30 * time.Minute) // Arbitrary starting poi
nt. |
| 29 b1 := makeBuild("build1", startTime, []string{"a", "b", "c", "d"}) |
| 30 assert.NoError(t, db.PutBuild(b1)) |
| 31 |
| 32 // Create the cache. Ensure that the existing build is present. |
| 33 c, err := NewBuildCache(db, time.Hour) |
| 34 assert.NoError(t, err) |
| 35 testGetBuildsForCommits(t, c, b1) |
| 36 |
| 37 // Bisect the first build. |
| 38 b2 := makeBuild("build2", startTime.Add(time.Minute), []string{"c", "d"}
) |
| 39 b1.Commits = []string{"a", "b"} |
| 40 assert.NoError(t, db.PutBuilds([]*Build{b2, b1})) |
| 41 assert.NoError(t, c.Update()) |
| 42 |
| 43 // Ensure that b2 (and not b1) shows up for commits "c" and "d". |
| 44 testGetBuildsForCommits(t, c, b1) |
| 45 testGetBuildsForCommits(t, c, b2) |
| 46 |
| 47 // Insert a build on a second bot. |
| 48 b3 := makeBuild("build3", startTime.Add(2*time.Minute), []string{"a", "b
"}) |
| 49 b3.Builder = "Another-Builder" |
| 50 assert.NoError(t, db.PutBuild(b3)) |
| 51 assert.NoError(t, c.Update()) |
| 52 builds, err := c.GetBuildsForCommits([]string{"b"}) |
| 53 assert.NoError(t, err) |
| 54 testutils.AssertDeepEqual(t, map[string]map[string]*Build{ |
| 55 "b": map[string]*Build{ |
| 56 b1.Builder: b1, |
| 57 b3.Builder: b3, |
| 58 }, |
| 59 }, builds) |
| 60 } |
OLD | NEW |