github.com/onsi/ginkgo@v1.16.6-0.20211118180735-4e1925ba4c95/internal/internal_integration/interrupt_test.go (about)

     1  package internal_integration_test
     2  
     3  import (
     4  	"time"
     5  
     6  	. "github.com/onsi/ginkgo"
     7  	"github.com/onsi/ginkgo/internal/interrupt_handler"
     8  	. "github.com/onsi/ginkgo/internal/test_helpers"
     9  	"github.com/onsi/ginkgo/types"
    10  	. "github.com/onsi/gomega"
    11  )
    12  
    13  var _ = Describe("When a test suite is interrupted", func() {
    14  	Describe("when it is interrupted in a BeforeSuite", func() {
    15  		BeforeEach(func() {
    16  			success, _ := RunFixture("interrupted test", func() {
    17  				BeforeSuite(rt.T("before-suite", func() {
    18  					interruptHandler.Interrupt(interrupt_handler.InterruptCauseTimeout)
    19  					time.Sleep(time.Hour)
    20  				}))
    21  				AfterSuite(rt.T("after-suite"))
    22  				It("A", rt.T("A"))
    23  				It("B", rt.T("B"))
    24  			})
    25  			Ω(success).Should(Equal(false))
    26  		})
    27  
    28  		It("runs the AfterSuite and skips all the tests", func() {
    29  			Ω(rt).Should(HaveTracked("before-suite", "after-suite"))
    30  			Ω(reporter.Did.FindByLeafNodeType(types.NodeTypeIt)).Should(BeZero())
    31  		})
    32  
    33  		It("reports the correct failure", func() {
    34  			summary := reporter.Did.FindByLeafNodeType(types.NodeTypeBeforeSuite)
    35  			Ω(summary.State).Should(Equal(types.SpecStateInterrupted))
    36  			Ω(summary.Failure.Message).Should(ContainSubstring("Interrupted by Timeout\nstack trace"))
    37  		})
    38  
    39  		It("reports the correct statistics", func() {
    40  			Ω(reporter.End).Should(BeASuiteSummary(false, NSpecs(2), NWillRun(2), NPassed(0), NSkipped(0), NFailed(0)))
    41  		})
    42  
    43  		It("reports the correct special failure reason", func() {
    44  			Ω(reporter.End.SpecialSuiteFailureReasons).Should(ContainElement("Interrupted by Timeout"))
    45  		})
    46  	})
    47  
    48  	Describe("when it is interrupted in a test", func() {
    49  		BeforeEach(func() {
    50  			conf.FlakeAttempts = 3
    51  			success, _ := RunFixture("interrupted test", func() {
    52  				BeforeSuite(rt.T("before-suite"))
    53  				AfterSuite(rt.T("after-suite"))
    54  				BeforeEach(rt.T("bef.1"))
    55  				AfterEach(rt.T("aft.1"))
    56  				Describe("container", func() {
    57  					BeforeEach(rt.T("bef.2"))
    58  					AfterEach(rt.T("aft.2"))
    59  					It("runs", rt.T("runs"))
    60  					Describe("nested-container", func() {
    61  						BeforeEach(rt.T("bef.3-interrupt!", func() {
    62  							interruptHandler.Interrupt(interrupt_handler.InterruptCauseTimeout)
    63  							time.Sleep(time.Hour)
    64  						}))
    65  						AfterEach(rt.T("aft.3a"))
    66  						AfterEach(rt.T("aft.3b", func() {
    67  							interruptHandler.Interrupt(interrupt_handler.InterruptCauseTimeout)
    68  							time.Sleep(time.Hour)
    69  						}))
    70  						Describe("deeply-nested-container", func() {
    71  							BeforeEach(rt.T("bef.4"))
    72  							AfterEach(rt.T("aft.4"))
    73  							It("the interrupted test", rt.T("the interrupted test"))
    74  							It("skipped.1", rt.T("skipped.1"))
    75  						})
    76  					})
    77  					It("skipped.2", rt.T("skipped.2"))
    78  				})
    79  			})
    80  			Ω(success).Should(Equal(false))
    81  		})
    82  
    83  		It("unwinds the after eaches at the appropriate nesting level, allowing additional interrupts of after eaches as it goes", func() {
    84  			Ω(rt).Should(HaveTracked("before-suite",
    85  				"bef.1", "bef.2", "runs", "aft.2", "aft.1",
    86  				"bef.1", "bef.2", "bef.3-interrupt!", "aft.3a", "aft.3b", "aft.2", "aft.1",
    87  				"after-suite"))
    88  		})
    89  
    90  		It("skips subsequent tests", func() {
    91  			Ω(reporter.Did.WithState(types.SpecStatePassed).Names()).Should(ConsistOf("runs"))
    92  			Ω(reporter.Did.WithState(types.SpecStateInterrupted).Names()).Should(ConsistOf("the interrupted test"))
    93  			Ω(reporter.Did.WithState(types.SpecStateSkipped).Names()).Should(ConsistOf("skipped.1", "skipped.2"))
    94  		})
    95  
    96  		It("reports the interrupted test as interrupted and emits a stack trace", func() {
    97  			message := reporter.Did.Find("the interrupted test").Failure.Message
    98  			Ω(message).Should(ContainSubstring("Interrupted by Timeout\nstack trace"))
    99  		})
   100  
   101  		It("reports the correct statistics", func() {
   102  			Ω(reporter.End).Should(BeASuiteSummary(false, NSpecs(4), NWillRun(4), NPassed(1), NSkipped(2), NFailed(1)))
   103  			Ω(reporter.End.SpecialSuiteFailureReasons).Should(ContainElement("Interrupted by Timeout"))
   104  		})
   105  	})
   106  })