<commit_before>package com.spotify.netty4.handler.codec.zmtp;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;

import static com.spotify.netty4.handler.codec.zmtp.TestUtil.bytes;
import static com.spotify.netty4.handler.codec.zmtp.TestUtil.cmp;
import static java.util.Arrays.asList;
import static java.util.Arrays.fill;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class ZMTPFramingEncoderTest {

  @Mock ChannelHandlerContext ctx;
  @Mock ChannelPromise promise;

  @Captor ArgumentCaptor<ByteBuf> bufCaptor;

  private static final byte[] LARGE_FILL = new byte[500];
  static {
    fill(LARGE_FILL, (byte)0x61);
  }

  @Before
  public void setUp() {
    when(ctx.write(bufCaptor.capture(), eq(promise))).thenReturn(promise);
  }

  @Test
  public void testEncodeZMTP1() throws Exception {

    ZMTPSession session = new ZMTPSession(ZMTPConnectionType.Addressed, 1024);
    session.actualVersion(1);
    ZMTPFramingEncoder enc = new ZMTPFramingEncoder(session);

    ZMTPMessage message = new ZMTPMessage(
        asList(ZMTPFrame.from("id0"), ZMTPFrame.from("id1")),
        asList(ZMTPFrame.from("f0")));

    enc.write(ctx, message, promise);
    final ByteBuf buf = bufCaptor.getValue();
    cmp(buf, 4, 1, 0x69, 0x64, 0x30, 4, 1, 0x69, 0x64, 0x31, 1, 1, 3, 0, 0x66, 0x30);
    buf.release();
  }

  @Test
  public void testEncodeZMTP2() throws Exception {

    ZMTPMessage message = new ZMTPMessage(
        asList(ZMTPFrame.from("id0"), ZMTPFrame.from("id1")),
        asList(ZMTPFrame.from("f0")));

    ZMTPSession session = new ZMTPSession(ZMTPConnectionType.Addressed, 1024);
    session.actualVersion(2);
    ZMTPFramingEncoder enc = new ZMTPFramingEncoder(session);

    enc.write(ctx, message, promise);
    final ByteBuf buf = bufCaptor.getValue();
    cmp(buf, 1, 3, 0x69, 0x64, 0x30, 1, 3, 0x69, 0x64, 0x31, 1, 0, 0, 2, 0x66, 0x30);
    buf.release();
  }

  @Test
  public void testEncodeZMTP2Long() throws Exception {
    ZMTPMessage message = new ZMTPMessage(
        asList(ZMTPFrame.from("id0")),
        asList(ZMTPFrame.from(LARGE_FILL)));
    ByteBuf buf = Unpooled.buffer();
    buf.writeBytes(bytes(1, 3, 0x69, 0x64, 0x30,
                         1, 0,
                         2, 0, 0, 0, 0, 0, 0, 0x01, 0xf4));
    buf.writeBytes(LARGE_FILL);

    ZMTPSession session = new ZMTPSession(ZMTPConnectionType.Addressed, 1024);
    session.actualVersion(2);
    ZMTPFramingEncoder enc = new ZMTPFramingEncoder(session);

    enc.write(ctx, message, promise);
    final ByteBuf buf2 = bufCaptor.getValue();

    cmp(buf, buf2);

    buf.release();
    buf2.release();
  }
}<commit_msg>fix test<commit_after>package com.spotify.netty4.handler.codec.zmtp;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.util.concurrent.EventExecutor;

import static com.spotify.netty4.handler.codec.zmtp.TestUtil.bytes;
import static com.spotify.netty4.handler.codec.zmtp.TestUtil.cmp;
import static java.util.Arrays.asList;
import static java.util.Arrays.fill;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class ZMTPFramingEncoderTest {

  @Mock ChannelHandlerContext ctx;
  @Mock ChannelPromise promise;
  @Mock EventExecutor executor;

  @Captor ArgumentCaptor<ByteBuf> bufCaptor;

  private static final byte[] LARGE_FILL = new byte[500];
  static {
    fill(LARGE_FILL, (byte)0x61);
  }

  @Before
  public void setUp() {
    when(ctx.write(bufCaptor.capture(), any(ChannelPromise.class))).thenReturn(promise);
    when(ctx.alloc()).thenReturn(ByteBufAllocator.DEFAULT);
    when(ctx.executor()).thenReturn(executor);
  }

  @Test
  public void testEncodeZMTP1() throws Exception {

    ZMTPSession session = new ZMTPSession(ZMTPConnectionType.Addressed, 1024);
    session.actualVersion(1);
    ZMTPFramingEncoder enc = new ZMTPFramingEncoder(session);

    ZMTPMessage message = new ZMTPMessage(
        asList(ZMTPFrame.from("id0"), ZMTPFrame.from("id1")),
        asList(ZMTPFrame.from("f0")));

    enc.write(ctx, message, promise);
    enc.flush(ctx);
    final ByteBuf buf = bufCaptor.getValue();
    cmp(buf, 4, 1, 0x69, 0x64, 0x30, 4, 1, 0x69, 0x64, 0x31, 1, 1, 3, 0, 0x66, 0x30);
    buf.release();
  }

  @Test
  public void testEncodeZMTP2() throws Exception {

    ZMTPMessage message = new ZMTPMessage(
        asList(ZMTPFrame.from("id0"), ZMTPFrame.from("id1")),
        asList(ZMTPFrame.from("f0")));

    ZMTPSession session = new ZMTPSession(ZMTPConnectionType.Addressed, 1024);
    session.actualVersion(2);
    ZMTPFramingEncoder enc = new ZMTPFramingEncoder(session);

    enc.write(ctx, message, promise);
    enc.flush(ctx);
    final ByteBuf buf = bufCaptor.getValue();
    cmp(buf, 1, 3, 0x69, 0x64, 0x30, 1, 3, 0x69, 0x64, 0x31, 1, 0, 0, 2, 0x66, 0x30);
    buf.release();
  }

  @Test
  public void testEncodeZMTP2Long() throws Exception {
    ZMTPMessage message = new ZMTPMessage(
        asList(ZMTPFrame.from("id0")),
        asList(ZMTPFrame.from(LARGE_FILL)));
    ByteBuf buf = Unpooled.buffer();
    buf.writeBytes(bytes(1, 3, 0x69, 0x64, 0x30,
                         1, 0,
                         2, 0, 0, 0, 0, 0, 0, 0x01, 0xf4));
    buf.writeBytes(LARGE_FILL);

    ZMTPSession session = new ZMTPSession(ZMTPConnectionType.Addressed, 1024);
    session.actualVersion(2);
    ZMTPFramingEncoder enc = new ZMTPFramingEncoder(session);

    enc.write(ctx, message, promise);
    enc.flush(ctx);
    final ByteBuf buf2 = bufCaptor.getValue();

    cmp(buf, buf2);

    buf.release();
    buf2.release();
  }
}
{'fraction_non_alphanumeric': 0.12494061757719715, 'fraction_numerical': 0.039904988123515436, 'mean_word_length': 3.9654088050314464, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5147482', 'n_tokens_mistral': 2462, 'n_tokens_neox': 2222, 'n_words': 453}