]> Cypherpunks repositories - gostls13.git/commitdiff
sync: deflake TestPool and TestPoolNew
authorAliaksandr Valialkin <valyala@gmail.com>
Thu, 25 May 2017 22:14:30 +0000 (01:14 +0300)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 28 Jun 2017 22:02:07 +0000 (22:02 +0000)
Prevent possible goroutine rescheduling to another P between
Put and Get calls by locking the goroutine to OS thread.

Inspired by the CL 42770.

Fixes #20198.

Change-Id: I18e24fcad1630658713e6b9d80d90d7941f604be
Reviewed-on: https://go-review.googlesource.com/44310
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Austin Clements <austin@google.com>
src/sync/export_test.go
src/sync/pool_test.go

index 6ed38dad89d19ee485910b4b769fb75ec5b16ccc..669076efad3f12c5f27c8d408da923c58099eb28 100644 (file)
@@ -7,3 +7,5 @@ package sync
 // Export for testing.
 var Runtime_Semacquire = runtime_Semacquire
 var Runtime_Semrelease = runtime_Semrelease
+var Runtime_procPin = runtime_procPin
+var Runtime_procUnpin = runtime_procUnpin
index 5a38cbfcb640f88171c3713f5bc431e693324754..9e5132bb18e168dfdb4dee96238f6150b5178202 100644 (file)
@@ -23,6 +23,10 @@ func TestPool(t *testing.T) {
        if p.Get() != nil {
                t.Fatal("expected empty")
        }
+
+       // Make sure that the goroutine doesn't migrate to another P
+       // between Put and Get calls.
+       Runtime_procPin()
        p.Put("a")
        p.Put("b")
        if g := p.Get(); g != "a" {
@@ -34,6 +38,7 @@ func TestPool(t *testing.T) {
        if g := p.Get(); g != nil {
                t.Fatalf("got %#v; want nil", g)
        }
+       Runtime_procUnpin()
 
        p.Put("c")
        debug.SetGCPercent(100) // to allow following GC to actually run
@@ -60,10 +65,16 @@ func TestPoolNew(t *testing.T) {
        if v := p.Get(); v != 2 {
                t.Fatalf("got %v; want 2", v)
        }
+
+       // Make sure that the goroutine doesn't migrate to another P
+       // between Put and Get calls.
+       Runtime_procPin()
        p.Put(42)
        if v := p.Get(); v != 42 {
                t.Fatalf("got %v; want 42", v)
        }
+       Runtime_procUnpin()
+
        if v := p.Get(); v != 3 {
                t.Fatalf("got %v; want 3", v)
        }