<commit_before>/*
 * Copyright 2015 MongoDB, Inc.
 *
 * 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
 *
 * 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.mongodb.connection

import category.Slow
import com.mongodb.MongoSocketOpenException
import com.mongodb.MongoSocketReadTimeoutException
import com.mongodb.OperationFunctionalSpecification
import com.mongodb.ServerAddress
import com.mongodb.connection.netty.NettyStreamFactory
import org.bson.BsonDocument
import org.bson.BsonInt32
import org.bson.BsonString
import org.junit.experimental.categories.Category
import spock.lang.IgnoreIf
import spock.lang.Unroll

import java.util.concurrent.TimeUnit

import static com.mongodb.ClusterFixture.getCredentialList
import static com.mongodb.ClusterFixture.getPrimary
import static com.mongodb.ClusterFixture.getSslSettings
import static com.mongodb.connection.CommandHelper.executeCommand

@Category(Slow)
@IgnoreIf({ System.getProperty('java.version').startsWith('1.6.') })
class AsyncStreamTimeoutsSpecification extends OperationFunctionalSpecification {

    static SocketSettings openSocketSettings = SocketSettings.builder().connectTimeout(1, TimeUnit.MILLISECONDS).build();
    static SocketSettings readSocketSettings = SocketSettings.builder().readTimeout(5, TimeUnit.SECONDS).build();

    @Unroll
    def 'should throw a MongoSocketOpenException when the #description Stream fails to open'() {
        given:
        def connection = new InternalStreamConnectionFactory(streamFactory, getCredentialList(), new NoOpConnectionListener())
                .create(new ServerId(new ClusterId(), new ServerAddress(new InetSocketAddress('192.168.255.255', 27017))));

        when:
        connection.open()

        then:
        thrown(MongoSocketOpenException)

        where:
        description             | streamFactory
        'AsynchronousSocket'    | new AsynchronousSocketChannelStreamFactory(openSocketSettings, SslSettings.builder().build())
        'NettyStream'           | new NettyStreamFactory(openSocketSettings, getSslSettings())
    }

    @Unroll
    def 'should throw a MongoSocketReadTimeoutException with the #description stream'() {
        given:
        def connection = new InternalStreamConnectionFactory(streamFactory, getCredentialList(), new NoOpConnectionListener())
                .create(new ServerId(new ClusterId(), getPrimary()))
        connection.open()

        getCollectionHelper().insertDocuments(new BsonDocument('_id', new BsonInt32(1)));
        def countCommand = new BsonDocument('count', new BsonString(getCollectionName()))
        countCommand.put('query', new BsonDocument('$where', new BsonString('sleep(5050); return true;')))

        when:
        executeCommand(getDatabaseName(), countCommand, connection)

        then:
        thrown(MongoSocketReadTimeoutException)

        cleanup:
        connection?.close()

        where:
        description             | streamFactory
        'AsynchronousSocket'    | new AsynchronousSocketChannelStreamFactory(readSocketSettings, SslSettings.builder().build())
        'NettyStream'           | new NettyStreamFactory(readSocketSettings, getSslSettings())
    }

}<commit_msg>AsyncStreamTimeoutsSpecification fixes for the Jenkins matrix<commit_after>/*
 * Copyright 2015 MongoDB, Inc.
 *
 * 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
 *
 * 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.mongodb.connection

import category.Slow
import com.mongodb.MongoSocketOpenException
import com.mongodb.MongoSocketReadTimeoutException
import com.mongodb.OperationFunctionalSpecification
import com.mongodb.ServerAddress
import com.mongodb.connection.netty.NettyStreamFactory
import org.bson.BsonDocument
import org.bson.BsonInt32
import org.bson.BsonString
import org.junit.experimental.categories.Category
import spock.lang.IgnoreIf

import java.util.concurrent.TimeUnit

import static com.mongodb.ClusterFixture.getCredentialList
import static com.mongodb.ClusterFixture.getPrimary
import static com.mongodb.ClusterFixture.getSslSettings
import static com.mongodb.connection.CommandHelper.executeCommand

@Category(Slow)
class AsyncStreamTimeoutsSpecification extends OperationFunctionalSpecification {

    static SocketSettings openSocketSettings = SocketSettings.builder().connectTimeout(1, TimeUnit.MILLISECONDS).build();
    static SocketSettings readSocketSettings = SocketSettings.builder().readTimeout(5, TimeUnit.SECONDS).build();

    @IgnoreIf({ System.getProperty('java.version').startsWith('1.6.') || getSslSettings().isEnabled() })
    def 'should throw a MongoSocketOpenException when the AsynchronousSocket Stream fails to open'() {
        given:
        def connection = new InternalStreamConnectionFactory(
                new AsynchronousSocketChannelStreamFactory(openSocketSettings, getSslSettings()), getCredentialList(),
                new NoOpConnectionListener()
        ).create(new ServerId(new ClusterId(), new ServerAddress(new InetSocketAddress('192.168.255.255', 27017))));

        when:
        connection.open()

        then:
        thrown(MongoSocketOpenException)
    }

    @IgnoreIf({ System.getProperty('java.version').startsWith('1.6.') || getSslSettings().isEnabled() })
    def 'should throw a MongoSocketReadTimeoutException with the AsynchronousSocket stream'() {
        given:
        def connection = new InternalStreamConnectionFactory(
                new AsynchronousSocketChannelStreamFactory(readSocketSettings, getSslSettings()), getCredentialList(),
                new NoOpConnectionListener()
        ).create(new ServerId(new ClusterId(), getPrimary()))
        connection.open()

        getCollectionHelper().insertDocuments(new BsonDocument('_id', new BsonInt32(1)));
        def countCommand = new BsonDocument('count', new BsonString(getCollectionName()))
        countCommand.put('query', new BsonDocument('$where', new BsonString('sleep(5050); return true;')))

        when:
        executeCommand(getDatabaseName(), countCommand, connection)

        then:
        thrown(MongoSocketReadTimeoutException)

        cleanup:
        connection?.close()
    }

    def 'should throw a MongoSocketOpenException when the Netty Stream fails to open'() {
        given:
        def connection = new InternalStreamConnectionFactory(
                new NettyStreamFactory(openSocketSettings, getSslSettings()), getCredentialList(), new NoOpConnectionListener()
        ).create(new ServerId(new ClusterId(), new ServerAddress(new InetSocketAddress('192.168.255.255', 27017))));

        when:
        connection.open()

        then:
        thrown(MongoSocketOpenException)
    }


    def 'should throw a MongoSocketReadTimeoutException with the Netty stream'() {
        given:
        def connection = new InternalStreamConnectionFactory(
                new NettyStreamFactory(readSocketSettings, getSslSettings()), getCredentialList(), new NoOpConnectionListener()
        ).create(new ServerId(new ClusterId(), getPrimary()))
        connection.open()

        getCollectionHelper().insertDocuments(new BsonDocument('_id', new BsonInt32(1)));
        def countCommand = new BsonDocument('count', new BsonString(getCollectionName()))
        countCommand.put('query', new BsonDocument('$where', new BsonString('sleep(5050); return true;')))

        when:
        executeCommand(getDatabaseName(), countCommand, connection)

        then:
        thrown(MongoSocketReadTimeoutException)

        cleanup:
        connection?.close()
    }

}
{'fraction_non_alphanumeric': 0.09053254437869822, 'fraction_numerical': 0.012071005917159764, 'mean_word_length': 4.060479041916167, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 3, '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': '3961796', 'n_tokens_mistral': 2264, 'n_tokens_neox': 2135, 'n_words': 557}