Archives par mot-clé : asynchrone

Attendre la fin d’un signal Qt de manière synchrone

Publié dans Snippet | Laisser un commentaire

Ou comment attendre la fin d’une méthode asynchrone.

Snippet

/**
 * Wait synchronously for a Qt signal (+ timeout management)
 * May be useful for test purpose
 * Not recommended for production usage
 */

// -- Configure the waiting
// Signal to wait
QEventLoop loop;
QObject::connect(&anObject, SIGNAL(signalToWait()), &loop, SLOT(quit()));
// Timeout to avoid infite waiting
QTimer timer;
timer.setInterval(10*1000); //10s
timer.setSingleShot(true);
QObject::connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));

// -- Do stuff that should trigger the signal
// ...

// -- Start to wait
// What is done after "loop.exec()" is not executed until "loop.quit()" is called
timer.start();
loop.exec();
timer.stop(); // Just in case there is no timeout

// -- Other stuff
// ...

jUnit et les tests asynchrones

Publié dans Snippet | Laisser un commentaire

Où comment tester des méthodes asynchrones (avec listaner ou callback) en Java, par exemple avec jUnit. En fait c’est un moyen rapide et relativement sûr (quoique non conseillé pour du vrai code) de rendre une méthode synchrone.

Snippet

public class AsynchronousTest
{
    /**
    * Asynchronous helper
    */
    private CountDownLatch lock = new CountDownLatch(1);
    /**
    * Received data from the asynchronous call
    */
    private Object receiveddata;

    @Test
    public void testAsynchronousCall() throws Exception {
        Object parameters = null;
        myAsynchronousCall(parameters, new MyCallback() {
                @Override
                public void onSuccess(Object data) {
                    receiveddata = data;
                    lock.countDown();
                }
            });

        boolean releaseBeforeTimeout = lock.await(2000, TimeUnit.MILLISECONDS);
        // Check timeout
        if (!releaseBeforeTimeout) {
            // Do stuff
            fail("Timeout");
        }

        assertNotNull(receiveddata);
        // Other tests
    }
}

Explications

L’idée de base est la suivante :

  • On initialise un CountDownLatch à 1, c’est-à-dire à un seul usage. Chaque appel à CountDownLatch::countDow() représente une utilisation.
  • On exécute la méthode asynchrone. La fin de cette méthode appelle CountDownLatch::countDow(). Mais avant d’en arriver là, pendant que la méthode s’exécute…
  • On regarde si le CountDownLatch est à 0 (dans le cas où la méthode asynchrone a déjà terminé) ou on attend qu’il passe à 0 (c’est-à-dire que la méthode asynchrone s’est terminée), ou on attend jusqu’à la fin d’un timeout (temps défini après lequel on considère que l’exécution de la méthode a échoué.