From 0080681519c9e56d66c012cde92cd0d7fc28b55b Mon Sep 17 00:00:00 2001 From: Frohwalt Egerer Date: Thu, 29 May 2014 23:46:29 +0200 Subject: [PATCH] Run some tests repeatedly, they seem not to be stable --- pom.xml | 2 +- .../apns/integration/ApnsConnectionTest.java | 16 ++++++--- .../notnoop/apns/utils/ApnsServerStub.java | 10 ++++-- .../com/notnoop/apns/utils/junit/Repeat.java | 11 ++++++ .../notnoop/apns/utils/junit/RepeatRule.java | 36 +++++++++++++++++++ 5 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 src/test/java/com/notnoop/apns/utils/junit/Repeat.java create mode 100644 src/test/java/com/notnoop/apns/utils/junit/RepeatRule.java diff --git a/pom.xml b/pom.xml index f132aa2d..5bc9d0f7 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.notnoop.apns apns - 0.2.4-SNAPSHOT + 1.0.0-SNAPSHOT jar Java Apple Push Notification Service Library diff --git a/src/test/java/com/notnoop/apns/integration/ApnsConnectionTest.java b/src/test/java/com/notnoop/apns/integration/ApnsConnectionTest.java index de143f7c..0d451a87 100644 --- a/src/test/java/com/notnoop/apns/integration/ApnsConnectionTest.java +++ b/src/test/java/com/notnoop/apns/integration/ApnsConnectionTest.java @@ -5,15 +5,22 @@ import com.notnoop.apns.EnhancedApnsNotification; import com.notnoop.apns.SimpleApnsNotification; import com.notnoop.apns.utils.ApnsServerStub; +import com.notnoop.apns.utils.junit.Repeat; +import com.notnoop.apns.utils.junit.RepeatRule; import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import static com.notnoop.apns.utils.FixedCertificates.*; import static org.junit.Assert.*; + @SuppressWarnings("ALL") public class ApnsConnectionTest { + @Rule + public RepeatRule rr = new RepeatRule(); + ApnsServerStub server; static SimpleApnsNotification msg1 = new SimpleApnsNotification("a87d8878d878a79", "{\"aps\":{}}"); static SimpleApnsNotification msg2 = new SimpleApnsNotification("a87d8878d878a88", "{\"aps\":{}}"); @@ -37,9 +44,9 @@ public void tearDown() { server = null; } + @Repeat(count = 50) @Test(timeout = 2000) public void sendOneSimple() throws InterruptedException { - ApnsService service = APNS.newService().withSSLContext(clientContext()) .withGatewayDestination(LOCALHOST, gatewayPort) @@ -51,10 +58,9 @@ public void sendOneSimple() throws InterruptedException { assertArrayEquals(msg1.marshall(), server.getReceived().toByteArray()); } + @Repeat(count = 50) @Test(timeout = 2000) public void sendOneQueued() throws InterruptedException { - - ApnsService service = APNS.newService().withSSLContext(clientContext()) .withGatewayDestination(LOCALHOST, gatewayPort) @@ -66,8 +72,8 @@ public void sendOneQueued() throws InterruptedException { assertArrayEquals(msg1.marshall(), server.getReceived().toByteArray()); } - - + + @Test public void sendOneSimpleWithoutTimeout() throws InterruptedException { server.getToWaitBeforeSend().set(2000); diff --git a/src/test/java/com/notnoop/apns/utils/ApnsServerStub.java b/src/test/java/com/notnoop/apns/utils/ApnsServerStub.java index 15b9a000..966762d9 100644 --- a/src/test/java/com/notnoop/apns/utils/ApnsServerStub.java +++ b/src/test/java/com/notnoop/apns/utils/ApnsServerStub.java @@ -6,6 +6,7 @@ import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; +import java.net.SocketException; import java.nio.ByteBuffer; import java.util.concurrent.Semaphore; import java.util.concurrent.atomic.AtomicInteger; @@ -114,7 +115,10 @@ public void sendError(int err, int id) { gatewayOutLock.acquire(); gatewayOutputStream.write(buf.array()); gatewayOutputStream.flush(); - } catch (Exception ex) { + } + + + catch (Exception ex) { ex.printStackTrace(); } } @@ -236,8 +240,10 @@ public void run() { // Close the socket in.close(); out.close(); + } catch (SocketException se) { + // Ignore closed socket. } catch (IOException ioex) { - System.err.println(ioex.toString()); + ioex.printStackTrace(); } messages.release(); } diff --git a/src/test/java/com/notnoop/apns/utils/junit/Repeat.java b/src/test/java/com/notnoop/apns/utils/junit/Repeat.java new file mode 100644 index 00000000..80cf10b1 --- /dev/null +++ b/src/test/java/com/notnoop/apns/utils/junit/Repeat.java @@ -0,0 +1,11 @@ +package com.notnoop.apns.utils.junit; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target({java.lang.annotation.ElementType.METHOD}) +public @interface Repeat { + public abstract int count(); +} diff --git a/src/test/java/com/notnoop/apns/utils/junit/RepeatRule.java b/src/test/java/com/notnoop/apns/utils/junit/RepeatRule.java new file mode 100644 index 00000000..f16de125 --- /dev/null +++ b/src/test/java/com/notnoop/apns/utils/junit/RepeatRule.java @@ -0,0 +1,36 @@ +package com.notnoop.apns.utils.junit; + + +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +public class RepeatRule implements TestRule { + + @Override + public Statement apply(Statement base, Description description) { + Repeat repeat = description.getAnnotation(Repeat.class); + if (repeat != null) { + return new RepeatStatement(repeat.count(), base); + } + return base; + } + + private static class RepeatStatement extends Statement { + + private final int count; + private final Statement base; + + private RepeatStatement(int count, Statement base) { + this.count = count; + this.base = base; + } + + @Override + public void evaluate() throws Throwable { + for (int i = count; i > 0; i--) { + base.evaluate(); + } + } + } +} \ No newline at end of file