aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/lib/profiler/ProfilerChartTest.java
diff options
context:
space:
mode:
authorGravatar laszlocsomor <laszlocsomor@google.com>2018-07-10 06:11:18 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-07-10 06:12:53 -0700
commitd17467e3bf9ff0408d201b960af71939b55b873b (patch)
tree926b928b4fac9decd8433a616e0a0cf271a6bab0 /src/test/java/com/google/devtools/build/lib/profiler/ProfilerChartTest.java
parent8c11fe9415e6d30069bdba559f6943747dd56e9f (diff)
Bazel server, tools: ensure Writers are closed
Follow-up to commit 09d20311d982606093ed881d779bb05a5ee70ed3. Use try-with-resources to ensure Writer objects are closed eagerly. Eagerly closing Writers avoids hanging on to file handles until the garbage collector finalizes the object, meaning Bazel on Windows (and other processes) can delete or mutate these files. Hopefully this avoids intermittent file deletion errors that sometimes occur on Windows. See https://github.com/bazelbuild/bazel/issues/5512 RELNOTES: none PiperOrigin-RevId: 203934471
Diffstat (limited to 'src/test/java/com/google/devtools/build/lib/profiler/ProfilerChartTest.java')
-rw-r--r--src/test/java/com/google/devtools/build/lib/profiler/ProfilerChartTest.java35
1 files changed, 19 insertions, 16 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/profiler/ProfilerChartTest.java b/src/test/java/com/google/devtools/build/lib/profiler/ProfilerChartTest.java
index 6b3bd519ad..43d6bbc2d1 100644
--- a/src/test/java/com/google/devtools/build/lib/profiler/ProfilerChartTest.java
+++ b/src/test/java/com/google/devtools/build/lib/profiler/ProfilerChartTest.java
@@ -36,6 +36,7 @@ import com.google.devtools.build.lib.testutil.Suite;
import com.google.devtools.build.lib.testutil.TestSpec;
import com.google.devtools.build.lib.vfs.Path;
import java.io.InputStream;
+import java.io.OutputStream;
import java.util.List;
import java.util.Locale;
import org.junit.Test;
@@ -249,23 +250,25 @@ public class ProfilerChartTest extends FoundationTestCase {
Path cacheDir = scratch.dir("/tmp");
Path cacheFile = cacheDir.getRelative("profile1.dat");
Profiler profiler = Profiler.instance();
- profiler.start(
- ProfiledTaskKinds.ALL,
- cacheFile.getOutputStream(),
- Profiler.Format.BINARY_BAZEL_FORMAT,
- "basic test",
- false,
- BlazeClock.instance(),
- BlazeClock.instance().nanoTime());
-
- // Write from multiple threads to generate multiple rows in the chart.
- for (int i = 0; i < noOfRows; i++) {
- Thread t = new Thread(runnable);
- t.start();
- t.join();
- }
+ try (OutputStream out = cacheFile.getOutputStream()) {
+ profiler.start(
+ ProfiledTaskKinds.ALL,
+ out,
+ Profiler.Format.BINARY_BAZEL_FORMAT,
+ "basic test",
+ false,
+ BlazeClock.instance(),
+ BlazeClock.instance().nanoTime());
+
+ // Write from multiple threads to generate multiple rows in the chart.
+ for (int i = 0; i < noOfRows; i++) {
+ Thread t = new Thread(runnable);
+ t.start();
+ t.join();
+ }
- profiler.stop();
+ profiler.stop();
+ }
try (InputStream in = cacheFile.getInputStream()) {
return ProfileInfo.loadProfile(in);
}