14360: Fix concurrent map access in tests.
authorTom Clegg <tclegg@veritasgenetics.com>
Tue, 30 Oct 2018 20:37:31 +0000 (16:37 -0400)
committerTom Clegg <tclegg@veritasgenetics.com>
Tue, 30 Oct 2018 20:37:31 +0000 (16:37 -0400)
Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tclegg@veritasgenetics.com>

lib/dispatchcloud/test/queue.go

index 0bad5a391e6f5e369a6c7f163135076b7c818aa9..152094f52ea3b355673077cdbf8888500b0b7111 100644 (file)
@@ -61,14 +61,20 @@ func (q *Queue) Forget(uuid string) {
 }
 
 func (q *Queue) Lock(uuid string) error {
+       q.mtx.Lock()
+       defer q.mtx.Unlock()
        return q.changeState(uuid, arvados.ContainerStateQueued, arvados.ContainerStateLocked)
 }
 
 func (q *Queue) Unlock(uuid string) error {
+       q.mtx.Lock()
+       defer q.mtx.Unlock()
        return q.changeState(uuid, arvados.ContainerStateLocked, arvados.ContainerStateQueued)
 }
 
 func (q *Queue) Cancel(uuid string) error {
+       q.mtx.Lock()
+       defer q.mtx.Unlock()
        return q.changeState(uuid, q.entries[uuid].Container.State, arvados.ContainerStateCancelled)
 }
 
@@ -98,9 +104,8 @@ func (q *Queue) notify() {
        }
 }
 
+// caller must have lock.
 func (q *Queue) changeState(uuid string, from, to arvados.ContainerState) error {
-       q.mtx.Lock()
-       defer q.mtx.Unlock()
        ent := q.entries[uuid]
        if ent.Container.State != from {
                return fmt.Errorf("lock failed: state=%q", ent.Container.State)