Skip to content

Commit 152cb65

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 ca5014f commit 152cb65

2 files changed

Lines changed: 46 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
@@ -414,12 +414,18 @@ func TestMemfdIdentitySource_ServesThenReleases(t *testing.T) {
414414
require.Equal(t, int(ps), n)
415415
require.Equal(t, data[page*ps:(page+1)*ps], b)
416416
}
417+
// Slice takes the same identity path and returns a copy of the memfd bytes.
418+
sl, err := src.Slice(2*ps, ps)
419+
require.NoError(t, err)
420+
require.Equal(t, data[2*ps:3*ps], sl)
417421
require.True(t, src.IsCached(t.Context(), 0, ps*4))
418422

419423
require.NoError(t, d.releaseMemfd())
420424

421-
_, err := src.ReadAt(make([]byte, ps), 0)
422425
var bna BytesNotAvailableError
426+
_, err = src.ReadAt(make([]byte, ps), 0)
427+
require.ErrorAs(t, err, &bna)
428+
_, err = src.Slice(0, ps)
423429
require.ErrorAs(t, err, &bna)
424430
require.False(t, src.IsCached(t.Context(), 0, ps))
425431
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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) when it can't apply,
25+
// so the caller keeps using the deduped header: a non-memfd-dedup source, and a
26+
// 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 := buildProvisionalMemfile(t.Context(), fakeDiffSource{}, true, nil, nil, nil)
32+
require.Nil(t, h)
33+
require.Nil(t, d)
34+
35+
// Disabled flag → no provisional serving.
36+
h, d = buildProvisionalMemfile(t.Context(), fakeDiffSource{}, false, nil, nil, nil)
37+
require.Nil(t, h)
38+
require.Nil(t, d)
39+
}

0 commit comments

Comments
 (0)