Example usage for io.netty.util.internal PlatformDependent threadLocalRandom

List of usage examples for io.netty.util.internal PlatformDependent threadLocalRandom

Introduction

In this page you can find the example usage for io.netty.util.internal PlatformDependent threadLocalRandom.

Prototype

public static Random threadLocalRandom() 

Source Link

Document

Return a Random which is not-threadsafe and so can only be used from the same thread.

Usage

From source file:org.apache.pulsar.client.impl.PulsarServiceNameResolver.java

License:Apache License

private static int randomIndex(int numAddresses) {
    return numAddresses == 1 ? 0 : PlatformDependent.threadLocalRandom().nextInt(numAddresses);
}

From source file:org.ballerinalang.mime.util.MimeUtil.java

License:Open Source License

/**
 * Get a new multipart boundary delimiter.
 *
 * @return a boundary string//  w w w . j  a va  2s  .  c o  m
 */
public static String getNewMultipartDelimiter() {
    return Long.toHexString(PlatformDependent.threadLocalRandom().nextLong());
}

From source file:org.ballerinalang.test.service.http.sample.RetrySampleTestCase.java

License:Open Source License

@Test(description = "Test retry functionality with multipart requests")
public void testMultiPart() throws IOException {
    String multipartDataBoundary = Long.toHexString(PlatformDependent.threadLocalRandom().nextLong());
    String multipartBody = "--" + multipartDataBoundary + "\r\n"
            + "Content-Disposition: form-data; name=\"foo\"" + "\r\n"
            + "Content-Type: text/plain; charset=UTF-8" + "\r\n" + "\r\n" + "Part1" + "\r\n" + "--"
            + multipartDataBoundary + "\r\n"
            + "Content-Disposition: form-data; name=\"filepart\"; filename=\"file-01.txt\"" + "\r\n"
            + "Content-Type: text/plain" + "\r\n" + "Content-Transfer-Encoding: binary" + "\r\n" + "\r\n"
            + "Part2" + StringUtil.NEWLINE + "\r\n" + "--" + multipartDataBoundary + "--" + "\r\n";
    Map<String, String> headers = new HashMap<>();
    headers.put(HttpHeaderNames.CONTENT_TYPE.toString(),
            "multipart/form-data; boundary=" + multipartDataBoundary);
    HttpResponse response = HttpClientRequest.doPost(serverInstance.getServiceURLHttp(servicePort, "retry"),
            multipartBody, headers);//from w w w.j av a 2 s . co  m
    Assert.assertEquals(response.getResponseCode(), 200, "Response code mismatched");
    Assert.assertTrue(
            response.getHeaders().get(HttpHeaderNames.CONTENT_TYPE.toString())
                    .contains("multipart/form-data;boundary=" + multipartDataBoundary),
            "Response is not form of multipart");
    Assert.assertTrue(response.getData().contains("form-data;name=\"foo\"content-id: 0Part1"),
            "Message content mismatched");
    Assert.assertTrue(
            response.getData()
                    .contains("form-data;name=\"filepart\";filename=\"file-01.txt\"content-id: 1Part2"),
            "Message content mismatched");
}

From source file:org.ballerinalang.test.service.http.sample.RetrySampleTestCase.java

License:Open Source License

