Skip to content

Commit

Permalink
Run some tests repeatedly, they seem not to be stable
Browse files Browse the repository at this point in the history
  • Loading branch information
Frohwalt Egerer committed May 29, 2014
1 parent 55c7725 commit 0080681
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.notnoop.apns</groupId>
<artifactId>apns</artifactId>
<version>0.2.4-SNAPSHOT</version>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Java Apple Push Notification Service Library</name>

Expand Down
16 changes: 11 additions & 5 deletions src/test/java/com/notnoop/apns/integration/ApnsConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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\":{}}");
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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);
Expand Down
10 changes: 8 additions & 2 deletions src/test/java/com/notnoop/apns/utils/ApnsServerStub.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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();
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/com/notnoop/apns/utils/junit/Repeat.java
Original file line number Diff line number Diff line change
@@ -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();
}
36 changes: 36 additions & 0 deletions src/test/java/com/notnoop/apns/utils/junit/RepeatRule.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}

0 comments on commit 0080681

Please sign in to comment.