Left: | ||
Right: |
OLD | NEW |
---|---|
1 // Copyright 2013 The Go Authors. All rights reserved. | 1 // Copyright 2013 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Pool is no-op under race detector, so all these tests do not work. | 5 // Pool is no-op under race detector, so all these tests do not work. |
6 // +build !race | 6 // +build !race |
7 | 7 |
8 package sync_test | 8 package sync_test |
9 | 9 |
10 import ( | 10 import ( |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
90 time.Sleep(time.Duration(i*100+10) * time.Millisecond) | 90 time.Sleep(time.Duration(i*100+10) * time.Millisecond) |
91 // 1 pointer can remain on stack or elsewhere | 91 // 1 pointer can remain on stack or elsewhere |
92 if atomic.LoadUint32(&fin) >= N-1 { | 92 if atomic.LoadUint32(&fin) >= N-1 { |
93 return | 93 return |
94 } | 94 } |
95 } | 95 } |
96 t.Fatalf("only %v out of %v resources are finalized", | 96 t.Fatalf("only %v out of %v resources are finalized", |
97 atomic.LoadUint32(&fin), N) | 97 atomic.LoadUint32(&fin), N) |
98 } | 98 } |
99 | 99 |
100 // Test that Pool releases resources on GC. | |
101 func TestPoolRelease(t *testing.T) { | |
102 var p Pool | |
103 const N = 100 | |
104 loop: | |
105 for try := 0; try < 3; try++ { | |
106 var fin uint32 | |
107 for i := 0; i < N; i++ { | |
108 v := new(string) | |
109 runtime.SetFinalizer(v, func(vv *string) { | |
110 atomic.AddUint32(&fin, 1) | |
111 }) | |
112 p.Put(v) | |
113 } | |
114 for i := 0; i < 5; i++ { | |
115 runtime.GC() | |
116 time.Sleep(time.Duration(i*100+10) * time.Millisecond) | |
bradfitz
2014/10/22 10:36:54
how long does this test typically take? Skip it du
dvyukov
2014/10/22 13:05:48
0.03s
the test above this one does the same
| |
117 // 1 pointer can remain on stack or elsewhere | |
118 if atomic.LoadUint32(&fin) >= N-1 { | |
119 continue loop | |
120 } | |
121 } | |
122 t.Fatalf("only %v out of %v resources are finalized on try %v", | |
123 atomic.LoadUint32(&fin), N, try) | |
bradfitz
2014/10/22 10:36:54
this atomic.LoadUint32 may not necessarily match w
dvyukov
2014/10/22 13:05:49
Done.
| |
124 } | |
125 } | |
126 | |
100 func TestPoolStress(t *testing.T) { | 127 func TestPoolStress(t *testing.T) { |
101 const P = 10 | 128 const P = 10 |
102 N := int(1e6) | 129 N := int(1e6) |
103 if testing.Short() { | 130 if testing.Short() { |
104 N /= 100 | 131 N /= 100 |
105 } | 132 } |
106 var p Pool | 133 var p Pool |
107 done := make(chan bool) | 134 done := make(chan bool) |
108 for i := 0; i < P; i++ { | 135 for i := 0; i < P; i++ { |
109 go func() { | 136 go func() { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
142 for pb.Next() { | 169 for pb.Next() { |
143 for b := 0; b < 100; b++ { | 170 for b := 0; b < 100; b++ { |
144 p.Put(1) | 171 p.Put(1) |
145 } | 172 } |
146 for b := 0; b < 100; b++ { | 173 for b := 0; b < 100; b++ { |
147 p.Get() | 174 p.Get() |
148 } | 175 } |
149 } | 176 } |
150 }) | 177 }) |
151 } | 178 } |
OLD | NEW |