Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent having to manually kill main.go after running dredd #1889

Open
kevgilmore opened this issue Jan 19, 2021 · 4 comments
Open

Prevent having to manually kill main.go after running dredd #1889

kevgilmore opened this issue Jan 19, 2021 · 4 comments
Assignees

Comments

@kevgilmore
Copy link

Is your feature request related to a problem? Please describe.
I keep having to kill the previous go server in system monitor (linux) before re-running dredd test.

Describe the solution you'd like
Auto kill the go server after tests have completed

Describe alternatives you've considered
Manually killing main.go from system monitor

Additional context
Sorry for making an issue, but I can't find the answer in the docs.
My project code is here https://github.com/hex6b/go-heroku

@abtris
Copy link
Contributor

abtris commented Feb 3, 2021

Your code have to support signals to be killed by dredd as normal application. Here is how to do it - https://stackoverflow.com/questions/18106749/golang-catch-signals

Related part in docs

@abtris abtris self-assigned this Feb 3, 2021
@kevgilmore
Copy link
Author

kevgilmore commented Mar 29, 2021

hey, still not able to get it to work after implementing the signals catch solution.

Here's my code

package main

import (
	"fmt"
	"log"
	"net/http"
	"os"
	"os/signal"
	"syscall"
)

func main() {
	port := os.Getenv("PORT")
	fmt.Printf("Started\n")

	if port == "" {
		port = "8080"
	}

	go func() {
		http.HandleFunc("/", HelloServer)
		if err := http.ListenAndServe(":"+port, nil); err != nil {
			log.Fatal(err)
		}
	}()

	signalChannel := make(chan os.Signal, 2)
	signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM)
	for {
		sig := <-signalChannel
		switch sig {
		case os.Interrupt:
			fmt.Println("sigint")
		case syscall.SIGTERM:
			fmt.Println("sigterm")
                        os.Exit(1)
			return
		}
	}
}


func HelloServer(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	fmt.Fprintf(w, "{\"message\":\"It works!\"}")
}

and my dredd.yml

color: true
dry-run: null
hookfiles: null
language: nodejs
require: null
server: go run main.go
server-wait: 3
init: false
custom: {}
names: false
only: []
reporter: []
output: []
header: []
sorted: false
user: null
inline-errors: false
details: false
method: []
loglevel: warning
path: []
hooks-worker-timeout: 5000
hooks-worker-connect-timeout: 1500
hooks-worker-connect-retry: 500
hooks-worker-after-connect-wait: 100
hooks-worker-term-timeout: 5000
hooks-worker-term-retry: 500
hooks-worker-handler-host: 127.0.0.1
hooks-worker-handler-port: 61321
config: ./dredd.yml
blueprint: apiary.apib
endpoint: 'http://127.0.0.1:8080'

From what I can see dredd does not seem to trigger SIGTERM after the test have finished. If I trigger SIGTERM another way the code above works.

Any ideas?

@kevgilmore kevgilmore changed the title Prevent having to manually kill main.go after each test Prevent having to manually kill main.go after running dredd Mar 29, 2021
@w-vi
Copy link
Member

w-vi commented Mar 29, 2021

@hex6b I suspect that the SIGTERM is lost somewhere when doing go run main.go can you instead compile the server and use the binary directly instead of the go run main.go? It can be the case that the SIGTERM is consudem by go run process instead of your sever.

@abtris
Copy link
Contributor

abtris commented Mar 29, 2021

The commit refers to golang/go#23716 that explains that this behaviour is not a bug actually. To fully understand the issue I’ve dived into golang repo.

  1. go run command kicks off work.Builder with action &work.Action{Mode: "go run", Func: buildRunProgram, ...} inside run.go file.
  2. Next the function buildRunProgram gets called and starts the compiled binary. It also sets up the signal handling using the channel Interrupted .
  3. When that channel is closed go run process is always exited with 1 status.

Cite from https://medium.com/@ns3777k/go-run-and-exit-codes-7ad1175821c1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants