PageRenderTime 89ms CodeModel.GetById 23ms RepoModel.GetById 2ms app.codeStats 0ms

/metric/metric_test.go

https://gitlab.com/verminio/gofw
Go | 82 lines | 63 code | 19 blank | 0 comment | 25 complexity | ffc54e4b7a4b256d432e0cbc3e1c201b MD5 | raw file
  1. package metric
  2. import (
  3. "testing"
  4. "time"
  5. "gitlab.com/verminio/gofw/gen"
  6. )
  7. func TestNew(t *testing.T) {
  8. id := gen.RandomString(5)
  9. c := New(id)
  10. if c.TrackingID != id {
  11. t.Error("Collection created with wrong id, expected ", id, " got ", c.TrackingID)
  12. }
  13. if len(c.Metrics) != 0 {
  14. t.Error("Collection created with wrong number of metrics, expected 0, got ", len(c.Metrics))
  15. }
  16. }
  17. func TestStart(t *testing.T) {
  18. id := gen.RandomString(5)
  19. c := New(id)
  20. c.Start()
  21. if len(c.Metrics) != 1 {
  22. t.Error("Wrong number of metrics, expected 1, got ", len(c.Metrics))
  23. }
  24. m := c.Metrics[0]
  25. if m.TrackingID != id {
  26. t.Error("Wrong TrackingID, expected ", id, " got ", m.TrackingID)
  27. }
  28. if m.Type != "START" {
  29. t.Error("Wrong metric type, expected START, got ", m.Type)
  30. }
  31. if m.Function != "gitlab.com/verminio/gofw/metric.TestStart" {
  32. t.Error("Wrong metric function, expected gitlab.com/verminio/gofw/metric.TestStart, got ", m.Function)
  33. }
  34. if m.Sequence != 1 {
  35. t.Error("Wrong metric sequence number, expected 1, got ", m.Sequence)
  36. }
  37. }
  38. func TestEnd(t *testing.T) {
  39. id := gen.RandomString(5)
  40. c := New(id)
  41. c.Start()
  42. time.Sleep(1 * time.Second)
  43. c.End()
  44. if len(c.Metrics) != 2 {
  45. t.Error("Wrong number of metrics, expected 2, got ", len(c.Metrics))
  46. }
  47. m := c.Metrics[1]
  48. if m.TrackingID != id {
  49. t.Error("Wrong TrackingID, expected ", id, " got ", m.TrackingID)
  50. }
  51. if m.Type != "END" {
  52. t.Error("Wrong metric type, expected END, got ", m.Type)
  53. }
  54. if m.Function != "gitlab.com/verminio/gofw/metric.TestEnd" {
  55. t.Error("Wrong metric function, expected gitlab.com/verminio/gofw/metric.TestEnd, got ", m.Function)
  56. }
  57. if m.Sequence != 2 {
  58. t.Error("Wrong metric sequence number, expected 2, got ", m.Sequence)
  59. }
  60. if !(m.Timestamp > c.Metrics[0].Timestamp) {
  61. t.Error("End Timestamp expected to be after Start Timestamp.")
  62. }
  63. }