Best Cerberus-source code snippet using org.cerberus.websocket.TestCaseExecutionEndPoint.ReentrantLock
Source:TestCaseExecutionEndPoint.java
...41import java.util.HashSet;42import java.util.Map;43import java.util.Set;44import java.util.concurrent.locks.Lock;45import java.util.concurrent.locks.ReentrantLock;46/**47 * {@link ServerEndpoint} to be kept informed about {@link TestCaseExecution} changes48 *49 * @author corentin50 * @author abourdon51 */52@ServerEndpoint(53 value = "/execution/{execution-id}",54 configurator = TestCaseExecutionEndPoint.SingletonConfigurator.class,55 decoders = {TestCaseExecutionDecoder.class},56 encoders = {TestCaseExecutionEncoder.class}57)58public class TestCaseExecutionEndPoint {59 /**60 * The {@link javax.websocket.server.ServerEndpointConfig.Configurator} of this {@link ServerEndpoint} that give always the same {@link TestCaseExecutionEndPoint} instance to deserve websocket support.61 */62 public static class SingletonConfigurator extends ServerEndpointConfig.Configurator {63 @Override64 public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException {65 if (!TestCaseExecutionEndPoint.class.equals(endpointClass)) {66 throw new InstantiationException("No suitable instance for endpoint class " + endpointClass.getName());67 }68 return (T) TestCaseExecutionEndPoint.getInstance();69 }70 }71 /**72 * The associated {@link Logger} to this class73 */74 private static final Logger LOG = LogManager.getLogger(TestCaseExecutionEndPoint.class);75 /**76 * The unique instance of this {@link TestCaseExecutionEndPoint} class77 */78 private static final TestCaseExecutionEndPoint INSTANCE = new TestCaseExecutionEndPoint();79 /**80 * Get the unique instance of this {@link TestCaseExecutionEndPoint} class81 *82 * @return the unique instance of this {@link TestCaseExecutionEndPoint}83 */84 public static TestCaseExecutionEndPoint getInstance() {85 return INSTANCE;86 }87 /**88 * All open WebSocket sessions, grouped by executions89 */90 private Lock mainLock = new ReentrantLock();91 private Map<String, Session> sessions = new HashMap<>();92 private Map<Long, Set<String>> executions = new HashMap<>();93 /**94 * Send the given {@link TestCaseExecution} for all session opened to this execution.95 * <p>96 * Message is sent only if the current timestamp is out of the {@link TestCaseExecution#getCerberus_featureflipping_websocketpushperiod()}97 *98 * @param execution the {@link TestCaseExecution} to send to opened sessions99 * @param forcePush if send has to be forced, regardless of the {@link TestCaseExecution#getCerberus_featureflipping_websocketpushperiod()}}100 * @see TestCaseExecution#getLastWebsocketPush()101 */102 public void send(TestCaseExecution execution, boolean forcePush) {103 // Check if sending is enabled104 if (!execution.isCerberus_featureflipping_activatewebsocketpush()) {...
ReentrantLock
Using AI Code Generation
1 public void send(TestCaseExecution tCExecution) {2 LOG.debug("Sending message to " + tCExecution.getId() + " : " + tCExecution.getTest() + " - " + tCExecution.getTestCase());3 try {4 lock.lock();5 Session session = sessions.get(tCExecution.getId());6 if (session != null) {7 session.getBasicRemote().sendText(gson.toJson(tCExecution));8 } else {9 LOG.debug("Session " + tCExecution.getId() + " not found");10 }11 } catch (IOException ex) {12 LOG.error("Unable to send message to " + tCExecution.getId(), ex);13 } finally {14 lock.unlock();15 }16 }17}
ReentrantLock
Using AI Code Generation
1import java.util.concurrent.TimeUnit;2import java.util.concurrent.locks.ReentrantLock;3ReentrantLock lock = new ReentrantLock();4if(lock.tryLock(5, TimeUnit.SECONDS)){5 try{6 }7 finally{8 lock.unlock();9 }10}11else{12}13import java.util.concurrent.TimeUnit;14import java.util.concurrent.locks.ReentrantReadWriteLock;15ReentrantReadWriteLock lock = new ReentrantReadWriteLock();16if(lock.writeLock().tryLock(5, TimeUnit.SECONDS)){17 try{18 }19 finally{20 lock.writeLock().unlock();21 }22}23else{24}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!