From 425e5068623477eb3da5340efc492d1418fffaeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 2 May 2020 12:19:23 +0200 Subject: [PATCH] Make all ValueObservation tests deterministic (avoid inverted expectations) Inverted expectations were just too flaky on Travis --- .../ValueObservationCountTests.swift | 8 ++--- .../ValueObservationRecordTests.swift | 8 +++-- .../GRDBTests/ValueObservationRecorder.swift | 29 +++++++++++++++---- .../GRDBTests/ValueObservationRowTests.swift | 8 +++-- Tests/GRDBTests/ValueObservationTests.swift | 6 ++-- 5 files changed, 42 insertions(+), 17 deletions(-) diff --git a/Tests/GRDBTests/ValueObservationCountTests.swift b/Tests/GRDBTests/ValueObservationCountTests.swift index bf8c9d144..8124b94fb 100644 --- a/Tests/GRDBTests/ValueObservationCountTests.swift +++ b/Tests/GRDBTests/ValueObservationCountTests.swift @@ -7,7 +7,7 @@ class ValueObservationCountTests: GRDBTestCase { try assertValueObservation( ValueObservation.tracking(T.fetchCount), - records: [0, 1, 1, 2, 3, 2], + records: [0, 1, 1, 2, 3, 4], setup: { db in try db.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") }, @@ -21,7 +21,7 @@ class ValueObservationCountTests: GRDBTestCase { try db.execute(sql: "DELETE FROM t WHERE id = 1") return .commit } - try db.execute(sql: "DELETE FROM t WHERE id = 2") + try db.execute(sql: "INSERT INTO t DEFAULT VALUES") }) } @@ -30,7 +30,7 @@ class ValueObservationCountTests: GRDBTestCase { try assertValueObservation( ValueObservation.tracking(T.fetchCount).removeDuplicates(), - records: [0, 1, 2, 3, 2], + records: [0, 1, 2, 3, 4], setup: { db in try db.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT)") }, @@ -44,7 +44,7 @@ class ValueObservationCountTests: GRDBTestCase { try db.execute(sql: "DELETE FROM t WHERE id = 1") return .commit } - try db.execute(sql: "DELETE FROM t WHERE id = 2") + try db.execute(sql: "INSERT INTO t DEFAULT VALUES") }) } } diff --git a/Tests/GRDBTests/ValueObservationRecordTests.swift b/Tests/GRDBTests/ValueObservationRecordTests.swift index 7dfa131f2..0568df3aa 100644 --- a/Tests/GRDBTests/ValueObservationRecordTests.swift +++ b/Tests/GRDBTests/ValueObservationRecordTests.swift @@ -77,7 +77,8 @@ class ValueObservationRecordTests: GRDBTestCase { Player(id: 1, name: "foo"), Player(id: 1, name: "foo"), Player(id: 2, name: "bar"), - nil], + nil, + Player(id: 3, name: "toto")], setup: { db in try db.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)") }, @@ -91,6 +92,7 @@ class ValueObservationRecordTests: GRDBTestCase { return .commit } try db.execute(sql: "DELETE FROM t") + try db.execute(sql: "INSERT INTO t (id, name) VALUES (3, 'toto')") }) // The fundamental technique for removing duplicates of non-Equatable types @@ -103,7 +105,8 @@ class ValueObservationRecordTests: GRDBTestCase { nil, Player(id: 1, name: "foo"), Player(id: 2, name: "bar"), - nil], + nil, + Player(id: 3, name: "toto")], setup: { db in try db.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)") }, @@ -117,6 +120,7 @@ class ValueObservationRecordTests: GRDBTestCase { return .commit } try db.execute(sql: "DELETE FROM t") + try db.execute(sql: "INSERT INTO t (id, name) VALUES (3, 'toto')") }) } } diff --git a/Tests/GRDBTests/ValueObservationRecorder.swift b/Tests/GRDBTests/ValueObservationRecorder.swift index 73c2e328b..2b308911e 100644 --- a/Tests/GRDBTests/ValueObservationRecorder.swift +++ b/Tests/GRDBTests/ValueObservationRecorder.swift @@ -459,17 +459,33 @@ extension GRDBTestCase { try writer.writeWithoutTransaction(recordedUpdates) - 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! + assertionFailure("Please rewrite your test, because it is too slow: make sure the last expected value is unique.") + let expectation = recorder + .prefix(expectedValues.count + 2 /* pool may perform double initial fetch */) + .inverted + #if SQLITE_HAS_CODEC || GRDBCUSTOMSQLITE + // debug SQLite builds can be *very* slow + recordedValues = try wait(for: expectation, timeout: 1) + #else + recordedValues = try wait(for: expectation, timeout: 0.3) + #endif + } if scheduler.immediateInitialValue() { - XCTAssertEqual(values.first, expectedValues.first) + XCTAssertEqual(recordedValues.first, expectedValues.first) } assertValueObservationRecordingMatch( - recorded: values, + recorded: recordedValues, expected: expectedValues, "\(#function), \(writer), \(scheduler)", file: file, line: line) } @@ -506,6 +522,7 @@ extension GRDBTestCase { recordedValues = try wait(for: expectation, timeout: 1) } else { // Slow! + assertionFailure("Please rewrite your test, because it is too slow: make sure the last expected value is unique.") let expectation = recorder .prefix(expectedValues.count + 2 /* pool may perform double initial fetch */) .inverted diff --git a/Tests/GRDBTests/ValueObservationRowTests.swift b/Tests/GRDBTests/ValueObservationRowTests.swift index 32c86c139..8b377eb95 100644 --- a/Tests/GRDBTests/ValueObservationRowTests.swift +++ b/Tests/GRDBTests/ValueObservationRowTests.swift @@ -61,7 +61,8 @@ class ValueObservationRowTests: GRDBTestCase { ["id":1, "name":"foo"], ["id":1, "name":"foo"], ["id":2, "name":"bar"], - nil], + nil, + ["id":3, "name":"toto"]], setup: { db in try db.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)") }, @@ -75,6 +76,7 @@ class ValueObservationRowTests: GRDBTestCase { return .commit } try db.execute(sql: "DELETE FROM t") + try db.execute(sql: "INSERT INTO t (id, name) VALUES (3, 'toto')") }) try assertValueObservation( @@ -83,7 +85,8 @@ class ValueObservationRowTests: GRDBTestCase { nil, ["id":1, "name":"foo"], ["id":2, "name":"bar"], - nil], + nil, + ["id":3, "name":"toto"]], setup: { db in try db.execute(sql: "CREATE TABLE t(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)") }, @@ -97,6 +100,7 @@ class ValueObservationRowTests: GRDBTestCase { return .commit } try db.execute(sql: "DELETE FROM t") + try db.execute(sql: "INSERT INTO t (id, name) VALUES (3, 'toto')") }) } diff --git a/Tests/GRDBTests/ValueObservationTests.swift b/Tests/GRDBTests/ValueObservationTests.swift index 199a9bb12..83fc8115d 100644 --- a/Tests/GRDBTests/ValueObservationTests.swift +++ b/Tests/GRDBTests/ValueObservationTests.swift @@ -62,7 +62,7 @@ class ValueObservationTests: GRDBTestCase { }) withExtendedLifetime(cancellable) { - waitForExpectations(timeout: 0.3, handler: nil) + waitForExpectations(timeout: 1, handler: nil) XCTAssertTrue(errorCaught) } } @@ -227,7 +227,7 @@ class ValueObservationTests: GRDBTestCase { try dbWriter.write { db in try db.execute(sql: "INSERT INTO t DEFAULT VALUES") } - waitForExpectations(timeout: 0.2, handler: nil) + waitForExpectations(timeout: 1, handler: nil) } try test(makeDatabaseQueue()) @@ -270,7 +270,7 @@ class ValueObservationTests: GRDBTestCase { try dbWriter.write { db in try db.execute(sql: "INSERT INTO t DEFAULT VALUES") } - waitForExpectations(timeout: 0.2, handler: nil) + waitForExpectations(timeout: 1, handler: nil) } try test(makeDatabaseQueue())