14360: Avoid overreporting instances during Create/List race.
[arvados.git] / lib / dispatchcloud / worker / pool_test.go
index 7ab4e1b6c239fbfe4fbda10b82f4fb3c4c105d36..3867e2c63e4b41b752b96cdbdf1c9a6b86b94159 100644 (file)
@@ -19,6 +19,16 @@ const GiB arvados.ByteSize = 1 << 30
 
 var _ = check.Suite(&PoolSuite{})
 
+type lessChecker struct {
+       *check.CheckerInfo
+}
+
+func (*lessChecker) Check(params []interface{}, names []string) (result bool, error string) {
+       return params[0].(int) < params[1].(int), ""
+}
+
+var less = &lessChecker{&check.CheckerInfo{Name: "less", Params: []string{"obtained", "expected"}}}
+
 type PoolSuite struct{}
 
 func (suite *PoolSuite) SetUpSuite(c *check.C) {
@@ -58,20 +68,18 @@ func (suite *PoolSuite) TestCreateUnallocShutdown(c *check.C) {
        pool.Create(type2)
        c.Check(pool.Unallocated()[type1], check.Equals, 1)
        c.Check(pool.Unallocated()[type2], check.Equals, 2)
-       // Unblock the pending Create calls and (before calling Sync!)
-       // wait for the pool to process the returned instances.
+
+       // Unblock the pending Create calls.
        go lameInstanceSet.Release(3)
+
+       // Wait for each instance to either return from its Create
+       // call, or show up in a poll.
        suite.wait(c, pool, notify, func() bool {
-               list, err := lameInstanceSet.Instances(nil)
-               return err == nil && len(list) == 3
+               pool.mtx.RLock()
+               defer pool.mtx.RUnlock()
+               return len(pool.workers) == 3
        })
 
-       c.Check(pool.Unallocated()[type1], check.Equals, 1)
-       c.Check(pool.Unallocated()[type2], check.Equals, 2)
-       pool.getInstancesAndSync()
-       c.Check(pool.Unallocated()[type1], check.Equals, 1)
-       c.Check(pool.Unallocated()[type2], check.Equals, 2)
-
        c.Check(pool.Shutdown(type2), check.Equals, true)
        suite.wait(c, pool, notify, func() bool {
                return pool.Unallocated()[type1] == 1 && pool.Unallocated()[type2] == 1
@@ -123,3 +131,5 @@ func (*stubExecutor) SetTarget(cloud.ExecutorTarget) {}
 func (*stubExecutor) Execute(cmd string, stdin io.Reader) ([]byte, []byte, error) {
        return nil, nil, nil
 }
+
+func (*stubExecutor) Close() {}