@Test(description = "Test retry functionality when request has nested body parts")
public void testNestedMultiPart() throws IOException {
    String multipartDataBoundary = Long.toHexString(PlatformDependent.threadLocalRandom().nextLong());
    String multipartMixedBoundary = Long.toHexString(PlatformDependent.threadLocalRandom().nextLong());
    String nestedMultipartBody = "--" + multipartDataBoundary + "\r\n"
            + "Content-Disposition: form-data; name=\"parent1\"" + "\r\n"
            + "Content-Type: text/plain; charset=UTF-8" + "\r\n" + "\r\n" + "Parent Part" + "\r\n" + "--"
            + multipartDataBoundary + "\r\n" + "Content-Disposition: form-data; name=\"parent2\"" + "\r\n"
            + "Content-Type: multipart/mixed; boundary=" + multipartMixedBoundary + "\r\n" + "\r\n" + "--"
            + multipartMixedBoundary + "\r\n" + "Content-Disposition: attachment; filename=\"file-02.txt\""
            + "\r\n" + "Content-Type: text/plain" + "\r\n" + "Content-Transfer-Encoding: binary" + "\r\n"
            + "\r\n" + "Child Part 1" + StringUtil.NEWLINE + "\r\n" + "--" + multipartMixedBoundary + "\r\n"
            + "Content-Disposition: attachment; filename=\"file-02.txt\"" + "\r\n" + "Content-Type: text/plain"
            + "\r\n" + "Content-Transfer-Encoding: binary" + "\r\n" + "\r\n" + "Child Part 2"
            + StringUtil.NEWLINE + "\r\n" + "--" + multipartMixedBoundary + "--" + "\r\n" + "--"
            + multipartDataBoundary + "--" + "\r\n";
    String expectedChildPart1 = "Content-Transfer-Encoding: binary" + "content-type: text/plain"
            + "content-disposition: attachment;filename=\"file-02.txt\"content-id: 0" + "Child Part 1";
    String expectedChildPart2 = "Content-Transfer-Encoding: binary" + "content-type: text/plain"
            + "content-disposition: attachment;filename=\"file-02.txt\"content-id: 1" + "Child Part 2";
    Map<String, String> headers = new HashMap<>();
    headers.put(HttpHeaderNames.CONTENT_TYPE.toString(),
            "multipart/form-data; boundary=" + multipartDataBoundary);
    HttpResponse response = HttpClientRequest.doPost(serverInstance.getServiceURLHttp(servicePort, "retry"),
            nestedMultipartBody, headers);
    Assert.assertEquals(response.getResponseCode(), 200, "Response code mismatched");
    Assert.assertTrue(//from w  w w  .  j  av  a  2s.c om
            response.getHeaders().get(HttpHeaderNames.CONTENT_TYPE.toString())
                    .contains("multipart/form-data;boundary=" + multipartDataBoundary),
            "Response is not form of multipart");
    Assert.assertTrue(response.getData().contains(expectedChildPart1), "Message content mismatched");
    Assert.assertTrue(response.getData().contains(expectedChildPart2), "Message content mismatched");
}

From source file:org.ballerinalang.test.service.resiliency.FailoverClientTestCase.java

License:Open Source License

@Test(description = "Test failover functionality with multipart requests")
public void testMultiPart() throws IOException {
    String multipartDataBoundary = Long.toHexString(PlatformDependent.threadLocalRandom().nextLong());
    String multipartBody = "--" + multipartDataBoundary + "\r\n"
            + "Content-Disposition: form-data; name=\"foo\"" + "\r\n"
            + "Content-Type: text/plain; charset=UTF-8" + "\r\n" + "\r\n" + "Part1" + "\r\n" + "--"
            + multipartDataBoundary + "\r\n"
            + "Content-Disposition: form-data; name=\"filepart\"; filename=\"file-01.txt\"" + "\r\n"
            + "Content-Type: text/plain" + "\r\n" + "Content-Transfer-Encoding: binary" + "\r\n" + "\r\n"
            + "Part2" + StringUtil.NEWLINE + "\r\n" + "--" + multipartDataBoundary + "--" + "\r\n";
    Map<String, String> headers = new HashMap<>();
    headers.put(HttpHeaderNames.CONTENT_TYPE.toString(),
            "multipart/form-data; boundary=" + multipartDataBoundary);
    HttpResponse response = HttpClientRequest.doPost(ballerinaServer.getServiceURLHttp(TYPICAL_SERVICE_PATH),
            multipartBody, headers);//from  w  w  w .jav  a2s. c o m
    Assert.assertEquals(response.getResponseCode(), 200, "Response code mismatched");
    Assert.assertTrue(
            response.getHeaders().get(HttpHeaderNames.CONTENT_TYPE.toString())
                    .contains("multipart/form-data;boundary=" + multipartDataBoundary),
            "Response is not form of multipart");
    Assert.assertTrue(response.getData().contains("form-data;name=\"foo\"content-id: 0Part1"),
            "Message content mismatched");
    Assert.assertTrue(
            response.getData()
                    .contains("form-data;name=\"filepart\";filename=\"file-01.txt\"content-id: 1Part2"),
            "Message content mismatched");
}

From source file:org.ballerinalang.test.service.resiliency.FailoverClientTestCase.java

