prom_test.go 951 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. "time"
  7. )
  8. func TestNewInterval(t *testing.T) {
  9. name := "counter_1"
  10. NewInterval(&IntervalConfig{
  11. Name: name,
  12. Rate: 1000,
  13. })
  14. defer func() {
  15. expectErrStr := fmt.Sprintf("%s already exists", name)
  16. errStr := recover()
  17. if errStr != expectErrStr {
  18. t.Errorf("must painc when name is not unique, expcted %s but got %v", expectErrStr, errStr)
  19. t.FailNow()
  20. }
  21. }()
  22. NewInterval(&IntervalConfig{
  23. Name: name,
  24. Rate: 1000,
  25. })
  26. }
  27. func TestInterval_Prom(t *testing.T) {
  28. name := "counter_1"
  29. itv := NewInterval(&IntervalConfig{
  30. Name: name,
  31. Rate: 1000,
  32. })
  33. count := 10
  34. mtStr := time.Now().Format(_timeFormat)
  35. for i := 0; i < count; i++ {
  36. itv.Prom(context.TODO(), itv.MTS(context.TODO(), mtStr))
  37. }
  38. if itv.counter != int64(count) {
  39. t.Errorf("interval counter of %s should be %d after get mts for %d times, but now it is %d", name, count, count, itv.counter)
  40. t.FailNow()
  41. }
  42. }