Skip to content

Commit fbd01d5

Browse files
committed
Tck test for length subscriber
1 parent 4050ed3 commit fbd01d5

File tree

3 files changed

+94
-29
lines changed

3 files changed

+94
-29
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.http.auth.aws.internal.signer.util;
17+
18+
import java.nio.ByteBuffer;
19+
import java.util.concurrent.CompletableFuture;
20+
import org.reactivestreams.Subscriber;
21+
import org.reactivestreams.Subscription;
22+
23+
class LengthCalculatingSubscriber implements Subscriber<ByteBuffer> {
24+
private final CompletableFuture<Long> contentLengthFuture = new CompletableFuture<>();
25+
private Subscription subscription;
26+
private long length = 0;
27+
28+
@Override
29+
public void onSubscribe(Subscription subscription) {
30+
if (this.subscription == null) {
31+
this.subscription = subscription;
32+
this.subscription.request(Long.MAX_VALUE);
33+
} else {
34+
subscription.cancel();
35+
}
36+
}
37+
38+
@Override
39+
public void onNext(ByteBuffer byteBuffer) {
40+
length += byteBuffer.remaining();
41+
}
42+
43+
@Override
44+
public void onError(Throwable throwable) {
45+
contentLengthFuture.completeExceptionally(throwable);
46+
}
47+
48+
@Override
49+
public void onComplete() {
50+
contentLengthFuture.complete(length);
51+
}
52+
53+
public CompletableFuture<Long> contentLengthFuture() {
54+
return contentLengthFuture;
55+
}
56+
}

core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/util/SignerUtils.java

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -339,33 +339,4 @@ public static String getContentHash(SdkHttpRequest.Builder requestBuilder) {
339339
private static SdkChecksum sha256Checksum() {
340340
return SdkChecksum.forAlgorithm(() -> "SHA256");
341341
}
342-
343-
private static class LengthCalculatingSubscriber implements Subscriber<ByteBuffer> {
344-
private final CompletableFuture<Long> contentLengthFuture = new CompletableFuture<>();
345-
private long length = 0;
346-
347-
@Override
348-
public void onSubscribe(Subscription subscription) {
349-
subscription.request(Long.MAX_VALUE);
350-
}
351-
352-
@Override
353-
public void onNext(ByteBuffer byteBuffer) {
354-
length += byteBuffer.remaining();
355-
}
356-
357-
@Override
358-
public void onError(Throwable throwable) {
359-
contentLengthFuture.completeExceptionally(throwable);
360-
}
361-
362-
@Override
363-
public void onComplete() {
364-
contentLengthFuture.complete(length);
365-
}
366-
367-
public CompletableFuture<Long> contentLengthFuture() {
368-
return contentLengthFuture;
369-
}
370-
}
371342
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.http.auth.aws.internal.signer.util;
17+
18+
import java.nio.ByteBuffer;
19+
import org.reactivestreams.Subscriber;
20+
import org.reactivestreams.tck.SubscriberBlackboxVerification;
21+
import org.reactivestreams.tck.TestEnvironment;
22+
23+
public class LengthCalculatingSubscriberTckTest extends SubscriberBlackboxVerification<ByteBuffer> {
24+
25+
public LengthCalculatingSubscriberTckTest() {
26+
super(new TestEnvironment());
27+
}
28+
29+
@Override
30+
public Subscriber<ByteBuffer> createSubscriber() {
31+
return new LengthCalculatingSubscriber();
32+
}
33+
34+
@Override
35+
public ByteBuffer createElement(int element) {
36+
return ByteBuffer.wrap(Integer.toString(element).getBytes());
37+
}
38+
}

0 commit comments

Comments
 (0)