License:Open Source License

@Test(description = "Test failover functionality when request has nested body parts")
public void testNestedMultiPart() throws IOException {
    String multipartDataBoundary = Long.toHexString(PlatformDependent.threadLocalRandom().nextLong());
    String multipartMixedBoundary = Long.toHexString(PlatformDependent.threadLocalRandom().nextLong());
    String nestedMultipartBody = "--" + multipartDataBoundary + "\r\n"
            + "Content-Disposition: form-data; name=\"parent1\"" + "\r\n"
            + "Content-Type: text/plain; charset=UTF-8" + "\r\n" + "\r\n" + "Parent Part" + "\r\n" + "--"
            + multipartDataBoundary + "\r\n" + "Content-Disposition: form-data; name=\"parent2\"" + "\r\n"
            + "Content-Type: multipart/mixed; boundary=" + multipartMixedBoundary + "\r\n" + "\r\n" + "--"
            + multipartMixedBoundary + "\r\n" + "Content-Disposition: attachment; filename=\"file-02.txt\""
            + "\r\n" + "Content-Type: text/plain" + "\r\n" + "Content-Transfer-Encoding: binary" + "\r\n"
            + "\r\n" + "Child Part 1" + StringUtil.NEWLINE + "\r\n" + "--" + multipartMixedBoundary + "\r\n"
            + "Content-Disposition: attachment; filename=\"file-02.txt\"" + "\r\n" + "Content-Type: text/plain"
            + "\r\n" + "Content-Transfer-Encoding: binary" + "\r\n" + "\r\n" + "Child Part 2"
            + StringUtil.NEWLINE + "\r\n" + "--" + multipartMixedBoundary + "--" + "\r\n" + "--"
            + multipartDataBoundary + "--" + "\r\n";
    String expectedChildPart1 = "Content-Transfer-Encoding: binary" + "content-type: text/plain"
            + "content-disposition: attachment;filename=\"file-02.txt\"content-id: 0" + "Child Part 1";
    String expectedChildPart2 = "Content-Transfer-Encoding: binary" + "content-type: text/plain"
            + "content-disposition: attachment;filename=\"file-02.txt\"content-id: 1" + "Child Part 2";
    Map<String, String> headers = new HashMap<>();
    headers.put(HttpHeaderNames.CONTENT_TYPE.toString(),
            "multipart/form-data; boundary=" + multipartDataBoundary);
    HttpResponse response = HttpClientRequest.doPost(ballerinaServer.getServiceURLHttp(TYPICAL_SERVICE_PATH),
            nestedMultipartBody, headers);
    Assert.assertEquals(response.getResponseCode(), 200, "Response code mismatched");
    Assert.assertTrue(//from   w  ww  .  j  a  va 2  s .  c  o  m
            response.getHeaders().get(HttpHeaderNames.CONTENT_TYPE.toString())
                    .contains("multipart/form-data;boundary=" + multipartDataBoundary),
            "Response is not form of multipart");
    Assert.assertTrue(response.getData().contains(expectedChildPart1), "Message content mismatched");
    Assert.assertTrue(response.getData().contains(expectedChildPart2), "Message content mismatched");
}

From source file:org.ballerinalang.test.service.resiliency.HttpResiliencyTest.java

License:Open Source License

