<commit_before>/**
 * Licensed to the zk1931 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 com.github.zk1931.jzab.transport;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import com.github.zk1931.jzab.MessageBuilder;
import com.github.zk1931.jzab.TestBase;
import com.github.zk1931.jzab.proto.ZabMessage.Message;
import com.github.zk1931.jzab.proto.ZabMessage.Message.MessageType;
import com.github.zk1931.jzab.SslParameters;
import com.github.zk1931.jzab.Zxid;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Test NettyTransport.
 */
public class NettyTransportTest extends TestBase {
  private static final Logger LOG = LoggerFactory
                                    .getLogger(NettyTransportTest.class);

  private static final Transport.Receiver NOOP = new Transport.Receiver() {
    @Override
    public void onReceived(String source, Message message) {
    }

    @Override
    public void onDisconnected(String source) {
    }
  };

  public static ByteBuffer createByteBuffer(int num) {
    ByteBuffer bb = ByteBuffer.allocate(4);
    bb.putInt(num);
    bb.flip();
    return bb;
  }

  public static Message createAck(Zxid zxid) {
    return MessageBuilder.buildAck(zxid);
  }

  /**
   * Make sure the constructor fails when the port is invalid.
   */
  @Test(timeout=10000, expected=IllegalArgumentException.class)
  public void testInvalidPort() throws Exception {
    NettyTransport transport = new NettyTransport(getHostPort(-1), NOOP,
                                                  getDirectory());
  }

  @Test(timeout=1000)
  public void testLocalSend() throws Exception {
    final String localId = getUniqueHostPort();

    // receiver simply appends messages to a list.
    final LinkedList<Zxid> messages = new LinkedList<>();
    Transport.Receiver receiver = new Transport.Receiver() {
      public void onReceived(String source, Message message) {
        Assert.assertEquals(localId, source);
        Zxid zxid = MessageBuilder.fromProtoZxid(message.getAck().getZxid());
        messages.add(zxid);
      }
      public void onDisconnected(String source) {
      }
    };

    // send messages to itself.
    NettyTransport transport = new NettyTransport(localId, receiver,
                                                  getDirectory());
    for (int i = 0; i < 20; i++) {
      transport.send(localId, createAck(new Zxid(0, i)));
    }

    // receive messages.
    for (int i = 0; i < 20; i++) {
      Zxid zxid = messages.pop();
      LOG.debug("Received a message: {}", zxid);
      Assert.assertEquals(new Zxid(0, i), zxid);
    }
    Assert.assertTrue(messages.isEmpty());
  }

  @Test(timeout=5000)
  public void testConnectFailed() throws Exception {
    final String peerA = getUniqueHostPort();
    final String peerB = getUniqueHostPort();
    final CountDownLatch disconnected = new CountDownLatch(1);
    Transport.Receiver receiverA = new Transport.Receiver() {
      public void onReceived(String source, Message message) {
      }
      public void onDisconnected(String source) {
        disconnected.countDown();
      }
    };
    NettyTransport transportA = new NettyTransport(peerA, receiverA,
                                                   getDirectory());
    transportA.send(peerB, createAck(new Zxid(0, 0)));
    disconnected.await();
  }

