<commit_before>/**
 * Copyright (c) 2017 Dell Inc., or its subsidiaries. All Rights Reserved.
 *
 * Licensed 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
 */
package io.pravega.segmentstore.storage;

import io.pravega.common.MathHelpers;
import io.pravega.common.hash.RandomFactory;
import io.pravega.segmentstore.contracts.BadOffsetException;
import io.pravega.segmentstore.contracts.SegmentProperties;
import io.pravega.segmentstore.contracts.StreamSegmentExistsException;
import io.pravega.segmentstore.contracts.StreamSegmentNotExistsException;
import io.pravega.segmentstore.contracts.StreamSegmentSealedException;
import io.pravega.test.common.AssertExtensions;
import io.pravega.test.common.ThreadPooledTestSuite;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.SequenceInputStream;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
import lombok.val;
import org.junit.Assert;
import org.junit.Test;

import static io.pravega.test.common.AssertExtensions.assertFutureThrows;
import static io.pravega.test.common.AssertExtensions.assertSuppliedFutureThrows;
import static io.pravega.test.common.AssertExtensions.assertThrows;

/**
 * Base class for testing any implementation of the Storage interface.
 */
public abstract class StorageTestBase extends ThreadPooledTestSuite {
    //region General Test arguments

    protected static final Duration TIMEOUT = Duration.ofSeconds(30);
    protected static final long DEFAULT_EPOCH = 1;
    protected static final int APPENDS_PER_SEGMENT = 10;
    protected static final String APPEND_FORMAT = "Segment_%s_Append_%d";
    private static final int SEGMENT_COUNT = 4;

    @Override
    protected int getThreadPoolSize() {
        return 5;
    }

    //endregion

    //region Tests

    /**
     * Tests the create() method.
     */
    @Test
    public void testCreate() {
        String segmentName = "foo_open";
        try (Storage s = createStorage()) {
            s.initialize(DEFAULT_EPOCH);
            createSegment(segmentName, s);
            Assert.assertTrue("Expected the segment to exist.", s.exists(segmentName, null).join());
            assertThrows("create() did not throw for existing StreamSegment.",
                    () -> createSegment(segmentName, s),
                    ex -> ex instanceof StreamSegmentExistsException);

            // Delete and make sure it can be recreated.
            s.openWrite(segmentName).thenCompose(handle -> s.delete(handle, null)).join();
            createSegment(segmentName, s);
            Assert.assertTrue("Expected the segment to exist.", s.exists(segmentName, null).join());
        }
    }

    /**
     * Tests the delete() method.
     */
    @Test
    public void testDelete() {
        String segmentName = "foo_open";
        try (Storage s = createStorage()) {
            s.initialize(DEFAULT_EPOCH);
            createSegment(segmentName, s);

            //Delete the segment.
            s.openWrite(segmentName).thenCompose(handle -> s.delete(handle, null)).join();
            Assert.assertFalse("Expected the segment to not exist.", s.exists(segmentName, null).join());
            assertThrows("getStreamSegmentInfo() did not throw for deleted StreamSegment.",
                    () -> s.getStreamSegmentInfo(segmentName, null).join(),
                    ex -> ex instanceof StreamSegmentNotExistsException);
        }
    }

    /**
     * Tests the openRead() and openWrite() methods.
     */
    @Test
    public void testOpen() {
        String segmentName = "foo_open";
        try (Storage s = createStorage()) {
            s.initialize(DEFAULT_EPOCH);

            // Segment does not exist.
            assertFutureThrows("openWrite() did not throw for non-existent StreamSegment.",
                    s.openWrite(segmentName),
                    ex -> ex instanceof StreamSegmentNotExistsException);

            assertFutureThrows("openRead() did not throw for non-existent StreamSegment.",
                    s.openRead(segmentName),
                    ex -> ex instanceof StreamSegmentNotExistsException);
        }
    }

