浏览代码

lib/sync: Skip the timing tests if the host timer is flaky

Jakob Borg 9 年之前
父节点
当前提交
d91e6023eb
共有 1 个文件被更改,包括 32 次插入0 次删除
  1. 32 0
      lib/sync/sync_test.go

+ 32 - 0
lib/sync/sync_test.go

@@ -21,6 +21,23 @@ const (
 	longWait     = 125 * time.Millisecond
 )
 
+var skipTimingTests = false
+
+func init() {
+	// Check a few times that a short sleep does not in fact overrun the log
+	// threshold. If it does, the timer accuracy is crap or the host is
+	// overloaded and we can't reliably run the tests in here. In the normal
+	// case this takes just 25*5 = 125 ms.
+	for i := 0; i < 25; i++ {
+		t0 := time.Now()
+		time.Sleep(shortWait)
+		if time.Since(t0) > logThreshold {
+			skipTimingTests = true
+			return
+		}
+	}
+}
+
 func TestTypes(t *testing.T) {
 	debug = false
 	l.SetDebug("sync", false)
@@ -57,6 +74,11 @@ func TestTypes(t *testing.T) {
 }
 
 func TestMutex(t *testing.T) {
+	if skipTimingTests {
+		t.Skip("insufficient timer accuracy")
+		return
+	}
+
 	debug = true
 	l.SetDebug("sync", true)
 	threshold = logThreshold
@@ -92,6 +114,11 @@ func TestMutex(t *testing.T) {
 }
 
 func TestRWMutex(t *testing.T) {
+	if skipTimingTests {
+		t.Skip("insufficient timer accuracy")
+		return
+	}
+
 	debug = true
 	l.SetDebug("sync", true)
 	threshold = logThreshold
@@ -152,6 +179,11 @@ func TestRWMutex(t *testing.T) {
 }
 
 func TestWaitGroup(t *testing.T) {
+	if skipTimingTests {
+		t.Skip("insufficient timer accuracy")
+		return
+	}
+
 	debug = true
 	l.SetDebug("sync", true)
 	threshold = logThreshold