-
-
Save matzew/bb2c11bf55e2e5c7334d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* JBoss, Home of Professional Open Source | |
* Copyright Red Hat, Inc., and individual contributors. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package org.jboss.aerogear.unifiedpush.message.sender; | |
import net.iharder.Base64; | |
import org.junit.Assert; | |
import org.junit.Test; | |
import java.io.IOException; | |
import java.io.OutputStream; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.nio.charset.Charset; | |
import java.util.UUID; | |
import java.util.concurrent.CountDownLatch; | |
import java.util.concurrent.ExecutionException; | |
import java.util.concurrent.atomic.AtomicBoolean; | |
import static org.junit.Assert.assertEquals; | |
/** | |
* Created by matzew on 27/11/14. | |
*/ | |
public class DeviceRegistrationTest { | |
@Test | |
public void registerSomeDevices() throws InterruptedException, ExecutionException, IOException { | |
final long start = System.currentTimeMillis(); | |
for (int i = 0; i < 1000; i++) { | |
concurrentRegistration(16); | |
} | |
final long end = System.currentTimeMillis(); | |
System.out.println("Took -> " + (end-start)); | |
} | |
private void concurrentRegistration(final int threads) throws InterruptedException { | |
final AtomicBoolean outcome = new AtomicBoolean(true); | |
final CountDownLatch startLatch = new CountDownLatch(1); | |
final CountDownLatch endLatch = new CountDownLatch(threads); | |
for (int i = 0; i < threads; i++) { | |
new Thread(new Runnable() { | |
@Override | |
public void run() { | |
try { | |
startLatch.await(); | |
try { | |
HttpURLConnection conn = post(generateToken()); | |
assertEquals(200, conn.getResponseCode()); | |
} catch (final Exception e) { | |
e.printStackTrace(); | |
outcome.compareAndSet(true, false); | |
} finally { | |
endLatch.countDown(); | |
} | |
} catch (InterruptedException e) { | |
//e.printStackTrace(); | |
} | |
} | |
}).start(); | |
} | |
startLatch.countDown(); | |
endLatch.await(); | |
if (!outcome.get()) { | |
Assert.fail("updateThreadSafety test failed. Please check stacktrace(s)"); | |
} | |
} | |
HttpURLConnection post(String token) throws IOException { | |
final String payload = "{\"deviceToken\" : \"" + token +"\"}"; | |
byte[] bytes = payload.getBytes(Charset.forName("UTF-8")); | |
HttpURLConnection conn = getConnection("http://localhost:8080/ag-push/rest/registry/device"); | |
conn.setDoOutput(true); | |
conn.setUseCaches(false); | |
conn.setFixedLengthStreamingMode(bytes.length); | |
conn.setRequestProperty("Content-Type", "application/json"); | |
conn.setRequestProperty("Authorization", "Basic " + Base64.encodeBytes("da7c72ff-2d86-4851-9a2b-11de84dc06d1:f68a9051-2840-4e3e-aecf-7f156b0bf3c4".getBytes())); | |
conn.setRequestMethod("POST"); | |
OutputStream out = null; | |
try { | |
out = conn.getOutputStream(); | |
out.write(bytes); | |
} finally { | |
// in case something blows up, while writing | |
// the payload, we wanna close the stream: | |
if (out != null) { | |
out.close(); | |
} | |
} | |
return conn; | |
} | |
HttpURLConnection getConnection(String url) throws IOException { | |
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); | |
return conn; | |
} | |
private String generateToken() { | |
String raw = UUID.randomUUID().toString() + UUID.randomUUID().toString() + UUID.randomUUID().toString() + UUID.randomUUID().toString(); | |
raw = raw.replaceAll("-",""); | |
return raw; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment