-
-
Notifications
You must be signed in to change notification settings - Fork 78
JENKINS-63760 notifyQueued used #70
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2207b37
JENKINS-63760 notifyQueued used
joseblas 7209084
JENKINS-63760 notifyQueued used
joseblas b1318d9
change JEnkinsfile to remove recommended config
joseblas 37adef7
JENKINS-63760 revering JenkinsFile change
joseblas 57681f7
JENKINS-63768 more time for metrics
joseblas 9546e06
JENKINS-63768 more time for metrics
joseblas a62cfe6
JENKINS-63768 test sanitation
joseblas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -25,48 +25,95 @@ | |
| package jenkins.metrics.impl; | ||
|
|
||
| import hudson.ExtensionList; | ||
| import hudson.model.FreeStyleBuild; | ||
| import hudson.model.FreeStyleProject; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import hudson.model.Run; | ||
| import hudson.model.queue.QueueTaskFuture; | ||
| import jenkins.metrics.api.Metrics; | ||
| import jenkins.metrics.api.QueueItemMetricsEvent; | ||
| import jenkins.metrics.api.QueueItemMetricsListener; | ||
| import org.junit.ClassRule; | ||
| import org.junit.Ignore; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.jvnet.hudson.test.BuildWatcher; | ||
| import org.jvnet.hudson.test.JenkinsRule; | ||
| import org.jvnet.hudson.test.SleepBuilder; | ||
| import org.jvnet.hudson.test.TestExtension; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.Matchers.allOf; | ||
| import static org.hamcrest.Matchers.containsString; | ||
| import static org.hamcrest.Matchers.emptyCollectionOf; | ||
| import static org.hamcrest.Matchers.greaterThan; | ||
| import static org.hamcrest.Matchers.is; | ||
| import static org.hamcrest.Matchers.lessThan; | ||
| import static org.hamcrest.Matchers.notNullValue; | ||
| import static org.junit.Assert.assertThat; | ||
|
|
||
| public class JenkinsMetricProviderImplTest { | ||
| @Rule | ||
| public JenkinsRule j = new JenkinsRule(); | ||
|
|
||
| @ClassRule | ||
| public static BuildWatcher w = new BuildWatcher(); | ||
|
|
||
| @Test | ||
| public void given__a_job_which_is_cancelled() throws Exception { | ||
| MyListener listener = ExtensionList.lookup(QueueItemMetricsListener.class).get(MyListener.class); | ||
| assertThat(listener, notNullValue()); | ||
|
|
||
| FreeStyleProject job = j.createProject(FreeStyleProject.class); | ||
|
|
||
| QueueTaskFuture<FreeStyleBuild> taskFuture = job.scheduleBuild2(10); | ||
| taskFuture.cancel(true); | ||
|
|
||
| try{ | ||
| j.waitForCompletion(taskFuture.waitForStart()); | ||
| }catch (Throwable t){ | ||
| //CancellationException expected | ||
| } | ||
|
|
||
| //wait for completion | ||
| while (!listener.state.toString().contains("C")){ | ||
| Thread.sleep(10); | ||
| } | ||
|
|
||
| assertThat(listener.state.toString(), containsString("Q")); | ||
|
||
| assertThat(listener.state.toString(), containsString("C")); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void given__a_job__when__built__then__events_and_metrics_include_build() throws Exception { | ||
| j.jenkins.setQuietPeriod(0); | ||
| MyListener listener = ExtensionList.lookup(QueueItemMetricsListener.class).get(MyListener.class); | ||
| assertThat(listener, notNullValue()); | ||
|
|
||
| FreeStyleProject p = j.createProject(FreeStyleProject.class); | ||
| p.setQuietPeriod(0); | ||
| p.getBuildersList().add(new SleepBuilder(3000)); | ||
| assertThat(Metrics.metricRegistry().getTimers().get("jenkins.job.building.duration").getCount(), is(0L)); | ||
| assertThat(listener.getEvents(), is(emptyCollectionOf(QueueItemMetricsEvent.class))); | ||
| j.assertBuildStatusSuccess(p.scheduleBuild2(2)); | ||
|
|
||
| //wait for completion | ||
| while (!listener.state.toString().contains("F")){ | ||
| Thread.sleep(10); | ||
| } | ||
| assertThat(listener.state.toString(), containsString("Q")); | ||
| assertThat(listener.state.toString(), containsString("S")); | ||
| assertThat(listener.state.toString(), containsString("F")); | ||
|
|
||
| assertThat(Metrics.metricRegistry().getTimers().get("jenkins.job.building.duration").getCount(), is(1L)); | ||
| assertThat(Metrics.metricRegistry().getTimers().get("jenkins.job.building.duration").getSnapshot().getMean(), | ||
| allOf(greaterThan(TimeUnit.MILLISECONDS.toNanos(2500)*1.0), | ||
| lessThan(TimeUnit.MILLISECONDS.toNanos(3500) * 1.0))); | ||
| lessThan(TimeUnit.MILLISECONDS.toNanos(3800) * 1.0))); | ||
| List<QueueItemMetricsEvent> events = listener.getEvents(); | ||
| assertThat(events.size(), is(3)); | ||
| Collections.sort(events, QueueItemMetricsEvent::compareEventSequence); | ||
|
|
@@ -89,16 +136,18 @@ public void given__a_job__when__built__then__events_and_metrics_include_build() | |
| assertThat(events.get(2).getExecutable(), is(Optional.of(p.getBuildByNumber(1)))); | ||
| assertThat(events.get(2).getQueuingTotalMillis().orElse(null), greaterThan(1500L)); | ||
| assertThat(events.get(2).getQueuingWaitingMillis().orElse(null), greaterThan(1500L)); | ||
| assertThat(events.get(2).getExecutingMillis().orElse(null), allOf(greaterThan(2500L), lessThan(3500L))); | ||
| assertThat(events.get(2).getExecutingMillis().orElse(null), allOf(greaterThan(2500L), lessThan(3800L))); | ||
| assertThat(events.get(2).getExecutorCount().orElse(null), is(1)); | ||
| } | ||
|
|
||
| @TestExtension | ||
| public static class MyListener extends QueueItemMetricsListener { | ||
| private final List<QueueItemMetricsEvent> events = new ArrayList<>(); | ||
| public StringBuilder state = new StringBuilder(); | ||
|
|
||
| @Override | ||
| public void onQueued(QueueItemMetricsEvent event) { | ||
| state.append("Q"); | ||
| synchronized (events) { | ||
| events.add(event); | ||
| } | ||
|
|
@@ -107,20 +156,23 @@ public void onQueued(QueueItemMetricsEvent event) { | |
|
|
||
| @Override | ||
| public void onCancelled(QueueItemMetricsEvent event) { | ||
| state.append("C"); | ||
| synchronized (events) { | ||
| events.add(event); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onStarted(QueueItemMetricsEvent event) { | ||
| state.append("S"); | ||
| synchronized (events) { | ||
| events.add(event); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onFinished(QueueItemMetricsEvent event) { | ||
| state.append("F"); | ||
| synchronized (events) { | ||
| events.add(event); | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has the potential to be a breaking change, but from a quick GitHub search it looks like there aren't any open source plugins using
QueueItemMetricsListenerin the first place, so the risk seems limited.