Skip to content

Instantly share code, notes, and snippets.

View Toparvion's full-sized avatar
:octocat:
Practicing creative engineering

Vladimir Plizga Toparvion

:octocat:
Practicing creative engineering
View GitHub Profile
@Toparvion
Toparvion / CompressTest.java
Last active November 26, 2023 11:08
A simple benchmark for comparing compression ratios of various compressing algorithms applied to a natural text
package pro.toparvion.stegotext.compress;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorStreamFactory;
import org.apache.commons.compress.utils.IOUtils;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.params.ParameterizedTest;
@Toparvion
Toparvion / WalkersDifferenceTest.java
Last active August 25, 2022 04:31
A unit test demonstrating the difference in JOL's layout and stats graph walkers (fails on v0.16)
package org.openjdk.jol.info;
import org.junit.Assert;
import org.junit.Test;
public class WalkersDifferenceTest {
static class A {
@Toparvion
Toparvion / ReactiveMdcFilter.java
Created November 19, 2021 02:55
A simple WebFilter (from Spring WebFlux framework) for correct handling of 'rid' URL query parameter as a similarly-named MDC entry (even in threads spawned by Reactor's Schedulers)
package pro.toparvion.sample.gateway.gatewaydemo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
@Toparvion
Toparvion / Resilience4J.RU.md
Created June 4, 2021 09:37
Описание работы и параметров CircuitBreaker'а в Resilience4J

В случае с Resilience4J предохранитель работает как конечный автомат с тремя нормальными состояниями:

и двумя специальными: DISABLED & FORCED_OPEN.

Дальше будет описана логика работы предохранителя в привязке к его конфигурируемым параметрам.

Предохранитель постоянно анализирует результаты проходящих через него вызовов и накапливает их в т.н. скользящем окне. Окна бывают двух типов в зависимости от параметра slidingWindowType:

@Toparvion
Toparvion / Yield.java
Created April 27, 2021 10:20
IDEA switch expression bug sample
package pro.toparvion.sample.yield;
public class Yield {
public static void main(String[] args) {
if (args.length < 2) {
System.out.println("Usage: yield <concat|find> operand1 operand2");
return;
}
String operation = args[0].toLowerCase();
@Toparvion
Toparvion / Jdk11FileWriteTest.java
Last active October 31, 2018 01:04
Sample test showcasing a side effect introduced with IDEA JDK 11 inspection for Files.write* methods
package tech.toparvion.analog.util.dev;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
@Toparvion
Toparvion / HOWTO.md
Last active March 15, 2024 22:07
JUnit test for conditional Spring Boot bean registration (@ConditionalOnProperty)

Problem

Suppose you have two classes that should be registered with Spring context exclusively, e.g. only one of the beans must exist in the context at any time based on some boolean property value. Of course you can do it by adding explicit if condition into your @Configuration class. But if the classes have no common interface it may be quite cumbersome. As an alternative you can use @ConditionalOnProperty annotation on your classes, e.g.:

@Service
@ConditionalOnProperty(name = "use-left-service", havingValue = "true", matchIfMissing = false)
public class LeftService
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.integration.core.MessagingTemplate;
import org.springframework.integration.dsl.AggregatorSpec;
import org.springframework.integration.store.MessageGroup;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import reactor.util.function.Tuple2;
@Toparvion
Toparvion / InputStreamTest.java
Created April 11, 2016 18:03
A very simple java8-style snippet to assert exception inside test method. Works without JUnit boilerplate (like rules, @expected annotations and so on) and without explicit try-catch block.
package tech.toparvion.gist;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import static tech.toparvion.gist.TestHelper.assertException;
public class InputStreamTest {