Skip to content

Commit

Permalink
[FIXED JENKINS-16634] Do not fail to write a log file just because so…
Browse files Browse the repository at this point in the history
…mething deleted the parent directory.

(cherry picked from commit afe17a4)
  • Loading branch information
jglick authored and olivergondza committed Feb 15, 2017
1 parent cf78e48 commit 9a34d9f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
1 change: 1 addition & 0 deletions core/pom.xml
Expand Up @@ -766,6 +766,7 @@ THE SOFTWARE.
<forkCount>0.5C</forkCount>
<reuseForks>true</reuseForks>
<argLine>-XX:MaxPermSize=128m -noverify</argLine> <!-- some versions of JDK7/8 causes VerifyError during mock tests: http://code.google.com/p/powermock/issues/detail?id=504 -->
<trimStackTrace>false</trimStackTrace> <!-- SUREFIRE-1226 workaround -->
</configuration>
</plugin>
<plugin><!-- set main class -->
Expand Down
Expand Up @@ -28,6 +28,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.io.FileUtils;

/**
* {@link OutputStream} that writes to a file.
Expand All @@ -48,6 +49,7 @@ public RewindableFileOutputStream(File out) {
private synchronized OutputStream current() throws IOException {
if (current == null) {
if (!closed) {
FileUtils.forceMkdir(out.getParentFile());
try {
current = new FileOutputStream(out,false);
} catch (FileNotFoundException e) {
Expand Down
Expand Up @@ -9,16 +9,20 @@
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import org.apache.commons.io.FileUtils;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import org.jvnet.hudson.test.Issue;

/**
* @author Kohsuke Kawaguchi
*/
public class ReopenableRotatingFileOutputStreamTest {
public class RewindableRotatingFileOutputStreamTest {

@Rule
public TemporaryFolder tmp = new TemporaryFolder();

@Test
public void rotation() throws IOException, InterruptedException {
File base = File.createTempFile("test", "log");
ReopenableRotatingFileOutputStream os = new ReopenableRotatingFileOutputStream(base,3);
File base = tmp.newFile("test.log");
RewindableRotatingFileOutputStream os = new RewindableRotatingFileOutputStream(base,3);
PrintWriter w = new PrintWriter(os,true);
for (int i=0; i<=4; i++) {
w.println("Content"+i);
Expand All @@ -35,4 +39,21 @@ public void rotation() throws IOException, InterruptedException {

os.deleteAll();
}

@Issue("JENKINS-16634")
@Test
public void deletedFolder() throws Exception {
File dir = tmp.newFolder("dir");
File base = new File(dir, "x.log");
RewindableRotatingFileOutputStream os = new RewindableRotatingFileOutputStream(base, 3);
for (int i = 0; i < 2; i++) {
FileUtils.deleteDirectory(dir);
os.write('.');
FileUtils.deleteDirectory(dir);
os.write('.');
FileUtils.deleteDirectory(dir);
os.rewind();
}
}

}

0 comments on commit 9a34d9f

Please sign in to comment.