Skip to content

Commit

Permalink
Improve test coverage for scheduler component.
Browse files Browse the repository at this point in the history
  • Loading branch information
svkcemk authored and aldettinger committed Oct 12, 2022
1 parent 8644a95 commit d72e4fb
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,24 @@
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.camel.ProducerTemplate;

@Path("/scheduler")
@ApplicationScoped
public class SchedulerResource {

@Inject
ProducerTemplate producerTemplate;
@Named("withDelayCounter")
AtomicInteger withDelayCounter;

@Inject
@Named("useFixedDelayCounter")
AtomicInteger useFixedDelayCounter;

@Named("withDelayRepeatCounter")
AtomicInteger withDelayRepeatCounter;

@Inject
@Named("greedyCounter")
AtomicInteger greedyCounter;

@Inject
@Named("schedulerCounter")
Expand All @@ -46,11 +56,67 @@ public int get() throws Exception {
return schedulerCounter.get();
}

@Path("/get-delay-count")
@GET
@Produces(MediaType.TEXT_PLAIN)
public int getCountDelay() {
return withDelayCounter.get();
}

@Path("/get-fixed-delay-count")
@GET
@Produces(MediaType.TEXT_PLAIN)
public int getCountFixedDelay() {
return useFixedDelayCounter.get();
}

@Path("/get-repeat-count")
@GET
@Produces(MediaType.TEXT_PLAIN)
public int getRepeatCount() {
return withDelayRepeatCounter.get();
}

@Path("/get-greedy-count")
@GET
@Produces(MediaType.TEXT_PLAIN)
public int getGreedyCount() {
return greedyCounter.get();
}

@javax.enterprise.inject.Produces
@ApplicationScoped
@Named("schedulerCounter")
AtomicInteger schedulerCounter() {
return new AtomicInteger();
}

@javax.enterprise.inject.Produces
@ApplicationScoped
@Named("withDelayRepeatCounter")
AtomicInteger withDelayRepeatCounter() {
return new AtomicInteger();
}

@javax.enterprise.inject.Produces
@ApplicationScoped
@Named("withDelayCounter")
AtomicInteger withDelayCounter() {
return new AtomicInteger();
}

@javax.enterprise.inject.Produces
@ApplicationScoped
@Named("useFixedDelayCounter")
AtomicInteger useFixedDelayCounter() {
return new AtomicInteger();
}

@javax.enterprise.inject.Produces
@ApplicationScoped
@Named("greedyCounter")
AtomicInteger greedyCounter() {
return new AtomicInteger();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,37 @@ public class SchedulerRoute extends RouteBuilder {
@Named("schedulerCounter")
AtomicInteger schedulerCounter;

@Inject
@Named("withDelayCounter")
AtomicInteger withDelayCounter;

@Inject
@Named("useFixedDelayCounter")
AtomicInteger useFixedDelayCounter;

@Inject
@Named("withDelayRepeatCounter")
AtomicInteger withDelayRepeatCounter;

@Inject
@Named("greedyCounter")
AtomicInteger greedyCounter;

@Override
public void configure() throws Exception {
from("scheduler:start?initialDelay=1")
from("scheduler:withInitialDelay?initialDelay=1")
.process(e -> schedulerCounter.incrementAndGet());

from("scheduler:withDelay?delay=100")
.process(e -> withDelayCounter.incrementAndGet());

from("scheduler:useFixedDelay?initialDelay=200&useFixedDelay=true")
.process(e -> useFixedDelayCounter.incrementAndGet());

from("scheduler:withDelayRepeat?delay=1&repeatCount=5")
.process(e -> withDelayRepeatCounter.incrementAndGet());

from("scheduler:greedy?delay=100&greedy=true")
.process(e -> greedyCounter.incrementAndGet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,48 @@
class SchedulerTest {

@Test
public void test() throws Exception {
// wait until the scheduler has run and return a counter that is > 0
public void testInitialDelay() throws Exception {
await().atMost(5, TimeUnit.SECONDS).until(() -> {
String body = RestAssured.get("/scheduler/get").then().statusCode(200).extract().body().asString();
return !body.equals("0");
});
}

@Test
public void testDelay() throws Exception {
await().atMost(2, TimeUnit.SECONDS).until(() -> {
String body = RestAssured.get("/scheduler/get-delay-count").then().statusCode(200).extract().body()
.asString();
return Integer.parseInt(body) > 2;
});

}

@Test
public void testFixedDelay() throws Exception {
await().atMost(2, TimeUnit.SECONDS).until(() -> {
String body = RestAssured.get("/scheduler/get-fixed-delay-count").then().statusCode(200).extract().body()
.asString();
return Integer.parseInt(body) > 2;
});
}

@Test
public void testDelayWithRepeat() throws Exception {
await().atMost(4, TimeUnit.SECONDS).until(() -> {
String body = RestAssured.get("/scheduler/get-repeat-count").then().statusCode(200).extract().body()
.asString();
return Integer.parseInt(body) >= 4;
});
}

@Test
public void testGreedyScheduler() throws Exception {
await().atMost(1, TimeUnit.SECONDS).until(() -> {
String body = RestAssured.get("/scheduler/get-greedy-count").then().statusCode(200).extract().body()
.asString();
return Integer.parseInt(body) > 10;
});
}

}

0 comments on commit d72e4fb

Please sign in to comment.