histogram_test.go 787 B

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright (c) 2017 Arista Networks, Inc.
  2. // Use of this source code is governed by the Apache License 2.0
  3. // that can be found in the COPYING file.
  4. package stats_test
  5. import (
  6. "encoding/json"
  7. "fmt"
  8. "testing"
  9. "notabug.org/themusicgod1/goarista/monitor/stats"
  10. )
  11. func testJSON(h *stats.Histogram) error {
  12. js, err := h.Value().MarshalJSON()
  13. if err != nil {
  14. return err
  15. }
  16. var v interface{}
  17. err = json.Unmarshal(js, &v)
  18. if err != nil {
  19. return fmt.Errorf("Failed to parse JSON: %s\nJSON was: %s", err, js)
  20. }
  21. return nil
  22. }
  23. // Ensure we can JSONify the histogram into valid JSON.
  24. func TestJSON(t *testing.T) {
  25. h := stats.NewHistogram(
  26. stats.HistogramOptions{NumBuckets: 10, GrowthFactor: 0,
  27. SmallestBucketSize: 20, MinValue: 0})
  28. testJSON(h)
  29. h.Add(42)
  30. testJSON(h)
  31. }