summaryrefslogtreecommitdiff
path: root/traceable/traceable_test.go
diff options
context:
space:
mode:
authorWill Hawkins <[email protected]>2022-07-01 14:46:43 -0400
committerWill Hawkins <[email protected]>2022-07-01 14:47:19 -0400
commit793a53ece7fd16cd52735c12f47584c438e1a58b (patch)
tree7ee40d62a5b8d530b5fe12ed0c695a39855e45c5 /traceable/traceable_test.go
parent4fd7d42026f85da367afaaafeefd217b983f71ca (diff)
[Feature] Support spec v2 2/n
Diffstat (limited to 'traceable/traceable_test.go')
-rw-r--r--traceable/traceable_test.go153
1 files changed, 153 insertions, 0 deletions
diff --git a/traceable/traceable_test.go b/traceable/traceable_test.go
new file mode 100644
index 0000000..e0fcb04
--- /dev/null
+++ b/traceable/traceable_test.go
@@ -0,0 +1,153 @@
+package traceable
+
+import (
+ "context"
+ "crypto/tls"
+ "io"
+ "io/ioutil"
+ "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(ioutil.Discard, get.Body)
+ get.Body.Close()
+ }()
+ go func() {
+ defer requestWg.Done()
+ get, err := client.Do(request_b)
+ if err != nil {
+ return
+ }
+ _, _ = io.Copy(ioutil.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,
+ )
+ }
+
+}