-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for Event and EventHandler (#1321)
* Added tests for event and eventhandler --------- Co-authored-by: sfc-gh-mknister <meg.knister@snowflake.com>
- Loading branch information
1 parent
450a948
commit 226428d
Showing
3 changed files
with
166 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/test/java/net/snowflake/client/core/EventHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |