github.com/s1s1ty/go@v0.0.0-20180207192209-104445e3140f/src/time/internal_test.go (about)

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