diff --git a/Tests/GRDBTests/ValueObservationRecorder.swift b/Tests/GRDBTests/ValueObservationRecorder.swift index 3b26dc39f..34dd9cafe 100644 --- a/Tests/GRDBTests/ValueObservationRecorder.swift +++ b/Tests/GRDBTests/ValueObservationRecorder.swift @@ -12,6 +12,7 @@ public class ValueObservationRecorder { private struct RecorderExpectation { var expectation: XCTestExpectation var remainingCount: Int? // nil for error expectation + var isIncluded: ((Value) -> Bool)? // nil for error expectation } /// The recorder state @@ -47,13 +48,21 @@ public class ValueObservationRecorder { state.values.append(value) - if let exp = state.recorderExpectation, let remainingCount = exp.remainingCount { + if let exp = state.recorderExpectation, + let remainingCount = exp.remainingCount, + let isIncluded = exp.isIncluded + { assert(remainingCount > 0) - exp.expectation.fulfill() - if remainingCount > 1 { - state.recorderExpectation = RecorderExpectation(expectation: exp.expectation, remainingCount: remainingCount - 1) - } else { - state.recorderExpectation = nil + if isIncluded(value) { + exp.expectation.fulfill() + if remainingCount > 1 { + state.recorderExpectation = RecorderExpectation( + expectation: exp.expectation, + remainingCount: remainingCount - 1, + isIncluded: isIncluded) + } else { + state.recorderExpectation = nil + } } } } @@ -77,7 +86,7 @@ public class ValueObservationRecorder { // MARK: ValueObservationExpectation API - func fulfillOnValue(_ expectation: XCTestExpectation, includingConsumed: Bool) { + func fulfillOnValue(_ expectation: XCTestExpectation, includingConsumed: Bool, isIncluded: @escaping (Value) -> Bool) { synchronized { preconditionCanFulfillExpectation() @@ -88,16 +97,20 @@ public class ValueObservationRecorder { return } - let values = state.values + let values = state.values.filter(isIncluded) + let consumedValues = state.values[0.. 0 { - state.recorderExpectation = RecorderExpectation(expectation: expectation, remainingCount: remainingCount) + state.recorderExpectation = RecorderExpectation( + expectation: expectation, + remainingCount: remainingCount, + isIncluded: isIncluded) } else { state.recorderExpectation = nil } @@ -113,7 +126,10 @@ public class ValueObservationRecorder { return } - state.recorderExpectation = RecorderExpectation(expectation: expectation, remainingCount: nil) + state.recorderExpectation = RecorderExpectation( + expectation: expectation, + remainingCount: nil, + isIncluded: nil) } } @@ -188,7 +204,17 @@ extension ValueObservationRecorder { } public func prefix(_ maxLength: Int) -> ValueObservationExpectations.Prefix { - ValueObservationExpectations.Prefix(recorder: self, maxLength: maxLength) + ValueObservationExpectations.Prefix( + recorder: self, + expectedFulfillmentCount: maxLength, + isIncluded: { _ in true }) + } + + public func prefix(until predicate: @escaping (Value) -> Bool) -> ValueObservationExpectations.Prefix { + ValueObservationExpectations.Prefix( + recorder: self, + expectedFulfillmentCount: 1, + isIncluded: predicate) } } @@ -309,7 +335,7 @@ extension XCTestCase { _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) - where + where R: BidirectionalCollection, E: BidirectionalCollection, R.Element == E.Element, @@ -475,15 +501,30 @@ extension GRDBTestCase { XCTAssertNotNil(value) } - let expectation = recorder - .prefix(expectedValues.count + 2 /* pool may perform double initial fetch */) - .inverted - let values = try wait(for: expectation, timeout: 0.3) + let recordedValues: [Reducer.Value] + let lastExpectedValue = expectedValues.last! + let waitForLast = expectedValues.firstIndex(of: lastExpectedValue) == expectedValues.count - 1 + if waitForLast { + // Optimization! + let expectation = recorder.prefix(until: { $0 == lastExpectedValue } ) + recordedValues = try wait(for: expectation, timeout: 1) + } else { + // Slow! + let expectation = recorder + .prefix(expectedValues.count + 2 /* pool may perform double initial fetch */) + .inverted + #if GRDBCIPHER_USE_ENCRYPTION + // SQLCipher can be *very* slow + recordedValues = try wait(for: expectation, timeout: 1) + #else + recordedValues = try wait(for: expectation, timeout: 0.3) + #endif + } - XCTAssertEqual(values.first, expectedValues.first) + XCTAssertEqual(recordedValues.first, expectedValues.first) assertValueObservationRecordingMatch( - recorded: values, + recorded: recordedValues, expected: expectedValues, "\(#function), \(writer), \(scheduler.impl)", file: file, line: line) } @@ -493,7 +534,7 @@ extension GRDBTestCase { try testRecordingEqualWhenWriteAfterStart(writer: makeDatabaseQueue()) try testRecordingEqualWhenWriteAfterFirstValue(writer: makeDatabaseQueue()) - + if observation.requiresWriteAccess { try testRecordingEqualWhenWriteAfterStart(writer: makeDatabasePool()) try testRecordingEqualWhenWriteAfterFirstValue(writer: makeDatabasePool()) @@ -556,7 +597,7 @@ extension GRDBTestCase { in: writer, scheduler: scheduler, onError: { _ in testErrorDispatching() }) - + let (_, error) = try wait(for: recorder.failure(), timeout: 0.3) if let error = error as? Failure { try testFailure(error, writer) @@ -630,7 +671,7 @@ extension ValueObservationExpectations { let recorder: ValueObservationRecorder public func _setup(_ expectation: XCTestExpectation) { - recorder.fulfillOnValue(expectation, includingConsumed: false) + recorder.fulfillOnValue(expectation, includingConsumed: false, isIncluded: { _ in true }) } public func get() throws -> Value { @@ -659,7 +700,7 @@ extension ValueObservationExpectations { public func _setup(_ expectation: XCTestExpectation) { expectation.isInverted = true - recorder.fulfillOnValue(expectation, includingConsumed: false) + recorder.fulfillOnValue(expectation, includingConsumed: false, isIncluded: { _ in true }) } public func get() throws { @@ -693,7 +734,7 @@ extension ValueObservationExpectations { expectation.fulfill() } else { expectation.expectedFulfillmentCount = count - recorder.fulfillOnValue(expectation, includingConsumed: false) + recorder.fulfillOnValue(expectation, includingConsumed: false, isIncluded: { _ in true }) } } @@ -716,31 +757,43 @@ extension ValueObservationExpectations { public struct Prefix: ValueObservationExpectation { let recorder: ValueObservationRecorder - let maxLength: Int + let expectedFulfillmentCount: Int + let isIncluded: (Value) -> Bool - init(recorder: ValueObservationRecorder, maxLength: Int) { - precondition(maxLength >= 0, "Invalid negative count") + init(recorder: ValueObservationRecorder, expectedFulfillmentCount: Int, isIncluded: @escaping (Value) -> Bool) { + precondition(expectedFulfillmentCount >= 0, "Invalid negative count") self.recorder = recorder - self.maxLength = maxLength + self.expectedFulfillmentCount = expectedFulfillmentCount + self.isIncluded = isIncluded } public func _setup(_ expectation: XCTestExpectation) { - if maxLength == 0 { + if expectedFulfillmentCount == 0 { // Such an expectation is immediately fulfilled, by essence. expectation.expectedFulfillmentCount = 1 expectation.fulfill() } else { - expectation.expectedFulfillmentCount = maxLength - recorder.fulfillOnValue(expectation, includingConsumed: true) + expectation.expectedFulfillmentCount = expectedFulfillmentCount + recorder.fulfillOnValue(expectation, includingConsumed: true, isIncluded: isIncluded) } } public func get() throws -> [Value] { - try recorder.value { (values, error, remainingValues, consume) in - if values.count >= maxLength { - let extraCount = max(maxLength + remainingValues.count - values.count, 0) + if expectedFulfillmentCount == 0 { + return [] + } + + return try recorder.value { (values, error, remainingValues, consume) in + let includedValues = values.filter(isIncluded) + if includedValues.count >= expectedFulfillmentCount { + let matchedCount = values + .indices + .filter { isIncluded(values[$0]) } + .prefix(expectedFulfillmentCount) + .last! + 1 + let extraCount = max(matchedCount + remainingValues.count - values.count, 0) consume(extraCount) - return Array(values.prefix(maxLength)) + return Array(values.prefix(matchedCount)) } if let error = error { throw error diff --git a/Tests/GRDBTests/ValueObservationRecorderTests.swift b/Tests/GRDBTests/ValueObservationRecorderTests.swift index 4f3bc843b..51e87afba 100644 --- a/Tests/GRDBTests/ValueObservationRecorderTests.swift +++ b/Tests/GRDBTests/ValueObservationRecorderTests.swift @@ -334,9 +334,9 @@ class ValueObservationRecorderTests: FailureTestCase { } catch ValueRecordingError.notEnoughValues { } } - // MARK: - Prefix + // MARK: - Prefix(maxLength) - func testPrefixSuccess() throws { + func testPrefixMaxLengthSuccess() throws { struct CustomError: Error { } do { let recorder = ValueObservationRecorder() @@ -374,19 +374,19 @@ class ValueObservationRecorderTests: FailureTestCase { } } - func testPrefixError() throws { + func testPrefixMaxLengthError() throws { struct CustomError: Error { } do { let recorder = ValueObservationRecorder() recorder.onError(CustomError()) - _ = try recorder.next(2).get() + _ = try recorder.prefix(2).get() XCTFail("Expected error") } catch is CustomError { } do { let recorder = ValueObservationRecorder() recorder.onChange("foo") recorder.onError(CustomError()) - _ = try recorder.next(2).get() + _ = try recorder.prefix(2).get() XCTFail("Expected error") } catch is CustomError { } do { @@ -394,7 +394,7 @@ class ValueObservationRecorderTests: FailureTestCase { DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(10)) { recorder.onError(CustomError()) } - _ = try wait(for: recorder.next(2), timeout: 0.5) + _ = try wait(for: recorder.prefix(2), timeout: 0.5) XCTFail("Expected error") } catch is CustomError { } do { @@ -405,42 +405,36 @@ class ValueObservationRecorderTests: FailureTestCase { recorder.onError(CustomError()) } } - _ = try wait(for: recorder.next(2), timeout: 0.5) + _ = try wait(for: recorder.prefix(2), timeout: 0.5) XCTFail("Expected error") } catch is CustomError { } } - func testPrefixTimeout() throws { + func testPrefixMaxLengthTimeout() throws { try assertFailure("Asynchronous wait failed") { - do { - let recorder = ValueObservationRecorder() - _ = try wait(for: recorder.next(2), timeout: 0.1) - XCTFail("Expected error") - } catch ValueRecordingError.notEnoughValues { } + let recorder = ValueObservationRecorder() + let values = try wait(for: recorder.prefix(2), timeout: 0.1) + XCTAssertEqual(values, []) } try assertFailure("Asynchronous wait failed") { - do { - let recorder = ValueObservationRecorder() + let recorder = ValueObservationRecorder() + recorder.onChange("foo") + let values = try wait(for: recorder.prefix(2), timeout: 0.1) + XCTAssertEqual(values, ["foo"]) + } + try assertFailure("Asynchronous wait failed") { + let recorder = ValueObservationRecorder() + DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(10)) { recorder.onChange("foo") - _ = try wait(for: recorder.next(2), timeout: 0.1) - XCTFail("Expected error") - } catch ValueRecordingError.notEnoughValues { } - } - try assertFailure("Asynchronous wait failed") { - do { - let recorder = ValueObservationRecorder() - DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(10)) { - recorder.onChange("foo") - } - _ = try wait(for: recorder.next(2), timeout: 0.5) - XCTFail("Expected error") - } catch ValueRecordingError.notEnoughValues { } + } + let values = try wait(for: recorder.prefix(2), timeout: 0.5) + XCTAssertEqual(values, ["foo"]) } } - // MARK: - Prefix Inverted + // MARK: - Prefix(maxLength) Inverted - func testPrefixInvertedSuccess() throws { + func testPrefixMaxLengthInvertedSuccess() throws { do { let recorder = ValueObservationRecorder() try XCTAssertEqual(recorder.prefix(1).inverted.get(), []) @@ -459,7 +453,7 @@ class ValueObservationRecorderTests: FailureTestCase { } } - func testPrefixInvertedError() throws { + func testPrefixMaxLengthInvertedError() throws { struct CustomError: Error { } do { let recorder = ValueObservationRecorder() @@ -476,7 +470,7 @@ class ValueObservationRecorderTests: FailureTestCase { } catch is CustomError { } } - func testPrefixInvertedTimeout() throws { + func testPrefixMaxLengthInvertedTimeout() throws { struct CustomError: Error { } do { let recorder = ValueObservationRecorder() @@ -528,6 +522,96 @@ class ValueObservationRecorderTests: FailureTestCase { } } + // MARK: - Prefix(maxLength) + + func testPrefixUntilSuccess() throws { + struct CustomError: Error { } + do { + let recorder = ValueObservationRecorder() + recorder.onChange("foo") + try XCTAssertEqual(recorder.prefix(until: { $0 == "foo" }).get(), ["foo"]) + recorder.onChange("bar") + recorder.onChange("baz") + try XCTAssertEqual(recorder.prefix(until: { $0 == "foo" }).get(), ["foo"]) + try XCTAssertEqual(recorder.prefix(until: { $0 == "bar" }).get(), ["foo", "bar"]) + try XCTAssertEqual(recorder.prefix(until: { $0 == "baz" }).get(), ["foo", "bar", "baz"]) + } + do { + let recorder = ValueObservationRecorder() + recorder.onChange("foo") + recorder.onError(CustomError()) + try XCTAssertEqual(recorder.prefix(until: { $0 == "foo" }).get(), ["foo"]) + } + do { + let recorder = ValueObservationRecorder() + DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(10)) { + recorder.onChange("foo") + DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(10)) { + recorder.onChange("bar") + } + } + try XCTAssertEqual(wait(for: recorder.prefix(until: { $0 == "bar" }), timeout: 0.5), ["foo", "bar"]) + } + } + + func testPrefixUntilError() throws { + struct CustomError: Error { } + do { + let recorder = ValueObservationRecorder() + recorder.onError(CustomError()) + _ = try recorder.prefix(until: { $0 == "bar" }).get() + XCTFail("Expected error") + } catch is CustomError { } + do { + let recorder = ValueObservationRecorder() + recorder.onChange("foo") + recorder.onError(CustomError()) + _ = try recorder.prefix(until: { $0 == "bar" }).get() + XCTFail("Expected error") + } catch is CustomError { } + do { + let recorder = ValueObservationRecorder() + DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(10)) { + recorder.onError(CustomError()) + } + _ = try wait(for: recorder.prefix(until: { $0 == "bar" }), timeout: 0.5) + XCTFail("Expected error") + } catch is CustomError { } + do { + let recorder = ValueObservationRecorder() + DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(10)) { + recorder.onChange("foo") + DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(10)) { + recorder.onError(CustomError()) + } + } + _ = try wait(for: recorder.prefix(until: { $0 == "bar" }), timeout: 0.5) + XCTFail("Expected error") + } catch is CustomError { } + } + + func testPrefixUntilTimeout() throws { + try assertFailure("Asynchronous wait failed") { + let recorder = ValueObservationRecorder() + let values = try wait(for: recorder.prefix(until: { $0 == "bar" }), timeout: 0.1) + XCTAssertEqual(values, []) + } + try assertFailure("Asynchronous wait failed") { + let recorder = ValueObservationRecorder() + recorder.onChange("foo") + let values = try wait(for: recorder.prefix(until: { $0 == "bar" }), timeout: 0.1) + XCTAssertEqual(values, ["foo"]) + } + try assertFailure("Asynchronous wait failed") { + let recorder = ValueObservationRecorder() + DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(10)) { + recorder.onChange("foo") + } + let values = try wait(for: recorder.prefix(until: { $0 == "bar" }), timeout: 0.5) + XCTAssertEqual(values, ["foo"]) + } + } + // MARK: - Failure func testFailureSuccess() throws {