title: How to test code that loops forever
body: <p>I have an app (<a href="https://github.com/epazote/epazote/blob/master/block.go" rel="noreferrer">epazote</a>) that once starts runs forever but I want to test some values before it blocks/waits until <kbd>ctrl</kbd>+<kbd>c</kbd> is pressed or is killed.</p>
  
  <p>Here is an small example: <a href="http://play.golang.org/p/t0spQRJB36" rel="noreferrer">http://play.golang.org/p/t0spQRJB36</a></p>
  
  <pre class="lang-golang prettyprint-override"><code>package main
  
  import (
      "fmt"
      "os"
      "os/signal"
  )
  
  type IAddString interface {
      AddString(string)
  }
  
  type addString struct{}
  
  func (self *addString) AddString(s string) {
      fmt.Println(s)
  }
  
  func block(a IAddString, s string) {
  
      // test this
      a.AddString(s)
  
  
      // ignore this while testing
      block := make(chan os.Signal)
      signal.Notify(block, os.Interrupt, os.Kill)
  
      for {
          signalType := &lt;-block
          switch signalType {
  
          default:
              signal.Stop(block)
              fmt.Printf("%q signal received.", signalType)
              os.Exit(0)
          }
      }
  }
  
  func main() {
      a := &amp;addString{}
      block(a, "foo")
  }
  </code></pre>
  
  <p>I would like to know if is posible to ignore some parts of the code while testing, or how to test this cases, I have implemented an interface, in this case for testing <code>AddString</code>  that helped me to test some parts but have no idea of how to avoid the "block" and test.</p>
  
  <p>Any ideas?</p>
  
  <p><strong>Update</strong>: Putting the code inside the loop <code>Addstring</code> in another function works but only for testing that function, but If I want to do a full code coverage, I still need to check/test the blocking part, for example how to test that is properly behaving when receiving <kbd>ctrl</kbd>+<kbd>c</kbd> or a <code>kill -HUP</code>, I was thinking on maybe creating a fake <code>signal.Notify</code> but don't know how to overwrite imported packages in case that could work. </p>