-
Notifications
You must be signed in to change notification settings - Fork 16
/
submit_test.go
89 lines (70 loc) · 1.82 KB
/
submit_test.go
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
// Copyright 2016 Circonus, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package circonusgometrics
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
apiclient "github.com/circonus-labs/go-apiclient"
)
func fakeBroker() *httptest.Server {
handler := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, `{"stats":1}`)
}
return httptest.NewServer(http.HandlerFunc(handler))
}
func TestSubmit(t *testing.T) {
t.Log("Testing submit.submit")
server := fakeBroker()
defer server.Close()
cfg := &Config{}
cfg.CheckManager.Check.SubmissionURL = server.URL
cm, err := NewCirconusMetrics(cfg)
if err != nil {
t.Fatalf("unexpected error (%s)", err)
}
newMetrics := make(map[string]*apiclient.CheckBundleMetric)
output := Metrics{"foo": Metric{Type: "n", Value: 1}}
// output["foo"] = map[string]interface{}{
// "_type": "n",
// "_value": 1,
// }
cm.submit(output, newMetrics)
}
func TestTrapCall(t *testing.T) {
t.Log("Testing submit.trapCall")
server := fakeBroker()
defer server.Close()
cfg := &Config{}
cfg.CheckManager.Check.SubmissionURL = server.URL
cm, err := NewCirconusMetrics(cfg)
if err != nil {
t.Fatalf("unexpected error (%s)", err)
}
for !cm.check.IsReady() {
t.Log("\twaiting for cm to init")
time.Sleep(1 * time.Second)
}
output := make(map[string]interface{})
output["foo"] = map[string]interface{}{
"_type": "n",
"_value": 1,
}
str, err := json.Marshal(output)
if err != nil {
t.Fatalf("unexpected error (%s)", err)
}
result, err := cm.trapCall(str)
if err != nil {
t.Fatalf("unexpected error (%s)", err)
}
if result.Stats != 1 {
t.Fatalf("Expected 1, got %#v", result)
}
}