forked from rosa/hakurei
internal/pkg: fine-grained cancellation
This enables a specific artifact to be targeted for cancellation. Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user