Skip to content

Commit

Permalink
RUMM-506 Add test for malformations in the middle of the file
Browse files Browse the repository at this point in the history
  • Loading branch information
ncreated committed Jun 12, 2020
1 parent ad030db commit 792c74c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class FileWriterTests: XCTestCase {
}

/// NOTE: Test added after incident-4797
func testGivenFileContainingData_whenNextDataFails_itDoesNotMalformTheFileContent() throws {
func testGivenFileContainingData_whenNextDataFails_itDoesNotMalformTheEndOfTheFile() throws {
let previousObjcExceptionHandler = objcExceptionHandler
defer { objcExceptionHandler = previousObjcExceptionHandler }

Expand Down Expand Up @@ -76,6 +76,38 @@ class FileWriterTests: XCTestCase {
)
}

/// NOTE: Test added after incident-4797
func testWhenIOExceptionsHappenRandomly_theFileIsNeverMalformed() throws {
let previousObjcExceptionHandler = objcExceptionHandler
defer { objcExceptionHandler = previousObjcExceptionHandler }

let expectation = self.expectation(description: "write completed")
let writer = FileWriter(
orchestrator: .mockWriteToSingleFile(in: temporaryDirectory),
queue: queue
)

objcExceptionHandler = ObjcExceptionHandlerNonDeterministicMock(
throwingError: ErrorMock("I/O exception"),
withProbability: 0.2
)

struct Foo: Codable {
let foo = "bar"
}

(0...500).forEach { _ in writer.write(value: Foo()) }

waitForWritesCompletion(on: queue, thenFulfill: expectation)
waitForExpectations(timeout: 5, handler: nil)
XCTAssertEqual(try temporaryDirectory.files().count, 1)

let fileData = try temporaryDirectory.files()[0].read()
let jsonDecoder = JSONDecoder()

XCTAssertNoThrow(try jsonDecoder.decode([Foo].self, from: "[".utf8Data + fileData + "]".utf8Data))
}

func testGivenErrorVerbosity_whenIndividualDataExceedsMaxWriteSize_itDropsDataAndPrintsError() throws {
let expectation1 = self.expectation(description: "write completed")
let expectation2 = self.expectation(description: "second write completed")
Expand Down
20 changes: 20 additions & 0 deletions Tests/DatadogTests/Datadog/Mocks/DatadogPrivateMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,23 @@ class ObjcExceptionHandlerDeferredMock: ObjcExceptionHandler {
currentCallsCount += 1
}
}

/// An `ObjcExceptionHandler` which throws given error with given probability.
class ObjcExceptionHandlerNonDeterministicMock: ObjcExceptionHandler {
private let probability: Int
let error: Error

/// Probability should be described as a number between `0` and `1`
init(throwingError: Error, withProbability probability: Double) {
self.error = throwingError
self.probability = Int(probability * 1_000)
}

override func rethrowToSwift(tryBlock: @escaping () -> Void) throws {
if Int.random(in: 0...1_000) < probability {
throw error
} else {
tryBlock()
}
}
}

0 comments on commit 792c74c

Please sign in to comment.