submit_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2016 Circonus, Inc. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package circonusgometrics
  5. import (
  6. "encoding/json"
  7. "fmt"
  8. "net/http"
  9. "net/http/httptest"
  10. "testing"
  11. "time"
  12. "github.com/circonus-labs/circonus-gometrics/api"
  13. )
  14. func fakeBroker() *httptest.Server {
  15. handler := func(w http.ResponseWriter, r *http.Request) {
  16. w.WriteHeader(200)
  17. w.Header().Set("Content-Type", "application/json")
  18. fmt.Fprintln(w, `{"stats":1}`)
  19. }
  20. return httptest.NewServer(http.HandlerFunc(handler))
  21. }
  22. func TestSubmit(t *testing.T) {
  23. t.Log("Testing submit.submit")
  24. server := fakeBroker()
  25. defer server.Close()
  26. cfg := &Config{}
  27. cfg.CheckManager.Check.SubmissionURL = server.URL
  28. cm, err := NewCirconusMetrics(cfg)
  29. if err != nil {
  30. t.Errorf("Expected no error, got '%v'", err)
  31. }
  32. newMetrics := make(map[string]*api.CheckBundleMetric)
  33. output := Metrics{"foo": Metric{Type: "n", Value: 1}}
  34. // output["foo"] = map[string]interface{}{
  35. // "_type": "n",
  36. // "_value": 1,
  37. // }
  38. cm.submit(output, newMetrics)
  39. }
  40. func TestTrapCall(t *testing.T) {
  41. t.Log("Testing submit.trapCall")
  42. server := fakeBroker()
  43. defer server.Close()
  44. cfg := &Config{}
  45. cfg.CheckManager.Check.SubmissionURL = server.URL
  46. cm, err := NewCirconusMetrics(cfg)
  47. if err != nil {
  48. t.Errorf("Expected no error, got '%v'", err)
  49. }
  50. for !cm.check.IsReady() {
  51. t.Log("\twaiting for cm to init")
  52. time.Sleep(1 * time.Second)
  53. }
  54. output := make(map[string]interface{})
  55. output["foo"] = map[string]interface{}{
  56. "_type": "n",
  57. "_value": 1,
  58. }
  59. str, err := json.Marshal(output)
  60. if err != nil {
  61. t.Errorf("Expected no error, got '%v'", err)
  62. }
  63. numStats, err := cm.trapCall(str)
  64. if err != nil {
  65. t.Errorf("Expected no error, got '%v'", err)
  66. }
  67. if numStats != 1 {
  68. t.Errorf("Expected 1, got %d", numStats)
  69. }
  70. }