|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to you under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.stormcrawler.util; |
| 19 | + |
| 20 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 23 | + |
| 24 | +import com.esotericsoftware.kryo.Kryo; |
| 25 | +import com.esotericsoftware.kryo.io.Output; |
| 26 | +import java.util.LinkedList; |
| 27 | +import java.util.List; |
| 28 | +import org.apache.storm.serialization.SerializationFactory; |
| 29 | +import org.apache.storm.utils.Utils; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | + |
| 32 | +class CollectionMetricTest { |
| 33 | + |
| 34 | + /** Kryo as Storm sets it up for a topology that registers no classes of its own. */ |
| 35 | + private static Kryo stormKryo() { |
| 36 | + return SerializationFactory.getKryo(Utils.readDefaultConfig()); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void returnsTheMeasurementsSinceTheLastCall() { |
| 41 | + final CollectionMetric metric = new CollectionMetric(); |
| 42 | + metric.addMeasurement(3); |
| 43 | + metric.addMeasurement(5); |
| 44 | + assertEquals(List.of(3L, 5L), metric.getValueAndReset()); |
| 45 | + assertEquals(List.of(), metric.getValueAndReset()); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void measurementsCanBeSentToAMetricsConsumerInAnotherWorker() { |
| 50 | + final CollectionMetric metric = new CollectionMetric(); |
| 51 | + metric.addMeasurement(3); |
| 52 | + final Kryo kryo = stormKryo(); |
| 53 | + assertDoesNotThrow( |
| 54 | + () -> kryo.writeClassAndObject(new Output(1024), metric.getValueAndReset())); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void linkedListCannotBeSent() { |
| 59 | + // why getValueAndReset() copies the measurements into an ArrayList |
| 60 | + final Kryo kryo = stormKryo(); |
| 61 | + assertThrows( |
| 62 | + IllegalArgumentException.class, |
| 63 | + () -> kryo.writeClassAndObject(new Output(1024), new LinkedList<Long>())); |
| 64 | + } |
| 65 | +} |
0 commit comments