@Override public RepositoryLock lockRepository() throws IOException { return new AzureRepositoryLock(getBlockBlob("repo.lock"), () -> { log.warn("Lost connection to the Azure. The client will be closed."); // TODO close the connection }).lock(); }
@Override public void unlock() throws IOException { doUpdate = false; executor.shutdown(); try { executor.awaitTermination(1, TimeUnit.MINUTES); } catch (InterruptedException e) { throw new IOException(e); } finally { releaseLease(); } }
@Override public RepositoryLock lockRepository() throws IOException { return new AzureRepositoryLock(getBlockBlob("repo.lock"), () -> { log.warn("Lost connection to the Azure. The client will be closed."); // TODO close the connection }).lock(); }
@Override public void unlock() throws IOException { doUpdate = false; executor.shutdown(); try { executor.awaitTermination(1, TimeUnit.MINUTES); } catch (InterruptedException e) { throw new IOException(e); } finally { releaseLease(); } }
@Test public void testFailingLock() throws URISyntaxException, IOException, StorageException { CloudBlockBlob blob = container.getBlockBlobReference("oak/repo.lock"); new AzureRepositoryLock(blob, () -> {}, 0).lock(); try { new AzureRepositoryLock(blob, () -> {}, 0).lock(); fail("The second lock should fail."); } catch (IOException e) { // it's fine } }
@Test public void testWaitingLock() throws URISyntaxException, IOException, StorageException, InterruptedException { CloudBlockBlob blob = container.getBlockBlobReference("oak/repo.lock"); Semaphore s = new Semaphore(0); new Thread(() -> { try { RepositoryLock lock = new AzureRepositoryLock(blob, () -> {}, 0).lock(); s.release(); Thread.sleep(1000); lock.unlock(); } catch (Exception e) { log.error("Can't lock or unlock the repo", e); } }).start(); s.acquire(); new AzureRepositoryLock(blob, () -> {}, 10).lock(); }