1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.core5.testing.reactive;
29
30 import io.reactivex.rxjava3.core.Emitter;
31 import io.reactivex.rxjava3.core.Flowable;
32 import io.reactivex.rxjava3.core.Single;
33 import io.reactivex.rxjava3.functions.Consumer;
34 import org.apache.hc.core5.annotation.Internal;
35 import org.apache.hc.core5.util.TextUtils;
36 import org.reactivestreams.Publisher;
37
38 import java.nio.ByteBuffer;
39 import java.nio.charset.StandardCharsets;
40 import java.security.MessageDigest;
41 import java.security.NoSuchAlgorithmException;
42 import java.util.Random;
43 import java.util.concurrent.atomic.AtomicReference;
44
45 @Internal
46 public class Reactive3TestUtils {
47
48 private final static byte[] RANGE = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
49 .getBytes(StandardCharsets.US_ASCII);
50
51
52
53
54
55
56
57 public static Flowable<ByteBuffer> produceStream(final long length) {
58 return produceStream(length, null);
59 }
60
61
62
63
64
65
66
67
68
69
70 public static Flowable<ByteBuffer> produceStream(final long length, final AtomicReference<String> hash) {
71 return produceStream(length, 128 * 1024, hash);
72 }
73
74
75
76
77
78
79
80
81
82
83 public static Flowable<ByteBuffer> produceStream(
84 final long length,
85 final int maximumBlockSize,
86 final AtomicReference<String> hash
87 ) {
88 return Flowable.generate(new Consumer<Emitter<ByteBuffer>>() {
89 final Random random = new Random(length);
90 long bytesEmitted;
91 final MessageDigest md = newMessageDigest();
92
93 @Override
94 public void accept(final Emitter<ByteBuffer> emitter) {
95 final long remainingLength = length - bytesEmitted;
96 if (remainingLength == 0) {
97 emitter.onComplete();
98 if (hash != null) {
99 hash.set(TextUtils.toHexString(md.digest()));
100 }
101 } else {
102 final int bufferLength = (int) Math.min(remainingLength, 1 + random.nextInt(maximumBlockSize));
103 final byte[] bs = new byte[bufferLength];
104 for (int i = 0; i < bufferLength; i++) {
105 final byte b = RANGE[(int) (random.nextDouble() * RANGE.length)];
106 bs[i] = b;
107 }
108 if (hash != null) {
109 md.update(bs);
110 }
111 emitter.onNext(ByteBuffer.wrap(bs));
112 bytesEmitted += bufferLength;
113 }
114 }
115 });
116 }
117
118
119
120
121 public static String getStreamHash(final long length) {
122 return TextUtils.toHexString(consumeStream(produceStream(length)).blockingGet().md.digest());
123 }
124
125
126
127
128 public static Single<StreamDescription> consumeStream(final Publisher<ByteBuffer> publisher) {
129 final StreamDescription seed = new StreamDescription(0, newMessageDigest());
130 return Flowable.fromPublisher(publisher)
131 .reduce(seed, (desc, byteBuffer) -> {
132 final long length = desc.length + byteBuffer.remaining();
133 desc.md.update(byteBuffer);
134 return new StreamDescription(length, desc.md);
135 });
136 }
137
138 private static MessageDigest newMessageDigest() {
139 try {
140 return MessageDigest.getInstance("MD5");
141 } catch (final NoSuchAlgorithmException ex) {
142 throw new AssertionError(ex);
143 }
144 }
145
146 public static class StreamDescription {
147 public final long length;
148 public final MessageDigest md;
149
150 public StreamDescription(final long length, final MessageDigest md) {
151 this.length = length;
152 this.md = md;
153 }
154 }
155 }