Skip to content

Commit

Permalink
[FIXED JENKINS-15816] Run.ID_FORMATTER caches stale timezone values.
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Nov 13, 2012
1 parent d4b4170 commit d84eca4
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
3 changes: 3 additions & 0 deletions changelog.html
Expand Up @@ -68,6 +68,9 @@ <h3><a name=v1.491>What's new in 1.491</a> <!--=DATE=--></h3>
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-15733">issue 15733</a>)
<li class=rfe>
Support failsafe the same way as surefire in maven2 jobs
<li class=bug>
Build records were broken if timezone was changed while running.
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-15816">issue 15816</a>)
</ul>
</div><!--=END=-->
<h3><a name=v1.490>What's new in 1.490</a> (2012/11/12)</h3>
Expand Down
6 changes: 4 additions & 2 deletions core/src/main/java/hudson/model/Run.java
Expand Up @@ -96,7 +96,6 @@
import javax.servlet.http.HttpServletResponse;

import jenkins.model.Jenkins;
import jenkins.model.lazy.BuildReference;
import jenkins.util.io.OnMaster;
import net.sf.json.JSONObject;
import org.apache.commons.io.input.NullInputStream;
Expand Down Expand Up @@ -240,11 +239,14 @@ private static enum State {
*/
private volatile transient RunExecution runner;

private static final SimpleDateFormat CANONICAL_ID_FORMATTER = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
protected static final ThreadLocal<SimpleDateFormat> ID_FORMATTER =
new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
synchronized (CANONICAL_ID_FORMATTER) {
return (SimpleDateFormat) CANONICAL_ID_FORMATTER.clone();
}
}
};

Expand Down
80 changes: 80 additions & 0 deletions core/src/test/java/hudson/model/RunTest.java
@@ -0,0 +1,80 @@
/*
* The MIT License
*
* Copyright 2012 Jesse Glick.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package hudson.model;

import java.util.TimeZone;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static org.junit.Assert.*;
import org.junit.Test;
import org.jvnet.hudson.test.Bug;

public class RunTest {

@Bug(15816)
@SuppressWarnings({"unchecked", "rawtypes"})
@Test public void timezoneOfID() throws Exception {
TimeZone origTZ = TimeZone.getDefault();
try {
final Run r;
String id;
TimeZone.setDefault(TimeZone.getTimeZone("America/Chicago"));
ExecutorService svc = Executors.newSingleThreadExecutor();
try {
r = svc.submit(new Callable<Run>() {
@Override public Run call() throws Exception {
return new Run(new StubJob(), 1234567890) {};
}
}).get();
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
id = r.getId();
assertEquals(id, svc.submit(new Callable<String>() {
@Override public String call() throws Exception {
return r.getId();
}
}).get());
} finally {
svc.shutdown();
}
TimeZone.setDefault(TimeZone.getTimeZone("America/New_York"));
svc = Executors.newSingleThreadExecutor();
try {
assertEquals(id, r.getId());
assertEquals(id, svc.submit(new Callable<String>() {
@Override
public String call() throws Exception {
return r.getId();
}
}).get());
} finally {
svc.shutdown();
}
} finally {
TimeZone.setDefault(origTZ);
}
}

}
5 changes: 5 additions & 0 deletions core/src/test/java/hudson/model/StubJob.java
Expand Up @@ -23,6 +23,7 @@
*/
package hudson.model;

import java.io.File;
import java.util.SortedMap;

/**
Expand Down Expand Up @@ -55,6 +56,10 @@ protected void removeRun(Run run) {
// TODO Auto-generated method stub

}

@Override protected File getBuildDir() {
return new File(System.getProperty("java.io.tmpdir"));
}

/**
* Override save so that nothig happens when setDisplayName() is called
Expand Down

0 comments on commit d84eca4

Please sign in to comment.