Search in sources :

Example 1 with MockS3Client

use of com.netflix.exhibitor.core.backup.s3.MockS3Client in project exhibitor by soabase.

the class TestS3PseudoLock method testGCOldLockFiles.

@Test
public void testGCOldLockFiles() throws Exception {
    final BlockingQueue<String> queue = new ArrayBlockingQueue<String>(1);
    ActivityLog mockLog = Mockito.mock(ActivityLog.class);
    MockS3Client client = new MockS3Client(null, null) {

        @Override
        public void deleteObject(String bucket, String key) throws Exception {
            queue.put(key);
        }
    };
    S3PseudoLock lock = new S3PseudoLock(client, "foo", "bar", 10, 10, 0);
    lock.lock(mockLog, 1, TimeUnit.DAYS);
    Thread.sleep(20);
    S3PseudoLock lock2 = new S3PseudoLock(client, "foo", "bar", 10, 10, 0);
    // should clean the previous lock
    lock2.lock(mockLog, 1, TimeUnit.DAYS);
    String cleaned = queue.poll(5, TimeUnit.SECONDS);
    Assert.assertNotNull(cleaned);
}
Also used : ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) MockS3Client(com.netflix.exhibitor.core.backup.s3.MockS3Client) ActivityLog(com.netflix.exhibitor.core.activity.ActivityLog) Test(org.testng.annotations.Test)

Example 2 with MockS3Client

use of com.netflix.exhibitor.core.backup.s3.MockS3Client in project exhibitor by soabase.

the class TestS3PseudoLock method testMulti.

@Test
public void testMulti() throws Exception {
    final int QTY = 5;
    final int POLLING_MS = 1;
    ActivityLog mockLog = Mockito.mock(ActivityLog.class);
    List<S3PseudoLock> locks = Lists.newArrayList();
    for (int i = 0; i < QTY; ++i) {
        MockS3Client client = new MockS3Client();
        S3PseudoLock lock = new S3PseudoLock(client, "foo", "-prefix-", 10000, POLLING_MS, 0);
        locks.add(lock);
    }
    for (S3PseudoLock lock : locks) {
        Assert.assertTrue(lock.lock(mockLog, 5, TimeUnit.SECONDS));
        try {
            //noinspection PointlessArithmeticExpression
            Thread.sleep(POLLING_MS * 2);
        } finally {
            lock.unlock();
        }
    }
}
Also used : MockS3Client(com.netflix.exhibitor.core.backup.s3.MockS3Client) ActivityLog(com.netflix.exhibitor.core.activity.ActivityLog) Test(org.testng.annotations.Test)

Example 3 with MockS3Client

use of com.netflix.exhibitor.core.backup.s3.MockS3Client in project exhibitor by soabase.

the class TestS3PseudoLock method testBlocking.

// Too flaky to be useful right now. See https://github.com/soabase/exhibitor/issues/329.
@Test(enabled = false)
public void testBlocking() throws Exception {
    final int QTY = 5;
    final int POLLING_MS = 1;
    final AtomicBoolean isLocked = new AtomicBoolean(false);
    final AtomicInteger lockCount = new AtomicInteger(0);
    final MockS3Client client = new MockS3Client(null, null);
    final ActivityLog mockLog = Mockito.mock(ActivityLog.class);
    ExecutorCompletionService<Void> completionService = new ExecutorCompletionService<Void>(Executors.newFixedThreadPool(QTY));
    for (int i = 0; i < QTY; ++i) {
        completionService.submit(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                S3PseudoLock lock = new S3PseudoLock(client, "foo", "bar", Integer.MAX_VALUE, POLLING_MS, 0);
                try {
                    Assert.assertTrue(lock.lock(mockLog, 10, TimeUnit.SECONDS));
                    Assert.assertTrue(isLocked.compareAndSet(false, true));
                    lockCount.incrementAndGet();
                    Thread.sleep(POLLING_MS);
                } finally {
                    if (isLocked.compareAndSet(true, false)) {
                        lock.unlock();
                    }
                }
                return null;
            }
        });
        Thread.sleep(1);
    }
    for (int i = 0; i < QTY; ++i) {
        completionService.take().get();
    }
    Assert.assertEquals(lockCount.get(), QTY);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MockS3Client(com.netflix.exhibitor.core.backup.s3.MockS3Client) ActivityLog(com.netflix.exhibitor.core.activity.ActivityLog) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) Test(org.testng.annotations.Test)

Example 4 with MockS3Client

use of com.netflix.exhibitor.core.backup.s3.MockS3Client in project exhibitor by soabase.

the class TestS3PseudoLock method testSingle.

@Test
public void testSingle() throws Exception {
    MockS3Client client = new MockS3Client();
    ActivityLog mockLog = Mockito.mock(ActivityLog.class);
    S3PseudoLock lock = new S3PseudoLock(client, "foo", "bar", 10000, 1, 0);
    Assert.assertTrue(lock.lock(mockLog, 5, TimeUnit.SECONDS));
    lock.unlock();
}
Also used : MockS3Client(com.netflix.exhibitor.core.backup.s3.MockS3Client) ActivityLog(com.netflix.exhibitor.core.activity.ActivityLog) Test(org.testng.annotations.Test)

Example 5 with MockS3Client

use of com.netflix.exhibitor.core.backup.s3.MockS3Client in project exhibitor by soabase.

the class TestS3PseudoLock method testWithDifferentLockKeySeparator.

@Test
public void testWithDifferentLockKeySeparator() throws Exception {
    MockS3Client client = new MockS3Client();
    ActivityLog mockLog = Mockito.mock(ActivityLog.class);
    S3PseudoLock lock = new S3PseudoLock(client, "foo", "bar", 10000, 1, 0, "#");
    Assert.assertTrue(lock.lock(mockLog, 5, TimeUnit.SECONDS));
    lock.unlock();
}
Also used : MockS3Client(com.netflix.exhibitor.core.backup.s3.MockS3Client) ActivityLog(com.netflix.exhibitor.core.activity.ActivityLog) Test(org.testng.annotations.Test)

Aggregations

ActivityLog (com.netflix.exhibitor.core.activity.ActivityLog)5 MockS3Client (com.netflix.exhibitor.core.backup.s3.MockS3Client)5 Test (org.testng.annotations.Test)5 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1