@Test public void getInstantSpeedDurationMillis() { doReturn(2L).when(calculator).nowMillis(); calculator.timestamp = 1; assertThat(calculator.getInstantSpeedDurationMillis()).isOne(); }
@Test public void getSizeFromContentUri() throws IOException { mockOkDownload(); final Uri contentUri = mock(Uri.class); final ContentResolver resolver = mock(ContentResolver.class); final OkDownload okDownload = OkDownload.with(); final Context context = mock(Context.class); when(okDownload.context()).thenReturn(context); when(context.getContentResolver()).thenReturn(resolver); // null cursor when(resolver.query(contentUri, null, null, null, null)).thenReturn(null); assertThat(Util.getSizeFromContentUri(contentUri)).isZero(); // valid cursor final Cursor cursor = mock(Cursor.class); when(resolver.query(contentUri, null, null, null, null)).thenReturn(cursor); when(cursor.getLong(anyInt())).thenReturn(1L); assertThat(Util.getSizeFromContentUri(contentUri)).isOne(); verify(cursor).close(); }
@Test public void splitMetricsOnNameFuseable() { final Mono<Integer> unnamedSource = Mono.just(0).map(v -> 100 / v); final Mono<Integer> namedSource = Mono.just(0).map(v -> 100 / v) .name("foo"); final Mono<Integer> unnamed = new MonoMetricsFuseable<>(unnamedSource, registry) .onErrorResume(e -> Mono.empty()); final Mono<Integer> named = new MonoMetricsFuseable<>(namedSource, registry) .onErrorResume(e -> Mono.empty()); Mono.when(unnamed, named).block(); Timer unnamedMeter = registry .find(METER_FLOW_DURATION) .tag(TAG_STATUS, TAGVALUE_ON_ERROR) .tag(TAG_EXCEPTION, ArithmeticException.class.getName()) .tag(TAG_SEQUENCE_NAME, REACTOR_DEFAULT_NAME) .timer(); Timer namedMeter = registry .find(METER_FLOW_DURATION) .tag(TAG_STATUS, TAGVALUE_ON_ERROR) .tag(TAG_EXCEPTION, ArithmeticException.class.getName()) .tag(TAG_SEQUENCE_NAME, "foo") .timer(); assertThat(unnamedMeter).isNotNull(); assertThat(unnamedMeter.count()).isOne(); assertThat(namedMeter).isNotNull(); assertThat(namedMeter.count()).isOne(); }
@Test public void splitMetricsOnNameFuseable() { final Flux<Integer> unnamedSource = Flux.just(0).map(v -> 100 / v); final Flux<Integer> namedSource = Flux.range(1, 40) .map(i -> 100 / (40 - i)) .name("foo"); final Flux<Integer> unnamed = new FluxMetricsFuseable<>(unnamedSource, registry) .onErrorResume(e -> Mono.empty()); final Flux<Integer> named = new FluxMetricsFuseable<>(namedSource, registry) .onErrorResume(e -> Mono.empty()); Mono.when(unnamed, named).block(); Timer unnamedMeter = registry .find(METER_FLOW_DURATION) .tag(TAG_STATUS, TAGVALUE_ON_ERROR) .tag(TAG_SEQUENCE_NAME, REACTOR_DEFAULT_NAME) .timer(); Timer namedMeter = registry .find(METER_FLOW_DURATION) .tag(TAG_STATUS, TAGVALUE_ON_ERROR) .tag(TAG_SEQUENCE_NAME, "foo") .timer(); assertThat(unnamedMeter).isNotNull(); assertThat(unnamedMeter.count()).isOne(); assertThat(namedMeter).isNotNull(); assertThat(namedMeter.count()).isOne(); }
@Test public void splitMetricsOnName() { final Mono<Integer> unnamedSource = Mono.<Integer>error(new ArithmeticException("boom")) .hide(); final Mono<Integer> unnamed = new MonoMetrics<>(unnamedSource, registry) .onErrorResume(e -> Mono.empty()); final Mono<Integer> namedSource = Mono.just(40) .name("foo") .map(i -> 100 / (40 - i)) .hide(); final Mono<Integer> named = new MonoMetrics<>(namedSource, registry) .onErrorResume(e -> Mono.empty()); Mono.when(unnamed, named).block(); Timer unnamedMeter = registry .find(METER_FLOW_DURATION) .tag(TAG_STATUS, TAGVALUE_ON_ERROR) .tag(TAG_EXCEPTION, ArithmeticException.class.getName()) .tag(TAG_SEQUENCE_NAME, REACTOR_DEFAULT_NAME) .timer(); Timer namedMeter = registry .find(METER_FLOW_DURATION) .tag(TAG_STATUS, TAGVALUE_ON_ERROR) .tag(TAG_EXCEPTION, ArithmeticException.class.getName()) .tag(TAG_SEQUENCE_NAME, "foo") .timer(); assertThat(unnamedMeter).isNotNull(); assertThat(namedMeter).isNotNull(); assertThat(unnamedMeter.count()).isOne(); assertThat(namedMeter.count()).isOne(); }
@Test public void splitMetricsOnName() { final Flux<Integer> unnamedSource = Flux.<Integer>error(new ArithmeticException("boom")) .hide(); final Flux<Integer> unnamed = new FluxMetrics<>(unnamedSource, registry) .onErrorResume(e -> Mono.empty()); final Flux<Integer> namedSource = Flux.range(1, 40) .name("foo") .map(i -> 100 / (40 - i)) .hide(); final Flux<Integer> named = new FluxMetrics<>(namedSource, registry) .onErrorResume(e -> Mono.empty()); Mono.when(unnamed, named).block(); Timer unnamedMeter = registry .find(METER_FLOW_DURATION) .tag(TAG_STATUS, TAGVALUE_ON_ERROR) .tag(TAG_EXCEPTION, ArithmeticException.class.getName()) .tag(TAG_SEQUENCE_NAME, REACTOR_DEFAULT_NAME) .timer(); Timer namedMeter = registry .find(METER_FLOW_DURATION) .tag(TAG_STATUS, TAGVALUE_ON_ERROR) .tag(TAG_EXCEPTION, ArithmeticException.class.getName()) .tag(TAG_SEQUENCE_NAME, "foo") .timer(); assertThat(unnamedMeter).isNotNull(); assertThat(namedMeter).isNotNull(); assertThat(unnamedMeter.count()).isOne(); assertThat(namedMeter.count()).isOne(); }
@Test public void testUpsertWithoutChange() { collection.insertOne(json("_id: 1, a: 2, b: 3")); Document query = json("_id: 1"); Document update = json("$set: {a: 2}"); UpdateResult updateResult = collection.updateOne(query, update, new UpdateOptions().upsert(true)); assertThat(updateResult.getModifiedCount()).isZero(); assertThat(updateResult.getMatchedCount()).isOne(); assertThat(collection.find().first()).isEqualTo(json("_id: 1, a: 2, b: 3")); }
@Test(timeout = 10000) public void text() throws Exception { try (Watcher<String> watcher = dogma.client().fileWatcher("directory", "my-service", Query.ofText("/endpoints.txt"))) { final CountDownLatch latch = new CountDownLatch(2); watcher.watch(unused -> latch.countDown()); final CentralDogmaEndpointGroup<String> endpointGroup = CentralDogmaEndpointGroup.ofWatcher( watcher, EndpointListDecoder.TEXT); endpointGroup.awaitInitialEndpoints(); assertThat(endpointGroup.endpoints()).isEqualTo(ENDPOINT_LIST); assertThat(latch.getCount()).isOne(); dogma.client().push("directory", "my-service", Revision.HEAD, "commit", Change.ofTextUpsert("/endpoints.txt", "foo.bar:1234")) .join(); await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> assertThat(latch.getCount()).isZero()); assertThat(endpointGroup.endpoints()).containsExactly(Endpoint.of("foo.bar", 1234)); } } }