How to use method of org.jmock.lib.concurrent.Blitzer class

Best Jmock-library code snippet using org.jmock.lib.concurrent.Blitzer.

Run Jmock-library automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1package org.jmock.lib.concurrent;
2
3import static java.util.concurrent.TimeUnit.MILLISECONDS;
4
5import java.util.concurrent.CountDownLatch;
6import java.util.concurrent.ExecutorService;
7import java.util.concurrent.Executors;
8import java.util.concurrent.TimeoutException;
9
10
11/**
12 * A class that "blitzes" an object by calling it many times, from multiple
13 * threads. Used for stress-testing synchronisation.
14 * 
15 * @author nat
16 */
17public class Blitzer {
18    /**
19     * The default number of threads to run concurrently.
20     */
21    public static final int DEFAULT_THREAD_COUNT = 2;
22
23    private final ExecutorService executorService;
24    private final int actionCount;
25    
26    public Blitzer(int actionCount) {
27        this(actionCount, DEFAULT_THREAD_COUNT);
28    }
29    
30    public Blitzer(int actionCount, int threadCount) {
31        this.actionCount = actionCount;
32        this.executorService = Executors.newFixedThreadPool(threadCount);
33    }
34    
35    public Blitzer(int actionCount, ExecutorService executorService) {
36        this.actionCount = actionCount;
37        this.executorService = executorService;;
38    }
39    
40    public int totalActionCount() {
41        return actionCount;
42    }
43
44    public void blitz(final Runnable action) throws InterruptedException {
45        spawnThreads(action).await();
46    }
47
48    public void blitz(long timeoutMs, final Runnable action) throws InterruptedException, TimeoutException {
49        if (!spawnThreads(action).await(timeoutMs, MILLISECONDS)) {
50            throw new TimeoutException("timed out waiting for blitzed actions to complete successfully");
51        }
52    }
53
54    private CountDownLatch spawnThreads(final Runnable action) {
55        final CountDownLatch finished = new CountDownLatch(actionCount);
56        
57        for (int i = 0; i < actionCount; i++) {
58            executorService.execute(new Runnable() {
59                public void run() {
60                    try {
61                        action.run();
62                    }
63                    finally {
64                        finished.countDown();
65                    }
66                }
67            });
68        }
69        
70        return finished;
71    }
72
73    public void shutdown() {
74        executorService.shutdown();
75    }
76}
77
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used method in Blitzer

Trigger code on LambdaTest Cloud Grid

Execute automation tests with on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)