Skip to content

Commit 12b5446

Browse files
kalyazinclaude
andcommitted
test(orch): cover MemfdIdentitySource.Slice and provisional fallback
Extend the MemfdIdentitySource test to exercise Slice (identity read while mapped, and BytesNotAvailableError after release), and add a test that buildProvisionalMemfile declines (returns nil, nil) for a non-memfd-dedup source or when disabled, so the caller falls back to the deduped header. Signed-off-by: Nikita Kalyazin <nikita.kalyazin@e2b.dev> Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent ef0a559 commit 12b5446

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

packages/orchestrator/pkg/sandbox/block/memfd_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,12 +464,18 @@ func TestMemfdIdentitySource_ServesThenReleases(t *testing.T) {
464464
require.Equal(t, int(ps), n)
465465
require.Equal(t, data[page*ps:(page+1)*ps], b)
466466
}
467+
// Slice takes the same identity path and returns a copy of the memfd bytes.
468+
sl, err := src.Slice(2*ps, ps)
469+
require.NoError(t, err)
470+
require.Equal(t, data[2*ps:3*ps], sl)
467471
require.True(t, src.IsCached(t.Context(), 0, ps*4))
468472

469473
require.NoError(t, d.releaseMemfd())
470474

471-
_, err := src.ReadAt(make([]byte, ps), 0)
472475
var bna BytesNotAvailableError
476+
_, err = src.ReadAt(make([]byte, ps), 0)
477+
require.ErrorAs(t, err, &bna)
478+
_, err = src.Slice(0, ps)
473479
require.ErrorAs(t, err, &bna)
474480
require.False(t, src.IsCached(t.Context(), 0, ps))
475481
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//go:build linux
2+
3+
package sandbox
4+
5+
import (
6+
"context"
7+
"testing"
8+
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
// fakeDiffSource is a block.DiffSource that is not a *block.DedupedMemfdCache,
13+
// so buildProvisionalMemfile must decline to produce a provisional header.
14+
type fakeDiffSource struct{}
15+
16+
func (fakeDiffSource) Close() error { return nil }
17+
func (fakeDiffSource) ReadAt([]byte, int64) (int, error) { return 0, nil }
18+
func (fakeDiffSource) Slice(int64, int64) ([]byte, error) { return nil, nil }
19+
func (fakeDiffSource) Size() (int64, error) { return 0, nil }
20+
func (fakeDiffSource) FileSize(context.Context) (int64, error) { return 0, nil }
21+
func (fakeDiffSource) BlockSize() int64 { return 0 }
22+
func (fakeDiffSource) Path(context.Context) (string, error) { return "", nil }
23+
24+
// buildProvisionalMemfile must fall back (return nil, nil, nil) when it can't
25+
// apply, so the caller keeps using the deduped header: a non-memfd-dedup source,
26+
// and a disabled flag.
27+
func TestBuildProvisionalMemfile_FallsBack(t *testing.T) {
28+
t.Parallel()
29+
30+
// Source is not a *block.DedupedMemfdCache → no provisional serving.
31+
h, d, swapDone := buildProvisionalMemfile(t.Context(), fakeDiffSource{}, true, nil, nil, nil)
32+
require.Nil(t, h)
33+
require.Nil(t, d)
34+
require.Nil(t, swapDone)
35+
36+
// Disabled flag → no provisional serving.
37+
h, d, swapDone = buildProvisionalMemfile(t.Context(), fakeDiffSource{}, false, nil, nil, nil)
38+
require.Nil(t, h)
39+
require.Nil(t, d)
40+
require.Nil(t, swapDone)
41+
}

0 commit comments

Comments
 (0)