Description:
worker/instancepoller: TestBatching less race-y
The existing TestBatching test was very prone to
failure due to the nature of the test. The test
ensures that requests to Instances are batched
using a token bucket.
Whereas the existing test streamed requests, the
new implementation compresses the addition of
requests into the point where we service an
Instances call. We can thus reliably determine
the number of batches that will be made, as calls
to Instances must complete before additional
requests are gathered.
Reviewers: mp+220014_ code.launchpad. net,
Message:
Please take a look.
Description: instancepoller: TestBatching less race-y
worker/
The existing TestBatching test was very prone to
failure due to the nature of the test. The test
ensures that requests to Instances are batched
using a token bucket.
Whereas the existing test streamed requests, the
new implementation compresses the addition of
requests into the point where we service an
Instances call. We can thus reliably determine
the number of batches that will be made, as calls
to Instances must complete before additional
requests are gathered.
Fixes lp:1319652
https:/ /code.launchpad .net/~axwalk/ juju-core/ lp1319652- instancepoller- testbatching/ +merge/ 220014
(do not edit description out of merge proposal)
Please review this at https:/ /codereview. appspot. com/96440043/
Affected files (+48, -24 lines): instancepoller/ aggregate_ test.go
A [revision details]
M worker/
Index: [revision details] 20140519073812- 9ribeb9ud0q2glc o
=== added file '[revision details]'
--- [revision details] 2012-01-01 00:00:00 +0000
+++ [revision details] 2012-01-01 00:00:00 +0000
@@ -0,0 +1,2 @@
+Old revision: tarmac-
+New revision: <email address hidden>
Index: worker/ instancepoller/ aggregate_ test.go instancepoller/ aggregate_ test.go' instancepoller/ aggregate_ test.go 2014-05-13 06:12:51 +0000 instancepoller/ aggregate_ test.go 2014-05-19 11:11:39 +0000 len(testGetter. ids), gc.DeepEquals, 2)
=== modified file 'worker/
--- worker/
+++ worker/
@@ -116,32 +116,54 @@
c.Assert(
}
+type batchingInstanc eGetter struct { ceGetter) Instances(ids []instance.Id) Instance, error) { etter.Instances (ids) ceGetter) startRequests() { ceGetter) startRequest() { instanceInfo( "foo") (&gatherTime, 10*time. Millisecond) eGetter) testGetter) testGetter. results, ("foobar" , []string{ "127.0. 0.1", "192.168.1.1"})) instanceInfo( "foo") time.Millisecon d) ).Sub(startTime ) testGetter. counter, jc.LessThan, expectedMax+1) testGetter. counter, jc.GreaterThan, 10) eGetter aggregator = newAggregator( &testGetter) instance. Instance, 100) results[ i] = newTestInstance ("foobar" , "127.0. 0.1", "192.168.1.1"}) batchSize = 10 wg.Add( len(testGetter. results) ) startRequest( ) wg.Wait( ) testGetter. counter, gc.Equals, testGetter. results) /testGetter. batchSize) +1)
+ testInstanceGetter
+ wg sync.WaitGroup
+ aggregator *aggregator
+ batchSize int
+ started int
+}
+
+func (g *batchingInstan
([]instance.
+ insts, err := g.testInstanceG
+ g.startRequests()
+ return insts, err
+}
+
+func (g *batchingInstan
+ n := len(g.results) - g.started
+ if n > g.batchSize {
+ n = g.batchSize
+ }
+ for i := 0; i < n; i++ {
+ g.startRequest()
+ }
+}
+
+func (g *batchingInstan
+ g.started++
+ go func() {
+ _, err := g.aggregator.
+ if err != nil {
+ panic(err)
+ }
+ g.wg.Done()
+ }()
+}
+
func (s *aggregateSuite) TestBatching(c *gc.C) {
s.PatchValue
- testGetter := new(testInstanc
-
- aggregator := newAggregator(
- for i := 0; i < 100; i++ {
- testGetter.results = append(
newTestInstance
- }
- var wg sync.WaitGroup
- makeRequest := func() {
- _, err := aggregator.
- c.Check(err, gc.IsNil)
- wg.Done()
- }
- startTime := time.Now()
- wg.Add(100)
- for i := 0; i < 100; i++ {
- go makeRequest()
- time.Sleep(
- }
- wg.Wait()
- totalTime := time.Now(
- // +1 because we expect one extra call for the first request
- expectedMax := int32((totalTime / (10 * time.Millisecond)) + 1)
- c.Assert(
- c.Assert(
+ var testGetter batchingInstanc
+ testGetter.
+ testGetter.results = make([]
+ for i := range testGetter.results {
+ testGetter.
[]string{
+ }
+ testGetter.
+ testGetter.
+ testGetter.
+ testGetter.
+ c.Assert(
int32(len(
}
func (s *aggregateSuite) TestError(c *gc.C) {