From 226428dd7e90d489671bdea0666760c3ec29873b Mon Sep 17 00:00:00 2001 From: Lorna <115649563+sfc-gh-ext-simba-lb@users.noreply.github.com> Date: Tue, 18 Apr 2023 15:46:17 -0700 Subject: [PATCH] Add tests for Event and EventHandler (#1321) * Added tests for event and eventhandler --------- Co-authored-by: sfc-gh-mknister --- .../net/snowflake/client/core/EventUtil.java | 11 ++- .../client/core/EventHandlerTest.java | 73 ++++++++++++++++ .../net/snowflake/client/core/EventTest.java | 83 +++++++++++++++++++ 3 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 src/test/java/net/snowflake/client/core/EventHandlerTest.java create mode 100644 src/test/java/net/snowflake/client/core/EventTest.java diff --git a/src/main/java/net/snowflake/client/core/EventUtil.java b/src/main/java/net/snowflake/client/core/EventUtil.java index cb5ddcaf5..470435c14 100644 --- a/src/main/java/net/snowflake/client/core/EventUtil.java +++ b/src/main/java/net/snowflake/client/core/EventUtil.java @@ -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 @@ -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 * diff --git a/src/test/java/net/snowflake/client/core/EventHandlerTest.java b/src/test/java/net/snowflake/client/core/EventHandlerTest.java new file mode 100644 index 000000000..48b381330 --- /dev/null +++ b/src/test/java/net/snowflake/client/core/EventHandlerTest.java @@ -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(); + } +} diff --git a/src/test/java/net/snowflake/client/core/EventTest.java b/src/test/java/net/snowflake/client/core/EventTest.java new file mode 100644 index 000000000..441eee25a --- /dev/null +++ b/src/test/java/net/snowflake/client/core/EventTest.java @@ -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()); + } + } +}