File tree 2 files changed +21
-0
lines changed
2 files changed +21
-0
lines changed Original file line number Diff line number Diff line change
1
+ package httpmock_test
2
+
3
+ import (
4
+ "net/http"
5
+ "testing"
6
+
7
+ . "github.com/jarcoal/httpmock"
8
+ )
9
+
10
+ func TestActivateNonDefaultRace (t * testing.T ) {
11
+ for i := 0 ; i < 10 ; i ++ {
12
+ go ActivateNonDefault (& http.Client {})
13
+ }
14
+ }
Original file line number Diff line number Diff line change @@ -777,6 +777,9 @@ var InitialTransport = http.DefaultTransport
777
777
// than http.DefaultClient).
778
778
var oldClients = map [* http.Client ]http.RoundTripper {}
779
779
780
+ // oldClientsLock protects oldClients from concurrent writes.
781
+ var oldClientsLock sync.Mutex
782
+
780
783
// Activate starts the mock environment. This should be called before
781
784
// your tests run. Under the hood this replaces the Transport on the
782
785
// http.DefaultClient with httpmock.DefaultTransport.
@@ -826,6 +829,8 @@ func ActivateNonDefault(client *http.Client) {
826
829
}
827
830
828
831
// save the custom client & it's RoundTripper
832
+ oldClientsLock .Lock ()
833
+ defer oldClientsLock .Unlock ()
829
834
if _ , ok := oldClients [client ]; ! ok {
830
835
oldClients [client ] = client .Transport
831
836
}
@@ -887,6 +892,8 @@ func Deactivate() {
887
892
http .DefaultTransport = InitialTransport
888
893
889
894
// reset the custom clients to use their original RoundTripper
895
+ oldClientsLock .Lock ()
896
+ defer oldClientsLock .Unlock ()
890
897
for oldClient , oldTransport := range oldClients {
891
898
oldClient .Transport = oldTransport
892
899
delete (oldClients , oldClient )
You can’t perform that action at this time.
0 commit comments