### Question:
FencedRpcEndpoint extends RpcEndpoint { public F getFencingToken() { return fencingToken; } protected  FencedRpcEndpoint(RpcService rpcService, String endpointId); protected  FencedRpcEndpoint(RpcService rpcService); F getFencingToken();  }### Answer:
@Test public void testRemoteAndSelfGateways() throws Exception { final UUID initialFencingToken = UUID.randomUUID(); final UUID newFencingToken = UUID.randomUUID(); final String value = "foobar"; final FencedTestingEndpoint fencedTestingEndpoint = new FencedTestingEndpoint(rpcService, value, initialFencingToken); try { fencedTestingEndpoint.start(); FencedTestingGateway selfGateway = fencedTestingEndpoint.getSelfGateway(FencedTestingGateway.class); FencedTestingGateway remoteGateway = rpcService.connect(fencedTestingEndpoint.getAddress(), initialFencingToken, FencedTestingGateway.class) .get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS); assertEquals(initialFencingToken, selfGateway.getFencingToken()); assertEquals(initialFencingToken, remoteGateway.getFencingToken()); assertEquals(value, selfGateway.foobar(timeout).get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS)); assertEquals(value, remoteGateway.foobar(timeout).get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS)); CompletableFuture<Acknowledge> newFencingTokenFuture = fencedTestingEndpoint.setFencingTokenInMainThread(newFencingToken, timeout); newFencingTokenFuture.get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS); assertEquals(newFencingToken, selfGateway.getFencingToken()); assertNotEquals(newFencingToken, remoteGateway.getFencingToken()); assertEquals(value, selfGateway.foobar(timeout).get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS)); try { remoteGateway.foobar(timeout).get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS); fail("This should have failed because we don't have the right fencing token."); } catch (ExecutionException e) { assertTrue(ExceptionUtils.stripExecutionException(e) instanceof FencingTokenException); } } finally { RpcUtils.terminateRpcEndpoint(fencedTestingEndpoint, timeout); } }
@Test public void testUnfencedRemoteGateway() throws Exception { final UUID initialFencingToken = UUID.randomUUID(); final String value = "foobar"; final FencedTestingEndpoint fencedTestingEndpoint = new FencedTestingEndpoint(rpcService, value, initialFencingToken); try { fencedTestingEndpoint.start(); FencedTestingGateway unfencedGateway = rpcService.connect(fencedTestingEndpoint.getAddress(), FencedTestingGateway.class) .get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS); try { unfencedGateway.foobar(timeout).get(timeout.toMilliseconds(), TimeUnit.MILLISECONDS); fail("This should have failed because we have an unfenced gateway."); } catch (ExecutionException e) { assertTrue(ExceptionUtils.stripExecutionException(e) instanceof RpcException); } try { unfencedGateway.getFencingToken(); fail("We should not be able to call getFencingToken on an unfenced gateway."); } catch (UnsupportedOperationException ignored) { } } finally { RpcUtils.terminateRpcEndpoint(fencedTestingEndpoint, timeout); } }