-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HWKMETRICS-199] initia support for virtual clock in REST API tests
H-Metrics will use the virtual clock when the system property hawkular.metrics.use-virtual-clock is set to true. It will cause the VirtualClockHandler to be deployed. It provides endpoints for fetching, setting, and incrementing the clock. There is still some work to do to make tests more repeatable. Tests still have to spin for some short delay to allow time for task execution to complete. This of course is problematic because there is no way to know how long to block. We need additional endpoints that provide functionality that are used in other tests. That is, tests essentially wait for notifications from the task scheduler that it has finished work for a time slice.
- Loading branch information
John Sanda
committed
Aug 11, 2015
1 parent
f9a1aa7
commit 5cd1fbf
Showing
12 changed files
with
302 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...s-api-jaxrs/src/main/java/org/hawkular/metrics/api/jaxrs/handler/VirtualClockHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright 2014-2015 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.hawkular.metrics.api.jaxrs.handler; | ||
|
||
import static javax.ws.rs.core.MediaType.APPLICATION_JSON; | ||
|
||
import java.util.Map; | ||
|
||
import javax.inject.Inject; | ||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.PUT; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.Response; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import org.hawkular.metrics.api.jaxrs.param.Duration; | ||
import org.hawkular.metrics.api.jaxrs.util.VirtualClock; | ||
import org.hawkular.metrics.tasks.api.TaskScheduler; | ||
|
||
|
||
/** | ||
* @author jsanda | ||
*/ | ||
@Path("/clock") | ||
@Consumes(APPLICATION_JSON) | ||
@Produces(APPLICATION_JSON) | ||
public class VirtualClockHandler { | ||
|
||
public static final String PATH = "/clock"; | ||
|
||
@Inject | ||
private VirtualClock virtualClock; | ||
|
||
@Inject | ||
private TaskScheduler taskScheduler; | ||
|
||
private static boolean started; | ||
|
||
@GET | ||
public Response getTime() { | ||
return Response.ok(ImmutableMap.<String, Object>of("now", virtualClock.now())).build(); | ||
} | ||
|
||
@PUT | ||
public Response setTime(Map<String, Object> params) { | ||
Long time = (Long) params.get("time"); | ||
virtualClock.advanceTimeTo(time); | ||
if (!started) { | ||
taskScheduler.start(); | ||
started = true; | ||
} | ||
return Response.ok().build(); | ||
} | ||
|
||
@POST | ||
public Response incrementTime(Duration duration) { | ||
virtualClock.advanceTimeBy(duration.getValue(), duration.getTimeUnit()); | ||
return Response.ok().build(); | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
api/metrics-api-jaxrs/src/main/java/org/hawkular/metrics/api/jaxrs/util/VirtualClock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2014-2015 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.hawkular.metrics.api.jaxrs.util; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import rx.schedulers.TestScheduler; | ||
|
||
/** | ||
* @author jsanda | ||
*/ | ||
public class VirtualClock { | ||
|
||
private TestScheduler scheduler; | ||
|
||
public VirtualClock() { | ||
} | ||
|
||
public VirtualClock(TestScheduler scheduler) { | ||
this.scheduler = scheduler; | ||
} | ||
|
||
public long now() { | ||
return scheduler.now(); | ||
} | ||
|
||
public void advanceTimeTo(long time) { | ||
scheduler.advanceTimeTo(time, TimeUnit.MILLISECONDS); | ||
} | ||
|
||
public void advanceTimeBy(long duration, TimeUnit timeUnit) { | ||
scheduler.advanceTimeBy(duration, timeUnit); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.