Skip to content

Commit

Permalink
Add tests for Event and EventHandler (#1321)
Browse files Browse the repository at this point in the history
* Added tests for event and eventhandler

---------

Co-authored-by: sfc-gh-mknister <meg.knister@snowflake.com>
  • Loading branch information
sfc-gh-ext-simba-lb and sfc-gh-mknister authored Apr 18, 2023
1 parent 450a948 commit 226428d
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/main/java/net/snowflake/client/core/EventUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class EventUtil {
public static final String DUMP_SUBDIR = "snowflake_dumps";

private static final String DUMP_FILE_ID = UUID.randomUUID().toString();
private static final String DUMP_PATH_PREFIX =
private static String DUMP_PATH_PREFIX =
systemGetProperty(DUMP_PATH_PROP) == null ? "/tmp" : systemGetProperty(DUMP_PATH_PROP);
private static final long MAX_DUMP_FILE_SIZE_BYTES =
systemGetProperty(DUMP_SIZE_PROP) == null
Expand All @@ -33,6 +33,15 @@ public class EventUtil {

private static int FLUSH_PERIOD_MS = 10000;

/**
* Junit is not recognizing the system properties for EventTest, so overriding the value here
*
* @param value
*/
public static void setDumpPathPrefixForTesting(String value) {
DUMP_PATH_PREFIX = value;
}

/**
* Initializes the common eventHandler instance for all sessions/threads
*
Expand Down
73 changes: 73 additions & 0 deletions src/test/java/net/snowflake/client/core/EventHandlerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved.
*/
package net.snowflake.client.core;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.file.Files;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.zip.GZIPInputStream;
import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class EventHandlerTest {
@Rule public TemporaryFolder tmpFolder = new TemporaryFolder();

@Before
public void setUp() throws IOException {
tmpFolder.newFolder("snowflake_dumps");
System.setProperty("snowflake.dump_path", tmpFolder.getRoot().getCanonicalPath());
}

@Test
public void testPublishRecord() {
LogRecord record = new LogRecord(Level.INFO, "test message");
EventHandler handler = new EventHandler(10, 5000);
assertEquals(0, handler.getLogBufferSize());
handler.publish(record);
assertEquals(1, handler.getLogBufferSize());
}

@Test
public void testDumpLogBuffer() throws IOException {
System.setProperty("snowflake.max_dumpfiles", "1");
System.setProperty("snowflake.max_dumpdir_size_mb", "100");

LogRecord record = new LogRecord(Level.INFO, "test message");
EventHandler handler = new EventHandler(10, 5000);
handler.publish(record);
handler.flush();

File logDumpFile = new File(EventUtil.getDumpPathPrefix() + "/sf_log_.dmp.gz");
GZIPInputStream gzip = new GZIPInputStream(Files.newInputStream(logDumpFile.toPath()));
StringWriter sWriter = new StringWriter();
IOUtils.copy(gzip, sWriter, "UTF-8");

assertTrue(sWriter.toString().contains("test message"));

gzip.close();
sWriter.close();
logDumpFile.delete();
}

@Test
public void testEventFlusher() {
EventHandler handler = new EventHandler(2, 1000);
handler.startFlusher();
handler.triggerBasicEvent(Event.EventType.STATE_TRANSITION, "test event");
assertEquals(handler.getBufferSize(), 1);
handler.triggerBasicEvent(Event.EventType.STATE_TRANSITION, "test event 2");
// buffer should flush when max entries is reached
assertEquals(handler.getBufferSize(), 0);
handler.stopFlusher();
}
}
83 changes: 83 additions & 0 deletions src/test/java/net/snowflake/client/core/EventTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved.
*/

package net.snowflake.client.core;

import static net.snowflake.client.core.EventUtil.DUMP_PATH_PROP;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.file.Files;
import java.util.zip.GZIPInputStream;
import org.apache.commons.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class EventTest {
@Rule public TemporaryFolder tmpFolder = new TemporaryFolder();
private File homeDirectory;
private File dmpDirectory;

@Before
public void setUp() throws IOException {
homeDirectory = tmpFolder.newFolder("homedir");
dmpDirectory = tmpFolder.newFolder("homedir", "snowflake_dumps");
}

@After
public void tearDown() {
dmpDirectory.delete();
}

@Test
public void testEvent() {
Event event = new BasicEvent(Event.EventType.NONE, "basic event");
event.setType(Event.EventType.NETWORK_ERROR);
event.setMessage("network timeout");
assertEquals(1, event.getType().getId());
assertEquals("network timeout", event.getMessage());
}

@Test
public void testWriteEventDumpLine() throws IOException {
try {
// Set dmp file path
String dumpPath = homeDirectory.getCanonicalPath();
System.setProperty(DUMP_PATH_PROP, dumpPath);
EventUtil.setDumpPathPrefixForTesting(dumpPath);
Event event = new BasicEvent(Event.EventType.NETWORK_ERROR, "network timeout");
event.writeEventDumpLine("network timeout after 60 seconds");
// Assert the dump path prefix function correctly leads to the temporary dump directory
// created
String dmpPath1 = EventUtil.getDumpPathPrefix();
String dmpPath2 = dmpDirectory.getCanonicalPath();
assertEquals("dump path is: " + EventUtil.getDumpPathPrefix(), dmpPath2, dmpPath1);
File dumpFile =
new File(
EventUtil.getDumpPathPrefix()
+ "/"
+ "sf_event_"
+ EventUtil.getDumpFileId()
+ ".dmp.gz");
GZIPInputStream gzip = new GZIPInputStream(Files.newInputStream(dumpFile.toPath()));
StringWriter sWriter = new StringWriter();
IOUtils.copy(gzip, sWriter, "UTF-8");

assertTrue(sWriter.toString().contains("network timeout after 60 seconds"));

gzip.close();
sWriter.close();
dumpFile.delete();
} finally {
System.clearProperty("snowflake.dump_path");
EventUtil.setDumpPathPrefixForTesting(EventUtil.getDumpPathPrefix());
}
}
}

0 comments on commit 226428d

Please sign in to comment.