Beispiel #1
0
  @Test
  public void testStaleLock_String() throws Exception {
    ColumnPrefixDistributedRowLock<String> lock1 =
        new ColumnPrefixDistributedRowLock<String>(keyspace, LOCK_CF_STRING, "testStaleLock")
            .withTtl(TTL)
            .withConsistencyLevel(ConsistencyLevel.CL_ONE)
            .expireLockAfter(1, TimeUnit.SECONDS);

    ColumnPrefixDistributedRowLock<String> lock2 =
        new ColumnPrefixDistributedRowLock<String>(keyspace, LOCK_CF_STRING, "testStaleLock")
            .failOnStaleLock(true)
            .withTtl(TTL)
            .withConsistencyLevel(ConsistencyLevel.CL_ONE)
            .expireLockAfter(9, TimeUnit.SECONDS);

    try {
      lock1.acquire();
      Thread.sleep(2000);
      try {
        lock2.acquire();
        Assert.fail();
      } catch (StaleLockException e) {
      } catch (Exception e) {
        Assert.fail(e.getMessage());
      } finally {
        lock2.release();
      }
    } catch (Exception e) {
      e.printStackTrace();
      Assert.fail(e.getMessage());
    } finally {
      lock1.release();
    }
  }
Beispiel #2
0
  @Test
  public void testTtlString() throws Exception {
    ColumnPrefixDistributedRowLock<String> lock =
        new ColumnPrefixDistributedRowLock<String>(keyspace, LOCK_CF_STRING, "testTtl")
            .withTtl(2)
            .withConsistencyLevel(ConsistencyLevel.CL_ONE)
            .expireLockAfter(1, TimeUnit.SECONDS);

    try {
      lock.acquire();
      Assert.assertEquals(1, lock.readLockColumns().size());
      Thread.sleep(3000);
      Assert.assertEquals(0, lock.readLockColumns().size());
    } catch (Exception e) {
      Assert.fail(e.getMessage());
    } finally {
      lock.release();
    }
    Assert.assertEquals(0, lock.readLockColumns().size());
  }