| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. |
| 4 |
| 5 package mutexpool |
| 6 |
| 7 import ( |
| 8 "sync" |
| 9 "testing" |
| 10 |
| 11 . "github.com/smartystreets/goconvey/convey" |
| 12 ) |
| 13 |
| 14 func TestPool(t *testing.T) { |
| 15 t.Parallel() |
| 16 |
| 17 Convey(`A mutex pool`, t, func() { |
| 18 var pool P |
| 19 |
| 20 Convey(`Can handle multiple concurrent Mutex accesses, and will
clean up when finished.`, func() { |
| 21 const total = 1000 |
| 22 var value int |
| 23 var wg sync.WaitGroup |
| 24 |
| 25 wg.Add(total) |
| 26 for i := 0; i < total; i++ { |
| 27 go func() { |
| 28 pool.WithMutex("foo", func() { |
| 29 value++ |
| 30 }) |
| 31 wg.Done() |
| 32 }() |
| 33 } |
| 34 |
| 35 wg.Wait() |
| 36 So(value, ShouldEqual, total) |
| 37 So(pool.mutexes, ShouldHaveLength, 0) |
| 38 }) |
| 39 |
| 40 Convey(`Can handle multiple keys.`, func() { |
| 41 const total = 100 |
| 42 var recLock func(int) |
| 43 recLock = func(v int) { |
| 44 if v < total { |
| 45 pool.WithMutex(v, func() { |
| 46 So(pool.mutexes, ShouldHaveLengt
h, v+1) |
| 47 recLock(v + 1) |
| 48 }) |
| 49 } |
| 50 } |
| 51 recLock(0) |
| 52 So(pool.mutexes, ShouldHaveLength, 0) |
| 53 }) |
| 54 }) |
| 55 } |
| OLD | NEW |