<commit_before>/**
 * Licensed to the Apache Software Foundation (ASF) 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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 com.emc.pravega.stream.impl.segment;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.verify;

import java.nio.ByteBuffer;

import org.junit.Ignore;
import org.junit.Test;

import com.emc.pravega.common.netty.ClientConnection;
import com.emc.pravega.common.netty.ConnectionFailedException;
import com.emc.pravega.common.netty.ReplyProcessor;
import com.emc.pravega.common.netty.WireCommands.ReadSegment;
import com.emc.pravega.common.netty.WireCommands.SegmentRead;
import com.emc.pravega.stream.impl.segment.AsyncSegmentInputStream.ReadFuture;
import com.emc.pravega.testcommon.Async;

public class AsyncSegmentInputStreamTest {

    @Test
    public void testRetry() throws ConnectionFailedException {
        String segment = "testRetry";
        String endpoint = "localhost";
        TestConnectionFactoryImpl connectionFactory = new TestConnectionFactoryImpl(endpoint);
        AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(connectionFactory , connectionFactory, segment);
        ClientConnection c = mock(ClientConnection.class);
        connectionFactory.provideConnection(endpoint, c);
        ReadFuture readFuture = in.read(1234, 5678);
        ReplyProcessor processor = connectionFactory.getProcessor(endpoint);
        verify(c).send(new ReadSegment(segment, 1234, 5678));
        processor.connectionDropped();
        verify(c).close();
        assertFalse(readFuture.isSuccess());
        SegmentRead segmentRead = new SegmentRead(segment, 1234, false, false, ByteBuffer.allocate(0));
        SegmentRead result = Async.testBlocking(() -> in.getResult(readFuture) , () -> {
            processor.segmentRead(segmentRead);
        });
        verify(c, times(2)).send(new ReadSegment(segment, 1234, 5678));
        assertTrue(readFuture.isSuccess());
        assertEquals(segmentRead, result);
        verifyNoMoreInteractions(c);
    }
    
    @Test
    public void testRead() throws ConnectionFailedException {
        String segment = "testRetry";
        String endpoint = "localhost";
        TestConnectionFactoryImpl connectionFactory = new TestConnectionFactoryImpl(endpoint);
        AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(connectionFactory , connectionFactory, segment);
        ClientConnection c = mock(ClientConnection.class);
        connectionFactory.provideConnection(endpoint, c);
        ReadFuture readFuture = in.read(1234, 5678);
        ReplyProcessor processor = connectionFactory.getProcessor(endpoint);
        verify(c).send(new ReadSegment(segment, 1234, 5678));
        SegmentRead segmentRead = new SegmentRead(segment, 1234, false, false, ByteBuffer.allocate(0));
        processor.segmentRead(segmentRead);
        assertTrue(readFuture.isSuccess());
        assertEquals(segmentRead, in.getResult(readFuture));
        verifyNoMoreInteractions(c);
    }
    
    @Test
    public void testWrongOffsetReturned() throws ConnectionFailedException {
        String segment = "testRetry";
        String endpoint = "localhost";
        TestConnectionFactoryImpl connectionFactory = new TestConnectionFactoryImpl(endpoint);
        AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(connectionFactory , connectionFactory, segment);
        ClientConnection c = mock(ClientConnection.class);
        connectionFactory.provideConnection(endpoint, c);
        ReadFuture readFuture = in.read(1234, 5678);
        ReplyProcessor processor = connectionFactory.getProcessor(endpoint);
        verify(c).send(new ReadSegment(segment, 1234, 5678));
        processor.segmentRead(new SegmentRead(segment, 1235, false, false, ByteBuffer.allocate(0)));<commit_msg>Fix tests to correctly test async send instead of sync send<commit_after>/**
 * Licensed to the Apache Software Foundation (ASF) 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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 com.emc.pravega.stream.impl.segment;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

import java.nio.ByteBuffer;

import org.junit.ClassRule;
import org.junit.Test;

import com.emc.pravega.common.netty.ClientConnection;
import com.emc.pravega.common.netty.ConnectionFailedException;
import com.emc.pravega.common.netty.ReplyProcessor;
import com.emc.pravega.common.netty.WireCommands.ReadSegment;
import com.emc.pravega.common.netty.WireCommands.SegmentRead;
import com.emc.pravega.stream.impl.segment.AsyncSegmentInputStream.ReadFuture;
import com.emc.pravega.testcommon.Async;

import lombok.Cleanup;

public class AsyncSegmentInputStreamTest {

    @Test
    public void testRetry() throws ConnectionFailedException {
        String segment = "testRetry";
        String endpoint = "localhost";
        TestConnectionFactoryImpl connectionFactory = new TestConnectionFactoryImpl(endpoint);
        @Cleanup
        AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(connectionFactory , connectionFactory, segment);
        ClientConnection c = mock(ClientConnection.class);
        connectionFactory.provideConnection(endpoint, c);
        ReadFuture readFuture = in.read(1234, 5678);
        ReplyProcessor processor = connectionFactory.getProcessor(endpoint);
        verify(c).sendAsync(new ReadSegment(segment, 1234, 5678));
        processor.connectionDropped();
        verify(c).close();
        assertFalse(readFuture.isSuccess());
        SegmentRead segmentRead = new SegmentRead(segment, 1234, false, false, ByteBuffer.allocate(0));
        SegmentRead result = Async.testBlocking(() -> in.getResult(readFuture) , () -> {
            processor.segmentRead(segmentRead);
        });
        verify(c).send(new ReadSegment(segment, 1234, 5678));
        assertTrue(readFuture.isSuccess());
        assertEquals(segmentRead, result);
        verifyNoMoreInteractions(c);
    }
    
    @Test
    public void testRead() throws ConnectionFailedException {
        String segment = "testRetry";
        String endpoint = "localhost";
        TestConnectionFactoryImpl connectionFactory = new TestConnectionFactoryImpl(endpoint);
        @Cleanup
        AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(connectionFactory , connectionFactory, segment);
        ClientConnection c = mock(ClientConnection.class);
        connectionFactory.provideConnection(endpoint, c);
        ReadFuture readFuture = in.read(1234, 5678);
        ReplyProcessor processor = connectionFactory.getProcessor(endpoint);
        verify(c).sendAsync(new ReadSegment(segment, 1234, 5678));
        SegmentRead segmentRead = new SegmentRead(segment, 1234, false, false, ByteBuffer.allocate(0));
        processor.segmentRead(segmentRead);
        assertTrue(readFuture.isSuccess());
        assertEquals(segmentRead, in.getResult(readFuture));
        verifyNoMoreInteractions(c);
    }
    
    @Test
    public void testWrongOffsetReturned() throws ConnectionFailedException {
        String segment = "testRetry";
        String endpoint = "localhost";
        TestConnectionFactoryImpl connectionFactory = new TestConnectionFactoryImpl(endpoint);
        @Cleanup
        AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(connectionFactory , connectionFactory, segment);
        ClientConnection c = mock(ClientConnection.class);
        connectionFactory.provideConnection(endpoint, c);
        ReadFuture readFuture = in.read(1234, 5678);
        ReplyProcessor processor = connectionFactory.getProcessor(endpoint);
        verify(c).sendAsync(new ReadSegment(segment, 1234, 5678));
        processor.segmentRead(new SegmentRead(segment, 1235, false, false, ByteBuffer.allocate(0)));
{'fraction_non_alphanumeric': 0.08896910500053085, 'fraction_numerical': 0.016031425841384436, 'mean_word_length': 4.122349102773247, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '5222601', 'n_tokens_mistral': 2479, 'n_tokens_neox': 2377, 'n_words': 639}