Skip to content

Commit

Permalink
AMP-94015 try support line breaks in file
Browse files Browse the repository at this point in the history
  • Loading branch information
qingzhuozhen committed Feb 29, 2024
1 parent cad2987 commit a4963a1
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,26 @@ class AndroidStorageTest {
assertEquals(13, eventsCount)
}

@Test
fun `verify could handle line break in event name`() {
val logger = ConsoleLogger()
val storageKey = "storageKey"
val prefix = "test"
val storage = AndroidStorage(context, storageKey, logger, prefix, testDiagnostics)

runBlocking {
storage.writeEvent(createEvent("test1\n"))
storage.rollover()
val eventsData = storage.readEventsContent()
eventsData.withIndex().forEach { (_, filePath) ->
val eventsString = storage.getEventsString(filePath)
val events = JSONArray(eventsString)
assertEquals(1, events.length())
assertEquals("test1\n", events.getJSONObject(0).getString("event_type"))
}
}
}

@Test
fun `verify malformed event show in diagonstics`() {
val logger = ConsoleLogger()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
import java.io.UnsupportedEncodingException
import java.net.URLDecoder
import java.net.URLEncoder
import java.util.Collections
import java.util.Random
import java.util.concurrent.ConcurrentHashMap
Expand Down Expand Up @@ -84,9 +87,16 @@ class EventsFileManager(
}
}
}

val contents = "${event}\n"
writeToFile(contents.toByteArray(), file)
try {
val contents = "${URLEncoder.encode(event, "UTF-8")}\n"
writeToFile(contents.toByteArray(), file, true)
} catch (e: UnsupportedEncodingException) {
diagnostics.addErrorLog("Failed to encode event: ${e.message}")
logger.error("Failed to encode event: ${e.message}")
} catch (e: Exception) {
diagnostics.addErrorLog("Failed to store event: ${e.message}")
logger.error("Failed to store event: ${e.message}")
}
}

private fun incrementFileIndex(): Boolean {
Expand Down Expand Up @@ -167,11 +177,12 @@ class EventsFileManager(
val events = JSONArray()
content.split("\n").forEach {
if (it.isNotEmpty()) {
val currentContent = URLDecoder.decode(it, "UTF-8")
try {
events.put(JSONObject(it))
events.put(JSONObject(currentContent))
} catch (e: JSONException) {
diagnostics.addMalformedEvent(it)
logger.error("Failed to parse event: $it")
diagnostics.addMalformedEvent(currentContent)
logger.error("Failed to parse event: $currentContent")
}
}
}
Expand Down Expand Up @@ -281,14 +292,20 @@ class EventsFileManager(
file: File,
append: Boolean = true,
) {
val contents = events.joinToString(separator = "\n", postfix = "\n") { it.toString() }
try {
val contents = events.joinToString(separator = "\n", postfix = "\n") { URLEncoder.encode(it.toString(), "UTF-8") }
file.createNewFile()
writeToFile(contents.toByteArray(), file, append)
rename(file)
} catch (e: IOException) {
diagnostics.addErrorLog("Failed to create or write to split file: ${e.message}")
logger.error("Failed to create or write to split file: ${file.path}")
} catch (e: UnsupportedEncodingException) {
diagnostics.addErrorLog("Failed to encode event: ${e.message}")
logger.error("Failed to encode event: ${e.message}")
} catch (e: Exception) {
diagnostics.addErrorLog("Failed to write to split file: ${e.message}")
logger.error("Failed to write to split file: ${file.path} for error: ${e.message}")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,27 @@ class EventsFileManagerTest {
}
}

@Test
fun `verify line breaks in event names`() {
val logger = ConsoleLogger()
val storageKey = "storageKey"
val propertiesFile = PropertiesFile(tempDir, storageKey, "test-prefix", logger)
val eventsFileManager =
EventsFileManager(tempDir, storageKey, propertiesFile, logger, testDiagnostics)
runBlocking {
eventsFileManager.storeEvent(createEvent("test1"))
eventsFileManager.storeEvent(createEvent("test2\\n"))
eventsFileManager.rollover()
val filePaths = eventsFileManager.read()
assertEquals(1, filePaths.size)
val eventsString = eventsFileManager.getEventString(filePaths[0])
val events = JSONArray(eventsString)
assertEquals(2, events.length())
assertEquals("test1", events.getJSONObject(0).getString("eventType"))
assertEquals("test2\n", events.getJSONObject(1).getString("eventType"))
}
}

@Test
fun `could handle earlier version of events file`() {
createEarlierVersionEventFiles()
Expand Down Expand Up @@ -276,6 +297,26 @@ class EventsFileManagerTest {
assertEquals(17, eventsCount)
}

@Test
fun `could handle earlier versions with line break in event name`() {
val file = File(tempDir, "storageKey-6")
file.writeText("{\"eventType\":\"test15\"},{\"eventType\":\"test16\\nsuffix\"}]")
val logger = ConsoleLogger()
val storageKey = "storageKey"
val propertiesFile = PropertiesFile(tempDir, storageKey, "test-prefix", logger)
val eventsFileManager =
EventsFileManager(tempDir, storageKey, propertiesFile, logger, testDiagnostics)
runBlocking {
val filePaths = eventsFileManager.read()
assertEquals(1, filePaths.size)
val eventsString = eventsFileManager.getEventString(filePaths[0])
val events = JSONArray(eventsString)
assertEquals(2, events.length())
assertEquals("test15", events.getJSONObject(0).getString("eventType"))
assertEquals("test16\nsuffix", events.getJSONObject(1).getString("eventType"))
}
}

@Test
fun `concurrent writes to the same event file manager instance`() {
val logger = ConsoleLogger()
Expand Down

0 comments on commit a4963a1

Please sign in to comment.