aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/pkg/pkg_test.go
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2026-04-17 18:56:43 +0900
committerOphestra <cat@gensokyo.uk>2026-04-17 19:33:21 +0900
commit3942272c3039b16e4731855641887f2442a2dc79 (patch)
treefed36b66fab2bd3883cecb14a1851bad81e0ce4e /internal/pkg/pkg_test.go
parent90369861568d38737001f1d7e1304e6914675359 (diff)
internal/pkg: fine-grained cancellation
This enables a specific artifact to be targeted for cancellation. Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'internal/pkg/pkg_test.go')
-rw-r--r--internal/pkg/pkg_test.go52
1 files changed, 45 insertions, 7 deletions
diff --git a/internal/pkg/pkg_test.go b/internal/pkg/pkg_test.go
index 387f7b31..dbaa0801 100644
--- a/internal/pkg/pkg_test.go
+++ b/internal/pkg/pkg_test.go
@@ -40,6 +40,23 @@ func unsafeOpen(
lock bool,
) (*pkg.Cache, error)
+// newRContext returns the address of a new [pkg.RContext] unsafely created for
+// the specified [testing.TB].
+func newRContext(tb testing.TB, c *pkg.Cache) *pkg.RContext {
+ var r pkg.RContext
+ rContextVal := reflect.ValueOf(&r).Elem().FieldByName("ctx")
+ reflect.NewAt(
+ rContextVal.Type(),
+ unsafe.Pointer(rContextVal.UnsafeAddr()),
+ ).Elem().Set(reflect.ValueOf(tb.Context()))
+ rCacheVal := reflect.ValueOf(&r).Elem().FieldByName("cache")
+ reflect.NewAt(
+ rCacheVal.Type(),
+ unsafe.Pointer(rCacheVal.UnsafeAddr()),
+ ).Elem().Set(reflect.ValueOf(c))
+ return &r
+}
+
func TestMain(m *testing.M) { container.TryArgv0(nil); os.Exit(m.Run()) }
// overrideIdent overrides the ID method of [Artifact].
@@ -876,17 +893,38 @@ func TestCache(t *testing.T) {
t.Fatalf("Scrub: error = %#v, want %#v", err, wantErrScrub)
}
- identPendingVal := reflect.ValueOf(c).Elem().FieldByName("identPending")
- identPending := reflect.NewAt(
- identPendingVal.Type(),
- unsafe.Pointer(identPendingVal.UnsafeAddr()),
- ).Elem().Interface().(map[unique.Handle[pkg.ID]]<-chan struct{})
- notify := identPending[unique.Make(pkg.ID{0xff})]
+ notify := c.Done(unique.Make(pkg.ID{0xff}))
go close(n)
- <-notify
+ if notify != nil {
+ <-notify
+ }
+ for c.Done(unique.Make(pkg.ID{0xff})) != nil {
+ }
<-wCureDone
}, pkg.MustDecode("E4vEZKhCcL2gPZ2Tt59FS3lDng-d_2SKa2i5G_RbDfwGn6EemptFaGLPUDiOa94C")},
+ {"cancel hanging", pkg.CValidateKnown, nil, func(t *testing.T, base *check.Absolute, c *pkg.Cache) {
+ started := make(chan struct{})
+ go func() {
+ <-started
+ if !c.Cancel(unique.Make(pkg.ID{0xff})) {
+ panic("missed cancellation")
+ }
+ }()
+ if _, _, err := c.Cure(overrideIdent{pkg.ID{0xff}, &stubArtifact{
+ kind: pkg.KindTar,
+ cure: func(t *pkg.TContext) error {
+ close(started)
+ <-t.Unwrap().Done()
+ return stub.UniqueError(0xbad)
+ },
+ }}); !reflect.DeepEqual(err, stub.UniqueError(0xbad)) {
+ t.Fatalf("Cure: error = %v", err)
+ }
+ for c.Cancel(unique.Make(pkg.ID{0xff})) {
+ }
+ }, pkg.MustDecode("E4vEZKhCcL2gPZ2Tt59FS3lDng-d_2SKa2i5G_RbDfwGn6EemptFaGLPUDiOa94C")},
+
{"no assume checksum", 0, nil, func(t *testing.T, base *check.Absolute, c *pkg.Cache) {
makeGarbage := func(work *check.Absolute, wantErr error) error {
if err := os.Mkdir(work.String(), 0700); err != nil {