Skip to content

Commit

Permalink
JUnit added + test added
Browse files Browse the repository at this point in the history
  • Loading branch information
darkwitch committed Mar 21, 2017
1 parent 605f69e commit fde64fb
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 1 deletion.
3 changes: 2 additions & 1 deletion build.gradle
Expand Up @@ -5,7 +5,7 @@ plugins {
group 'udpserversocketchannel'
version '1.0'

sourceCompatibility = 1.7
sourceCompatibility = 1.8

compileJava.dependsOn(clean)

Expand All @@ -23,6 +23,7 @@ repositories {

dependencies {
compile group: 'io.netty', name: 'netty-all', version: '4.1.9.Final'
testCompile 'junit:junit:4.12'
}

jar {
Expand Down
@@ -0,0 +1,143 @@
package udpserversocketchannel.channel;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import org.junit.Assert;
import org.junit.Test;

import javax.xml.bind.DatatypeConverter;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class NioUdpServerChannelTest {
private static final String[] packets = new String[]{
"ab0100130047420f00000000006e004460d058",
"ab0100640047420f0000000000f0004460d05826fd00000f383633373033303330303537323135140000000000000000000000000000000000000000800ebd0d0000f3000000000000000000000000000000000000000000000000000000000000000000",
"ab01008f0047420f0000000000cb00000000004460d058d058000008df170300ff191c0000fe030f0000fe160d0000fed301000002b9040000fd74e724040074e7240400000000000000000000003a590000002b0500000051990200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"ab0100130041420f00000000006e005366d058",
"ab0100640041420f0000000000f0005366d0580efc00000f383633373033303330303538383333140000000000000000000000000000000000000000ac0ed70d00007c010000000000000000000000000000000000000000000000000000000000000000",
"ab01008f0041420f0000000000cb00000000005466d058573300000833c10200ffac1c0000fe4e190000fe5e030000feb7010000028c110000fd8f656c000030ab010000000000000000000000006d15000000003200000051990200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"ab0100130046420f00000000006e006868d058",
"ab0100640046420f0000000000f0006868d058eafc00000f383633373033303330303633323339140000000000000000000000000000000000000000a30ec20d0000fa000000000000000000000000000000000000000000000000000000000000000000",
"ab01008f0046420f0000000000cb00000000006968d0584853000008be8b0200ff691b0000fee20f0000fe870b0000fed90000000280020000fd71e724040071e7240400000000000000000000003f59000000ad0f00000051990200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"ab0100130042420f00000000006e001c69d058",
"ab0100640042420f0000000000f0001c69d058fafb00000f383633373033303330303635363232140000000000000000000000000000000000000000a60ef00d000063010000000000000000000000000000000000000000000000000000000000000000",
"ab01008f0042420f0000000000cb00000000001d69d0585311000008026f0000ff951c0000fe2d100000fe680c0000fe2a0100000296020000fd0f346b000026ab01000000000000000000000000530c000000a00f00000051990200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"ab0100640045420f0000000000f0007372d05876fd00000f383633373033303330303537333330140000000000000000000000000000000000000000ba0e2c0e000009010000000000000000000000000000000000000000000000000000000000000000",
"ab01008f0045420f0000000000cb00000000007472d058e766000008a5500300ff711c0000fee5100000fe8c0b0000fe4301000002b6030000fdf240250400f24025040000000000000000000000dc520000002a2b00000051990200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"ab0100130048420f00000000006e007777d058",
"ab0100640048420f0000000000f0007777d05822fc00000f383633373033303330303635383132140000000000000000000000000000000000000000c50ec90d000014010000000000000000000000000000000000000000000000000000000000000000",
"ab01008f0048420f0000000000cb00000000007877d0583a68000007e9f70200fec81b0000fe02110000fec60a0000fed600000002ac020000fdd4336b000076c9010000000000000000000000006d15000000403800000051990200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"ab0100130044420f00000000006e00dc77d058",
"ab0100640044420f0000000000f000dc77d0585efc00000f383633373033303330303538383137140000000000000000000000000000000000000000ac0ed70d000010010000000000000000000000000000000000000000000000000000000000000000",
"ab01008f0044420f0000000000cb0000000000dd77d0581de300000893f10500ffef1b0000fe61120000fe8e090000fec400000002b8020000fd52c46300003aab010000000000000000000000008869000000844e00000051990200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"ab0100640047420f0000000000f000637cd0583afd00000f383633373033303330303537323135140000000000000000000000000000000000000000800e1b0e0000f5000000000000000000000000000000000000000000000000000000000000000000",
"ab01008f0047420f0000000000cb0000000000647cd058d158000008ed170300ff161c0000fe1f100000fef70b0000feec000000029d020000fd74e724040074e7240400000000000000000000003c590000004b5300000051990200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"ab0100640041420f0000000000f0007282d0580efc00000f383633373033303330303538383333140000000000000000000000000000000000000000810e370e000081010000000000000000000000000000000000000000000000000000000000000000",
"ab01008f0041420f0000000000cb00000000007382d0585b330000089cc10200ff471c0000fee3180000fe64030000fe070200000296140000fd8f656c000030ab010000000000000000000000006f15000000208000000051990200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"ab0100640046420f0000000000f0008b84d058eafc00000f383633373033303330303633323339140000000000000000000000000000000000000000940e1c0e0000f9000000000000000000000000000000000000000000000000000000000000000000",
"ab01008f0046420f0000000000cb00000000008c84d0584953000008cc8b0200ff371b0000fe6d100000feca0a0000fee700000002d7020000fd71e724040071e7240400000000000000000000004159000000d15d00000051990200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"ab0100640042420f0000000000f0003a85d058e6fb00000f3836333730333033303036353632321400000000000000000000000000000000000000008f0e2b0e000062010000000000000000000000000000000000000000000000000000000000000000",
"ab01008f0042420f0000000000cb00000000003b85d05855110000080f6f0000ffee1b0000fe7b100000fe730b0000fedc0000000296020000fd0f346b000026ab01000000000000000000000000550c000000c05d00000051990200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"ab0100640045420f0000000000f000938ed05876fd00000f383633373033303330303537333330140000000000000000000000000000000000000000a80e2b0e00000d010000000000000000000000000000000000000000000000000000000000000000",
"ab01008f0045420f0000000000cb0000000000948ed058ea66000008b9500300ffd91b0000fec3100000fe160b0000fe6a0100000254040000fdf240250400f24025040000000000000000000000de520000004a7900000051990200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"ab0100640048420f0000000000f0009693d05822fc00000f383633373033303330303635383132140000000000000000000000000000000000000000a80e270e000010010000000000000000000000000000000000000000000000000000000000000000",
"ab01008f0048420f0000000000cb00000000009793d0584b6800000781f80200fe161b0000fef4100000fe220a0000fee20000000200030000fdd4336b000076c9010000000000000000000000006f15000000608600000051990200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"ab0100640044420f0000000000f000fc93d0585efc00000f3836333730333033303035383831371400000000000000000000000000000000000000009a0e3a0e000010010000000000000000000000000000000000000000000000000000000000000000",
"ab01008f0044420f0000000000cb0000000000fd93d05822e3000008b9f10500ff7b1b0000fe04100000fe770b0000fee701000002b8050000fd52c46300003aab010000000000000000000000008a69000000a49c00000051990200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"ab0100640047420f0000000000f0008398d05826fd00000f383633373033303330303537323135140000000000000000000000000000000000000000810e1c0e0000fb000000000000000000000000000000000000000000000000000000000000000000",
"ab01008f0047420f0000000000cb00000000008398d058d358000008fb170300ff641b0000fe58100000fe0c0b0000feea00000002d0020000fd74e724040074e7240400000000000000000000003e590000006aa100000051990200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"ab0100640041420f0000000000f000939ed0580efc00000f383633373033303330303538383333140000000000000000000000000000000000000000a20e370e000084010000000000000000000000000000000000000000000000000000000000000000",
"ab01008f0041420f0000000000cb0000000000939ed0585f330000080fc20200ff121c0000fef2180000fe20030000fe200200000266170000fd8f656c000030ab01000000000000000000000000711500000040ce00000051990200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"ab0100640046420f0000000000f000aba0d058d6fc00000f383633373033303330303633323339140000000000000000000000000000000000000000840e1c0e0000fd000000000000000000000000000000000000000000000000000000000000000000",
"ab01008f0046420f0000000000cb0000000000aca0d0584b53000008da8b0200fff51a0000fe4f100000fea60a0000fee200000002d0020000fd71e724040071e7240400000000000000000000004359000000ecab00000051990200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"ab0100640042420f0000000000f0005aa1d058fafb00000f383633373033303330303635363232140000000000000000000000000000000000000000970e2e0e00006b010000000000000000000000000000000000000000000000000000000000000000",
"ab01008f0042420f0000000000cb00000000005aa1d05857110000081d6f0000ff0a1c0000fe76100000fe940b0000feea00000002ba020000fd0f346b000026ab01000000000000000000000000570c000000e0ab00000051990200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"ab0100130045420f00000000006e00b3aad058",
"ab0100640045420f0000000000f000b3aad05876fd00000f383633373033303330303537333330140000000000000000000000000000000000000000c90ecf0d00000e010000000000000000000000000000000000000000000000000000000000000000",
"ab01008f0045420f0000000000cb0000000000b4aad058ec66000008cf500300ffe31b0000fea1100000fe420b0000fe660100000238040000fdf240250400f24025040000000000000000000000e0520000006ac700000051990200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"ab0100640048420f0000000000f000b7afd05822fc00000f383633373033303330303635383132140000000000000000000000000000000000000000c30e260e000011010000000000000000000000000000000000000000000000000000000000000000",
"ab01008f0048420f0000000000cb0000000000b7afd0585a6800000716f90200fe1c1b0000fece100000fe4e0a0000feeb0000000212030000fdd4336b000076c901000000000000000000000000711500000080d400000051990200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"ab0100640044420f0000000000f0001db0d05836fc00000f3836333730333033303035383831371400000000000000000000000000000000000000009d0e370e000014010000000000000000000000000000000000000000000000000000000000000000",
"ab01008f0044420f0000000000cb00000000001db0d05827e3000008e0f10500ffba1b0000fe4a100000fe700b0000fe160200000248060000fd52c46300003aab010000000000000000000000008c69000000c4ea00000051990200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"ab0100130047420f00000000006e00a3b4d058",
"ab0100640047420f0000000000f000a3b4d05826fd00000f3836333730333033303035373231351400000000000000000000000000000000000000007e0ebf0d0000fa000000000000000000000000000000000000000000000000000000000000000000",
"ab01008f0047420f0000000000cb0000000000a4b4d058d55800000809180300ffba1b0000fe7e100000fe3c0b0000fe200100000267030000fd74e724040074e72404000000000000000000000040590000008aef00000051990200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"ab0100130041420f00000000006e00b3bad058",
"ab0100640041420f0000000000f000b3bad0580efc00000f383633373033303330303538383333140000000000000000000000000000000000000000ad0ed70d000080010000000000000000000000000000000000000000000000000000000000000000",
"ab01008f0041420f0000000000cb0000000000b4bad058633300000888c20200ff3d1a0000feea160000fe53030000fe3d020000021e170000fd8f656c000030ab010000000000000000000000007315000000601c01000051990200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"ab0100130046420f00000000006e00cbbcd058",
"ab0100640046420f0000000000f000ccbcd058d6fc00000f383633373033303330303633323339140000000000000000000000000000000000000000a10ec20d0000f7000000000000000000000000000000000000000000000000000000000000000000",
"ab01008f0046420f0000000000cb0000000000ccbcd0584d53000008ea8b0200ff5e190000fede100000fe80080000feda0000000267030000fd71e724040071e724040000000000000000000000455900000011fa00000051990200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"ab0100130042420f00000000006e007abdd058",
"ab0100640042420f0000000000f0007abdd058e6fb00000f383633373033303330303635363232140000000000000000000000000000000000000000a80ef10d000064010000000000000000000000000000000000000000000000000000000000000000",
"ab01008f0042420f0000000000cb00000000007bbdd05859110000082f6f0000ff881a0000fee00f0000fea80a0000fe3f0100000205040000fd0f346b000026ab01000000000000000000000000590c00000000fa00000051990200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"ab0100640045420f0000000000f000d3c6d05876fd00000f383633373033303330303537333330140000000000000000000000000000000000000000b80e2d0e00000b010000000000000000000000000000000000000000000000000000000000000000",
"ab01008f0045420f0000000000cb0000000000d4c6d058f066000008ed500300ff8b1a0000feda100000feb1090000fe8e0100000274050000fdf240250400f24025040000000000000000000000e2520000008a1501000051990200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"ab0100130048420f00000000006e00d7cbd058",
"ab0100640048420f0000000000f000d7cbd05822fc00000f383633373033303330303635383132140000000000000000000000000000000000000000c70ecc0d000013010000000000000000000000000000000000000000000000000000000000000000",
"ab01008f0048420f0000000000cb0000000000d8cbd0586a68000007c1f90200fe3f1a0000fe15110000fe2a090000feed0000000278030000fdd4336b000076c9010000000000000000000000007315000000a02201000051990200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"ab0100130044420f00000000006e003dccd058",
"ab0100640044420f0000000000f0003dccd0585efc00000f383633373033303330303538383137140000000000000000000000000000000000000000ac0ed30d00000e010000000000000000000000000000000000000000000000000000000000000000",
"ab01008f0044420f0000000000cb00000000003eccd0582de30000080df20500ffbb1a0000fe7b0e0000fe400c0000fe1b03000002b8080000fd52c46300003aab010000000000000000000000008e69000000e43801000051990200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
};

@Test
public void manyFrames_test() throws Exception {
List<String> receivedData = new ArrayList<>();

ServerBootstrap bootstrap = new ServerBootstrap()
.group(new NioEventLoopGroup(), new DefaultEventLoop())
.channel(NioUdpServerChannel.class)
.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel channel) throws Exception {
channel.pipeline()
.addLast(new Collect(receivedData::add));
}
});
System.err.println("Binding... ");
bootstrap.bind(16666).syncUninterruptibly();


DatagramChannel datagramChannel = DatagramChannel.open();
datagramChannel.connect(new InetSocketAddress("127.0.0.1", 16666));

for (String packet : packets) {
System.out.println("Sending: " + packet);
final byte[] bytes = DatatypeConverter.parseHexBinary(packet);
datagramChannel.write(ByteBuffer.wrap(bytes));
Thread.sleep(50);
}

Thread.sleep(100);
Assert.assertEquals("", packets.length, receivedData.size());
for (int i = 0; i < packets.length; i++) {
Assert.assertEquals("Received packet number " + i + " is different", packets[i], receivedData.get(i));
}
}


public static class Collect extends SimpleChannelInboundHandler<ByteBuf> {
private Consumer<String> consumer;

public Collect(Consumer<String> consumer) {
this.consumer = consumer;
}

@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf bytebuf) throws Exception {
final String hexDump = ByteBufUtil.hexDump(bytebuf);
System.out.println("Received: " + hexDump);
consumer.accept(hexDump);
}
}

}

0 comments on commit fde64fb

Please sign in to comment.