示例#1
0
  @Test
  public void should_reinit_batch_context_and_consistency_after_exception() throws Exception {
    Tweet tweet1 = TweetTestBuilder.tweet().randomId().content("simple_tweet1").buid();
    Tweet tweet2 = TweetTestBuilder.tweet().randomId().content("simple_tweet2").buid();

    em.persist(tweet1);

    // Start batch
    CQLBatchingEntityManager batchEm = emf.createBatchingEntityManager();
    batchEm.startBatch();

    batchEm.startBatch(EACH_QUORUM, EACH_QUORUM);
    batchEm.persist(tweet2);

    try {
      batchEm.endBatch();
    } catch (Exception e) {
      assertThatBatchContextHasBeenReset(batchEm);
    }

    Thread.sleep(1000);
    logAsserter.prepareLogLevel();
    batchEm.persist(tweet2);
    batchEm.endBatch();
    logAsserter.assertConsistencyLevels(ONE, ONE);
  }
示例#2
0
  @Test
  public void should_batch_with_custom_consistency_level() throws Exception {
    Tweet tweet1 = TweetTestBuilder.tweet().randomId().content("simple_tweet1").buid();
    Tweet tweet2 = TweetTestBuilder.tweet().randomId().content("simple_tweet2").buid();
    Tweet tweet3 = TweetTestBuilder.tweet().randomId().content("simple_tweet3").buid();

    em.persist(tweet1);

    // Start batch
    CQLBatchingEntityManager batchEm = emf.createBatchingEntityManager();
    batchEm.startBatch();

    batchEm.startBatch(QUORUM, ALL);

    logAsserter.prepareLogLevel();

    Tweet foundTweet1 = batchEm.find(Tweet.class, tweet1.getId());

    assertThat(foundTweet1.getContent()).isEqualTo(tweet1.getContent());

    batchEm.persist(tweet2);
    batchEm.persist(tweet3);

    batchEm.endBatch();

    logAsserter.assertConsistencyLevels(QUORUM, ALL);
    assertThatBatchContextHasBeenReset(batchEm);
  }
示例#3
0
  @Test
  public void should_batch_counters() throws Exception {
    // Start batch
    CQLBatchingEntityManager batchEm = emf.createBatchingEntityManager();
    batchEm.startBatch();

    CompleteBean entity = CompleteBeanTestBuilder.builder().randomId().name("name").buid();

    entity = batchEm.merge(entity);

    entity.setLabel("label");

    Tweet welcomeTweet = TweetTestBuilder.tweet().randomId().content("welcomeTweet").buid();
    entity.setWelcomeTweet(welcomeTweet);

    entity.getVersion().incr(10L);
    batchEm.merge(entity);

    Row result = session.execute("SELECT label from CompleteBean where id=" + entity.getId()).one();
    assertThat(result).isNull();

    result =
        session
            .execute(
                "SELECT counter_value from achilles_counter_table where fqcn='"
                    + CompleteBean.class.getCanonicalName()
                    + "' and primary_key='"
                    + entity.getId()
                    + "' and property_name='version'")
            .one();
    assertThat(result.getLong("counter_value")).isEqualTo(10L);

    // Flush
    batchEm.endBatch();

    result = session.execute("SELECT label from CompleteBean where id=" + entity.getId()).one();
    assertThat(result.getString("label")).isEqualTo("label");

    result =
        session
            .execute(
                "SELECT counter_value from achilles_counter_table where fqcn='"
                    + CompleteBean.class.getCanonicalName()
                    + "' and primary_key='"
                    + entity.getId()
                    + "' and property_name='version'")
            .one();
    assertThat(result.getLong("counter_value")).isEqualTo(10L);
    assertThatBatchContextHasBeenReset(batchEm);
  }
示例#4
0
  @Test
  public void should_batch_several_entities() throws Exception {
    CompleteBean bean = CompleteBeanTestBuilder.builder().randomId().name("name").buid();
    Tweet tweet1 = TweetTestBuilder.tweet().randomId().content("tweet1").buid();
    Tweet tweet2 = TweetTestBuilder.tweet().randomId().content("tweet2").buid();

    // Start batch
    CQLBatchingEntityManager batchEm = emf.createBatchingEntityManager();
    batchEm.startBatch();

    batchEm.merge(bean);
    batchEm.merge(tweet1);
    batchEm.merge(tweet2);
    batchEm.merge(user);

    CompleteBean foundBean = batchEm.find(CompleteBean.class, bean.getId());
    Tweet foundTweet1 = batchEm.find(Tweet.class, tweet1.getId());
    Tweet foundTweet2 = batchEm.find(Tweet.class, tweet2.getId());
    User foundUser = batchEm.find(User.class, user.getId());

    assertThat(foundBean).isNull();
    assertThat(foundTweet1).isNull();
    assertThat(foundTweet2).isNull();
    assertThat(foundUser).isNull();

    // Flush
    batchEm.endBatch();

    foundBean = batchEm.find(CompleteBean.class, bean.getId());
    foundTweet1 = batchEm.find(Tweet.class, tweet1.getId());
    foundTweet2 = batchEm.find(Tweet.class, tweet2.getId());
    foundUser = batchEm.find(User.class, user.getId());

    assertThat(foundBean.getName()).isEqualTo("name");
    assertThat(foundTweet1.getContent()).isEqualTo("tweet1");
    assertThat(foundTweet2.getContent()).isEqualTo("tweet2");
    assertThat(foundUser.getFirstname()).isEqualTo("fn");
    assertThat(foundUser.getLastname()).isEqualTo("ln");
    assertThatBatchContextHasBeenReset(batchEm);
  }
示例#5
0
  @Test
  public void should_reinit_batch_context_after_exception() throws Exception {
    User user =
        UserTestBuilder.user().id(123456494L).firstname("firstname").lastname("lastname").buid();
    Tweet tweet = TweetTestBuilder.tweet().randomId().content("simple_tweet").creator(user).buid();

    // Start batch
    CQLBatchingEntityManager batchEm = emf.createBatchingEntityManager();
    batchEm.startBatch();

    try {
      batchEm.persist(tweet);
    } catch (AchillesException e) {
      batchEm.cleanBatch();
      assertThatBatchContextHasBeenReset(batchEm);

      assertThat(batchEm.find(Tweet.class, tweet.getId())).isNull();
    }

    // batchEm should reinit batch context
    batchEm.persist(user);
    batchEm.endBatch();

    User foundUser = batchEm.find(User.class, user.getId());
    assertThat(foundUser.getFirstname()).isEqualTo("firstname");
    assertThat(foundUser.getLastname()).isEqualTo("lastname");

    batchEm.persist(tweet);
    batchEm.endBatch();

    Tweet foundTweet = batchEm.find(Tweet.class, tweet.getId());
    assertThat(foundTweet.getContent()).isEqualTo("simple_tweet");
    assertThat(foundTweet.getCreator().getId()).isEqualTo(foundUser.getId());
    assertThat(foundTweet.getCreator().getFirstname()).isEqualTo("firstname");
    assertThat(foundTweet.getCreator().getLastname()).isEqualTo("lastname");
    assertThatBatchContextHasBeenReset(batchEm);
  }