RunUntilEndpointHit stalls current goroutine executions and prints the URL to local address. When URL is hit this function returns with nil to continue the execution. It also watches for SIGINT, SIGKILL or SIGHUP signals and does the same when any of those is seen. This function is useful when you
()
| 43 | // as opposed to RunUntilSignal for certain IDEs that does not send correct signal on stop (e.g. Goland pre 2022.3, |
| 44 | // see https://youtrack.jetbrains.com/issue/GO-5982). |
| 45 | func RunUntilEndpointHit() (err error) { |
| 46 | once := sync.Once{} |
| 47 | wg := sync.WaitGroup{} |
| 48 | stopWG := sync.WaitGroup{} |
| 49 | stopWG.Add(1) |
| 50 | |
| 51 | l, err := net.Listen("tcp", "localhost:0") |
| 52 | if err != nil { |
| 53 | return err |
| 54 | } |
| 55 | srv := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 56 | once.Do(func() { stopWG.Done() }) |
| 57 | })} |
| 58 | |
| 59 | wg.Add(2) |
| 60 | go func() { |
| 61 | if serr := srv.Serve(l); serr != nil { |
| 62 | once.Do(func() { |
| 63 | err = errors.Wrap(serr, "unexpected error") |
| 64 | stopWG.Done() |
| 65 | }) |
| 66 | } |
| 67 | wg.Done() |
| 68 | }() |
| 69 | ctx, cancel := context.WithCancel(context.Background()) |
| 70 | go func() { |
| 71 | s := make(chan os.Signal, 1) |
| 72 | signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP) |
| 73 | select { |
| 74 | case <-s: |
| 75 | case <-ctx.Done(): |
| 76 | } |
| 77 | once.Do(func() { stopWG.Done() }) |
| 78 | wg.Done() |
| 79 | }() |
| 80 | |
| 81 | fmt.Println("Waiting for user HTTP request on ", "http://"+l.Addr().String(), " or SIGINT, SIGKILL or SIGHUP signal...") |
| 82 | stopWG.Wait() |
| 83 | |
| 84 | // Cleanup. |
| 85 | cancel() |
| 86 | _ = l.Close() |
| 87 | wg.Wait() |
| 88 | return nil |
| 89 | } |
| 90 | |
| 91 | // RunUntilSignal stops the current goroutine execution and watches for SIGINT, SIGKILL and SIGHUP signals. Once spotted it continues |
| 92 | // the execution. This function is useful when you want to interact with e2e tests and manually decide when to finish. |
searching dependent graphs…