@Test(description = "Test failover functionality with multipart requests")
public void testMultiPart() throws IOException {
    String multipartDataBoundary = Long.toHexString(PlatformDependent.threadLocalRandom().nextLong());
    String multipartBody = "--" + multipartDataBoundary + "\r\n"
            + "Content-Disposition: form-data; name=\"foo\"" + "\r\n"
            + "Content-Type: text/plain; charset=UTF-8" + "\r\n" + "\r\n" + "Part1" + "\r\n" + "--"
            + multipartDataBoundary + "\r\n"
            + "Content-Disposition: form-data; name=\"filepart\"; filename=\"file-01.txt\"" + "\r\n"
            + "Content-Type: text/plain" + "\r\n" + "Content-Transfer-Encoding: binary" + "\r\n" + "\r\n"
            + "Part2" + StringUtil.NEWLINE + "\r\n" + "--" + multipartDataBoundary + "--" + "\r\n";
    Map<String, String> headers = new HashMap<>();
    headers.put(HttpHeaderNames.CONTENT_TYPE.toString(),
            "multipart/form-data; boundary=" + multipartDataBoundary);
    HttpResponse response = HttpClientRequest
            .doPost(serverInstance.getServiceURLHttp(9301, TYPICAL_SERVICE_PATH), multipartBody, headers);
    Assert.assertEquals(response.getResponseCode(), 200, "Response code mismatched");
    Assert.assertTrue(//from  ww w .ja v a  2  s .  co m
            response.getHeaders().get(HttpHeaderNames.CONTENT_TYPE.toString())
                    .contains("multipart/form-data;boundary=" + multipartDataBoundary),
            "Response is not form of multipart");
    Assert.assertTrue(response.getData().contains("form-data;name=\"foo\"content-id: 0Part1"),
            "Message content mismatched");
    Assert.assertTrue(
            response.getData()
                    .contains("form-data;name=\"filepart\";filename=\"file-01.txt\"content-id: 1Part2"),
            "Message content mismatched");
}

From source file:org.ballerinalang.test.service.resiliency.HttpResiliencyTest.java

License:Open Source License

@Test(description = "Test failover functionality when request has nested body parts")
public void testNestedMultiPart() throws IOException {
    String multipartDataBoundary = Long.toHexString(PlatformDependent.threadLocalRandom().nextLong());
    String multipartMixedBoundary = Long.toHexString(PlatformDependent.threadLocalRandom().nextLong());
    String nestedMultipartBody = "--" + multipartDataBoundary + "\r\n"
            + "Content-Disposition: form-data; name=\"parent1\"" + "\r\n"
            + "Content-Type: text/plain; charset=UTF-8" + "\r\n" + "\r\n" + "Parent Part" + "\r\n" + "--"
            + multipartDataBoundary + "\r\n" + "Content-Disposition: form-data; name=\"parent2\"" + "\r\n"
            + "Content-Type: multipart/mixed; boundary=" + multipartMixedBoundary + "\r\n" + "\r\n" + "--"
            + multipartMixedBoundary + "\r\n" + "Content-Disposition: attachment; filename=\"file-02.txt\""
            + "\r\n" + "Content-Type: text/plain" + "\r\n" + "Content-Transfer-Encoding: binary" + "\r\n"
            + "\r\n" + "Child Part 1" + StringUtil.NEWLINE + "\r\n" + "--" + multipartMixedBoundary + "\r\n"
            + "Content-Disposition: attachment; filename=\"file-02.txt\"" + "\r\n" + "Content-Type: text/plain"
            + "\r\n" + "Content-Transfer-Encoding: binary" + "\r\n" + "\r\n" + "Child Part 2"
            + StringUtil.NEWLINE + "\r\n" + "--" + multipartMixedBoundary + "--" + "\r\n" + "--"
            + multipartDataBoundary + "--" + "\r\n";
    String expectedChildPart1 = "Content-Transfer-Encoding: binary" + "content-type: text/plain"
            + "content-disposition: attachment;filename=\"file-02.txt\"content-id: 0" + "Child Part 1";
    String expectedChildPart2 = "Content-Transfer-Encoding: binary" + "content-type: text/plain"
            + "content-disposition: attachment;filename=\"file-02.txt\"content-id: 1" + "Child Part 2";
    Map<String, String> headers = new HashMap<>();
    headers.put(HttpHeaderNames.CONTENT_TYPE.toString(),
            "multipart/form-data; boundary=" + multipartDataBoundary);
    HttpResponse response = HttpClientRequest
            .doPost(serverInstance.getServiceURLHttp(9302, TYPICAL_SERVICE_PATH), nestedMultipartBody, headers);
    Assert.assertEquals(response.getResponseCode(), 200, "Response code mismatched");
    Assert.assertTrue(/*ww w  .j a va 2  s . c  om*/
            response.getHeaders().get(HttpHeaderNames.CONTENT_TYPE.toString())
                    .contains("multipart/form-data;boundary=" + multipartDataBoundary),
            "Response is not form of multipart");
    Assert.assertTrue(response.getData().contains(expectedChildPart1), "Message content mismatched");
    Assert.assertTrue(response.getData().contains(expectedChildPart2), "Message content mismatched");
}