Search in sources :

Example 1 with DebugBot

use of won.bot.impl.DebugBot in project webofneeds by researchstudio-sat.

the class BotTests method testIsShutdownMultiThreaded.

/**
 * Makes sure that the shutdown cannot be entered by more than one thread.
 */
@Test
public void testIsShutdownMultiThreaded() throws BrokenBarrierException, InterruptedException {
    // set of threads that managed to enter the shutdown method
    final Set<Thread> threadsInShutdown = Collections.synchronizedSet(new HashSet<Thread>());
    // bot impl that remembers which thread entered the shutdown method
    final Bot bot = new DebugBot() {

        @Override
        protected void doShutdown() {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            threadsInShutdown.add(Thread.currentThread());
        }
    };
    // start 10 threads that shut down the bot
    int numThreads = 10;
    final Random rnd = new Random(System.currentTimeMillis());
    final CyclicBarrier barrier = new CyclicBarrier(numThreads + 1);
    for (int i = 0; i < numThreads; i++) {
        Thread thread = new Thread() {

            @Override
            public void run() {
                try {
                    Thread.sleep(rnd.nextInt(100));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    bot.shutdown();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    barrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }
        };
        thread.start();
    }
    barrier.await();
    // make sure the bot is shutdownd
    Assert.assertTrue(bot.getLifecyclePhase().isDown());
    // make sure it was shutdownd only once
    Assert.assertTrue(threadsInShutdown.size() == 1);
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) DebugBot(won.bot.impl.DebugBot) DebugBot(won.bot.impl.DebugBot) Bot(won.bot.framework.bot.Bot) BaseBot(won.bot.framework.bot.base.BaseBot) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 2 with DebugBot

use of won.bot.impl.DebugBot in project webofneeds by researchstudio-sat.

the class BotTests method testIsInitializedMultiThreaded.

/**
 * Makes sure that the initialize cannot be entered by more than one thread.
 */
@Test
public void testIsInitializedMultiThreaded() throws BrokenBarrierException, InterruptedException {
    // set of threads that managed to enter the initialize method
    final Set<Thread> threadsInInit = Collections.synchronizedSet(new HashSet<Thread>());
    // bot impl that remembers which thread entered the initialize method
    final Bot bot = new DebugBot() {

        @Override
        protected void doInitialize() {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            threadsInInit.add(Thread.currentThread());
        }
    };
    // start 10 threads that initialize the bot
    int numThreads = 10;
    final Random rnd = new Random(System.currentTimeMillis());
    final CyclicBarrier barrier = new CyclicBarrier(numThreads + 1);
    for (int i = 0; i < numThreads; i++) {
        Thread thread = new Thread() {

            @Override
            public void run() {
                try {
                    Thread.sleep(rnd.nextInt(100));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    bot.initialize();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    barrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }
        };
        thread.start();
    }
    barrier.await();
    // make sure the bot is initialized
    Assert.assertTrue(bot.getLifecyclePhase().isActive());
    // make sure it was initialized only once
    Assert.assertTrue(threadsInInit.size() == 1);
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) DebugBot(won.bot.impl.DebugBot) DebugBot(won.bot.impl.DebugBot) Bot(won.bot.framework.bot.Bot) BaseBot(won.bot.framework.bot.base.BaseBot) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 3 with DebugBot

use of won.bot.impl.DebugBot in project webofneeds by researchstudio-sat.

the class BotTests method testIsShutdown.

@Test
public void testIsShutdown() {
    Bot bot = new DebugBot();
    try {
        bot.shutdown();
    } catch (Exception e) {
        e.printStackTrace();
    }
    Assert.assertTrue(bot.getLifecyclePhase().isDown());
}
Also used : DebugBot(won.bot.impl.DebugBot) DebugBot(won.bot.impl.DebugBot) Bot(won.bot.framework.bot.Bot) BaseBot(won.bot.framework.bot.base.BaseBot) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) Test(org.junit.Test)

Example 4 with DebugBot

use of won.bot.impl.DebugBot in project webofneeds by researchstudio-sat.

the class BotTests method testIsInitialized.

@Test
public void testIsInitialized() {
    Bot bot = new DebugBot();
    try {
        bot.initialize();
    } catch (Exception e) {
        e.printStackTrace();
    }
    Assert.assertTrue(bot.getLifecyclePhase().isActive());
}
Also used : DebugBot(won.bot.impl.DebugBot) DebugBot(won.bot.impl.DebugBot) Bot(won.bot.framework.bot.Bot) BaseBot(won.bot.framework.bot.base.BaseBot) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) Test(org.junit.Test)

Aggregations

BrokenBarrierException (java.util.concurrent.BrokenBarrierException)4 Test (org.junit.Test)4 Bot (won.bot.framework.bot.Bot)4 BaseBot (won.bot.framework.bot.base.BaseBot)4 DebugBot (won.bot.impl.DebugBot)4 CyclicBarrier (java.util.concurrent.CyclicBarrier)2