File: http_test.go

package info (click to toggle)
golang-github-openzipkin-zipkin-go 0.1.5%2Bgit20190103.2fd7f4a-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 532 kB
  • sloc: makefile: 22
file content (77 lines) | stat: -rw-r--r-- 1,636 bytes parent folder | download | duplicates (2)
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
package http_test

import (
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"testing"

	"fmt"
	"time"

	"strings"

	"github.com/openzipkin/zipkin-go/idgenerator"
	"github.com/openzipkin/zipkin-go/model"
	zipkinhttp "github.com/openzipkin/zipkin-go/reporter/http"
)

func TestSpanIsBeingReported(t *testing.T) {
	idGen := idgenerator.NewRandom64()
	traceID := idGen.TraceID()

	nSpans := 2
	var aSpans []model.SpanModel
	var eSpans []string

	for i := 0; i < nSpans; i++ {
		span := model.SpanModel{
			SpanContext: model.SpanContext{
				TraceID: traceID,
				ID:      idGen.SpanID(traceID),
			},
			Name:      "name",
			Kind:      model.Client,
			Timestamp: time.Now(),
		}

		aSpans = append(aSpans, span)
		eSpans = append(
			eSpans,
			fmt.Sprintf(
				`{"timestamp":%d,"traceId":"%s","id":"%s","name":"%s","kind":"%s"}`,
				span.Timestamp.Round(time.Microsecond).UnixNano()/1e3,
				span.SpanContext.TraceID,
				span.SpanContext.ID,
				span.Name,
				span.Kind,
			),
		)
	}

	eSpansPayload := fmt.Sprintf("[%s]", strings.Join(eSpans, ","))

	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "POST" {
			t.Errorf("expected 'POST' request, got '%s'", r.Method)
		}

		aSpanPayload, err := ioutil.ReadAll(r.Body)
		if err != nil {
			t.Errorf("unexpected error: %s", err.Error())
		}

		if eSpansPayload != string(aSpanPayload) {
			t.Errorf("unexpected span payload \nwant %s, \nhave %s\n", eSpansPayload, string(aSpanPayload))
		}
	}))

	defer ts.Close()

	rep := zipkinhttp.NewReporter(ts.URL)
	defer rep.Close()

	for _, span := range aSpans {
		rep.Send(span)
	}
}