@Override public CoapNetworkRule setDelay(int delayInMillis) { return (CoapNetworkRule) super.setDelay(delayInMillis); }
@Override public Statement apply(final Statement base, final Description description) { if (supports(usedMode)) { return new Statement() { @Override public void evaluate() throws Throwable { applyConfig(description); try { base.evaluate(); } finally { closeConfig(); } } }; } else { LOGGER.log(Level.WARNING, "Skip {0} not applicable with socket mode {1}", new Object[] { description, usedMode }); return SKIP; } }
/** * Apply configuration of rule. * * Handles nesting of rules pushing rule to stack and setup configuration * calling {@link #initNetwork(boolean)}. */ private final void applyConfig(final Description description) { int size; boolean first; synchronized (RULES_STACK) { this.description = description; first = RULES_STACK.isEmpty(); RULES_STACK.push(this); size = RULES_STACK.size(); } LOGGER.log(Level.INFO, "{0} rules active.", size); initNetwork(first); }
/** * Close configuration of rule. * * Handles nesting of rules by removing the top rule from the stack and * setup configuration of the next rule calling * {@link #initNetwork(boolean)}, when available. When the last rule is * removed from stack and no next rule is available, calls * {@link #closeNetwork()}. */ private final void closeConfig() { int size; NetworkRule closedRule; NetworkRule activeRule; synchronized (RULES_STACK) { closedRule = RULES_STACK.pop(); activeRule = RULES_STACK.peek(); size = RULES_STACK.size(); } LOGGER.log(Level.INFO, "{0} rules active.", size); if (this != closedRule) { throw new IllegalStateException("closed rule differs!"); } if (null == activeRule) { closeNetwork(); } else { activeRule.initNetwork(false); } }
@Override protected void closeNetwork() { EndpointManager.reset(); messageThreads.set(DEFAULT_MESSAGE_THREADS); NetworkConfig.setStandard(null); super.closeNetwork(); }
@Override public DatagramSocketImpl createDatagramSocketImpl() { if (!isActive()) { String message = "Use " + NetworkRule.class.getName() + " to define DatagramSocket behaviour!"; LOGGER.log(Level.SEVERE, message); /* * check, if datagram socket is created in the scope of * a NetworkRule. */ throw new IOError(new SocketException(message)); } return super.createDatagramSocketImpl(); } });
@Override protected void initNetwork(boolean first) { if (first) { EndpointManager.reset(); } createStandardTestConfig(); super.initNetwork(first); }
/** * Set delay for processing messages. * * Only used in DIRECT mode. * * @param delayInMillis delay in milliseconds * @return this rule * @throws IllegalArgumentException, if the value is smaller then 0 or * DIRECT is not within the supported modes. */ public NetworkRule setDelay(int delayInMillis) { if (0 > delayInMillis) { throw new IllegalArgumentException("delays must be at least 0, not " + delayInMillis + "!"); } if (!supports(Mode.DIRECT)) { throw new IllegalArgumentException("delays could only be used for DIRECT DatagramSockets!"); } this.delayInMillis = delayInMillis; return this; }