solver: add test for minimal cache exporter mode

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
docker-18.09
Tonis Tiigi 2018-05-07 17:11:42 -07:00
parent 5ef868cce3
commit 500f2a357d
2 changed files with 130 additions and 4 deletions

View File

@ -83,6 +83,10 @@ func (e *exporter) ExportTo(ctx context.Context, t CacheExporterTarget, opt Cach
addRecord = *e.override addRecord = *e.override
} }
if e.record == nil && len(e.k.Deps()) > 0 {
e.record = getBestResult(e.records)
}
var remote *Remote var remote *Remote
if v := e.record; v != nil && len(e.k.Deps()) > 0 && addRecord { if v := e.record; v != nil && len(e.k.Deps()) > 0 && addRecord {
cm := v.cacheManager cm := v.cacheManager
@ -158,10 +162,6 @@ func (e *exporter) ExportTo(ctx context.Context, t CacheExporterTarget, opt Cach
} }
} }
if e.record == nil && len(e.k.Deps()) > 0 {
e.record = getBestResult(e.records)
}
if v := e.record; v != nil && len(deps) == 0 { if v := e.record; v != nil && len(deps) == 0 {
cm := v.cacheManager cm := v.cacheManager
key := cm.getID(v.key) key := cm.getID(v.key)

View File

@ -2176,6 +2176,132 @@ func TestCacheExporting(t *testing.T) {
require.Equal(t, expTarget.records[2].links, 0) require.Equal(t, expTarget.records[2].links, 0)
} }
func TestCacheExportingModeMin(t *testing.T) {
t.Parallel()
ctx := context.TODO()
cacheManager := newTrackingCacheManager(NewInMemoryCacheManager())
l := NewJobList(SolverOpt{
ResolveOpFunc: testOpResolver,
DefaultCache: cacheManager,
})
defer l.Close()
j0, err := l.NewJob("j0")
require.NoError(t, err)
defer func() {
if j0 != nil {
j0.Discard()
}
}()
g0 := Edge{
Vertex: vtxSum(1, vtxOpt{
inputs: []Edge{
{Vertex: vtxSum(2, vtxOpt{
inputs: []Edge{
{Vertex: vtxConst(3, vtxOpt{})},
},
})},
{Vertex: vtxConst(5, vtxOpt{})},
},
}),
}
res, err := j0.Build(ctx, g0)
require.NoError(t, err)
require.Equal(t, unwrapInt(res), 11)
require.NoError(t, j0.Discard())
j0 = nil
expTarget := newTestExporterTarget()
_, err = res.CacheKey().Exporter.ExportTo(ctx, expTarget, testExporterOpts(false))
require.NoError(t, err)
expTarget.normalize()
require.Equal(t, len(expTarget.records), 4)
require.Equal(t, expTarget.records[0].results, 1)
require.Equal(t, expTarget.records[1].results, 0)
require.Equal(t, expTarget.records[2].results, 0)
require.Equal(t, expTarget.records[3].results, 0)
require.Equal(t, expTarget.records[0].links, 2)
require.Equal(t, expTarget.records[1].links, 1)
require.Equal(t, expTarget.records[2].links, 0)
require.Equal(t, expTarget.records[3].links, 0)
j1, err := l.NewJob("j1")
require.NoError(t, err)
defer func() {
if j1 != nil {
j1.Discard()
}
}()
res, err = j1.Build(ctx, g0)
require.NoError(t, err)
require.Equal(t, unwrapInt(res), 11)
require.NoError(t, j1.Discard())
j1 = nil
expTarget = newTestExporterTarget()
_, err = res.CacheKey().Exporter.ExportTo(ctx, expTarget, testExporterOpts(false))
require.NoError(t, err)
expTarget.normalize()
// the order of the records isn't really significant
require.Equal(t, len(expTarget.records), 4)
require.Equal(t, expTarget.records[0].results, 1)
require.Equal(t, expTarget.records[1].results, 0)
require.Equal(t, expTarget.records[2].results, 0)
require.Equal(t, expTarget.records[3].results, 0)
require.Equal(t, expTarget.records[0].links, 2)
require.Equal(t, expTarget.records[1].links, 1)
require.Equal(t, expTarget.records[2].links, 0)
require.Equal(t, expTarget.records[3].links, 0)
// one more check with all mode
j2, err := l.NewJob("j2")
require.NoError(t, err)
defer func() {
if j2 != nil {
j2.Discard()
}
}()
res, err = j2.Build(ctx, g0)
require.NoError(t, err)
require.Equal(t, unwrapInt(res), 11)
require.NoError(t, j2.Discard())
j2 = nil
expTarget = newTestExporterTarget()
_, err = res.CacheKey().Exporter.ExportTo(ctx, expTarget, testExporterOpts(true))
require.NoError(t, err)
expTarget.normalize()
// the order of the records isn't really significant
require.Equal(t, len(expTarget.records), 4)
require.Equal(t, expTarget.records[0].results, 1)
require.Equal(t, expTarget.records[1].results, 1)
require.Equal(t, expTarget.records[2].results, 0)
require.Equal(t, expTarget.records[3].results, 0)
require.Equal(t, expTarget.records[0].links, 2)
require.Equal(t, expTarget.records[1].links, 1)
require.Equal(t, expTarget.records[2].links, 0)
require.Equal(t, expTarget.records[3].links, 0)
}
func TestSlowCacheAvoidAccess(t *testing.T) { func TestSlowCacheAvoidAccess(t *testing.T) {
t.Parallel() t.Parallel()
ctx := context.TODO() ctx := context.TODO()