internal_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2011 The Go Authors. 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 time
  5. func init() {
  6. // force US/Pacific for time zone tests
  7. ForceUSPacificForTesting()
  8. }
  9. var Interrupt = interrupt
  10. var DaysIn = daysIn
  11. func empty(arg interface{}, seq uintptr) {}
  12. // Test that a runtimeTimer with a duration so large it overflows
  13. // does not cause other timers to hang.
  14. //
  15. // This test has to be in internal_test.go since it fiddles with
  16. // unexported data structures.
  17. func CheckRuntimeTimerOverflow() {
  18. // We manually create a runtimeTimer to bypass the overflow
  19. // detection logic in NewTimer: we're testing the underlying
  20. // runtime.addtimer function.
  21. r := &runtimeTimer{
  22. when: runtimeNano() + (1<<63 - 1),
  23. f: empty,
  24. arg: nil,
  25. }
  26. startTimer(r)
  27. // Start a goroutine that should send on t.C right away.
  28. t := NewTimer(1)
  29. defer func() {
  30. // Subsequent tests won't work correctly if we don't stop the
  31. // overflow timer and kick the timer proc back into service.
  32. //
  33. // The timer proc is now sleeping and can only be awoken by
  34. // adding a timer to the *beginning* of the heap. We can't
  35. // wake it up by calling NewTimer since other tests may have
  36. // left timers running that should have expired before ours.
  37. // Instead we zero the overflow timer duration and start it
  38. // once more.
  39. stopTimer(r)
  40. t.Stop()
  41. r.when = 0
  42. startTimer(r)
  43. }()
  44. // If the test fails, we will hang here until the timeout in the testing package
  45. // fires, which is 10 minutes. It would be nice to catch the problem sooner,
  46. // but there is no reliable way to guarantee that timerproc schedules without
  47. // doing something involving timerproc itself. Previous failed attempts have
  48. // tried calling runtime.Gosched and runtime.GC, but neither is reliable.
  49. // So we fall back to hope: We hope we don't hang here.
  50. <-t.C
  51. }