Skip to content

Instantly share code, notes, and snippets.

@husobee
Created November 17, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save husobee/92f742c851f1083fd3cb to your computer and use it in GitHub Desktop.
Save husobee/92f742c851f1083fd3cb to your computer and use it in GitHub Desktop.
go coverage external tests
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>
body {
background: black;
color: rgb(80, 80, 80);
}
body, pre, #legend span {
font-family: Menlo, monospace;
font-weight: bold;
}
#topbar {
background: black;
position: fixed; [69/252]
top: 0; left: 0; right: 0;
height: 42px;
border-bottom: 1px solid rgb(80, 80, 80);
}
#content {
margin-top: 50px;
}
#nav, #legend {
float: left;
margin-left: 10px;
}
#legend {
margin-top: 12px;
}
#nav {
margin-top: 10px;
}
#legend span {
margin: 0 5px;
}
.cov0 { color: rgb(192, 0, 0) }
.cov1 { color: rgb(128, 128, 128) }
.cov2 { color: rgb(116, 140, 131) }
.cov3 { color: rgb(104, 152, 134) }
.cov4 { color: rgb(92, 164, 137) }
.cov5 { color: rgb(80, 176, 140) }
.cov6 { color: rgb(68, 188, 143) }
.cov7 { color: rgb(56, 200, 146) }
.cov8 { color: rgb(44, 212, 149) }
.cov9 { color: rgb(32, 224, 152) }
.cov10 { color: rgb(20, 236, 155) }
</style>
</head>
<body>
<div id="topbar">
<div id="nav">
<select id="files">
<option value="file0">codecovergist/main.go (94.4%)</option>
</select>
</div>
<div id="legend">
<span>not tracked</span>
<span class="cov0">not covered</span>
<span class="cov8">covered</span>
</div>
</div>
<div id="content">
<pre class="file" id="file0" >package main
import (
"fmt"
"net/http"
"time"
"github.com/codegangsta/negroni"
"github.com/husobee/vestigo"
"github.com/tylerb/graceful"
)
var (
srv = &amp;graceful.Server{
Timeout: 5 * time.Second,
}
stop chan bool
testMode bool = false
)
// runMain - entrypoint to perform external testing of service
func runMain() <span class="cov8" title="1">{
// start the stop channel
stop = make(chan bool)
// put the service in "testMode"
testMode = true
// run the main entry point
go main()
// watch for the stop channel
&lt;-stop
// stop the graceful server
srv.Stop(5 * time.Second)
}</span>
func main() <span class="cov8" title="1">{
// setup middlware stack
n := negroni.Classic()
// setup routes
router := vestigo.NewRouter()
// endpoints
router.Post("/test", func(w http.ResponseWriter, r *http.Request) </span><span class="cov8" title="1">{
if false </span><span class="cov0" title="0">{
// we should see this endpoint not covered
// if we hit the /test endpoint externally
fmt.Println("totally never getting here")
}</span>
<span class="cov8" title="1">w.WriteHeader(200)
w.Write([]byte("done"))</span>
})
// only if we are in testMode should we attempt to add the death blow
<span class="cov8" title="1">if testMode </span><span class="cov8" title="1">{
// death blow endpoint - endpoint that will stop the service if stop is
// a live channel (only live if started from RunMain)
router.Post("/deathblow", func(w http.ResponseWriter, r *http.Request) </span><span class="cov8" title="1">{
// end the graceful server if being run from RunMain()
stop &lt;- true
}</span>)
}
// add our router to negroni
<span class="cov8" title="1">n.UseHandler(router)
// graceful start/stop server
srv.Server = &amp;http.Server{Addr: ":1234", Handler: n}
// serve http
srv.ListenAndServe()</span>
}
</pre>
</div>
</body>
<script>
(function() {
var files = document.getElementById('files');
var visible = document.getElementById('file0');
files.addEventListener('change', onChange, false);
function onChange() {
visible.style.display = 'none';
visible = document.getElementById(files.value);
visible.style.display = 'block';
window.scrollTo(0, 0);
}
})();
</script>
</html>
package main
import (
"fmt"
"net/http"
"time"
"github.com/codegangsta/negroni"
"github.com/husobee/vestigo"
"github.com/tylerb/graceful"
)
var (
srv = &graceful.Server{
Timeout: 5 * time.Second,
}
stop chan bool
testMode bool = false
)
// runMain - entrypoint to perform external testing of service
func runMain() {
// start the stop channel
stop = make(chan bool)
// put the service in "testMode"
testMode = true
// run the main entry point
go main()
// watch for the stop channel
<-stop
// stop the graceful server
srv.Stop(5 * time.Second)
}
func main() {
// setup middlware stack
n := negroni.Classic()
// setup routes
router := vestigo.NewRouter()
// endpoints
router.Post("/test", func(w http.ResponseWriter, r *http.Request) {
if false {
// we should see this endpoint not covered
// if we hit the /test endpoint externally
fmt.Println("totally never getting here")
}
w.WriteHeader(200)
w.Write([]byte("done"))
})
// only if we are in testMode should we attempt to add the death blow
if testMode {
// death blow endpoint - endpoint that will stop the service if stop is
// a live channel (only live if started from RunMain)
router.Post("/deathblow", func(w http.ResponseWriter, r *http.Request) {
// end the graceful server if being run from RunMain()
stop <- true
})
}
// add our router to negroni
n.UseHandler(router)
// graceful start/stop server
srv.Server = &http.Server{Addr: ":1234", Handler: n}
// serve http
srv.ListenAndServe()
}
package main
import (
"testing"
)
// TestMain - test to drive external testing coverage
func TestMain(t *testing.T) {
runMain()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment