func TestStartShouldExitOnChanClose(t *testing.T) { watchers := []watcher.Watcher{ time_watcher.NewWatcher(100*time.Millisecond, 5), time_watcher.NewWatcher(200*time.Millisecond, 5), mock_watcher.NewChanCloser(500 * time.Millisecond), } handler := mock_handler.NewHandler() runner := NewRunner(handler, watchers, 10) runner.Start() if handler.CallCount == 0 { t.Errorf("Expected at least a call [%d] \n", handler.CallCount) } runner.Stop() }
func main() { flagset.Parse(os.Args[1:]) flagset.Usage = usage if len(os.Args) < 2 { flagset.Usage() os.Exit(0) } if flags.Version { fmt.Printf("etcdexpose v%s", currentVersion) os.Exit(0) } if flags.Port == 0 { fmt.Println("You must provide a valid port to expose with -p flag") os.Exit(1) } if flags.CheckPort == 0 { flags.CheckPort = flags.Port } sigch := make(chan os.Signal, 1) signal.Notify(sigch, os.Interrupt) cfg := client.Config{ Endpoints: []string{flags.Server}, Transport: client.DefaultTransport, HeaderTimeoutPerRequest: time.Second, } c, err := client.New(cfg) if err != nil { log.Fatal(err) } kapi := client.NewKeysAPI(c) renderer, err := utils.NewValueRenderer(flags.Template, flags.Port) if err != nil { log.Fatalf("Invalid template given") } healthCheck, err := utils.NewHealthCheck( flags.HealthPath, flags.CheckPort, flags.Retry, flags.RetryDelay, flags.Timeout, ) if err != nil { log.Fatalf("Invalid format given") } namespace_client := utils.NewEtcdClient( kapi, flags.Namespace, flags.Key, flags.Ttl, ) var handler handler.Handler = nil if flags.Multiple { handler = multiple.NewMutlipleValueExpose( namespace_client, renderer, healthCheck, ) } else { handler = single.NewSingleValueExpose( namespace_client, renderer, healthCheck, ) } watchers := []watcher.Watcher{ etcd.NewWatcher(kapi, flags.Namespace, bufferSize), } if flags.Interval > 0 { timeWatcher := time_watcher.NewWatcher( time.Duration(flags.Interval)*time.Second, bufferSize, ) watchers = append(watchers, timeWatcher) } runner := runner.NewRunner( handler, watchers, len(watchers)*bufferSize, ) go func() { s := <-sigch log.Printf("Received signal [%v] stopping application\n", s) runner.Stop() os.Exit(0) }() for { log.Println("Starting runner...") runner.Start() log.Println("Runner exited, Stopping...") runner.Stop() log.Println("waiting 5s before retry ...") time.Sleep(5 * time.Second) } }