Skip to content

Commit

Permalink
Add Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raynigon committed Nov 8, 2022
1 parent 78d08ae commit 06d13b9
Show file tree
Hide file tree
Showing 12 changed files with 282 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
import com.raynigon.ecs.logging.async.executor.DefaultMdcForkJoinPool;
import com.raynigon.ecs.logging.async.model.MdcRunnable;
import com.raynigon.ecs.logging.async.scheduler.MdcScheduledExecutorService;
import com.raynigon.ecs.logging.async.service.AsyncMetricsService;
import com.raynigon.ecs.logging.async.service.MicrometerMetricsService;
import com.raynigon.ecs.logging.async.service.NoOpMetricsService;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down Expand Up @@ -51,4 +58,17 @@ public ThreadPoolTaskExecutor threadPoolTaskExecutor(){
executor.initialize();
return executor;
}

@Bean
@ConditionalOnClass(MeterRegistry.class)
@ConditionalOnProperty(name = "raynigon.logging.async.metrics.enabled", havingValue = "true")
public AsyncMetricsService micrometerAsyncMetricsService(MeterRegistry meterRegistry){
return new MicrometerMetricsService(meterRegistry);
}

@Bean
@ConditionalOnMissingBean(AsyncMetricsService.class)
public AsyncMetricsService fallbackAsyncMetricsService(){
return new NoOpMetricsService();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,8 @@
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
@ConditionalOnClass(MeterRegistry.class)
@ConditionalOnProperty(name = "raynigon.logging.async.metrics.enabled", havingValue = "true")
public class MicrometerMetricsService implements AsyncMetricsService {


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

import com.raynigon.ecs.logging.async.service.helper.NoOpTimer;
import com.raynigon.ecs.logging.async.service.helper.TimerWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.stereotype.Service;

@Service
@ConditionalOnMissingBean(AsyncMetricsService.class)
public class NoOpMetricsService implements AsyncMetricsService {
@Override
public TimerWrapper createQueueTimer(Class<?> source) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.raynigon.ecs.logging.async

import com.raynigon.ecs.logging.async.helper.MeterRegistryProvider
import com.raynigon.ecs.logging.async.service.AsyncMetricsService
import com.raynigon.ecs.logging.async.service.NoOpMetricsService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.ContextConfiguration
import spock.lang.Specification

@EnableAutoConfiguration
@ContextConfiguration(classes = [MdcExecutorConfiguration, MeterRegistryProvider])
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = [])
class FallbackMetricsServiceITSpec extends Specification {

@Autowired
AsyncMetricsService metricsService

def "test"(){
expect:
metricsService instanceof NoOpMetricsService
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.raynigon.ecs.logging.async

import com.raynigon.ecs.logging.async.helper.MeterRegistryProvider
import com.raynigon.ecs.logging.async.service.AsyncMetricsService
import com.raynigon.ecs.logging.async.service.MicrometerMetricsService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.ContextConfiguration
import spock.lang.Specification

@EnableAutoConfiguration
@ContextConfiguration(classes = [MdcExecutorConfiguration, MeterRegistryProvider])
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = ["raynigon.logging.async.metrics.enabled=true"])
class MicrometerMetricsServiceITSpec extends Specification {

@Autowired
AsyncMetricsService metricsService

def "test"() {
expect:
metricsService instanceof MicrometerMetricsService
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ class DefaultAsyncServiceSpec extends Specification {

SimpleMeterRegistry meterRegistry = new SimpleMeterRegistry()

AsyncMetricsService metricsService = new MicrometerMetricsService(meterRegistry)

@Subject
AsyncService service = new DefaultAsyncService(pool, meterRegistry)
AsyncService service = new DefaultAsyncService(pool, metricsService)

def 'supplyAsync gets executed'() {
given:
Expand All @@ -35,8 +37,8 @@ class DefaultAsyncServiceSpec extends Specification {
1 * pool.execute(_ as Runnable) >> { Runnable r -> r.run() }

and:
meterRegistry.find(DefaultAsyncService.QUEUE_TIMER_NAME).timer().count() == 1
meterRegistry.find(DefaultAsyncService.EXECUTION_TIMER_NAME).timer().count() == 1
meterRegistry.find(MicrometerMetricsService.QUEUE_TIMER_NAME).timer().count() == 1
meterRegistry.find(MicrometerMetricsService.EXECUTION_TIMER_NAME).timer().count() == 1
}

def 'submit gets executed'() {
Expand All @@ -62,7 +64,7 @@ class DefaultAsyncServiceSpec extends Specification {
}

and:
meterRegistry.find(DefaultAsyncService.QUEUE_TIMER_NAME).timer().count() == 1
meterRegistry.find(DefaultAsyncService.EXECUTION_TIMER_NAME).timer().count() == 1
meterRegistry.find(MicrometerMetricsService.QUEUE_TIMER_NAME).timer().count() == 1
meterRegistry.find(MicrometerMetricsService.EXECUTION_TIMER_NAME).timer().count() == 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.raynigon.ecs.logging.async.service

import io.micrometer.core.instrument.simple.SimpleMeterRegistry
import spock.lang.Specification
import spock.lang.Subject

class MicrometerMetricsServiceSpec extends Specification {

SimpleMeterRegistry meterRegistry = new SimpleMeterRegistry()

@Subject
MicrometerMetricsService service = new MicrometerMetricsService(meterRegistry)

def "create queue timer"() {
when:
service.createQueueTimer(String.class).record({ null })

then:
meterRegistry.find(MicrometerMetricsService.QUEUE_TIMER_NAME).timer().count() == 1
meterRegistry.find(MicrometerMetricsService.EXECUTION_TIMER_NAME).timer() == null
}

def "create execution timer"() {
when:
service.createExecutionTimer(String.class).record({ null })

then:
meterRegistry.find(MicrometerMetricsService.QUEUE_TIMER_NAME).timer() == null
meterRegistry.find(MicrometerMetricsService.EXECUTION_TIMER_NAME).timer().count() == 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.raynigon.ecs.logging.async.service

import spock.lang.Specification
import spock.lang.Subject

class NoOpMetricsServiceSpec extends Specification {

@Subject
NoOpMetricsService service = new NoOpMetricsService()

def "create queue timer"() {
expect:
service.createQueueTimer(String.class) != null
}

def "create execution timer"() {
expect:
service.createExecutionTimer(String.class) != null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.raynigon.ecs.logging.async.service.helper

import io.micrometer.core.instrument.simple.SimpleMeterRegistry
import io.micrometer.core.instrument.Timer;
import spock.lang.Specification

class MicrometerSampleSpec extends Specification {

def "can be created"() {
given:
Timer timer = new SimpleMeterRegistry().timer("test")
Timer.Sample sample = Timer.start()

when:
def result = new MicrometerSample(timer, sample)

then:
noExceptionThrown()
}

def "can be stopped"() {
given:
Timer timer = new SimpleMeterRegistry().timer("test")
Timer.Sample sample = Timer.start()
MicrometerSample wrapper = new MicrometerSample(timer, sample)

when:
def result = wrapper.stop()

then:
result > 0
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.raynigon.ecs.logging.async.service.helper

import io.micrometer.core.instrument.Timer
import io.micrometer.core.instrument.simple.SimpleMeterRegistry
import spock.lang.Specification

import java.util.concurrent.Callable
import java.util.function.Supplier

class MicrometerTimerSpec extends Specification {

def "supplier gets executed"() {
given:
Timer timer = new SimpleMeterRegistry().timer("test")
MicrometerTimer wrapper = new MicrometerTimer(timer);
Supplier supplier = Mock()

when:
wrapper.record(supplier)

then:
1 * supplier.get() >> null

and:
timer.count() == 1
}

def "callable gets executed"() {
given:
Timer timer = new SimpleMeterRegistry().timer("test")
MicrometerTimer wrapper = new MicrometerTimer(timer);
Callable callable = Mock()

when:
wrapper.recordCallable(callable)

then:
1 * callable.call() >> null

and:
timer.count() == 1
}

def "start created sample"() {
given:
Timer timer = new SimpleMeterRegistry().timer("test")
MicrometerTimer wrapper = new MicrometerTimer(timer);

when:
def result = wrapper.start()

then:
result instanceof MicrometerSample
}

def "stop returns minus duration"() {
given:
Timer timer = new SimpleMeterRegistry().timer("test")
MicrometerTimer wrapper = new MicrometerTimer(timer);
SampleWrapper sample = wrapper.start()

when:
def result = wrapper.stop(sample)

then:
result > 0
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.raynigon.ecs.logging.async.service.helper

import spock.lang.Specification

class NoOpSampleSpec extends Specification {

def "can be created"() {
expect:
new NoOpSample() != null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.raynigon.ecs.logging.async.service.helper


import spock.lang.Specification

import java.util.concurrent.Callable
import java.util.function.Supplier

class NoOpTimerSpec extends Specification {

def "supplier gets executed"() {
given:
NoOpTimer wrapper = new NoOpTimer();
Supplier supplier = Mock()

when:
wrapper.record(supplier)

then:
1 * supplier.get() >> null
}

def "callable gets executed"() {
given:
NoOpTimer wrapper = new NoOpTimer();
Callable callable = Mock()

when:
wrapper.recordCallable(callable)

then:
1 * callable.call() >> null
}

def "start created sample"() {
expect:
new NoOpTimer().start() instanceof NoOpSample
}

def "stop returns minus 1"() {
expect:
new NoOpTimer().stop(null) == -1
}
}

0 comments on commit 06d13b9

Please sign in to comment.