package usecase import ( "context" "errors" "testing" "testing/synctest" "time" ) func TestOperationScopeAttribution(t *testing.T) { synctest.Test(t, func(t *testing.T) { parent := context.Background() disabled := NewOperationScope(parent, 0) defer disabled.Close() if disabled.Context != parent || disabled.Finalize(context.DeadlineExceeded) != context.DeadlineExceeded { t.Fatal("disabled scope changed context or error") } outer := NewOperationScope(parent, time.Second) defer outer.Close() inner := NewOperationScope(outer.Context, 2*time.Second) defer inner.Close() time.Sleep(time.Second) synctest.Wait() if !errors.Is(outer.Finalize(context.DeadlineExceeded), ErrOperationTimeout) { t.Fatal("own deadline not attributed") } if errors.Is(inner.Finalize(context.DeadlineExceeded), ErrOperationTimeout) { t.Fatal("inherited scope misattributed") } other := errors.New("other") if outer.Finalize(other) != other || outer.Finalize(nil) != nil { t.Fatal("unrelated failure or completion changed") } canceled, cancel := context.WithCancel(parent) child := NewOperationScope(canceled, time.Second) defer child.Close() cancel() if errors.Is(child.Finalize(context.Canceled), ErrOperationTimeout) { t.Fatal("parent cancellation misattributed") } deadline, stop := context.WithTimeout(parent, time.Second) defer stop() child = NewOperationScope(deadline, 2*time.Second) defer child.Close() time.Sleep(time.Second) synctest.Wait() if errors.Is(child.Finalize(context.DeadlineExceeded), ErrOperationTimeout) { t.Fatal("parent deadline misattributed") } }) }