github.com/onsi/ginkgo@v1.16.6-0.20211118180735-4e1925ba4c95/internal/test_helpers/fake_interrupt_handler.go (about) 1 package test_helpers 2 3 import ( 4 "sync" 5 6 "github.com/onsi/ginkgo/internal/interrupt_handler" 7 ) 8 9 type FakeInterruptHandler struct { 10 triggerInterrupt chan bool 11 12 c chan interface{} 13 stop chan interface{} 14 lock *sync.Mutex 15 interrupted bool 16 cause interrupt_handler.InterruptCause 17 interruptPlaceholderMessage string 18 emittedInterruptPlaceholderMessage string 19 } 20 21 func NewFakeInterruptHandler() *FakeInterruptHandler { 22 handler := &FakeInterruptHandler{ 23 triggerInterrupt: make(chan bool), 24 c: make(chan interface{}), 25 lock: &sync.Mutex{}, 26 interrupted: false, 27 stop: make(chan interface{}), 28 } 29 handler.registerForInterrupts() 30 return handler 31 } 32 33 func (handler *FakeInterruptHandler) Stop() { 34 close(handler.stop) 35 } 36 37 func (handler *FakeInterruptHandler) registerForInterrupts() { 38 go func() { 39 for { 40 select { 41 case <-handler.triggerInterrupt: 42 case <-handler.stop: 43 return 44 } 45 handler.lock.Lock() 46 handler.interrupted = true 47 handler.emittedInterruptPlaceholderMessage = handler.interruptPlaceholderMessage 48 close(handler.c) 49 handler.c = make(chan interface{}) 50 handler.lock.Unlock() 51 } 52 }() 53 } 54 55 func (handler *FakeInterruptHandler) Interrupt(cause interrupt_handler.InterruptCause) { 56 handler.lock.Lock() 57 handler.cause = cause 58 handler.lock.Unlock() 59 60 handler.triggerInterrupt <- true 61 } 62 63 func (handler *FakeInterruptHandler) Status() interrupt_handler.InterruptStatus { 64 handler.lock.Lock() 65 defer handler.lock.Unlock() 66 67 return interrupt_handler.InterruptStatus{ 68 Interrupted: handler.interrupted, 69 Channel: handler.c, 70 Cause: handler.cause, 71 } 72 } 73 74 func (handler *FakeInterruptHandler) SetInterruptPlaceholderMessage(message string) { 75 handler.lock.Lock() 76 defer handler.lock.Unlock() 77 78 handler.interruptPlaceholderMessage = message 79 } 80 81 func (handler *FakeInterruptHandler) ClearInterruptPlaceholderMessage() { 82 handler.lock.Lock() 83 defer handler.lock.Unlock() 84 85 handler.interruptPlaceholderMessage = "" 86 } 87 88 func (handler *FakeInterruptHandler) EmittedInterruptPlaceholderMessage() string { 89 handler.lock.Lock() 90 defer handler.lock.Unlock() 91 return handler.emittedInterruptPlaceholderMessage 92 } 93 94 func (handler *FakeInterruptHandler) InterruptMessageWithStackTraces() string { 95 handler.lock.Lock() 96 defer handler.lock.Unlock() 97 98 return handler.cause.String() + "\nstack trace" 99 }