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.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 sa = datagramSocketSupplier.get();
        final DatagramSocket sb = datagramSocketSupplier.get();
        final Broadcast broadcast1 = new UdpBroadcast(sa, InetAddress.getLoopbackAddress(), sb.getLocalPort());
        final Broadcast broadcast2 = new UdpBroadcast(sb, InetAddress.getLoopbackAddress(), sa.getLocalPort());

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

        subscriber.awaitTerminalEvent();

        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 sa = datagramSocketSupplier.get();
        final DatagramSocket sb = datagramSocketSupplier.get();
        final Broadcast broadcast1 = new UdpBroadcast(sa, InetAddress.getLoopbackAddress(), sb.getLocalPort());
        final Broadcast broadcast2 = new UdpBroadcast(sb, InetAddress.getLoopbackAddress(), sa.getLocalPort());

        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();

        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 sa = datagramSocketSupplier.get();
        final DatagramSocket sb = datagramSocketSupplier.get();
        final Broadcast broadcast1 = new UdpBroadcast(sa, InetAddress.getLoopbackAddress(), sb.getLocalPort());

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

        subscriber.awaitTerminalEvent();
        subscriber.assertError(SocketException.class);
    }
}

@@ -1294,33 +1294,33 @@
 DatagramSocket s
-a
+1
  = datagramSocke
@@ -1358,33 +1358,33 @@
 DatagramSocket s
-b
+2
  = datagramSocke
@@ -1447,33 +1447,33 @@
 w UdpBroadcast(s
-a
+1
 , InetAddress.ge
@@ -1485,33 +1485,33 @@
 backAddress(), s
-b
+2
 .getLocalPort())
@@ -1559,33 +1559,33 @@
 w UdpBroadcast(s
-b
+2
 , InetAddress.ge
@@ -1597,33 +1597,33 @@
 backAddress(), s
-a
+1
 .getLocalPort())
@@ -2207,33 +2207,33 @@
 DatagramSocket s
-a
+1
  = datagramSocke
@@ -2271,33 +2271,33 @@
 DatagramSocket s
-b
+2
  = datagramSocke
@@ -2360,33 +2360,33 @@
 w UdpBroadcast(s
-a
+1
 , InetAddress.ge
@@ -2398,33 +2398,33 @@
 backAddress(), s
-b
+2
 .getLocalPort())
@@ -2480,17 +2480,17 @@
 adcast(s
-b
+2
 , InetAd
@@ -2518,17 +2518,17 @@
 ess(), s
-a
+1
 .getLoca
@@ -3375,17 +3375,17 @@
 Socket s
-a
+1
  = datag
@@ -3439,17 +3439,17 @@
 Socket s
-b
+2
  = datag
@@ -3528,17 +3528,17 @@
 adcast(s
-a
+1
 , InetAd
@@ -3566,17 +3566,17 @@
 ess(), s
-b
+2
 .getLoca
@@ -3595,17 +3595,17 @@
        s
-a
+1
 .close()

Fix variable naming in UdpBroadcastTest