github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/server/servemux_test.go (about) 1 // Copyright 2018 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package server 12 13 import ( 14 "fmt" 15 "net/http" 16 "net/http/httptest" 17 "net/url" 18 "sync" 19 "testing" 20 "time" 21 22 "github.com/cockroachdb/cockroach/pkg/util/leaktest" 23 "github.com/cockroachdb/cockroach/pkg/util/timeutil" 24 ) 25 26 func TestServeMuxConcurrency(t *testing.T) { 27 defer leaktest.AfterTest(t)() 28 29 const duration = 20 * time.Millisecond 30 start := timeutil.Now() 31 32 // TODO(peter): This test reliably fails using http.ServeMux with a 33 // "concurrent map read and write error" on go1.10. The bug in http.ServeMux 34 // is fixed in go1.11. 35 var mux safeServeMux 36 var wg sync.WaitGroup 37 wg.Add(2) 38 39 go func() { 40 defer wg.Done() 41 f := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}) 42 for i := 1; timeutil.Since(start) < duration; i++ { 43 mux.Handle(fmt.Sprintf("/%d", i), f) 44 } 45 }() 46 47 go func() { 48 defer wg.Done() 49 for i := 1; timeutil.Since(start) < duration; i++ { 50 r := &http.Request{ 51 Method: "GET", 52 URL: &url.URL{ 53 Path: "/", 54 }, 55 } 56 w := httptest.NewRecorder() 57 mux.ServeHTTP(w, r) 58 } 59 }() 60 61 wg.Wait() 62 }