|
| 1 | +package io.weaviate.client6.v1.api.collections.batch; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.Optional; |
| 5 | + |
| 6 | +import org.assertj.core.api.Assertions; |
| 7 | +import org.assertj.core.api.InstanceOfAssertFactories; |
| 8 | +import org.junit.Before; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import io.grpc.stub.StreamObserver; |
| 12 | +import io.weaviate.client6.v1.api.collections.query.ConsistencyLevel; |
| 13 | +import io.weaviate.client6.v1.internal.grpc.protocol.WeaviateProtoBase; |
| 14 | +import io.weaviate.client6.v1.internal.grpc.protocol.WeaviateProtoBatch; |
| 15 | + |
| 16 | +public class TranslatingStreamFactoryTest { |
| 17 | + private static final StreamObserver<Event> NOP_RECV = new StreamObserver<Event>() { |
| 18 | + |
| 19 | + @Override |
| 20 | + public void onCompleted() { |
| 21 | + throw new UnsupportedOperationException("Unimplemented method 'onCompleted'"); |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + public void onError(Throwable t) { |
| 26 | + throw new UnsupportedOperationException("Unimplemented method 'onError'"); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public void onNext(Event reply) { |
| 31 | + throw new UnsupportedOperationException("Unimplemented method 'onNext'"); |
| 32 | + } |
| 33 | + }; |
| 34 | + |
| 35 | + private static final SpyStreamObserver<WeaviateProtoBatch.BatchStreamRequest> SPY_SEND; |
| 36 | + private static final SpyStreamObserver<Event> SPY_RECV; |
| 37 | + |
| 38 | + // RECV cannot be final, because we will only assign it when createStream |
| 39 | + // is called in the static constructor. |
| 40 | + private static StreamObserver<WeaviateProtoBatch.BatchStreamReply> RECV; |
| 41 | + private static final StreamObserver<Message> SEND; |
| 42 | + |
| 43 | + static { |
| 44 | + SPY_SEND = new SpyStreamObserver<>(); |
| 45 | + SPY_RECV = new SpyStreamObserver<>(); |
| 46 | + |
| 47 | + SEND = new TranslatingStreamFactory(recv -> { |
| 48 | + RECV = recv; // capture the underlying observer |
| 49 | + return SPY_SEND; |
| 50 | + }).createStream(SPY_RECV); |
| 51 | + } |
| 52 | + |
| 53 | + @Before |
| 54 | + public void resetSpies() { |
| 55 | + SPY_SEND.reset(); |
| 56 | + SPY_RECV.reset(); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testMessenger_onNext_start() { |
| 61 | + SEND.onNext(Message.start(Optional.of(ConsistencyLevel.ONE))); |
| 62 | + |
| 63 | + Assertions.assertThat(SPY_SEND.getLast()) |
| 64 | + .asInstanceOf(InstanceOfAssertFactories.type(WeaviateProtoBatch.BatchStreamRequest.class)) |
| 65 | + .extracting(WeaviateProtoBatch.BatchStreamRequest::getStart).as("start").isNotNull() |
| 66 | + .extracting(WeaviateProtoBatch.BatchStreamRequest.Start::getConsistencyLevel).as("consistency level") |
| 67 | + .isEqualTo(WeaviateProtoBase.ConsistencyLevel.CONSISTENCY_LEVEL_ONE); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testMessenger_onNext_stop() { |
| 72 | + SEND.onNext(Message.stop()); |
| 73 | + |
| 74 | + Assertions.assertThat(SPY_SEND.getLast()) |
| 75 | + .asInstanceOf(InstanceOfAssertFactories.type(WeaviateProtoBatch.BatchStreamRequest.class)) |
| 76 | + .extracting(WeaviateProtoBatch.BatchStreamRequest::getStop).as("stop").isNotNull(); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testMessenger_onNext_data() { |
| 81 | + // This message sets BatchStreamRequest.Data so it's not null. |
| 82 | + Message message = builder -> builder |
| 83 | + .setData(WeaviateProtoBatch.BatchStreamRequest.Data.newBuilder().build()); |
| 84 | + SEND.onNext(message); |
| 85 | + |
| 86 | + Assertions.assertThat(SPY_SEND.getLast()) |
| 87 | + .asInstanceOf(InstanceOfAssertFactories.type(WeaviateProtoBatch.BatchStreamRequest.class)) |
| 88 | + .extracting(WeaviateProtoBatch.BatchStreamRequest::getData).as("data").isNotNull(); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testMessenger_onError() { |
| 93 | + Throwable whaam = new Exception("Whaam!"); |
| 94 | + SEND.onError(whaam); |
| 95 | + Assertions.assertThat(SPY_SEND.getLast()).isEqualTo(whaam); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testMessenger_onCompleted() { |
| 100 | + SEND.onCompleted(); |
| 101 | + Assertions.assertThat(SPY_SEND.getLast()).isEqualTo(SpyStreamObserver.COMPLETED); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void testEventer_onNext_started() { |
| 106 | + RECV.onNext(WeaviateProtoBatch.BatchStreamReply.newBuilder() |
| 107 | + .setStarted(WeaviateProtoBatch.BatchStreamReply.Started.newBuilder()).build()); |
| 108 | + |
| 109 | + Assertions.assertThat(SPY_RECV.getLast()).isEqualTo(Event.STARTED); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void testEventer_onNext_shuttingDown() { |
| 114 | + RECV.onNext(WeaviateProtoBatch.BatchStreamReply.newBuilder() |
| 115 | + .setShuttingDown(WeaviateProtoBatch.BatchStreamReply.ShuttingDown.newBuilder()).build()); |
| 116 | + |
| 117 | + Assertions.assertThat(SPY_RECV.getLast()).isEqualTo(Event.SHUTTING_DOWN); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testEventer_onNext_oom() { |
| 122 | + // TODO(dyma): update to read seconds |
| 123 | + RECV.onNext(WeaviateProtoBatch.BatchStreamReply.newBuilder() |
| 124 | + .setOutOfMemory(WeaviateProtoBatch.BatchStreamReply.OutOfMemory.newBuilder()).build()); |
| 125 | + |
| 126 | + Assertions.assertThat(SPY_RECV.getLast()) |
| 127 | + .asInstanceOf(InstanceOfAssertFactories.type(Event.Oom.class)) |
| 128 | + .returns(300, Event.Oom::delaySeconds); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testEventer_onNext_backoff() { |
| 133 | + RECV.onNext(WeaviateProtoBatch.BatchStreamReply.newBuilder() |
| 134 | + .setBackoff(WeaviateProtoBatch.BatchStreamReply.Backoff.newBuilder() |
| 135 | + .setBatchSize(92)) |
| 136 | + .build()); |
| 137 | + |
| 138 | + Assertions.assertThat(SPY_RECV.getLast()) |
| 139 | + .asInstanceOf(InstanceOfAssertFactories.type(Event.Backoff.class)) |
| 140 | + .returns(92, Event.Backoff::maxSize); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void testEventer_onNext_acks() { |
| 145 | + RECV.onNext(WeaviateProtoBatch.BatchStreamReply.newBuilder() |
| 146 | + .setAcks(WeaviateProtoBatch.BatchStreamReply.Acks.newBuilder() |
| 147 | + .addAllUuids(List.of("uuid-1", "uuid-2")) |
| 148 | + .addAllBeacons(List.of("beacon-1", "beacon-2"))) |
| 149 | + .build()); |
| 150 | + |
| 151 | + Assertions.assertThat(SPY_RECV.getLast()) |
| 152 | + .asInstanceOf(InstanceOfAssertFactories.type(Event.Acks.class)) |
| 153 | + .extracting(Event.Acks::acked) |
| 154 | + .asInstanceOf(InstanceOfAssertFactories.list(String.class)) |
| 155 | + .containsOnly("uuid-1", "uuid-2", "beacon-1", "beacon-2"); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + public void testEventer_onNext_results() { |
| 160 | + RECV.onNext(WeaviateProtoBatch.BatchStreamReply.newBuilder() |
| 161 | + .setResults(WeaviateProtoBatch.BatchStreamReply.Results.newBuilder() |
| 162 | + .addSuccesses(WeaviateProtoBatch.BatchStreamReply.Results.Success.newBuilder().setUuid("uuid-1")) |
| 163 | + .addSuccesses(WeaviateProtoBatch.BatchStreamReply.Results.Success.newBuilder().setUuid("beacon-1")) |
| 164 | + .addErrors(WeaviateProtoBatch.BatchStreamReply.Results.Error.newBuilder() |
| 165 | + .setUuid("uuid-2").setError("bad uuid!")) |
| 166 | + .addErrors(WeaviateProtoBatch.BatchStreamReply.Results.Error.newBuilder() |
| 167 | + .setBeacon("beacon-2").setError("bad beacon!"))) |
| 168 | + .build()); |
| 169 | + |
| 170 | + Event.Results results = Assertions.assertThat(SPY_RECV.getLast()) |
| 171 | + .asInstanceOf(InstanceOfAssertFactories.type(Event.Results.class)) |
| 172 | + .actual(); |
| 173 | + |
| 174 | + Assertions.assertThat(results) |
| 175 | + .extracting(Event.Results::successful, InstanceOfAssertFactories.list(String.class)) |
| 176 | + .containsOnly("uuid-1", "beacon-1"); |
| 177 | + |
| 178 | + Assertions.assertThat(results) |
| 179 | + .extracting(Event.Results::errors, InstanceOfAssertFactories.map(String.class, String.class)) |
| 180 | + .containsKeys("uuid-2", "beacon-2") |
| 181 | + .containsValues("bad uuid!", "bad beacon!"); |
| 182 | + } |
| 183 | + |
| 184 | + @Test(expected = ProtocolViolationException.class) |
| 185 | + public void testEventer_onNext_notSet() { |
| 186 | + RECV.onNext(WeaviateProtoBatch.BatchStreamReply.newBuilder().build()); |
| 187 | + } |
| 188 | + |
| 189 | + @Test |
| 190 | + public void testEventer_onError() { |
| 191 | + Throwable whaam = new Exception("Whaam!"); |
| 192 | + RECV.onError(whaam); |
| 193 | + Assertions.assertThat(SPY_RECV.getLast()).isEqualTo(whaam); |
| 194 | + } |
| 195 | + |
| 196 | + @Test |
| 197 | + public void testEventer_onCompleted() { |
| 198 | + RECV.onCompleted(); |
| 199 | + Assertions.assertThat(SPY_RECV.getLast()).isEqualTo(SpyStreamObserver.COMPLETED); |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * Test utility that recods the value that the last callback was called with. |
| 204 | + */ |
| 205 | + private static class SpyStreamObserver<T> implements StreamObserver<T> { |
| 206 | + /** Well-known value for {@link #onCompleted} callback. */ |
| 207 | + static final Object COMPLETED = new Object(); |
| 208 | + |
| 209 | + // Latest message delivered to this observer. |
| 210 | + private Object last; |
| 211 | + |
| 212 | + Object getLast() { |
| 213 | + return last; |
| 214 | + } |
| 215 | + |
| 216 | + void reset() { |
| 217 | + last = null; |
| 218 | + } |
| 219 | + |
| 220 | + @Override |
| 221 | + public void onNext(T req) { |
| 222 | + last = req; |
| 223 | + } |
| 224 | + |
| 225 | + @Override |
| 226 | + public void onError(Throwable t) { |
| 227 | + last = t; |
| 228 | + } |
| 229 | + |
| 230 | + @Override |
| 231 | + public void onCompleted() { |
| 232 | + last = COMPLETED; |
| 233 | + } |
| 234 | + |
| 235 | + } |
| 236 | +} |
0 commit comments