Fix misuse of gorilla/mux.Router{} use mux.NewRouter() instead

Co-authored-by: Roberto Abdelkader Martínez Pérez <robertomartinezp@gmail.com>
This commit is contained in:
pancho horrillo
2019-10-09 15:22:50 +02:00
parent 6e387eab37
commit 49252a78a6
+19 -21
View File
@@ -66,13 +66,13 @@ func TestSwappableMuxGetIsAbleToReadWhileOthersAreReading(t *testing.T) {
func TestSwappableMuxSetSetsTheGivenMux(t *testing.T) { func TestSwappableMuxSetSetsTheGivenMux(t *testing.T) {
sm := swappableMux{} sm := swappableMux{}
mux := &mux.Router{ m := mux.NewRouter()
KeepContext: true, // nolint
} m.KeepContext = true
sm.set(mux) sm.set(m)
//nolint // nolint
if !sm.root.KeepContext { if !sm.root.KeepContext {
t.Error("mux not set") t.Error("mux not set")
} }
@@ -80,11 +80,11 @@ func TestSwappableMuxSetSetsTheGivenMux(t *testing.T) {
func TestSwappableMuxSetSetsTheSameInstance(t *testing.T) { func TestSwappableMuxSetSetsTheSameInstance(t *testing.T) {
sm := swappableMux{} sm := swappableMux{}
mux := &mux.Router{} m := mux.NewRouter()
sm.set(mux) sm.set(m)
if mux != sm.root { if m != sm.root {
t.Error("Set mux is not the same instance") t.Error("Set mux is not the same instance")
} }
} }
@@ -96,7 +96,7 @@ func TestSwappableMuxSetWaitsForWriterToReleaseMutex(t *testing.T) {
defer sm.m.Unlock() defer sm.m.Unlock()
c := make(chan bool) c := make(chan bool)
go func() { sm.set(&mux.Router{}); c <- true }() go func() { sm.set(mux.NewRouter()); c <- true }()
time.Sleep(10 * time.Millisecond) time.Sleep(10 * time.Millisecond)
@@ -114,7 +114,7 @@ func TestSwappableMuxSetWaitsForReadersToReleaseMutex(t *testing.T) {
defer sm.m.RUnlock() defer sm.m.RUnlock()
c := make(chan bool) c := make(chan bool)
go func() { sm.set(&mux.Router{}); c <- true }() go func() { sm.set(mux.NewRouter()); c <- true }()
time.Sleep(10 * time.Millisecond) time.Sleep(10 * time.Millisecond)
@@ -128,10 +128,10 @@ func TestSwappableMuxSetWaitsForReadersToReleaseMutex(t *testing.T) {
func TestServeHTTPCallsInnerMux(t *testing.T) { func TestServeHTTPCallsInnerMux(t *testing.T) {
called := false called := false
mux := &mux.Router{} m := mux.NewRouter()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { called = true }) m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { called = true })
sm := swappableMux{root: mux} sm := swappableMux{root: m}
req := httptest.NewRequest("GET", "/", nil) req := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
@@ -143,15 +143,13 @@ func TestServeHTTPCallsInnerMux(t *testing.T) {
} }
} }
// TODO: test that a read lock does not impede calling ServeHTTP
func TestServeHTTPCanServeWhenMuxIsReadLocked(t *testing.T) { func TestServeHTTPCanServeWhenMuxIsReadLocked(t *testing.T) {
called := false called := false
mux := &mux.Router{} m := mux.NewRouter()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { called = true }) m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { called = true })
sm := swappableMux{root: mux} sm := swappableMux{root: m}
sm.m.RLock() sm.m.RLock()
req := httptest.NewRequest("GET", "/", nil) req := httptest.NewRequest("GET", "/", nil)
@@ -169,10 +167,10 @@ func TestServeHTTPCanServeWhenMuxIsReadLocked(t *testing.T) {
func TestServeHTTPCallsInnerMuxAfterAcquiringLock(t *testing.T) { func TestServeHTTPCallsInnerMuxAfterAcquiringLock(t *testing.T) {
called := false called := false
mux := &mux.Router{} m := mux.NewRouter()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { called = true }) m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { called = true })
sm := swappableMux{root: mux} sm := swappableMux{root: m}
sm.m.Lock() sm.m.Lock()
req := httptest.NewRequest("GET", "/", nil) req := httptest.NewRequest("GET", "/", nil)