  @Test(timeout=5000)
  public void testSend() throws Exception {
    final String peerA = getUniqueHostPort();
    final String peerB = getUniqueHostPort();

    // receiver simply appends messages to a list.
    int messageCount = 20;
    final LinkedList<Zxid> messagesA = new LinkedList<>();
    final LinkedList<Zxid> messagesB = new LinkedList<>();

    final CountDownLatch latchA = new CountDownLatch(messageCount);
    final CountDownLatch latchB = new CountDownLatch(messageCount);

    Transport.Receiver receiverA = new Transport.Receiver() {
      public void onReceived(String source, Message message) {
        Assert.assertEquals(peerB, source);<commit_msg>set NettyTransportTest.testSend timeout to 10 seconds.<commit_after>/**
 * Licensed to the zk1931 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 com.github.zk1931.jzab.transport;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import com.github.zk1931.jzab.MessageBuilder;
import com.github.zk1931.jzab.TestBase;
import com.github.zk1931.jzab.proto.ZabMessage.Message;
import com.github.zk1931.jzab.proto.ZabMessage.Message.MessageType;
import com.github.zk1931.jzab.SslParameters;
import com.github.zk1931.jzab.Zxid;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Test NettyTransport.
 */
public class NettyTransportTest extends TestBase {
  private static final Logger LOG = LoggerFactory
                                    .getLogger(NettyTransportTest.class);

  private static final Transport.Receiver NOOP = new Transport.Receiver() {
    @Override
    public void onReceived(String source, Message message) {
    }

    @Override
    public void onDisconnected(String source) {
    }
  };

  public static ByteBuffer createByteBuffer(int num) {
    ByteBuffer bb = ByteBuffer.allocate(4);
    bb.putInt(num);
    bb.flip();
    return bb;
  }

  public static Message createAck(Zxid zxid) {
    return MessageBuilder.buildAck(zxid);
  }

  /**
   * Make sure the constructor fails when the port is invalid.
   */
  @Test(timeout=10000, expected=IllegalArgumentException.class)
  public void testInvalidPort() throws Exception {
    NettyTransport transport = new NettyTransport(getHostPort(-1), NOOP,
                                                  getDirectory());
  }

  @Test(timeout=1000)
  public void testLocalSend() throws Exception {
    final String localId = getUniqueHostPort();

    // receiver simply appends messages to a list.
    final LinkedList<Zxid> messages = new LinkedList<>();
    Transport.Receiver receiver = new Transport.Receiver() {
      public void onReceived(String source, Message message) {
        Assert.assertEquals(localId, source);
        Zxid zxid = MessageBuilder.fromProtoZxid(message.getAck().getZxid());
        messages.add(zxid);
      }
      public void onDisconnected(String source) {
      }
    };

    // send messages to itself.
    NettyTransport transport = new NettyTransport(localId, receiver,
                                                  getDirectory());
    for (int i = 0; i < 20; i++) {
      transport.send(localId, createAck(new Zxid(0, i)));
    }

    // receive messages.
    for (int i = 0; i < 20; i++) {
      Zxid zxid = messages.pop();
      LOG.debug("Received a message: {}", zxid);
      Assert.assertEquals(new Zxid(0, i), zxid);
    }
    Assert.assertTrue(messages.isEmpty());
  }

  @Test(timeout=5000)
  public void testConnectFailed() throws Exception {
    final String peerA = getUniqueHostPort();
    final String peerB = getUniqueHostPort();
    final CountDownLatch disconnected = new CountDownLatch(1);
    Transport.Receiver receiverA = new Transport.Receiver() {
      public void onReceived(String source, Message message) {
      }
      public void onDisconnected(String source) {
        disconnected.countDown();
      }
    };
    NettyTransport transportA = new NettyTransport(peerA, receiverA,
                                                   getDirectory());
    transportA.send(peerB, createAck(new Zxid(0, 0)));
    disconnected.await();
  }

  @Test(timeout=10000)
  public void testSend() throws Exception {
    final String peerA = getUniqueHostPort();
    final String peerB = getUniqueHostPort();

    // receiver simply appends messages to a list.
    int messageCount = 20;
    final LinkedList<Zxid> messagesA = new LinkedList<>();
    final LinkedList<Zxid> messagesB = new LinkedList<>();

    final CountDownLatch latchA = new CountDownLatch(messageCount);
    final CountDownLatch latchB = new CountDownLatch(messageCount);

    Transport.Receiver receiverA = new Transport.Receiver() {
      public void onReceived(String source, Message message) {
        Assert.assertEquals(peerB, source);
{'fraction_non_alphanumeric': 0.09453413732736822, 'fraction_numerical': 0.013907800038902938, 'mean_word_length': 3.2845833333333334, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6542425', 'n_tokens_mistral': 2963, 'n_tokens_neox': 2839, 'n_words': 836}