forked from juju/utils
-
Notifications
You must be signed in to change notification settings - Fork 1
/
limiter_test.go
81 lines (70 loc) · 2.07 KB
/
limiter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2011, 2012, 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package utils_test
import (
"fmt"
"time"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
gc "launchpad.net/gocheck"
"github.com/juju/utils"
)
const longWait = 10 * time.Second
type limiterSuite struct {
testing.IsolationSuite
}
var _ = gc.Suite(&limiterSuite{})
func (*limiterSuite) TestAcquireUntilFull(c *gc.C) {
l := utils.NewLimiter(2)
c.Check(l.Acquire(), jc.IsTrue)
c.Check(l.Acquire(), jc.IsTrue)
c.Check(l.Acquire(), jc.IsFalse)
}
func (*limiterSuite) TestBadRelease(c *gc.C) {
l := utils.NewLimiter(2)
c.Check(l.Release(), gc.ErrorMatches, "Release without an associated Acquire")
}
func (*limiterSuite) TestAcquireAndRelease(c *gc.C) {
l := utils.NewLimiter(2)
c.Check(l.Acquire(), jc.IsTrue)
c.Check(l.Acquire(), jc.IsTrue)
c.Check(l.Acquire(), jc.IsFalse)
c.Check(l.Release(), gc.IsNil)
c.Check(l.Acquire(), jc.IsTrue)
c.Check(l.Release(), gc.IsNil)
c.Check(l.Release(), gc.IsNil)
c.Check(l.Release(), gc.ErrorMatches, "Release without an associated Acquire")
}
func (*limiterSuite) TestAcquireWaitBlocksUntilRelease(c *gc.C) {
l := utils.NewLimiter(2)
calls := make([]string, 0, 10)
start := make(chan bool, 0)
waiting := make(chan bool, 0)
done := make(chan bool, 0)
go func() {
<-start
calls = append(calls, fmt.Sprintf("%v", l.Acquire()))
calls = append(calls, fmt.Sprintf("%v", l.Acquire()))
calls = append(calls, fmt.Sprintf("%v", l.Acquire()))
waiting <- true
l.AcquireWait()
calls = append(calls, "waited")
calls = append(calls, fmt.Sprintf("%v", l.Acquire()))
done <- true
}()
// Start the routine, and wait for it to get to the first checkpoint
start <- true
select {
case <-waiting:
case <-time.After(longWait):
c.Fatalf("timed out waiting for 'waiting' to trigger")
}
c.Check(l.Acquire(), jc.IsFalse)
l.Release()
select {
case <-done:
case <-time.After(longWait):
c.Fatalf("timed out waiting for 'done' to trigger")
}
c.Check(calls, gc.DeepEquals, []string{"true", "true", "false", "waited", "false"})
}