Skip to content

Commit

Permalink
Merge pull request #860 from jobrunr/bugfix/GH-857
Browse files Browse the repository at this point in the history
Add warning message if using Kotlin default parameter values (see #857)
  • Loading branch information
rdehuyss committed Oct 27, 2023
2 parents b473a14 + 43a7b11 commit 287d5ba
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@

import static java.util.Arrays.asList;
import static org.jobrunr.utils.reflection.ReflectionUtils.cast;
import static org.jobrunr.utils.reflection.ReflectionUtils.classExists;

public class AllJVMInstructions {

private static final Map<Integer, Function<JobDetailsBuilder, AbstractJVMInstruction>> instructions = new HashMap<>();
private static final Map<Integer, String> unsupportedInstructions = new HashMap<>();
private static final Map<Integer, String> kotlinUnsupportedInstructions = new HashMap<>();

static {
instructions.put(Opcodes.AASTORE, AAStoreInstruction::new);
Expand Down Expand Up @@ -71,6 +73,8 @@ public class AllJVMInstructions {
.forEach(instr -> unsupportedInstructions.put(instr, "You are dividing two numbers while enqueueing/scheduling jobs" + mathematicalPerformanceSuffix));
asList(Opcodes.IREM, Opcodes.LREM, Opcodes.FREM, Opcodes.DREM)
.forEach(instr -> unsupportedInstructions.put(instr, "You are calculating the remainder (%) for two numbers while enqueueing/scheduling jobs" + mathematicalPerformanceSuffix));

kotlinUnsupportedInstructions.put(Opcodes.ACONST_NULL, "You are (probably) using Kotlin default parameter values which is not supported by JobRunr.");
}

private AllJVMInstructions() {
Expand All @@ -82,6 +86,8 @@ public static <T extends AbstractJVMInstruction> T get(int opcode, JobDetailsBui
if (instructionBuilder == null) {
if (unsupportedInstructions.containsKey(opcode)) {
throw new IllegalArgumentException("Unsupported lambda", new UnsupportedOperationException(unsupportedInstructions.get(opcode)));
} else if (classExists("kotlin.KotlinVersion") && kotlinUnsupportedInstructions.containsKey(opcode)) {
throw new IllegalArgumentException("Unsupported lambda", new UnsupportedOperationException(kotlinUnsupportedInstructions.get(opcode)));
}
throw JobRunrException.shouldNotHappenException(new IllegalArgumentException("Instruction " + opcode + " not found"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonValue
import org.awaitility.Awaitility.await
import org.awaitility.Durations
import org.jobrunr.JobRunrAssertions.assertThat
import org.jobrunr.JobRunrAssertions.assertThatCode
import org.jobrunr.configuration.JobRunr
import org.jobrunr.jobs.mappers.JobMapper
import org.jobrunr.jobs.states.StateName.*
Expand Down Expand Up @@ -57,6 +58,15 @@ class JobSchedulerTest {
assertThat(job).hasStates(ENQUEUED, PROCESSING, SUCCEEDED)
}

@Test
fun `test enqueue lambda with default parameter throws exception`() {
val testService = TestService()
assertThatCode { jobScheduler.enqueue { testService.doWorkWithDefaultParameter() } }
.isInstanceOf(IllegalArgumentException::class.java)
.hasMessage("Unsupported lambda")
.hasRootCauseMessage("You are (probably) using Kotlin default parameter values which is not supported by JobRunr.")
}

@Test
fun `test enqueue lambda with service dependency`() {
val testService = TestService()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package org.jobrunr.scheduling

import org.jobrunr.jobs.annotations.Job
import java.util.*

class TestService {

fun doWork(s: String) {
println(s)
}

fun doWorkWithDefaultParameter(id: UUID = UUID.randomUUID()) {
println("id: $id")
}

@Job(name = "Some neat Job Display Name")
fun doWorkWithJobName(s: String, jobName: String) {
println("$s $jobName")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonValue
import org.awaitility.Awaitility.await
import org.awaitility.Durations
import org.jobrunr.JobRunrAssertions
import org.jobrunr.JobRunrAssertions.assertThat
import org.jobrunr.configuration.JobRunr
import org.jobrunr.jobs.mappers.JobMapper
Expand Down Expand Up @@ -57,6 +58,15 @@ class JobSchedulerTest {
assertThat(job).hasStates(ENQUEUED, PROCESSING, SUCCEEDED)
}

@Test
fun `test enqueue lambda with default parameter throws exception`() {
val testService = TestService()
JobRunrAssertions.assertThatCode { jobScheduler.enqueue { testService.doWorkWithDefaultParameter() } }
.isInstanceOf(IllegalArgumentException::class.java)
.hasMessage("Unsupported lambda")
.hasRootCauseMessage("You are (probably) using Kotlin default parameter values which is not supported by JobRunr.")
}

@Test
fun `test enqueue lambda with service dependency`() {
val testService = TestService()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jobrunr.scheduling

import org.jobrunr.jobs.annotations.Job
import java.util.*

class TestService {

Expand All @@ -10,6 +11,10 @@ class TestService {
println(s)
}

fun doWorkWithDefaultParameter(id: UUID = UUID.randomUUID()) {
println("id: $id")
}

fun doWorkWithPair(s: Pair<String, String>) {
println("Pair: " + s.first + "; " + s.second)
}
Expand Down

0 comments on commit 287d5ba

Please sign in to comment.