1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
/*
* This file is part of Go Responsiveness.
*
* Go Responsiveness is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software Foundation,
* either version 2 of the License, or (at your option) any later version.
* Go Responsiveness is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with Go Responsiveness. If not, see <https://www.gnu.org/licenses/>.
*/
package traceable
import (
"context"
"crypto/tls"
"io"
"net/http"
"net/http/httptrace"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/network-quality/goresponsiveness/debug"
)
type CountingTraceable struct {
Counter *uint64
}
func (lgd *CountingTraceable) SetDnsStartTimeInfo(
now time.Time,
dnsStartInfo httptrace.DNSStartInfo,
) {
}
func (lgd *CountingTraceable) SetDnsDoneTimeInfo(
now time.Time,
dnsDoneInfo httptrace.DNSDoneInfo,
) {
}
func (lgd *CountingTraceable) SetConnectStartTime(
now time.Time,
) {
}
func (lgd *CountingTraceable) SetConnectDoneTimeError(
now time.Time,
err error,
) {
}
func (lgd *CountingTraceable) SetGetConnTime(now time.Time) {
}
func (lgd *CountingTraceable) SetGotConnTimeInfo(
now time.Time,
gotConnInfo httptrace.GotConnInfo,
) {
atomic.AddUint64(lgd.Counter, 1)
}
func (lgd *CountingTraceable) SetTLSHandshakeStartTime(
now time.Time,
) {
}
func (lgd *CountingTraceable) SetTLSHandshakeDoneTimeState(
now time.Time,
connectionState tls.ConnectionState,
) {
}
func (lgd *CountingTraceable) SetHttpWroteRequestTimeInfo(
now time.Time,
info httptrace.WroteRequestInfo,
) {
}
func (lgd *CountingTraceable) SetHttpResponseReadyTime(
now time.Time,
) {
}
// Ensure that two different http client request tracers started with the same context
// do not compose and receive each other's callbacks.
func TestDuplicativeTraceables(t *testing.T) {
singleCtx, singleCtxCancel_ := context.WithCancel(context.Background())
defer singleCtxCancel_()
client := http.Client{}
counterA := new(uint64)
countingTracerA := CountingTraceable{Counter: counterA}
counterB := new(uint64)
countingTracerB := CountingTraceable{Counter: counterB}
debugging := debug.NewDebugWithPrefix(debug.Debug, "TestDuplicativeTraceables")
request_a, err := http.NewRequestWithContext(
httptrace.WithClientTrace(
singleCtx,
GenerateHttpTimingTracer(&countingTracerA, debugging.Level),
),
"GET",
"https://www.google.com/",
nil,
)
if err != nil {
t.Fatalf("Failed to create request A to GET google.com")
}
request_b, err := http.NewRequestWithContext(
httptrace.WithClientTrace(
singleCtx,
GenerateHttpTimingTracer(&countingTracerB, debugging.Level),
),
"GET",
"https://www.google.com/",
nil,
)
if err != nil {
t.Fatalf("Failed to create request B to GET google.com")
}
requestWg := new(sync.WaitGroup)
requestWg.Add(2)
go func() {
defer requestWg.Done()
get, err := client.Do(request_a)
if err != nil {
return
}
_, _ = io.Copy(io.Discard, get.Body)
get.Body.Close()
}()
go func() {
defer requestWg.Done()
get, err := client.Do(request_b)
if err != nil {
return
}
_, _ = io.Copy(io.Discard, get.Body)
get.Body.Close()
}()
requestWg.Wait()
if !(*counterA == 1 && *counterB == 1) {
t.Fatalf(
"Two separate tracers received overlapping callbacks on each other's http requests: (counterA: %d, counterB: %d)",
*counterA,
*counterB,
)
}
}
|