Commit Graph
5 Commits
Author SHA1 Message Date
Sebastiaan van Stijn bcfba26ca8 internal/cleanup: remove Background(), add tests for Do()
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-05-07 13:06:57 +02:00
Sebastiaan van Stijn ada7bdf19f replace "cleanup.Background" for "context.WithoutCancel"
the `cleanup.Background` utility was introduced in f606c4eba7,
at which time the project used go1.19, and Go's stdlib context did not yet
have [`context.WithoutCancel`], which was introduced in go1.21.

This patch replaces `cleanup.Background` for `context.WithoutCancel`, which
is near-identical, and part of go stdlib;

`cleanup.Background`:

    type clearCancel struct {
        context.Context
    }

    func (cc clearCancel) Deadline() (deadline time.Time, ok bool) {
        return
    }

    func (cc clearCancel) Done() <-chan struct{} {
        return nil
    }

    func (cc clearCancel) Err() error {
        return nil
    }

    // Background creates a new context which clears out the parent errors
    func Background(ctx context.Context) context.Context {
        return clearCancel{ctx}
    }

`context.WithoutCancel`:

    // WithoutCancel returns a derived context that points to the parent context
    // and is not canceled when parent is canceled.
    // The returned context returns no Deadline or Err, and its Done channel is nil.
    // Calling [Cause] on the returned context returns nil.
    func WithoutCancel(parent Context) Context {
        if parent == nil {
            panic("cannot create context from nil parent")
        }
        return withoutCancelCtx{parent}
    }

    type withoutCancelCtx struct {
        c Context
    }

    func (withoutCancelCtx) Deadline() (deadline time.Time, ok bool) {
        return
    }

    func (withoutCancelCtx) Done() <-chan struct{} {
        return nil
    }

    func (withoutCancelCtx) Err() error {
        return nil
    }

    func (c withoutCancelCtx) Value(key any) any {
        return value(c, key)
    }

    func (c withoutCancelCtx) String() string {
        return contextName(c.c) + ".WithoutCancel"
    }

[`context.WithoutCancel`]: https://pkg.go.dev/context@go1.21.0#WithoutCancel

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-05-07 12:34:59 +02:00
Sebastiaan van Stijn c4435bb48e internal/cleanup: fix package godoc
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-05-07 12:33:31 +02:00
Akhil Mohan 1027b314a6 ignore the static check when using anonymous struct in testing
Signed-off-by: Akhil Mohan <akhilerm@gmail.com>
2024-08-14 14:50:39 +05:30
Derek McGowan c79ecfe246 Move pkg/cleanup to internal/cleanup
Signed-off-by: Derek McGowan <derek@mcg.dev>
2024-01-17 09:56:34 -08:00