Skip to content

Commit

Permalink
SNOW-748294 Fix IncidentUtilLatestIT tests (#1319)
Browse files Browse the repository at this point in the history
* SNOW-748294 Fix IncidentUtilLatestIT tests
  • Loading branch information
sfc-gh-mknister committed Apr 17, 2023
1 parent dd53905 commit a570c99
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
14 changes: 9 additions & 5 deletions src/main/java/net/snowflake/client/core/IncidentUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
import com.yammer.metrics.core.Clock;
import com.yammer.metrics.core.VirtualMachineMetrics;
import com.yammer.metrics.reporting.MetricsServlet;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.*;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPOutputStream;
Expand Down Expand Up @@ -71,6 +68,8 @@ public static String oneLiner(String prefix, Throwable thrown) {
public static void dumpVmMetrics(String incidentId) {
PrintWriter writer = null;
try {
File dmpFileDirectory = new File(EventUtil.getDumpPathPrefix());
dmpFileDirectory.mkdirs();
String dumpFile =
EventUtil.getDumpPathPrefix() + "/" + INC_DUMP_FILE_NAME + incidentId + INC_DUMP_FILE_EXT;

Expand Down Expand Up @@ -197,7 +196,12 @@ private static void writeVmMetrics(JsonGenerator json, VirtualMachineMetrics vm)
json.writeNumberField("thread_count", vm.threadCount());
json.writeNumberField("current_time", Clock.defaultClock().time());
json.writeNumberField("uptime", vm.uptime());
json.writeNumberField("fd_usage", vm.fileDescriptorUsage());
try {
// This function can throw an error with java 11.
json.writeNumberField("fd_usage", vm.fileDescriptorUsage());
} catch (Exception e) {
logger.info("Error writing fd_usage", e);
}

json.writeFieldName("thread-states");
json.writeStartObject();
Expand Down
17 changes: 11 additions & 6 deletions src/test/java/net/snowflake/client/core/IncidentUtilLatestIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

package net.snowflake.client.core;

import static net.snowflake.client.core.IncidentUtil.INC_DUMP_FILE_EXT;
import static net.snowflake.client.core.IncidentUtil.INC_DUMP_FILE_NAME;
import static org.junit.Assert.assertEquals;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringWriter;
Expand All @@ -33,16 +34,20 @@ public void testOneLinerDescription() {
/** Tests dumping JVM metrics for the current process */
@Test
public void testDumpVmMetrics() throws IOException {
File targetVMFile = new File(getFullPathFileInResource("snowflake_dumps/" + FILE_NAME));
int index = targetVMFile.getPath().indexOf("snowflake_dumps");
String dumpPath = targetVMFile.getPath().substring(0, index - 1);
String dumpPath = tmpFolder.newFolder().getCanonicalPath();
System.setProperty("snowflake.dump_path", dumpPath);

String incidentId = "123456";

// write the VM metrics to the dump file
IncidentUtil.dumpVmMetrics("123456");
IncidentUtil.dumpVmMetrics(incidentId);

// Get location of where file will be written
String targetVMFileLocation =
EventUtil.getDumpPathPrefix() + "/" + INC_DUMP_FILE_NAME + incidentId + INC_DUMP_FILE_EXT;

// Read back the file contents
GZIPInputStream gzip = new GZIPInputStream(new FileInputStream(targetVMFile));
GZIPInputStream gzip = new GZIPInputStream(new FileInputStream(targetVMFileLocation));
StringWriter sWriter = new StringWriter();
IOUtils.copy(gzip, sWriter, "UTF-8");
String output = sWriter.toString();
Expand Down
Binary file not shown.

0 comments on commit a570c99

Please sign in to comment.