<commit_before>/*
 * Licensed to the University of California, Berkeley under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package tachyon.master.block;

import java.util.List;
import java.util.Set;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import tachyon.StorageLevelAlias;
import tachyon.master.block.meta.MasterWorkerInfo;
import tachyon.master.journal.Journal;
import tachyon.master.journal.ReadWriteJournal;
import tachyon.test.Tester;
import tachyon.thrift.Command;
import tachyon.thrift.CommandType;
import tachyon.thrift.NetAddress;

/**
 * Unit tests for tachyon.master.block.BlockMaster.
 */
public class BlockMasterTest implements Tester<BlockMaster> {

  private BlockMaster.PrivateAccess mPrivateAccess;
  private BlockMaster mMaster;

  @Override
  public void receiveAccess(Object access) {
    mPrivateAccess = (BlockMaster.PrivateAccess) access;
  }

  @Rule
  public TemporaryFolder mTestFolder = new TemporaryFolder();

  @Before
  public void initialize() throws Exception {
    Journal blockJournal = new ReadWriteJournal(mTestFolder.newFolder().getAbsolutePath());
    mMaster = new BlockMaster(blockJournal);
    mMaster.grantAccess(BlockMasterTest.this); // initializes mPrivateAccess
  }

  @Test
  public void countBytesTest() throws Exception {
    Assert.assertEquals(0L, mMaster.getCapacityBytes());
    Assert.assertEquals(0L, mMaster.getUsedBytes());
    Assert.assertEquals(ImmutableList.of(0L, 0L, 0L), mMaster.getTotalBytesOnTiers());
    Assert.assertEquals(ImmutableList.of(0L, 0L, 0L), mMaster.getUsedBytesOnTiers());
    long worker1 = mMaster.getWorkerId(new NetAddress("localhost", 80, 81));
    long worker2 = mMaster.getWorkerId(new NetAddress("localhost", 82, 83));
    addWorker(mMaster, worker1, ImmutableList.of(100L, 200L, 30L), ImmutableList.of(20L, 50L, 10L));
    Assert.assertEquals(330L, mMaster.getCapacityBytes());
    Assert.assertEquals(80L, mMaster.getUsedBytes());<commit_msg>Add test for remove blocks.<commit_after>/*
 * Licensed to the University of California, Berkeley under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package tachyon.master.block;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import tachyon.StorageLevelAlias;
import tachyon.master.block.meta.MasterWorkerInfo;
import tachyon.master.journal.Journal;
import tachyon.master.journal.ReadWriteJournal;
import tachyon.test.Tester;
import tachyon.thrift.Command;
import tachyon.thrift.CommandType;
import tachyon.thrift.NetAddress;

/**
 * Unit tests for tachyon.master.block.BlockMaster.
 */
public class BlockMasterTest implements Tester<BlockMaster> {

  private BlockMaster.PrivateAccess mPrivateAccess;
  private BlockMaster mMaster;

  @Override
  public void receiveAccess(Object access) {
    mPrivateAccess = (BlockMaster.PrivateAccess) access;
  }

  @Rule
  public TemporaryFolder mTestFolder = new TemporaryFolder();

  @Before
  public void initialize() throws Exception {
    Journal blockJournal = new ReadWriteJournal(mTestFolder.newFolder().getAbsolutePath());
    mMaster = new BlockMaster(blockJournal);
    mMaster.grantAccess(BlockMasterTest.this); // initializes mPrivateAccess
  }

  @Test
  public void removeBlocksTest() throws Exception {
    mMaster.start(true);
    long workerId = mMaster.getWorkerId(new NetAddress("test", 1, 2));
    long worker2 = mMaster.getWorkerId(new NetAddress("test2", 1, 2));
    List<Long> workerBlocks = Arrays.asList(1L, 2L, 3L);
    HashMap<Long, List<Long>> noBlocksInTier = Maps.newHashMap();
    HashMap<Long, List<Long>> workerBlocksInTier = Maps.newHashMap();
    mMaster.workerRegister(workerId, Arrays.asList(100L), Arrays.asList(0L), noBlocksInTier);
    mMaster.workerRegister(worker2, Arrays.asList(100L), Arrays.asList(0L), noBlocksInTier);
    mMaster.commitBlock(workerId, 1L, 1, 1L, 1L);
    mMaster.commitBlock(workerId, 2L, 1, 2L, 1L);
    mMaster.commitBlock(workerId, 3L, 1, 3L, 1L);
    mMaster.commitBlock(worker2, 1L, 1, 1L, 1L);
    mMaster.commitBlock(worker2, 2L, 1, 2L, 1L);
    mMaster.commitBlock(worker2, 3L, 1, 3L, 1L);
    mMaster.removeBlocks(workerBlocks);
  }

  @Test
  public void countBytesTest() throws Exception {
    Assert.assertEquals(0L, mMaster.getCapacityBytes());
    Assert.assertEquals(0L, mMaster.getUsedBytes());
    Assert.assertEquals(ImmutableList.of(0L, 0L, 0L), mMaster.getTotalBytesOnTiers());
    Assert.assertEquals(ImmutableList.of(0L, 0L, 0L), mMaster.getUsedBytesOnTiers());
    long worker1 = mMaster.getWorkerId(new NetAddress("localhost", 80, 81));
    long worker2 = mMaster.getWorkerId(new NetAddress("localhost", 82, 83));
    addWorker(mMaster, worker1, ImmutableList.of(100L, 200L, 30L), ImmutableList.of(20L, 50L, 10L));
    Assert.assertEquals(330L, mMaster.getCapacityBytes());
    Assert.assertEquals(80L, mMaster.getUsedBytes());
{'fraction_non_alphanumeric': 0.10238164391519024, 'fraction_numerical': 0.018733662503630556, 'mean_word_length': 5.094690265486726, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, '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': '4263435', 'n_tokens_mistral': 2141, 'n_tokens_neox': 2027, 'n_words': 562}