<commit_before>/*
 * Copyright (C) 2014  <NAME>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package c5db.log;

import c5db.interfaces.replication.IndexCommitNotice;
import c5db.interfaces.replication.Replicator;
import c5db.interfaces.replication.ReplicatorReceipt;
import org.apache.hadoop.hbase.HRegionInfo;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.regionserver.wal.HLog;
import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.jetlang.channels.Channel;
import org.jetlang.channels.MemoryChannel;
import org.jetlang.fibers.Fiber;
import org.jetlang.fibers.ThreadFiber;
import org.jmock.Expectations;
import org.jmock.integration.junit4.JUnitRuleMockery;
import org.jmock.lib.concurrent.Synchroniser;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import static c5db.FutureActions.returnFutureWithValue;

public class OLogShimTest {
  private static final String QUORUM_ID = "q";
  private static final String ANOTHER_QUORUM_ID = "q2";

  @Rule
  public JUnitRuleMockery context = new JUnitRuleMockery() {{
    setThreadingPolicy(new Synchroniser());
  }};

  private final Replicator replicator = context.mock(Replicator.class);
  @SuppressWarnings("deprecation")
  private final HTableDescriptor descriptor = new HTableDescriptor();
  @SuppressWarnings("deprecation")
  private final HRegionInfo info = new HRegionInfo();
  private final TableName tableName = descriptor.getTableName();

  private final Channel<IndexCommitNotice> commitNoticeChannel = new MemoryChannel<>();
  private final Fiber oLogShimFiber = new ThreadFiber();

  private HLog hLog;

  @Before
  public void setOverallExpectationsAndCreateTestObject() {
    context.checking(new Expectations() {{
      allowing(replicator).getQuorumId();
      will(returnValue(QUORUM_ID));

      allowing(replicator).getId();
      will(returnValue(1L));

      allowing(replicator).getCommitNoticeChannel();
      will(returnValue(commitNoticeChannel));
    }});

    oLogShimFiber.start();
    hLog = new OLogShim(replicator, oLogShimFiber);
  }

  @After
  public void disposeOfFiber() {
    oLogShimFiber.dispose();
  }

  @Test
  public void logsOneReplicationDatumPerSubmittedWALEdit() throws Exception {
    context.checking(new Expectations() {{
      oneOf(replicator).logData(with(anyData()));
    }});

    hLog.appendNoSync(info, tableName, aWalEditWithMultipleKeyValues(), aClusterIdList(), currentTime(), descriptor);
  }<commit_msg>Add an additional required expectation to the mocked Replicator in OLogShimTest<commit_after>/*
 * Copyright (C) 2014  <NAME>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package c5db.log;

import c5db.interfaces.replication.IndexCommitNotice;
import c5db.interfaces.replication.Replicator;
import c5db.interfaces.replication.ReplicatorReceipt;
import org.apache.hadoop.hbase.HRegionInfo;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.regionserver.wal.HLog;
import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.jetlang.channels.Channel;
import org.jetlang.channels.MemoryChannel;
import org.jetlang.fibers.Fiber;
import org.jetlang.fibers.ThreadFiber;
import org.jmock.Expectations;
import org.jmock.integration.junit4.JUnitRuleMockery;
import org.jmock.lib.concurrent.Synchroniser;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import static c5db.FutureActions.returnFutureWithValue;

public class OLogShimTest {
  private static final String QUORUM_ID = "q";
  private static final String ANOTHER_QUORUM_ID = "q2";

  @Rule
  public JUnitRuleMockery context = new JUnitRuleMockery() {{
    setThreadingPolicy(new Synchroniser());
  }};

  private final Replicator replicator = context.mock(Replicator.class);
  @SuppressWarnings("deprecation")
  private final HTableDescriptor descriptor = new HTableDescriptor();
  @SuppressWarnings("deprecation")
  private final HRegionInfo info = new HRegionInfo();
  private final TableName tableName = descriptor.getTableName();

  private final Channel<IndexCommitNotice> commitNoticeChannel = new MemoryChannel<>();
  private final Fiber oLogShimFiber = new ThreadFiber();

  private HLog hLog;

  @Before
  public void setOverallExpectationsAndCreateTestObject() {
    context.checking(new Expectations() {{
      allowing(replicator).getQuorumId();
      will(returnValue(QUORUM_ID));

      allowing(replicator).getId();
      will(returnValue(1L));

      allowing(replicator).getCommitNoticeChannel();
      will(returnValue(commitNoticeChannel));

      allowing(replicator).getEventChannel();
      will(returnValue(new MemoryChannel<>()));
    }});

    oLogShimFiber.start();
    hLog = new OLogShim(replicator, oLogShimFiber);
  }

  @After
  public void disposeOfFiber() {
    oLogShimFiber.dispose();
  }

  @Test
  public void logsOneReplicationDatumPerSubmittedWALEdit() throws Exception {
    context.checking(new Expectations() {{
      oneOf(replicator).logData(with(anyData()));
    }});

    hLog.appendNoSync(info, tableName, aWalEditWithMultipleKeyValues(), aClusterIdList(), currentTime(), descriptor);
  }
{'fraction_non_alphanumeric': 0.09693076374018558, 'fraction_numerical': 0.0037116345467523197, 'mean_word_length': 4.897306397306397, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2364529', 'n_tokens_mistral': 2145, 'n_tokens_neox': 2099, 'n_words': 543}