Skip to content

Commit

Permalink
Revert "update(Context): add Cause-variant methods"
Browse files Browse the repository at this point in the history
This reverts commit 8c41aac.
  • Loading branch information
b97tsk committed Jun 29, 2024
1 parent a07b7c3 commit b6fc865
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 72 deletions.
45 changes: 0 additions & 45 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ import (
// After the first call, subsequent calls to a CancelFunc do nothing.
type CancelFunc = context.CancelFunc

// A CancelCauseFunc behaves like a [CancelFunc] but additionally sets
// the cancellation cause.
// This cause can be retrieved by calling [Context.Cause] on the canceled
// [Context] or on any of its derived Contexts.
type CancelCauseFunc = context.CancelCauseFunc

// A Context carries a [context.Context], an optional [sync.WaitGroup], and
// an optional panic handler.
type Context struct {
Expand Down Expand Up @@ -82,16 +76,6 @@ func (c Context) AfterFunc(f func()) (stop func() bool) {
}
}

// Cause returns a non-nil error explaining why c was canceled.
// The first cancellation of c or one of its parents sets the cause.
// If that cancellation happened via a call to CancelCauseFunc(err),
// then Cause returns err.
// Otherwise c.Cause() returns the same value as c.Err().
// Cause returns nil if c has not been canceled yet.
func (c Context) Cause() error {
return context.Cause(c.Context)
}

// WithCancel returns a copy of c with a new Done channel.
// The returned context's Done channel is closed when the returned cancel
// function is called or when c's Done channel is closed, whichever happens
Expand All @@ -106,19 +90,6 @@ func (c Context) WithCancel() (Context, CancelFunc) {
return c, cancel
}

// WithCancelCause behaves like [Context.WithCancel] but returns
// a [CancelCauseFunc] instead of a [CancelFunc].
// Calling the returned [CancelCauseFunc] with a non-nil error (the "cause")
// records that error in the returned [Context]; it can then be retrieved
// using [Context.Cause].
// Calling the returned [CancelCauseFunc] with nil sets the cause to
// [context.Canceled].
func (c Context) WithCancelCause() (Context, CancelCauseFunc) {
ctx, cancel := context.WithCancelCause(c.Context)
c.Context = ctx
return c, cancel
}

// WithDeadline returns a copy of c with the deadline adjusted to be no later
// than d. If c's deadline is already earlier than d, c.WithDeadline(d) is
// semantically equivalent to c. The returned context's Done channel is closed
Expand All @@ -134,15 +105,6 @@ func (c Context) WithDeadline(d time.Time) (Context, CancelFunc) {
return c, cancel
}

// WithDeadlineCause behaves like [Context.WithDeadline] but also sets
// the cause of the returned [Context] when the deadline is exceeded.
// The returned [CancelFunc] does not set the cause.
func (c Context) WithDeadlineCause(d time.Time, cause error) (Context, CancelFunc) {
ctx, cancel := context.WithDeadlineCause(c.Context, d, cause)
c.Context = ctx
return c, cancel
}

// WithTimeout returns c.WithDeadline(time.Now().Add(timeout)).
//
// Canceling this context releases resources associated with it, so code
Expand All @@ -152,13 +114,6 @@ func (c Context) WithTimeout(timeout time.Duration) (Context, CancelFunc) {
return c.WithDeadline(time.Now().Add(timeout))
}

// WithTimeoutCause behaves like [Context.WithTimeout] but also sets the cause
// of the returned [Context] when the timeout expires.
// The returned [CancelFunc] does not set the cause.
func (c Context) WithTimeoutCause(timeout time.Duration, cause error) (Context, CancelFunc) {
return c.WithDeadlineCause(time.Now().Add(timeout), cause)
}

// WithNewWaitGroup returns a copy of c with WaitGroup field set to
// a new [sync.WaitGroup].
func (c Context) WithNewWaitGroup() Context {
Expand Down
27 changes: 0 additions & 27 deletions coverage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package rx_test

import (
"testing"
"time"

"github.com/b97tsk/rx"
. "github.com/b97tsk/rx/internal/rxtest"
Expand All @@ -24,32 +23,6 @@ func TestAdditionalCoverage(t *testing.T) {
_ = rx.Compose9(op, op, op, op, op, op, op, op, op).Apply(ob)
})

t.Run("Context.WithCancelCause", func(t *testing.T) {
ctx, cancel := rx.NewBackgroundContext().WithCancelCause()
cancel(ErrTest)
if ctx.Cause() != ErrTest {
t.Fail()
}
})

t.Run("Context.WithDeadlineCause", func(t *testing.T) {
ctx, cancel := rx.NewBackgroundContext().WithDeadlineCause(time.Now().Add(Step(1)), ErrTest)
defer cancel()
time.Sleep(Step(2))
if ctx.Cause() != ErrTest {
t.Fail()
}
})

t.Run("Context.WithTimeoutCause", func(t *testing.T) {
ctx, cancel := rx.NewBackgroundContext().WithTimeoutCause(Step(1), ErrTest)
defer cancel()
time.Sleep(Step(2))
if ctx.Cause() != ErrTest {
t.Fail()
}
})

t.Run("Go", func(t *testing.T) {
v, err := rx.Pipe1(rx.Just(42), rx.Go[int]()).BlockingSingle(rx.NewBackgroundContext())
if err != nil {
Expand Down

0 comments on commit b6fc865

Please sign in to comment.