How to use InfiniteTimeout class of org.jmock.lib.concurrent.internal package

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

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 org.hamcrest.StringDescription.asString;
4
5import java.util.concurrent.TimeoutException;
6
7import org.jmock.api.Invocation;
8import org.jmock.api.Invokable;
9import org.jmock.api.ThreadingPolicy;
10import org.jmock.internal.StatePredicate;
11import org.jmock.lib.concurrent.internal.FixedTimeout;
12import org.jmock.lib.concurrent.internal.InfiniteTimeout;
13import org.jmock.lib.concurrent.internal.Timeout;
14import org.junit.Assert;
15
16
17/**
18 * A ThreadingPolicy that makes the Mockery thread-safe and
19 * helps tests synchronise with background threads.
20 * 
21 * @author Nat Pryce
22 */
23public class Synchroniser implements ThreadingPolicy {
24    private final Object sync = new Object();
25    private Error firstError = null;
26    
27    
28    /** 
29     * Waits for a StatePredicate to become active.  
30     * 
31     * Warning: this will wait forever unless the test itself has a timeout.
32     *   
33     * @param p the StatePredicate to wait for
34     * @throws InterruptedException
35     */
36    public void waitUntil(StatePredicate p) throws InterruptedException {
37        waitUntil(p, new InfiniteTimeout());
38    }
39    
40    /** 
41     * Waits up to a timeout for a StatePredicate to become active.  Fails the
42     * test if the timeout expires.
43     *   
44     * @param p the StatePredicate to wait for
45     * @param timeoutMs the timeout in milliseconds
46     * @throws InterruptedException
47     */
48    public void waitUntil(StatePredicate p, long timeoutMs) throws InterruptedException {
49        waitUntil(p, new FixedTimeout(timeoutMs));
50    }
51    
52    private void waitUntil(StatePredicate p, Timeout timeout) throws InterruptedException {
53        synchronized(sync) {
54            while (!p.isActive()) {
55                try {
56                    sync.wait(timeout.timeRemaining());
57                }
58                catch (TimeoutException e) {
59                    if (firstError != null) {
60                        throw firstError;
61                    }
62                    else {
63                        Assert.fail("timed out waiting for " + asString(p));
64                    }
65                }
66            }
67        }
68        
69    }
70    
71    public Invokable synchroniseAccessTo(final Invokable mockObject) {
72        return new Invokable() {
73            public Object invoke(Invocation invocation) throws Throwable {
74                return synchroniseInvocation(mockObject, invocation);
75            }
76        };
77    }
78
79    private Object synchroniseInvocation(Invokable mockObject, Invocation invocation) throws Throwable {
80        synchronized (sync) {
81            try {
82                return mockObject.invoke(invocation);
83            }
84            catch (Error e) {
85                if (firstError == null) {
86                    firstError = e;
87                }
88                throw e;
89            }
90            finally {
91                sync.notifyAll();
92            }
93        }
94    }
95}
96
Full Screen
copy
1/*
2 * Copyright (c) 2009-2018, toby weston & tempus-fugit committers
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 
5 * you may not use this file except in compliance with the License. 
6 * You may obtain a copy of the License at 
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0 
9 *
10 * Unless required by applicable law or agreed to in writing, software 
11 * distributed under the License is distributed on an "AS IS" BASIS, 
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13 * See the License for the specific language governing permissions and 
14 * limitations under the License.
15 */
16
17package com.google.code.tempusfugit.concurrency;
18
19import com.google.code.tempusfugit.temporal.Duration;
20import org.jmock.api.Invocation;
21import org.jmock.api.Invokable;
22import org.jmock.api.ThreadingPolicy;
23import org.jmock.internal.StatePredicate;
24import org.jmock.lib.concurrent.internal.FixedTimeout;
25import org.jmock.lib.concurrent.internal.InfiniteTimeout;
26import org.jmock.lib.concurrent.internal.Timeout;
27import org.junit.Assert;
28
29import java.util.concurrent.TimeoutException;
30import java.util.concurrent.locks.Condition;
31import java.util.concurrent.locks.Lock;
32import java.util.concurrent.locks.ReentrantLock;
33
34import static com.google.code.tempusfugit.temporal.Duration.millis;
35import static java.util.concurrent.TimeUnit.MILLISECONDS;
36import static org.hamcrest.StringDescription.asString;
37
38public class TimingOutSynchroniser implements ThreadingPolicy {
39
40    private final Lock lock = new ReentrantLock();
41    private final Condition awaitingStatePredicate = lock.newCondition();
42    private final Duration lockTimeout;
43
44    private Error firstError = null;
45
46    public TimingOutSynchroniser() {
47        this(millis(250));
48    }
49
50    public TimingOutSynchroniser(Duration timeout) {
51        this.lockTimeout = timeout;
52    }
53
54    public void waitUntil(StatePredicate predicate) throws InterruptedException {
55        waitUntil(predicate, new InfiniteTimeout());
56    }
57
58    /**
59     * Waits up to a timeout for a StatePredicate to become active.  Fails the
60     * test if the timeout expires.
61     * @param predicate will wait up to the specified timeout for this predicate to become true
62     * @param timeoutMs timeout in millis
63     * @throws InterruptedException if the wait is interrupted by the interrupt flag being set elsewhere
64     */
65    public void waitUntil(StatePredicate predicate, long timeoutMs) throws InterruptedException {
66        waitUntil(predicate, new FixedTimeout(timeoutMs));
67    }
68
69    private void waitUntil(StatePredicate predicate, Timeout testTimeout) throws InterruptedException {
70        try {
71            lock.tryLock(lockTimeout.inMillis(), MILLISECONDS);
72            while (!predicate.isActive()) {
73                try {
74                    awaitingStatePredicate.await(testTimeout.timeRemaining(), MILLISECONDS);
75                } catch (TimeoutException e) {
76                    if (firstError != null)
77                        throw firstError;
78                    Assert.fail("timed out waiting for " + asString(predicate));
79                }
80            }
81        } finally {
82            if (lock.tryLock())
83                lock.unlock();
84        }
85
86    }
87
88    public Invokable synchroniseAccessTo(final Invokable mockObject) {
89        return invocation -> synchroniseInvocation(mockObject, invocation);
90    }
91
92    private Object synchroniseInvocation(Invokable mockObject, Invocation invocation) throws Throwable {
93        try {
94            lock.tryLock(lockTimeout.inMillis(), MILLISECONDS);
95            try {
96                return mockObject.invoke(invocation);
97            } catch (Error e) {
98                if (firstError == null)
99                    firstError = e;
100                throw e;
101            } finally {
102                awaitingStatePredicate.signalAll();
103            }
104        } finally {
105            if (lock.tryLock())
106                lock.unlock();
107        }
108    }
109}
110
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 popular Stackoverflow questions on InfiniteTimeout

    No relevant questions found for this class 😞

Most used methods in InfiniteTimeout

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

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)