-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functional tests, fix unset exit codes on panics (#42)
* Refactor to support functional tests * Fix unset exit code from uncaught exception * Fix unset exit code for panic in next tick of event loop * Fix data race on shadowed "err" * Poke CI * Increase test timeout for Windows race detector to complete * Move test types and helpers to the bottom * Refactor into table-driven tests * Remove single-use helper * Handle errors with testing.T in testParse helper * Return errors instead of exit codes * Extract HTTP server, use ctx for startup/shutdown * Increase test timeout for Windows again * Handle testParseOther error as assertion failure * Rename shutdownNoWait to startShutdown for clarity
- Loading branch information
1 parent
bb77d44
commit 2d1bbcf
Showing
7 changed files
with
342 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"net" | ||
"net/http" | ||
neturl "net/url" | ||
"time" | ||
) | ||
|
||
func startHTTPServer(ctx context.Context, handler http.Handler, logger *log.Logger) (url string, shutdown context.CancelFunc, err error) { | ||
// Need to generate a random port every time for tests in parallel to run. | ||
l, err := net.Listen("tcp", "localhost:") | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
|
||
server := &http.Server{ | ||
Handler: handler, | ||
} | ||
go func() { // serves HTTP | ||
err := server.Serve(l) | ||
if err != http.ErrServerClosed { | ||
logger.Println(err) | ||
} | ||
}() | ||
|
||
shutdownCtx, startShutdown := context.WithCancel(ctx) | ||
shutdownComplete := make(chan struct{}, 1) | ||
go func() { // waits for canceled ctx or triggered shutdown, then shuts down HTTP | ||
<-shutdownCtx.Done() | ||
shutdownTimeoutCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
err := server.Shutdown(shutdownTimeoutCtx) | ||
if err != nil { | ||
logger.Println(err) | ||
} | ||
shutdownComplete <- struct{}{} | ||
}() | ||
|
||
shutdown = func() { | ||
startShutdown() | ||
<-shutdownComplete | ||
} | ||
url = (&neturl.URL{ | ||
Scheme: "http", | ||
Host: l.Addr().String(), | ||
}).String() | ||
return url, shutdown, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.