package rx.broadcast;

import org.junit.After;
import org.junit.Test;
import rx.Observable;
import rx.observers.TestSubscriber;

import java.io.Closeable;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

public class UdpBroadcastTest {
    private final List<Closeable> resources = new LinkedList<>();

    private final Supplier<DatagramSocket> datagramSocketSupplier = () -> {
        try {
            final DatagramSocket socket = new DatagramSocket();
            resources.add(socket);
            return socket;
        } catch (final SocketException e) {
            throw new RuntimeException(e);
        }
    };

    @After
    public final void tearDown() {
        resources.forEach(closeable -> {
            try {
                closeable.close();
            } catch (final IOException e) {
                // ???
            }
        });
    }

    @SuppressWarnings({"checkstyle:magicnumber"})
    @Test
    public final void valuesOfTypeDoesReceiveBroadcastValue() {
        final TestSubscriber<Object> subscriber = new TestSubscriber<>();
        final DatagramSocket s1 = datagramSocketSupplier.get();
        final DatagramSocket s2 = datagramSocketSupplier.get();
        final Broadcast broadcast1 = new UdpBroadcast<>(
            s1, InetAddress.getLoopbackAddress(), s2.getLocalPort(), new BasicOrder<>());
        final Broadcast broadcast2 = new UdpBroadcast<>(
            s2, InetAddress.getLoopbackAddress(), s1.getLocalPort(), new BasicOrder<>());

        broadcast2.valuesOfType(TestValue.class).first().subscribe(subscriber);
        broadcast1.send(new TestValue(42)).toBlocking().subscribe();

        subscriber.awaitTerminalEvent(10, TimeUnit.SECONDS);

        subscriber.assertNoErrors();
        subscriber.assertValueCount(1);
        subscriber.assertReceivedOnNext(Collections.singletonList(new TestValue(42)));
    }

    @SuppressWarnings({"checkstyle:magicnumber"})
    @Test
    public final void eachSubscriptionDoesSendTheBroadcast() {
        final TestSubscriber<Object> subscriber = new TestSubscriber<>();
        final DatagramSocket s1 = datagramSocketSupplier.get();
        final DatagramSocket s2 = datagramSocketSupplier.get();
        final Broadcast broadcast1 = new UdpBroadcast<>(
            s1, InetAddress.getLoopbackAddress(), s2.getLocalPort(), new BasicOrder<>());
        final Broadcast broadcast2 = new UdpBroadcast<>(
            s2, InetAddress.getLoopbackAddress(), s1.getLocalPort(), new BasicOrder<>());

        broadcast2.valuesOfType(TestValue.class).take(4).subscribe(subscriber);

        final Observable<Void> testValue = broadcast1.send(new TestValue(42));
        testValue.toBlocking().subscribe();
        testValue.toBlocking().subscribe();
        testValue.toBlocking().subscribe();
        testValue.toBlocking().subscribe();

        subscriber.awaitTerminalEvent(10, TimeUnit.SECONDS);

        subscriber.assertNoErrors();
        subscriber.assertValueCount(4);
        subscriber.assertReceivedOnNext(Arrays.asList(
            new TestValue(42), new TestValue(42), new TestValue(42), new TestValue(42)));
    }

    @SuppressWarnings({"checkstyle:magicnumber"})
    @Test
    public final void errorSendingBroadcastIsReceivedInOnError() {
        final TestSubscriber<Void> subscriber = new TestSubscriber<>();
        final DatagramSocket s1 = datagramSocketSupplier.get();
        final DatagramSocket s2 = datagramSocketSupplier.get();
        final Broadcast broadcast1 = new UdpBroadcast<>(
            s1, InetAddress.getLoopbackAddress(), s2.getLocalPort(), new BasicOrder<>());

        s1.close();
        broadcast1.send(new TestValue(42)).toBlocking().subscribe(subscriber);

        subscriber.awaitTerminalEvent(10, TimeUnit.SECONDS);
        subscriber.assertError(SocketException.class);
    }
}

@@ -168,32 +168,64 @@
 io.IOException;%0A
+import java.net.DatagramPacket;%0A
 import java.net.
@@ -4092,11 +4092,1404 @@
 );%0A    %7D
+%0A%0A    @SuppressWarnings(%7B%22checkstyle:magicnumber%22%7D)%0A    @Test%0A    public final void deserializationErrorDoesNotTerminateStream() throws IOException %7B%0A        final TestSubscriber%3CObject%3E subscriber = new TestSubscriber%3C%3E();%0A        final DatagramSocket s1 = datagramSocketSupplier.get();%0A        final DatagramSocket s2 = datagramSocketSupplier.get();%0A        final Broadcast broadcast1 = new UdpBroadcast%3C%3E(%0A            s1, InetAddress.getLoopbackAddress(), s2.getLocalPort(), new BasicOrder%3C%3E());%0A        final Broadcast broadcast2 = new UdpBroadcast%3C%3E(%0A            s2, InetAddress.getLoopbackAddress(), s1.getLocalPort(), new BasicOrder%3C%3E());%0A%0A        broadcast2.valuesOfType(TestValue.class).take(4).subscribe(subscriber);%0A%0A        final Observable%3CVoid%3E testValue = broadcast1.send(new TestValue(42));%0A        s1.send(new DatagramPacket(new byte%5B%5D%7B42, 43, 44, 45%7D, 4, InetAddress.getLoopbackAddress(), s2.getLocalPort()));%0A        testValue.toBlocking().subscribe();%0A        testValue.toBlocking().subscribe();%0A        testValue.toBlocking().subscribe();%0A        testValue.toBlocking().subscribe();%0A%0A        subscriber.awaitTerminalEvent(10, TimeUnit.SECONDS);%0A        subscriber.assertNoErrors();%0A        subscriber.assertValueCount(4);%0A        subscriber.assertReceivedOnNext(Arrays.asList(%0A            new TestValue(42), new TestValue(42), new TestValue(42), new TestValue(42)));%0A    %7D
 %0A%7D%0A

Add test case for deserialization issue