Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(264)

Unified Diff: common/sync/mutexpool/pool_test.go

Issue 2640323003: common/sync/mutexpool: Add mutexpool. (Closed)
Patch Set: Comments. Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « common/sync/mutexpool/pool.go ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: common/sync/mutexpool/pool_test.go
diff --git a/common/sync/mutexpool/pool_test.go b/common/sync/mutexpool/pool_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..8b3a638e5a2031b40505da9ce0d5940367dc3138
--- /dev/null
+++ b/common/sync/mutexpool/pool_test.go
@@ -0,0 +1,55 @@
+// Copyright 2017 The LUCI Authors. All rights reserved.
+// Use of this source code is governed under the Apache License, Version 2.0
+// that can be found in the LICENSE file.
+
+package mutexpool
+
+import (
+ "sync"
+ "testing"
+
+ . "github.com/smartystreets/goconvey/convey"
+)
+
+func TestPool(t *testing.T) {
+ t.Parallel()
+
+ Convey(`A mutex pool`, t, func() {
+ var pool P
+
+ Convey(`Can handle multiple concurrent Mutex accesses, and will clean up when finished.`, func() {
+ const total = 1000
+ var value int
+ var wg sync.WaitGroup
+
+ wg.Add(total)
+ for i := 0; i < total; i++ {
+ go func() {
+ pool.WithMutex("foo", func() {
+ value++
+ })
+ wg.Done()
+ }()
+ }
+
+ wg.Wait()
+ So(value, ShouldEqual, total)
+ So(pool.mutexes, ShouldHaveLength, 0)
+ })
+
+ Convey(`Can handle multiple keys.`, func() {
+ const total = 100
+ var recLock func(int)
+ recLock = func(v int) {
+ if v < total {
+ pool.WithMutex(v, func() {
+ So(pool.mutexes, ShouldHaveLength, v+1)
+ recLock(v + 1)
+ })
+ }
+ }
+ recLock(0)
+ So(pool.mutexes, ShouldHaveLength, 0)
+ })
+ })
+}
« no previous file with comments | « common/sync/mutexpool/pool.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698