Use common httpserver across fission (#2409)

* Defining httpserver package to capture httpserver shutdown and
introduces uniform running of http server across codebase.
* Add unit tests for httpserver

Signed-off-by: Sanket Sudake <sanketsudake@gmail.com>
This commit is contained in:
Sanket Sudake
2022-04-14 11:35:58 +05:30
committed by GitHub
parent b638a6d047
commit 8442e21621
18 changed files with 139 additions and 93 deletions
+6 -11
View File
@@ -17,6 +17,7 @@ limitations under the License.
package router
import (
"context"
"log"
"net/http"
"testing"
@@ -26,6 +27,7 @@ import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/fission/fission/pkg/utils/httpserver"
"github.com/fission/fission/pkg/utils/metrics"
)
@@ -47,13 +49,6 @@ func verifyRequest(expectedResponse string) {
testRequest(targetURL, expectedResponse)
}
func startServer(mr *mutableRouter) {
err := http.ListenAndServe(":3333", mr)
if err != nil {
log.Fatal(err)
}
}
func spamServer(quit chan bool) {
i := 0
for {
@@ -83,10 +78,10 @@ func TestMutableMux(t *testing.T) {
panicIf(err)
mr := newMutableRouter(logger, muxRouter)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// start http server
log.Print("Start http server")
go startServer(mr)
go httpserver.StartServer(ctx, logger, "router", "3333", mr)
// continuously make requests, panic if any fails
time.Sleep(100 * time.Millisecond)
@@ -102,7 +97,7 @@ func TestMutableMux(t *testing.T) {
// change the muxer
log.Print("Change mux router")
newMuxRouter := mux.NewRouter()
muxRouter.Use(metrics.HTTPMetricMiddleware())
newMuxRouter.Use(metrics.HTTPMetricMiddleware())
newMuxRouter.HandleFunc("/", NewHandler)
mr.updateRouter(newMuxRouter)