Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 96e915c

Browse files
committed
include cancellation reason when cancelling spec context
1 parent 4fcd0b3 commit 96e915c

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

internal/internal_integration/interrupt_and_timeout_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ var _ = Describe("Interrupts and Timeouts", func() {
195195
interruptHandler.Interrupt(interrupt_handler.InterruptCauseSignal)
196196
select {
197197
case <-c.Done():
198+
Ω(context.Cause(c)).Should(MatchError(interrupt_handler.InterruptCauseSignal.String()))
198199
F("bam")
199200
case <-time.After(time.Hour):
200201
}
@@ -236,6 +237,7 @@ var _ = Describe("Interrupts and Timeouts", func() {
236237
interruptHandler.Interrupt(interrupt_handler.InterruptCauseSignal)
237238
select {
238239
case <-c.Done():
240+
Ω(context.Cause(c)).Should(MatchError(interrupt_handler.InterruptCauseSignal.String()))
239241
time.Sleep(time.Hour)
240242
case <-time.After(time.Hour):
241243
}
@@ -275,6 +277,7 @@ var _ = Describe("Interrupts and Timeouts", func() {
275277
interruptHandler.Interrupt(interrupt_handler.InterruptCauseSignal)
276278
select {
277279
case <-c.Done():
280+
Ω(context.Cause(c)).Should(MatchError(interrupt_handler.InterruptCauseSignal.String()))
278281
time.Sleep(time.Millisecond * 100)
279282
interruptHandler.Interrupt(interrupt_handler.InterruptCauseSignal)
280283
time.Sleep(time.Hour)
@@ -325,12 +328,14 @@ var _ = Describe("Interrupts and Timeouts", func() {
325328
It("A", rt.TSC("A", func(c SpecContext) {
326329
interruptHandler.Interrupt(interrupt_handler.InterruptCauseSignal)
327330
<-c.Done()
331+
Ω(context.Cause(c)).Should(MatchError(interrupt_handler.InterruptCauseSignal.String()))
328332
}))
329333
It("B", rt.T("B"))
330334
AfterEach(rt.TSC("aft-inner", func(c SpecContext) {
331335
t := time.Now()
332336
select {
333337
case <-c.Done():
338+
Ω(context.Cause(c)).Should(MatchError("grace period timeout occurred"))
334339
times.Set("aft-inner", time.Since(t))
335340
time.Sleep(time.Second)
336341
case <-time.After(time.Second):
@@ -380,11 +385,13 @@ var _ = Describe("Interrupts and Timeouts", func() {
380385
It("A", rt.TSC("A", func(c SpecContext) {
381386
interruptHandler.Interrupt(interrupt_handler.InterruptCauseSignal)
382387
<-c.Done()
388+
Ω(context.Cause(c)).Should(MatchError(interrupt_handler.InterruptCauseSignal.String()))
383389
}))
384390
It("B", rt.T("B"))
385391
AfterEach(rt.TSC("aft-inner", func(c SpecContext) {
386392
select {
387393
case <-c.Done():
394+
Ω(context.Cause(c)).Should(MatchError("node timeout occurred"))
388395
times.Set("aft-inner", time.Since(t))
389396
time.Sleep(time.Second)
390397
case <-time.After(time.Second):
@@ -432,11 +439,13 @@ var _ = Describe("Interrupts and Timeouts", func() {
432439
It("A", rt.TSC("A", func(c SpecContext) {
433440
interruptHandler.Interrupt(interrupt_handler.InterruptCauseSignal)
434441
<-c.Done()
442+
Ω(context.Cause(c)).Should(MatchError(interrupt_handler.InterruptCauseSignal.String()))
435443
}))
436444
It("B", rt.T("B"))
437445
AfterEach(rt.TSC("aft-inner", func(c SpecContext) {
438446
interruptHandler.Interrupt(interrupt_handler.InterruptCauseSignal)
439447
<-c.Done()
448+
Ω(context.Cause(c)).Should(MatchError(interrupt_handler.InterruptCauseSignal.String()))
440449
}))
441450
})
442451
AfterEach(rt.T("aft-outer"))
@@ -479,11 +488,13 @@ var _ = Describe("Interrupts and Timeouts", func() {
479488
It("A", rt.TSC("A", func(c SpecContext) {
480489
interruptHandler.Interrupt(interrupt_handler.InterruptCauseSignal)
481490
<-c.Done()
491+
Ω(context.Cause(c)).Should(MatchError(interrupt_handler.InterruptCauseSignal.String()))
482492
}))
483493
It("B", rt.T("B"))
484494
AfterEach(rt.TSC("aft-inner", func(c SpecContext) {
485495
interruptHandler.Interrupt(interrupt_handler.InterruptCauseSignal)
486496
<-c.Done()
497+
Ω(context.Cause(c)).Should(MatchError(interrupt_handler.InterruptCauseSignal.String()))
487498
}))
488499
})
489500
AfterEach(rt.T("aft-outer"))
@@ -536,6 +547,7 @@ var _ = Describe("Interrupts and Timeouts", func() {
536547
BeforeEach(rt.TSC("bef-inner", func(c SpecContext) {
537548
interruptHandler.Interrupt(interrupt_handler.InterruptCauseSignal)
538549
<-c.Done()
550+
Ω(context.Cause(c)).Should(MatchError(interrupt_handler.InterruptCauseSignal.String()))
539551
}))
540552

541553
Context("even more nested", func() {
@@ -629,6 +641,7 @@ var _ = Describe("Interrupts and Timeouts", func() {
629641
Describe("when it exits in time", func() {
630642
It("A", rt.TSC("A", func(c SpecContext) {
631643
<-c.Done()
644+
Ω(context.Cause(c)).Should(MatchError("node timeout occurred"))
632645
rt.Run("A-cancelled")
633646
Fail("subsequent failure message")
634647
}), NodeTimeout(time.Millisecond*100))
@@ -637,13 +650,15 @@ var _ = Describe("Interrupts and Timeouts", func() {
637650
Describe("with no configured grace period", func() {
638651
It("B", rt.TSC("B", func(c SpecContext) {
639652
<-c.Done()
653+
Ω(context.Cause(c)).Should(MatchError("node timeout occurred"))
640654
time.Sleep(time.Hour)
641655
}), NodeTimeout(time.Millisecond*100))
642656
})
643657

644658
Describe("with a configured grace period", func() {
645659
It("C", rt.TSC("C", func(c SpecContext) {
646660
<-c.Done()
661+
Ω(context.Cause(c)).Should(MatchError("node timeout occurred"))
647662
time.Sleep(time.Hour)
648663
}), NodeTimeout(time.Millisecond*100), GracePeriod(time.Millisecond*50))
649664
})
@@ -714,6 +729,7 @@ var _ = Describe("Interrupts and Timeouts", func() {
714729
}, func(c SpecContext, b []byte) {
715730
rt.Run(string(b))
716731
<-c.Done()
732+
Ω(context.Cause(c)).Should(MatchError("node timeout occurred"))
717733
times.Set(string(b), time.Since(t))
718734
}, NodeTimeout(time.Millisecond*100))
719735

@@ -724,6 +740,7 @@ var _ = Describe("Interrupts and Timeouts", func() {
724740
}, func(c SpecContext) {
725741
rt.Run("afts-proc-1")
726742
<-c.Done()
743+
Ω(context.Cause(c)).Should(MatchError("node timeout occurred"))
727744
times.Set("afts-proc-1", time.Since(t))
728745
}, NodeTimeout(time.Millisecond*200))
729746
})
@@ -763,6 +780,7 @@ var _ = Describe("Interrupts and Timeouts", func() {
763780
AfterEach(rt.TSC("aft-1", func(c SpecContext) {
764781
times.Set("A", time.Since(t))
765782
<-c.Done()
783+
Ω(context.Cause(c)).Should(MatchError("grace period timeout occurred"))
766784
times.Set("aft-1-cancel", time.Since(t))
767785
writer.Println("aft-1")
768786
time.Sleep(time.Hour)
@@ -771,6 +789,7 @@ var _ = Describe("Interrupts and Timeouts", func() {
771789
AfterEach(rt.TSC("aft-2", func(c SpecContext) {
772790
times.Set("aft-1-out", time.Since(t))
773791
<-c.Done()
792+
Ω(context.Cause(c)).Should(MatchError("grace period timeout occurred"))
774793
times.Set("aft-2-cancel", time.Since(t))
775794
writer.Println("aft-2")
776795
time.Sleep(time.Hour)
@@ -779,6 +798,7 @@ var _ = Describe("Interrupts and Timeouts", func() {
779798
AfterEach(rt.TSC("aft-3", func(c SpecContext) {
780799
times.Set("aft-2-out", time.Since(t))
781800
<-c.Done()
801+
Ω(context.Cause(c)).Should(MatchError("node timeout occurred"))
782802
times.Set("aft-3-cancel", time.Since(t))
783803
writer.Println("aft-3")
784804
time.Sleep(time.Hour)
@@ -839,6 +859,7 @@ var _ = Describe("Interrupts and Timeouts", func() {
839859
BeforeEach(rt.TSC("bef-A", func(c SpecContext) {
840860
t := time.Now()
841861
<-c.Done()
862+
Ω(context.Cause(c)).Should(MatchError("node timeout occurred"))
842863
times.Set("bef-A", time.Since(t))
843864
}), NodeTimeout(time.Millisecond*100))
844865

@@ -849,6 +870,7 @@ var _ = Describe("Interrupts and Timeouts", func() {
849870
BeforeEach(rt.TSC("bef-B", func(c SpecContext) {
850871
t := time.Now()
851872
<-c.Done()
873+
Ω(context.Cause(c)).Should(MatchError("spec timeout occurred"))
852874
times.Set("bef-B", time.Since(t))
853875
}), NodeTimeout(time.Millisecond*250))
854876

@@ -859,6 +881,7 @@ var _ = Describe("Interrupts and Timeouts", func() {
859881
BeforeEach(rt.TSC("bef-C", func(c SpecContext) {
860882
t := time.Now()
861883
<-c.Done()
884+
Ω(context.Cause(c)).Should(MatchError("suite timeout occurred"))
862885
times.Set("bef-C", time.Since(t))
863886
}), NodeTimeout(time.Millisecond*300))
864887

@@ -914,6 +937,7 @@ var _ = Describe("Interrupts and Timeouts", func() {
914937
Context("container", func() {
915938
It("A", rt.TSC("A", func(c SpecContext) {
916939
<-c.Done()
940+
Ω(context.Cause(c)).Should(MatchError("suite timeout occurred"))
917941
}))
918942

919943
It("B", rt.T("B"))
@@ -947,13 +971,15 @@ var _ = Describe("Interrupts and Timeouts", func() {
947971
rt.Run(key)
948972
t := time.Now()
949973
<-c.Done()
974+
Ω(context.Cause(c)).Should(MatchError("node timeout occurred"))
950975
times.Set(key, time.Since(t))
951976
}, NodeTimeout(time.Millisecond*100), "dc-1")
952977

953978
DeferCleanup(func(c SpecContext, key string) {
954979
rt.Run(key)
955980
t := time.Now()
956981
<-c.Done()
982+
Ω(context.Cause(c)).Should(MatchError("node timeout occurred"))
957983
times.Set(key, time.Since(t))
958984
}, NodeTimeout(time.Millisecond*100), "dc-2")
959985

@@ -962,6 +988,7 @@ var _ = Describe("Interrupts and Timeouts", func() {
962988
rt.Run(key)
963989
t := time.Now()
964990
<-c.Done()
991+
Ω(context.Cause(c)).Should(MatchError("node timeout occurred"))
965992
times.Set(key, time.Since(t))
966993
}, NodeTimeout(time.Millisecond*100), context.WithValue(context.Background(), "key", "dc"), "-3")
967994

internal/spec_context.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type specContext struct {
1717
context.Context
1818
*ProgressReporterManager
1919

20-
cancel context.CancelFunc
20+
cancel context.CancelCauseFunc
2121

2222
suite *Suite
2323
}
@@ -30,7 +30,7 @@ Note that while SpecContext is used to enforce deadlines by Ginkgo it is not con
3030
This is because Ginkgo needs finer control over when the context is canceled. Specifically, Ginkgo needs to generate a ProgressReport before it cancels the context to ensure progress is captured where the spec is currently running. The only way to avoid a race here is to manually control the cancellation.
3131
*/
3232
func NewSpecContext(suite *Suite) *specContext {
33-
ctx, cancel := context.WithCancel(context.Background())
33+
ctx, cancel := context.WithCancelCause(context.Background())
3434
sc := &specContext{
3535
cancel: cancel,
3636
suite: suite,

internal/suite.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ func (suite *Suite) runNode(node Node, specDeadline time.Time, text string) (typ
858858
}
859859

860860
sc := NewSpecContext(suite)
861-
defer sc.cancel()
861+
defer sc.cancel(fmt.Errorf("spec has finished"))
862862

863863
suite.selectiveLock.Lock()
864864
suite.currentSpecContext = sc
@@ -958,7 +958,7 @@ func (suite *Suite) runNode(node Node, specDeadline time.Time, text string) (typ
958958

959959
// tell the spec to stop. it's important we generate the progress report first to make sure we capture where
960960
// the spec is actually stuck
961-
sc.cancel()
961+
sc.cancel(fmt.Errorf("%s timeout occurred", timeoutInPlay))
962962
//and now we wait for the grace period
963963
gracePeriodChannel = time.After(gracePeriod)
964964
case <-interruptStatus.Channel:
@@ -985,7 +985,7 @@ func (suite *Suite) runNode(node Node, specDeadline time.Time, text string) (typ
985985
}
986986

987987
progressReport = progressReport.WithoutOtherGoroutines()
988-
sc.cancel()
988+
sc.cancel(fmt.Errorf(interruptStatus.Message()))
989989

990990
if interruptStatus.Level == interrupt_handler.InterruptLevelBailOut {
991991
if interruptStatus.ShouldIncludeProgressReport() {

0 commit comments

Comments
 (0)