Add ServeHTTP() method to swappableMux struct

Co-authored-by: Roberto Abdelkader Martínez Pérez <robertomartinezp@gmail.com>
This commit is contained in:
pancho horrillo
2019-10-08 15:06:08 +02:00
parent 3fa2357f71
commit 3b405bd9f6
2 changed files with 89 additions and 12 deletions
+8 -3
View File
@@ -1,6 +1,7 @@
package user
import (
"net/http"
"sync"
"github.com/gorilla/mux"
@@ -8,18 +9,22 @@ import (
type swappableMux struct {
m sync.RWMutex
root mux.Router
root *mux.Router
}
func (sm *swappableMux) get() mux.Router {
func (sm *swappableMux) get() *mux.Router {
sm.m.RLock()
defer sm.m.RUnlock()
return sm.root
}
func (sm *swappableMux) set(mux mux.Router) {
func (sm *swappableMux) set(mux *mux.Router) {
sm.m.Lock()
sm.root = mux
sm.m.Unlock()
}
func (sm *swappableMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
sm.get().ServeHTTP(w, r)
}
+81 -9
View File
@@ -1,6 +1,8 @@
package user
import (
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"
@@ -30,7 +32,7 @@ func TestSwappableMuxGetWaitsForTheMutexToBeReleased(t *testing.T) {
sm.m.Lock()
defer sm.m.Unlock()
c := make(chan mux.Router)
c := make(chan *mux.Router)
go func() { c <- sm.get() }()
time.Sleep(10 * time.Millisecond)
@@ -48,7 +50,7 @@ func TestSwappableMuxGetIsAbleToReadWhileOthersAreReading(t *testing.T) {
sm.m.RLock()
defer sm.m.RUnlock()
c := make(chan mux.Router)
c := make(chan *mux.Router)
go func() { c <- sm.get() }()
time.Sleep(10 * time.Millisecond)
@@ -62,7 +64,7 @@ func TestSwappableMuxGetIsAbleToReadWhileOthersAreReading(t *testing.T) {
func TestSwappableMuxSetSetsTheGivenMux(t *testing.T) {
sm := swappableMux{}
mux := mux.Router{
mux := &mux.Router{
KeepContext: true,
}
@@ -74,14 +76,14 @@ func TestSwappableMuxSetSetsTheGivenMux(t *testing.T) {
}
}
func TestSwappableMuxSetSetsADifferentInstance(t *testing.T) {
func TestSwappableMuxSetSetsTheSameInstance(t *testing.T) {
sm := swappableMux{}
mux := mux.Router{}
mux := &mux.Router{}
sm.set(mux)
if &mux == &sm.root {
t.Error("Set mux is the same instance")
if mux != sm.root {
t.Error("Set mux is not the same instance")
}
}
@@ -92,7 +94,7 @@ func TestSwappableMuxSetWaitsForWriterToReleaseMutex(t *testing.T) {
defer sm.m.Unlock()
c := make(chan bool)
go func() { sm.set(mux.Router{}); c <- true }()
go func() { sm.set(&mux.Router{}); c <- true }()
time.Sleep(10 * time.Millisecond)
@@ -110,7 +112,7 @@ func TestSwappableMuxSetWaitsForReadersToReleaseMutex(t *testing.T) {
defer sm.m.RUnlock()
c := make(chan bool)
go func() { sm.set(mux.Router{}); c <- true }()
go func() { sm.set(&mux.Router{}); c <- true }()
time.Sleep(10 * time.Millisecond)
@@ -120,3 +122,73 @@ func TestSwappableMuxSetWaitsForReadersToReleaseMutex(t *testing.T) {
default:
}
}
func TestServeHTTPCallsInnerMux(t *testing.T) {
called := false
mux := &mux.Router{}
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { called = true })
sm := swappableMux{root: mux}
req := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
sm.ServeHTTP(w, req)
if !called {
t.Error("Inner mux wasn't called")
}
}
// TODO: test that a read lock does not impede calling ServeHTTP
func TestServeHTTPCanServeWhenMuxIsReadLocked(t *testing.T) {
called := false
mux := &mux.Router{}
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { called = true })
sm := swappableMux{root: mux}
sm.m.RLock()
req := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
go sm.ServeHTTP(w, req)
time.Sleep(10 * time.Millisecond)
if !called {
t.Error("Inner mux not called while mutex is read locked")
}
}
func TestServeHTTPCallsInnerMuxAfterAcquiringLock(t *testing.T) {
called := false
mux := &mux.Router{}
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { called = true })
sm := swappableMux{root: mux}
sm.m.Lock()
req := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
go sm.ServeHTTP(w, req)
time.Sleep(10 * time.Millisecond)
if called {
t.Fatal("Mutex not acquired")
}
sm.m.Unlock()
time.Sleep(10 * time.Millisecond)
if !called {
t.Error("Inner mux wasn't called after mutex released")
}
}