supervisor_test.go 982 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package supervisor
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. func create() *Supervisor {
  7. now := time.Now()
  8. end := now.Add(time.Hour * 1)
  9. conf := &Config{
  10. On: true,
  11. Begin: now,
  12. End: end,
  13. }
  14. return New(conf)
  15. }
  16. func TestSupervisor(t *testing.T) {
  17. sv := create()
  18. in := sv.conf.Begin.Add(time.Second * 10)
  19. out := sv.conf.End.Add(time.Second * 10)
  20. if sv.forbid("GET", in) {
  21. t.Error("Request should never be blocked on GET method")
  22. }
  23. if !sv.forbid("POST", in) {
  24. t.Errorf("Request should be blocked on POST method at %+v", in)
  25. }
  26. if sv.forbid("POST", out) {
  27. t.Errorf("Request should not be blocked at %+v", out)
  28. }
  29. }
  30. func TestReload(t *testing.T) {
  31. zero := time.Unix(0, 0)
  32. conf := &Config{
  33. On: false,
  34. Begin: zero,
  35. End: zero,
  36. }
  37. sv := create()
  38. // reload with nil
  39. sv.Reload(nil)
  40. // reload with valid config
  41. sv.Reload(conf)
  42. if sv.conf != conf && sv.on == false {
  43. t.Errorf("Failed to reload config %+v, current config is %+v", conf, sv.conf)
  44. }
  45. }