<commit_before>/*
 * 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 org.f1x.store;


import org.f1x.SessionIDBean;
import org.f1x.TestCommon;
import org.f1x.api.FixAcceptorSettings;
import org.f1x.api.FixInitiatorSettings;
import org.f1x.api.FixVersion;
import org.f1x.api.message.MessageBuilder;
import org.f1x.api.message.MessageParser;
import org.f1x.api.message.fields.*;
import org.f1x.api.session.SessionID;
import org.f1x.api.session.SessionStatus;
import org.f1x.v1.FixSessionAcceptor;
import org.f1x.v1.FixSessionInitiator;
import org.f1x.v1.SingleSessionAcceptor;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

public class Test_ConcurrentMessageStore extends TestCommon {
    private static final int NUM_CLIENTS = 16;
    private static final int NUM_MESSAGES_PER_CLIENT_THREAD = 100;
    private static final int MAX_MESSAGE_SIZE = 4096;

    private static final int PORT = 7890;
    private static final String INITIATOR_SENDER_ID = "INITIATOR";
    private static final String ACCEPTOR_SENDER_ID = "ACCEPTOR";

    private final SessionID ServerSessionID = new SessionIDBean(ACCEPTOR_SENDER_ID, INITIATOR_SENDER_ID);
    private final SessionID ClientSessionID = new SessionIDBean(INITIATOR_SENDER_ID, ACCEPTOR_SENDER_ID);

    @Test(timeout = 120000)
    public void concurrentSend() throws InterruptedException, IOException {


        final MessageStore sentMessages = new InMemoryMessageStore(1 << 20);
        final MessageStore receivedMessages = new InMemoryMessageStore(1 << 20);

        final TestServer server = new TestServer(PORT, ServerSessionID, receivedMessages);
        final TestClient client = new TestClient(PORT, ClientSessionID, sentMessages);


        final Thread acceptorThread = new Thread(server, "EchoServer");
        acceptorThread.start();

        final Thread initiatorThread = new Thread(client, "EchoClient");
        initiatorThread.start();


        client.waitForAllMessages();

        client.disconnect("End of test");
        client.close();
        server.close();

        // compare sent and received messages
        final int comparedPrefixSize = 150;
        final byte [] sentMessageBuffer= new byte [MAX_MESSAGE_SIZE];
        final byte [] receivedMessageBuffer= new byte [MAX_MESSAGE_SIZE];
        for (int i=2; i <= NUM_CLIENTS*NUM_MESSAGES_PER_CLIENT_THREAD; i++) {

            Assert.assertEquals("Sent MsgSeqNum", i, sentMessages.get(i, sentMessageBuffer));
            Assert.assertEquals("Received MsgSeqNum", i, receivedMessages.get(i, receivedMessageBuffer));<commit_msg>Concurrent sending test for MessageStore<commit_after>/*
 * 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 org.f1x.store;


import org.f1x.SessionIDBean;
import org.f1x.TestCommon;
import org.f1x.api.FixAcceptorSettings;
import org.f1x.api.FixInitiatorSettings;
import org.f1x.api.FixVersion;
import org.f1x.api.message.MessageBuilder;
import org.f1x.api.message.MessageParser;
import org.f1x.api.message.fields.*;
import org.f1x.api.session.SessionID;
import org.f1x.api.session.SessionStatus;
import org.f1x.v1.FixSessionAcceptor;
import org.f1x.v1.FixSessionInitiator;
import org.f1x.v1.SingleSessionAcceptor;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

public class Test_ConcurrentMessageStore extends TestCommon {
    private static final int NUM_CLIENTS = 16;
    private static final int NUM_MESSAGES_PER_CLIENT_THREAD = 1024;
    private static final int MAX_MESSAGE_SIZE = 4096;

    private static final int PORT = 7890;
    private static final String INITIATOR_SENDER_ID = "INITIATOR";
    private static final String ACCEPTOR_SENDER_ID = "ACCEPTOR";

    private final SessionID ServerSessionID = new SessionIDBean(ACCEPTOR_SENDER_ID, INITIATOR_SENDER_ID);
    private final SessionID ClientSessionID = new SessionIDBean(INITIATOR_SENDER_ID, ACCEPTOR_SENDER_ID);

    @Test(timeout = 120000)
    public void concurrentSend() throws InterruptedException, IOException {


        final MessageStore sentMessages = new InMemoryMessageStore(1 << 24);
        final MessageStore receivedMessages = new InMemoryMessageStore(1 << 24);

        final TestServer server = new TestServer(PORT, ServerSessionID, receivedMessages);
        final TestClient client = new TestClient(PORT, ClientSessionID, sentMessages);


        final Thread acceptorThread = new Thread(server, "EchoServer");
        acceptorThread.start();

        final Thread initiatorThread = new Thread(client, "EchoClient");
        initiatorThread.start();


        client.waitForAllMessages();

        client.disconnect("End of test");
        client.close();
        server.close();

        // compare sent and received messages
        final int comparedPrefixSize = 150;
        final byte [] sentMessageBuffer= new byte [MAX_MESSAGE_SIZE];
        final byte [] receivedMessageBuffer= new byte [MAX_MESSAGE_SIZE];
        for (int i=2; i <= NUM_CLIENTS*NUM_MESSAGES_PER_CLIENT_THREAD; i++) {

            Assert.assertEquals("Sent MsgSeqNum", i, sentMessages.get(i, sentMessageBuffer));
            Assert.assertEquals("Received MsgSeqNum", i, receivedMessages.get(i, receivedMessageBuffer));
{'fraction_non_alphanumeric': 0.08402029169308814, 'fraction_numerical': 0.01601141407736208, 'mean_word_length': 4.167076167076167, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, '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': '1216457', 'n_tokens_mistral': 1864, 'n_tokens_neox': 1778, 'n_words': 525}