Best Ginkgo code snippet using internal.PauseIntercepting
output_interceptor.go
Source:output_interceptor.go
...38type OutputInterceptor interface {39 StartInterceptingOutput()40 StartInterceptingOutputAndForwardTo(io.Writer)41 StopInterceptingAndReturnOutput() string42 PauseIntercepting()43 ResumeIntercepting()44 Shutdown()45}46func NewOutputInterceptor() OutputInterceptor {47 return NewOSGlobalReassigningOutputInterceptor()48}49type NoopOutputInterceptor struct{}50func (interceptor NoopOutputInterceptor) StartInterceptingOutput() {}51func (interceptor NoopOutputInterceptor) StartInterceptingOutputAndForwardTo(io.Writer) {}52func (interceptor NoopOutputInterceptor) StopInterceptingAndReturnOutput() string { return "" }53func (interceptor NoopOutputInterceptor) PauseIntercepting() {}54func (interceptor NoopOutputInterceptor) ResumeIntercepting() {}55func (interceptor NoopOutputInterceptor) Shutdown() {}56type pipePair struct {57 reader *os.File58 writer *os.File59}60func startPipeFactory(pipeChannel chan pipePair, shutdown chan interface{}) {61 for {62 //make the next pipe...63 pair := pipePair{}64 pair.reader, pair.writer, _ = os.Pipe()65 select {66 //...and provide it to the next consumer (they are responsible for closing the files)67 case pipeChannel <- pair:68 continue69 //...or close the files if we were told to shutdown70 case <-shutdown:71 pair.reader.Close()72 pair.writer.Close()73 return74 }75 }76}77type interceptorImplementation interface {78 CreateStdoutStderrClones() (*os.File, *os.File)79 ConnectPipeToStdoutStderr(*os.File)80 RestoreStdoutStderrFromClones(*os.File, *os.File)81 ShutdownClones(*os.File, *os.File)82}83type genericOutputInterceptor struct {84 intercepting bool85 stdoutClone *os.File86 stderrClone *os.File87 pipe pipePair88 shutdown chan interface{}89 emergencyBailout chan interface{}90 pipeChannel chan pipePair91 interceptedContent chan string92 forwardTo io.Writer93 accumulatedOutput string94 implementation interceptorImplementation95}96func (interceptor *genericOutputInterceptor) StartInterceptingOutput() {97 interceptor.StartInterceptingOutputAndForwardTo(io.Discard)98}99func (interceptor *genericOutputInterceptor) StartInterceptingOutputAndForwardTo(w io.Writer) {100 if interceptor.intercepting {101 return102 }103 interceptor.accumulatedOutput = ""104 interceptor.forwardTo = w105 interceptor.ResumeIntercepting()106}107func (interceptor *genericOutputInterceptor) StopInterceptingAndReturnOutput() string {108 if interceptor.intercepting {109 interceptor.PauseIntercepting()110 }111 return interceptor.accumulatedOutput112}113func (interceptor *genericOutputInterceptor) ResumeIntercepting() {114 if interceptor.intercepting {115 return116 }117 interceptor.intercepting = true118 if interceptor.stdoutClone == nil {119 interceptor.stdoutClone, interceptor.stderrClone = interceptor.implementation.CreateStdoutStderrClones()120 interceptor.shutdown = make(chan interface{})121 go startPipeFactory(interceptor.pipeChannel, interceptor.shutdown)122 }123 // Now we make a pipe, we'll use this to redirect the input to the 1 and 2 file descriptors (this is how everything else in the world is tring to log to stdout and stderr)124 // we get the pipe from our pipe factory. it runs in the background so we can request the next pipe while the spec being intercepted is running125 interceptor.pipe = <-interceptor.pipeChannel126 interceptor.emergencyBailout = make(chan interface{})127 //Spin up a goroutine to copy data from the pipe into a buffer, this is how we capture any output the user is emitting128 go func() {129 buffer := &bytes.Buffer{}130 destination := io.MultiWriter(buffer, interceptor.forwardTo)131 copyFinished := make(chan interface{})132 reader := interceptor.pipe.reader133 go func() {134 io.Copy(destination, reader)135 reader.Close() // close the read end of the pipe so we don't leak a file descriptor136 close(copyFinished)137 }()138 select {139 case <-copyFinished:140 interceptor.interceptedContent <- buffer.String()141 case <-interceptor.emergencyBailout:142 interceptor.interceptedContent <- ""143 }144 }()145 interceptor.implementation.ConnectPipeToStdoutStderr(interceptor.pipe.writer)146}147func (interceptor *genericOutputInterceptor) PauseIntercepting() {148 if !interceptor.intercepting {149 return150 }151 // first we have to close the write end of the pipe. To do this we have to close all file descriptors pointing152 // to the write end. So that would be the pipewriter itself, and FD #1 and FD #2 if we've Dup2'd them153 interceptor.pipe.writer.Close() // the pipewriter itself154 // we also need to stop intercepting. we do that by reconnecting the stdout and stderr file descriptions back to their respective #1 and #2 file descriptors;155 // this also closes #1 and #2 before it points that their original stdout and stderr file descriptions156 interceptor.implementation.RestoreStdoutStderrFromClones(interceptor.stdoutClone, interceptor.stderrClone)157 var content string158 select {159 case content = <-interceptor.interceptedContent:160 case <-time.After(BAILOUT_TIME):161 /*162 By closing all the pipe writer's file descriptors associated with the pipe writer's file description the io.Copy reading from the reader163 should eventually receive an EOF and exit.164 **However**, if the user has spun up an external process and passed in os.Stdout/os.Stderr to cmd.Stdout/cmd.Stderr then the external process165 will have a file descriptor pointing to the pipe writer's file description and it will not close until the external process exits.166 That would leave us hanging here waiting for the io.Copy to close forever. Instead we invoke this emergency escape valve. This returns whatever167 content we've got but leaves the io.Copy running. This ensures the external process can continue writing without hanging at the cost of leaking a goroutine168 and file descriptor (those these will be cleaned up when the process exits).169 We tack on a message to notify the user that they've hit this edgecase and encourage them to address it.170 */171 close(interceptor.emergencyBailout)172 content = <-interceptor.interceptedContent + BAILOUT_MESSAGE173 }174 interceptor.accumulatedOutput += content175 interceptor.intercepting = false176}177func (interceptor *genericOutputInterceptor) Shutdown() {178 interceptor.PauseIntercepting()179 if interceptor.stdoutClone != nil {180 close(interceptor.shutdown)181 interceptor.implementation.ShutdownClones(interceptor.stdoutClone, interceptor.stderrClone)182 interceptor.stdoutClone = nil183 interceptor.stderrClone = nil184 }185}186/* This is used on windows builds but included here so it can be explicitly tested on unix systems too */187func NewOSGlobalReassigningOutputInterceptor() OutputInterceptor {188 return &genericOutputInterceptor{189 interceptedContent: make(chan string),190 pipeChannel: make(chan pipePair),191 shutdown: make(chan interface{}),192 implementation: &osGlobalReassigningOutputInterceptorImpl{},...
output_interceptor_test.go
Source:output_interceptor_test.go
...74 })75 It("doesn't get stuck if it's paused and resumed before starting an external process that attaches to stdout/stderr", func() {76 // See GitHub issue #851: https://github.com/onsi/ginkgo/issues/85177 interceptor.StartInterceptingOutput()78 interceptor.PauseIntercepting()79 cmd := exec.Command("sleep", "60")80 //by threading stdout and stderr through, the sleep process will hold them open and prevent the interceptor from stopping:81 cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr82 Ω(cmd.Start()).Should(Succeed())83 interceptor.ResumeIntercepting()84 fmt.Println("hi stdout")85 fmt.Fprintln(os.Stderr, "hi stderr")86 output := interceptor.StopInterceptingAndReturnOutput()87 Ω(output).Should(Equal("hi stdout\nhi stderr\n"))88 Ω(output).ShouldNot(ContainSubstring(internal.BAILOUT_MESSAGE))89 cmd.Process.Kill()90 })91 It("can start/stop/pause/resume correctly", func() {92 interceptor.StartInterceptingOutput()93 fmt.Fprint(os.Stdout, "O-A")94 fmt.Fprint(os.Stderr, "E-A")95 interceptor.PauseIntercepting()96 fmt.Fprint(os.Stdout, "O-B")97 fmt.Fprint(os.Stderr, "E-B")98 interceptor.ResumeIntercepting()99 fmt.Fprint(os.Stdout, "O-C")100 fmt.Fprint(os.Stderr, "E-C")101 interceptor.ResumeIntercepting() //noop102 fmt.Fprint(os.Stdout, "O-D")103 fmt.Fprint(os.Stderr, "E-D")104 interceptor.PauseIntercepting()105 fmt.Fprint(os.Stdout, "O-E")106 fmt.Fprint(os.Stderr, "E-E")107 interceptor.PauseIntercepting() //noop108 fmt.Fprint(os.Stdout, "O-F")109 fmt.Fprint(os.Stderr, "E-F")110 interceptor.ResumeIntercepting()111 fmt.Fprint(os.Stdout, "O-G")112 fmt.Fprint(os.Stderr, "E-G")113 interceptor.StartInterceptingOutput() //noop114 fmt.Fprint(os.Stdout, "O-H")115 fmt.Fprint(os.Stderr, "E-H")116 interceptor.PauseIntercepting()117 output := interceptor.StopInterceptingAndReturnOutput()118 Ω(output).Should(Equal("O-AE-AO-CE-CO-DE-DO-GE-GO-HE-H"))119 })120 }121 Context("the OutputInterceptor for this OS", func() {122 BeforeEach(func() {123 interceptor = internal.NewOutputInterceptor()124 DeferCleanup(interceptor.Shutdown)125 })126 sharedInterceptorTests()127 })128 Context("the OSGlobalReassigningOutputInterceptor used on windows", func() {129 BeforeEach(func() {130 interceptor = internal.NewOSGlobalReassigningOutputInterceptor()...
PauseIntercepting
Using AI Code Generation
1import (2func main() {3 hook := test.NewGlobal()4 logrus.Warn("Hello World!")5 hook.PauseIntercepting()6 logrus.Warn("Hello World!")7 hook.UnpauseIntercepting()8 logrus.Warn("Hello World!")9 fmt.Println(hook.Entries)10}11[{0xc0000b4000 2020-10-10 17:47:31.855 +0530 IST m=+0.000000001 Hello World! map[]}]
PauseIntercepting
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello World")4 time.Sleep(5 * time.Second)5}6import (7func main() {8 fmt.Println("Hello World")9 time.Sleep(5 * time.Second)10}11import (12func main() {13 fmt.Println("Hello World")14 time.Sleep(5 * time.Second)15}16import (17func main() {18 fmt.Println("Hello World")19 time.Sleep(5 * time.Second)20}21import (22func main() {23 fmt.Println("Hello World")24 time.Sleep(5 * time.Second)25}26import (27func main() {28 fmt.Println("Hello World")29 time.Sleep(5 * time.Second)30}31import (32func main() {33 fmt.Println("Hello World")34 time.Sleep(5 * time.Second)35}36import (37func main() {38 fmt.Println("Hello World")39 time.Sleep(5 * time.Second)40}41import (42func main() {43 fmt.Println("Hello World")44 time.Sleep(5 * time.Second)45}46import (47func main() {
PauseIntercepting
Using AI Code Generation
1import (2func main() {3 vm := otto.New()4 vm.Set("print", func(call otto.FunctionCall) otto.Value {5 fmt.Println(call.Argument(0).String())6 return otto.Value{}7 })8 vm.Set("exit", func(call otto.FunctionCall) otto.Value {9 os.Exit(0)10 return otto.Value{}11 })12 _, err := vm.Run(`13 function print(value) {14 console.log(value);15 }16 function exit() {17 process.exit();18 }19 function run() {20 print('hello world');21 exit();22 }23 run();24 if err != nil {25 fmt.Println(err)26 }27}28import (29func main() {30 vm := otto.New()31 vm.Set("print", func(call otto.FunctionCall) otto.Value {32 fmt.Println(call.Argument(0).String())33 return otto.Value{}34 })35 vm.Set("exit", func(call otto.FunctionCall) otto.Value {36 os.Exit(0)37 return otto.Value{}38 })39 _, err := vm.Run(`40 function print(value) {41 console.log(value);42 }43 function exit() {44 process.exit();45 }46 function run() {47 print('hello world');48 exit();49 }50 run();51 if err != nil {52 fmt.Println(err)53 }54}55import (56func main() {57 vm := otto.New()58 vm.Set("print", func(call otto.FunctionCall) otto.Value {59 fmt.Println(call.Argument(0).String())60 return otto.Value{}61 })62 vm.Set("exit", func(call otto.FunctionCall) otto.Value {63 os.Exit(0)64 return otto.Value{}65 })66 _, err := vm.Run(`67 function print(value)
PauseIntercepting
Using AI Code Generation
12: import (26: func main() {37: log.Root().SetHandler(term.NewStderrHandler())48: log.Root().SetHandler(log.LvlFilterHandler(log.LvlTrace, log.Root().GetHandler()))59: log.Info("Hello")610: }73: import (87: func main() {98: log.Root().SetHandler(term.NewStderrHandler())109: log.Root().SetHandler(log.LvlFilterHandler(log.LvlTrace, log.Root().GetHandler()))1110: log.Info("Hello")1211: log.Debug("Hello")1312: }
PauseIntercepting
Using AI Code Generation
1import (2func main() {3 interceptor.PauseIntercepting()4 fmt.Println("Hello World")5}6import (7func main() {8 interceptor.ResumeIntercepting()9 fmt.Println("Hello World")10}
PauseIntercepting
Using AI Code Generation
1I am new to Go and I am trying to understand the difference between the two. I have seen a lot of examples where people will use the underscore to import a package. For example:2import (3I understand that this will import the package, but not use it in the code. I am wondering why you would do this? I have also seen this used in the standard library, for example:4import (5import (6import (7import (8import (9import (10import (11import (12import (13import (
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!