github.com/onsi/ginkgo@v1.16.6-0.20211118180735-4e1925ba4c95/integration/_fixtures/interceptor_fixture/interceptor_fixture_suite_test.go (about)

     1  package main_test
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/exec"
     7  	"testing"
     8  	"time"
     9  
    10  	. "github.com/onsi/ginkgo"
    11  	"github.com/onsi/ginkgo/internal"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  func TestInterceptorFixture(t *testing.T) {
    16  	RegisterFailHandler(Fail)
    17  	RunSpecs(t, "InterceptorFixture Suite")
    18  }
    19  
    20  var _ = Describe("Ensuring the OutputInterceptor handles the edge case where an external process keeps the interceptor's pipe open", func() {
    21  	var interceptor internal.OutputInterceptor
    22  
    23  	sharedBehavior := func() {
    24  		It("can avoid getting stuck, but also doesn't block the external process", func() {
    25  			interceptor.StartInterceptingOutput()
    26  			cmd := exec.Command("./interceptor")
    27  			cmd.Stdout = os.Stdout
    28  			cmd.Stderr = os.Stderr
    29  			Ω(cmd.Start()).Should(Succeed())
    30  
    31  			By("Bailing out because the pipe is stuck")
    32  			outputChan := make(chan string)
    33  			go func() {
    34  				outputChan <- interceptor.StopInterceptingAndReturnOutput()
    35  			}()
    36  			var output string
    37  			Eventually(outputChan, internal.BAILOUT_TIME*2).Should(Receive(&output))
    38  			Ω(output).Should(Equal(internal.BAILOUT_MESSAGE))
    39  
    40  			By("Not subsequently bailing out because the new pipe isn't tied to an external process")
    41  			interceptor.StartInterceptingOutput()
    42  			output = interceptor.StopInterceptingAndReturnOutput()
    43  			Ω(output).ShouldNot(ContainSubstring("STDOUT"), "we're no longer capturing this output")
    44  			Ω(output).ShouldNot(ContainSubstring("STDERR"), "we're no longer capturing this output")
    45  			Ω(output).ShouldNot(ContainSubstring(internal.BAILOUT_MESSAGE), "we didn't have to bail out")
    46  
    47  			expected := ""
    48  			for i := 0; i < 300; i++ {
    49  				expected += fmt.Sprintf("FILE %d\n", i)
    50  			}
    51  			Eventually(func(g Gomega) string {
    52  				out, err := os.ReadFile("file-output")
    53  				g.Ω(err).ShouldNot(HaveOccurred())
    54  				return string(out)
    55  			}, 5*time.Second).Should(Equal(expected))
    56  
    57  			os.Remove("file-output")
    58  		})
    59  
    60  		It("works successfully if the user pauses then resumes around starging an external process", func() {
    61  			interceptor.StartInterceptingOutput()
    62  			interceptor.PauseIntercepting()
    63  
    64  			cmd := exec.Command("./interceptor")
    65  			cmd.Stdout = os.Stdout
    66  			cmd.Stderr = os.Stderr
    67  			Ω(cmd.Start()).Should(Succeed())
    68  
    69  			interceptor.ResumeIntercepting()
    70  			output := interceptor.StopInterceptingAndReturnOutput()
    71  			Ω(output).ShouldNot(ContainSubstring(internal.BAILOUT_MESSAGE), "we didn't have to bail out")
    72  
    73  			interceptor.StartInterceptingOutput()
    74  			output = interceptor.StopInterceptingAndReturnOutput()
    75  			Ω(output).ShouldNot(ContainSubstring(internal.BAILOUT_MESSAGE), "we still didn't have to bail out")
    76  
    77  			expected := ""
    78  			for i := 0; i < 300; i++ {
    79  				expected += fmt.Sprintf("FILE %d\n", i)
    80  			}
    81  			Eventually(func(g Gomega) string {
    82  				out, err := os.ReadFile("file-output")
    83  				g.Ω(err).ShouldNot(HaveOccurred())
    84  				return string(out)
    85  			}, 5*time.Second).Should(Equal(expected))
    86  
    87  			os.Remove("file-output")
    88  		})
    89  	}
    90  
    91  	Context("the dup2 interceptor", func() {
    92  		BeforeEach(func() {
    93  			interceptor = internal.NewOutputInterceptor()
    94  		})
    95  
    96  		sharedBehavior()
    97  	})
    98  
    99  	Context("the global reassigning interceptor", func() {
   100  		BeforeEach(func() {
   101  			interceptor = internal.NewOSGlobalReassigningOutputInterceptor()
   102  		})
   103  
   104  		sharedBehavior()
   105  	})
   106  })