/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.qpid.proton4j.amqp.driver.actions;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.apache.qpid.proton4j.amqp.driver.AMQPTestDriver;
import org.apache.qpid.proton4j.amqp.driver.ScriptedAction;

/**
 * Action that will signal when the script has completed all expectations and
 * scripted actions or when a configured timeout occurs.
 */
public class ScriptCompleteAction implements ScriptedAction {

    protected final AMQPTestDriver driver;
    protected final CountDownLatch complete = new CountDownLatch(1);

    public ScriptCompleteAction(AMQPTestDriver driver) {
        this.driver = driver;
    }

    @Override
    public ScriptCompleteAction now() {
        complete.countDown();
        return this;
    }

    @Override
    public ScriptCompleteAction queue() {
        driver.addScriptedElement(this);
        return this;
    }

    @Override
    public ScriptCompleteAction perform(AMQPTestDriver driver) {
        complete.countDown();
        return this;
    }

    public void await() throws InterruptedException {
        complete.await();
    }

    public void await(long timeout, TimeUnit units ) throws InterruptedException {
        complete.await(timeout, units);
    }
}

@@ -932,16 +932,62 @@
 imeUnit;
+%0Aimport java.util.concurrent.TimeoutException;
 %0A%0Aimport
@@ -2058,32 +2058,37 @@
 ption %7B%0A        
+if (!
 complete.await(t
@@ -2101,17 +2101,148 @@
 , units)
-;
+) %7B%0A            throw new AssertionError(%22Timed out waiting for scripted expectations to be met%22, new TimeoutException());%0A        %7D
 %0A    %7D%0A%7D

Throw an assertion once the script await completion times out