Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

smaller coding improvements #1

Merged
merged 2 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/fibonacci-worker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<kotlin.version>1.9.24</kotlin.version>
<camunda.version>8.5.3</camunda.version>
<mockk.version>1.12.0</mockk.version>
<kotest.version>5.9.1</kotest.version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -61,6 +62,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.kotest</groupId>
<artifactId>kotest-assertions-core-jvm</artifactId>
<version>${kotest.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,41 @@ package de.envite.greenbpm.camunda_process_carbon_pricing.fibonacci_worker
import io.micrometer.core.instrument.Counter
import io.micrometer.core.instrument.Gauge
import io.micrometer.core.instrument.MeterRegistry
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.util.concurrent.atomic.AtomicInteger

@Service
class JobExporterService @Autowired constructor(private val registry: MeterRegistry) {
internal const val BASE_NAME = "fibonacciworker_jobs"
internal const val JOB_KEY_TAG = "jobKey"

@Service
class JobExporterService(private val registry: MeterRegistry) {

val jobsInExecution = AtomicInteger(0)
private val jobsInExecution = AtomicInteger(0)

init {
Gauge.builder("fibonacciworker_jobs_in_execution") {
Gauge.builder("${BASE_NAME}_in_execution") {
jobsInExecution.toDouble()
}
.description("Number of jobs currently in execution")
.register(registry)
}

fun reportJobStarted(jobId: String) {
Counter.builder("fibonacciworker_jobs_started")
.tag("jobKey", jobId)
Counter.builder("${BASE_NAME}_started")
.tag(JOB_KEY_TAG, jobId)
.register(registry)
.increment()

jobsInExecution.incrementAndGet()
}

fun reportJobFinished(jobId: String) {
Counter.builder("fibonacciworker_jobs_finished")
Counter.builder("${BASE_NAME}_finished")
.description("Increases for every job handling finished")
.tag("jobKey", jobId)
.tag(JOB_KEY_TAG, jobId)
.register(registry)
.increment()
jobsInExecution.decrementAndGet()


jobsInExecution.decrementAndGet()
}

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package de.envite.greenbpm.camunda_process_carbon_pricing.fibonacci_worker

import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import io.kotest.matchers.shouldBe
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.MethodSource

class FibonacciKtTest{
@ParameterizedTest(name= "For {0}, the Fibonacci is {1}")
class FibonacciKtTest {
@ParameterizedTest(name = "For {0}, the Fibonacci is {1}")
@MethodSource("getData")
fun `should return correct fibonacci values`(input: Int, expected: Int){
assertEquals(fibonacciRec(input), expected)
}
fun `should return correct fibonacci values`(input: Int, expected: Int) = fibonacciRec(input) shouldBe expected

companion object{
@JvmStatic
Expand Down
Original file line number Diff line number Diff line change
@@ -1,84 +1,37 @@
package de.envite.greenbpm.camunda_process_carbon_pricing.fibonacci_worker

import io.micrometer.core.instrument.Counter
import io.micrometer.core.instrument.Gauge
import io.micrometer.core.instrument.MeterRegistry
import io.micrometer.core.instrument.Tag
import org.junit.jupiter.api.BeforeEach
import org.springframework.boot.test.context.SpringBootTest

import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.mockk
import io.mockk.verify
import org.junit.jupiter.api.Assertions.assertEquals
import io.kotest.assertions.assertSoftly
import io.kotest.matchers.shouldBe
import io.micrometer.core.instrument.simple.SimpleMeterRegistry
import org.junit.jupiter.api.Test
import org.mockito.ArgumentMatchers.*

@SpringBootTest
class JobExporterServiceTest {
private lateinit var meterRegistry: MeterRegistry
private lateinit var jobExporterService: JobExporterService
private lateinit var jobsStartedCounter: Counter
private lateinit var jobsFinishedCounter: Counter

@BeforeEach
fun setup(){
meterRegistry = mockk(relaxed = true)
jobsStartedCounter = mockk(relaxed = true)
jobsFinishedCounter = mockk(relaxed = true)

every { Counter.builder("fibonacciworker_jobs_started")
.tag("jobKey","testId")
.register(meterRegistry)
} returns jobsStartedCounter

every { Counter.builder("fibonacciworker_jobs_finished")
.tag("jobKey","testId")
.register(meterRegistry)
} returns jobsFinishedCounter

jobExporterService = JobExporterService(meterRegistry)
}

@Test
fun `reportJobStarted should increase jobsStartedCounter`() {
val id = "testId"

jobExporterService.reportJobStarted(id)

verify { jobsStartedCounter.increment() }
class JobExporterServiceTest {

assertEquals(1, jobExporterService.jobsInExecution.get())
}
private val meterRegistry: SimpleMeterRegistry = SimpleMeterRegistry()
private val classUnderTest: JobExporterService = JobExporterService(meterRegistry)

@Test
fun `reportJobFinished should increase jobsFinishedCounter`() {
fun `reportJobStarted should increase jobsStartedCounter and update gauge`() {
val id = "testId"

jobExporterService.reportJobFinished(id)

verify { jobsFinishedCounter.increment() }

assertEquals(-1, jobExporterService.jobsInExecution.get())
classUnderTest.reportJobStarted(id)

assertSoftly {
meterRegistry.get("${BASE_NAME}_started").counter().count() shouldBe 1.0
meterRegistry.get("${BASE_NAME}_in_execution").gauge().value() shouldBe 1.0
}
}

@Test
fun `increase and decrease jobsInExecution`() {
fun `reportJobStarted should decrease jobsStartedCounter and update gauge`() {
val id = "testId"

jobExporterService.reportJobStarted(id)

assertEquals(1, jobExporterService.jobsInExecution.get())

jobExporterService.reportJobFinished(id)

assertEquals(0, jobExporterService.jobsInExecution.get())
classUnderTest.reportJobFinished(id)

assertSoftly {
meterRegistry.get("${BASE_NAME}_finished").counter().count() shouldBe 1.0
meterRegistry.get("${BASE_NAME}_in_execution").gauge().value() shouldBe -1.0
}
}




}