    /**
     * Tests the write() method.
     *
     * @throws Exception if an unexpected error occurred.
     */
    @Test
    public void testWrite() throws Exception {
        String segmentName = "foo_write";
        int appendCount = 100;
<commit_msg>Issue #3198: Speed up HdfsStorageTest.testWrite() (#3199)<commit_after>/**
 * Copyright (c) 2017 Dell Inc., or its subsidiaries. All Rights Reserved.
 *
 * Licensed 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
 */
package io.pravega.segmentstore.storage;

import io.pravega.common.MathHelpers;
import io.pravega.common.hash.RandomFactory;
import io.pravega.segmentstore.contracts.BadOffsetException;
import io.pravega.segmentstore.contracts.SegmentProperties;
import io.pravega.segmentstore.contracts.StreamSegmentExistsException;
import io.pravega.segmentstore.contracts.StreamSegmentNotExistsException;
import io.pravega.segmentstore.contracts.StreamSegmentSealedException;
import io.pravega.test.common.AssertExtensions;
import io.pravega.test.common.ThreadPooledTestSuite;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.SequenceInputStream;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
import lombok.val;
import org.junit.Assert;
import org.junit.Test;

import static io.pravega.test.common.AssertExtensions.assertFutureThrows;
import static io.pravega.test.common.AssertExtensions.assertSuppliedFutureThrows;
import static io.pravega.test.common.AssertExtensions.assertThrows;

/**
 * Base class for testing any implementation of the Storage interface.
 */
public abstract class StorageTestBase extends ThreadPooledTestSuite {
    //region General Test arguments

    protected static final Duration TIMEOUT = Duration.ofSeconds(30);
    protected static final long DEFAULT_EPOCH = 1;
    protected static final int APPENDS_PER_SEGMENT = 10;
    protected static final String APPEND_FORMAT = "Segment_%s_Append_%d";
    private static final int SEGMENT_COUNT = 4;

    @Override
    protected int getThreadPoolSize() {
        return 5;
    }

    //endregion

    //region Tests

    /**
     * Tests the create() method.
     */
    @Test
    public void testCreate() {
        String segmentName = "foo_open";
        try (Storage s = createStorage()) {
            s.initialize(DEFAULT_EPOCH);
            createSegment(segmentName, s);
            Assert.assertTrue("Expected the segment to exist.", s.exists(segmentName, null).join());
            assertThrows("create() did not throw for existing StreamSegment.",
                    () -> createSegment(segmentName, s),
                    ex -> ex instanceof StreamSegmentExistsException);

            // Delete and make sure it can be recreated.
            s.openWrite(segmentName).thenCompose(handle -> s.delete(handle, null)).join();
            createSegment(segmentName, s);
            Assert.assertTrue("Expected the segment to exist.", s.exists(segmentName, null).join());
        }
    }

    /**
     * Tests the delete() method.
     */
    @Test
    public void testDelete() {
        String segmentName = "foo_open";
        try (Storage s = createStorage()) {
            s.initialize(DEFAULT_EPOCH);
            createSegment(segmentName, s);

            //Delete the segment.
            s.openWrite(segmentName).thenCompose(handle -> s.delete(handle, null)).join();
            Assert.assertFalse("Expected the segment to not exist.", s.exists(segmentName, null).join());
            assertThrows("getStreamSegmentInfo() did not throw for deleted StreamSegment.",
                    () -> s.getStreamSegmentInfo(segmentName, null).join(),
                    ex -> ex instanceof StreamSegmentNotExistsException);
        }
    }

    /**
     * Tests the openRead() and openWrite() methods.
     */
    @Test
    public void testOpen() {
        String segmentName = "foo_open";
        try (Storage s = createStorage()) {
            s.initialize(DEFAULT_EPOCH);

            // Segment does not exist.
            assertFutureThrows("openWrite() did not throw for non-existent StreamSegment.",
                    s.openWrite(segmentName),
                    ex -> ex instanceof StreamSegmentNotExistsException);

            assertFutureThrows("openRead() did not throw for non-existent StreamSegment.",
                    s.openRead(segmentName),
                    ex -> ex instanceof StreamSegmentNotExistsException);
        }
    }

    /**
     * Tests the write() method.
     *
     * @throws Exception if an unexpected error occurred.
     */
    @Test
    public void testWrite() throws Exception {
        String segmentName = "foo_write";
        int appendCount = 10;

{'fraction_non_alphanumeric': 0.09928776443074307, 'fraction_numerical': 0.004571064101201233, 'mean_word_length': 2.8604842018875667, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '538195', 'n_tokens_mistral': 2541, 'n_tokens_neox': 2488, 'n_words': 659}