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