aboutsummaryrefslogtreecommitdiff
path: root/middleware/proxy/proxy_test.go
diff options
context:
space:
mode:
authorGravatar Miek Gieben <miek@miek.nl> 2017-04-26 10:58:14 +0100
committerGravatar GitHub <noreply@github.com> 2017-04-26 10:58:14 +0100
commit3b5b6a233fc669dfb47a6dc4eb8899c3e992cc32 (patch)
tree5db5d0834e2d3c6143387c3c96fe904782c1f5d9 /middleware/proxy/proxy_test.go
parent003b1bf678f6fc1d551fed5184adccde2137e86f (diff)
downloadcoredns-3b5b6a233fc669dfb47a6dc4eb8899c3e992cc32.tar.gz
coredns-3b5b6a233fc669dfb47a6dc4eb8899c3e992cc32.tar.zst
coredns-3b5b6a233fc669dfb47a6dc4eb8899c3e992cc32.zip
middleware/proxy: Kill goroutines on stop (#646)
* middleware/proxy: Kill goroutines on stop Ports caddy's https://github.com/mholt/caddy/commit/59bf71c2932c3b814a6a1211c492a1aa9f71d4a1 Excludes the proxy_test.go test part though. Fixes #644 * Add tests
Diffstat (limited to 'middleware/proxy/proxy_test.go')
-rw-r--r--middleware/proxy/proxy_test.go85
1 files changed, 85 insertions, 0 deletions
diff --git a/middleware/proxy/proxy_test.go b/middleware/proxy/proxy_test.go
new file mode 100644
index 000000000..0a44d1b82
--- /dev/null
+++ b/middleware/proxy/proxy_test.go
@@ -0,0 +1,85 @@
+package proxy
+
+import (
+ "fmt"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "sync/atomic"
+ "testing"
+ "time"
+
+ "github.com/mholt/caddy/caddyfile"
+)
+
+func TestStop(t *testing.T) {
+ config := "proxy . %s {\n health_check /healthcheck:%s %dms \n}"
+ tests := []struct {
+ name string
+ intervalInMilliseconds int
+ numHealthcheckIntervals int
+ }{
+ {
+ "No Healthchecks After Stop - 5ms, 1 intervals",
+ 5,
+ 1,
+ },
+ {
+ "No Healthchecks After Stop - 5ms, 2 intervals",
+ 5,
+ 2,
+ },
+ {
+ "No Healthchecks After Stop - 5ms, 3 intervals",
+ 5,
+ 3,
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+
+ // Set up proxy.
+ var counter int64
+ backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ r.Body.Close()
+ atomic.AddInt64(&counter, 1)
+ }))
+
+ defer backend.Close()
+
+ port := backend.URL[17:] // Remove all crap up to the port
+ back := backend.URL[7:] // Remove http://
+ c := caddyfile.NewDispenser("Testfile", strings.NewReader(fmt.Sprintf(config, back, port, test.intervalInMilliseconds)))
+ upstreams, err := NewStaticUpstreams(&c)
+ if err != nil {
+ t.Error("Expected no error. Got:", err.Error())
+ }
+
+ // Give some time for healthchecks to hit the server.
+ time.Sleep(time.Duration(test.intervalInMilliseconds*test.numHealthcheckIntervals) * time.Millisecond)
+
+ for _, upstream := range upstreams {
+ if err := upstream.Stop(); err != nil {
+ t.Error("Expected no error stopping upstream. Got: ", err.Error())
+ }
+ }
+
+ counterValueAfterShutdown := atomic.LoadInt64(&counter)
+
+ // Give some time to see if healthchecks are still hitting the server.
+ time.Sleep(time.Duration(test.intervalInMilliseconds*test.numHealthcheckIntervals) * time.Millisecond)
+
+ if counterValueAfterShutdown == 0 {
+ t.Error("Expected healthchecks to hit test server. Got no healthchecks.")
+ }
+
+ counterValueAfterWaiting := atomic.LoadInt64(&counter)
+ if counterValueAfterWaiting != counterValueAfterShutdown {
+ t.Errorf("Expected no more healthchecks after shutdown. Got: %d healthchecks after shutdown", counterValueAfterWaiting-counterValueAfterShutdown)
+ }
+
+ })
+
+ }
+}