Skip to content

Commit

Permalink
test: Test asyncReduce with a class
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-coye committed Jun 17, 2024
1 parent 90d2858 commit a2a4ea5
Showing 1 changed file with 157 additions and 5 deletions.
162 changes: 157 additions & 5 deletions Tests/InfomaniakConcurrencyTests/UTCollection+async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ enum SomeError: Error {
case some
}

final class SomeClass: Sendable {
init(value: Int) {
self.value = value
}

let value: Int

var valueAsync: Int {
get async throws {
let nanoseconds = UInt64.random(in: 100_000 ..< 100_000_000)
try? await Task.sleep(nanoseconds: nanoseconds)
return value
}
}
}

// MARK: - UTCollection async -

/// Minimal testing of the `async` functions, that are shorthands for the `concurrent` functions that are well tested
Expand Down Expand Up @@ -97,7 +113,9 @@ final class UTCollection_async: XCTestCase {

// MARK: - asyncReduce

func testAsyncReduce() {
// asyncReduce to int

func testAsyncReduceInt() {
asyncTestWrapper {
// GIVEN
let collectionToProcess = Array(0 ... 50)
Expand All @@ -118,7 +136,7 @@ final class UTCollection_async: XCTestCase {
}
}

func testAsyncReduceThrow() {
func testAsyncReduceIntThrow() {
asyncTestWrapper {
// GIVEN
let collectionToProcess = Array(0 ... 50)
Expand Down Expand Up @@ -149,7 +167,7 @@ final class UTCollection_async: XCTestCase {
}
}

func testAsyncReduceInto() {
func testAsyncReduceIntInto() {
asyncTestWrapper {
// GIVEN
let collectionToProcess = Array(0 ... 50)
Expand All @@ -171,7 +189,7 @@ final class UTCollection_async: XCTestCase {
}
}

func testAsyncReduceIntoThrow() {
func testAsyncReduceIntoIntThrow() {
asyncTestWrapper {
// GIVEN
let collectionToProcess = Array(0 ... 50)
Expand All @@ -180,7 +198,7 @@ final class UTCollection_async: XCTestCase {
do {
let seed = 0
let asyncSum = try await collectionToProcess.asyncReduce(into: seed) { partialResult, item in
// We arbitrarily remove elements
// Throw exception during the reduce process
if item % 10 == 0 {
throw SomeError.some
}
Expand All @@ -202,4 +220,138 @@ final class UTCollection_async: XCTestCase {
}
}
}

// asyncReduce to Class

func testAsyncReduceClass() {
asyncTestWrapper {
// GIVEN
var collectionToProcess = [SomeClass]()
for index in 0 ... 50 {
collectionToProcess.append(SomeClass(value: index))
}

// WHEN
let asyncSum = try await collectionToProcess.asyncReduce(0) { partialResult, item in
let value = try await item.valueAsync
return partialResult + value
}

// THEN
let sum = collectionToProcess.reduce(0) { $0 + $1.value }
XCTAssertEqual(sum, asyncSum, "Expecting a naive sum to equal the async one")
}
}

func testAsyncReduceClassThrow() {
asyncTestWrapper {
// GIVEN
var collectionToProcess = [SomeClass]()
for index in 0 ... 50 {
collectionToProcess.append(SomeClass(value: index))
}

// WHEN
do {
let asyncSum = try await collectionToProcess.asyncReduce(0) { partialResult, item in
let value = try await item.valueAsync

// Throw exception during the reduce process
if value % 10 == 0 {
throw SomeError.some
}

return partialResult + value
}

XCTFail("expected to throw, got \(asyncSum) instead")
}

// THEN
catch {
guard let someError = error as? SomeError,
someError == .some else {
XCTFail("unexpected error: \(error)")
return
}
// all good
}
}
}

func testAsyncReduceClassInto() {
asyncTestWrapper {
// GIVEN
var collectionToProcess = [SomeClass]()
for index in 0 ... 50 {
collectionToProcess.append(SomeClass(value: index))
}

// WHEN
let seed = 0
let asyncSum = try await collectionToProcess.asyncReduce(into: seed) { partialResult, item in
let value = try await item.valueAsync
partialResult = partialResult + value
}

// THEN
let sum = collectionToProcess.reduce(0) { $0 + $1.value }
XCTAssertEqual(sum, asyncSum, "Expecting a naive sum to equal the async one")
}
}

func testAsyncReduceIntoClassThrow() {
asyncTestWrapper {
// GIVEN
var collectionToProcess = [SomeClass]()
for index in 0 ... 50 {
collectionToProcess.append(SomeClass(value: index))
}

// WHEN
do {
let seed = 0
let asyncSum = try await collectionToProcess.asyncReduce(into: seed) { partialResult, item in
let value = try await item.valueAsync

// Throw exception during the reduce process
if value % 10 == 0 {
throw SomeError.some
}

partialResult = partialResult + value
}

XCTFail("expected to throw, got \(asyncSum) instead")
}

// THEN
catch {
guard let someError = error as? SomeError,
someError == .some else {
XCTFail("unexpected error: \(error)")
return
}
// all good
}
}
}

// asyncReduce no throw

func testAsyncReduceIntNoThrow() {
asyncTestWrapper {
// GIVEN
let collectionToProcess = Array(0 ... 50)

// WHEN
let asyncSum = await collectionToProcess.asyncReduce(0) { partialResult, item in
return partialResult + item
}

// THEN
let sum = collectionToProcess.reduce(0) { $0 + $1 }
XCTAssertEqual(sum, asyncSum, "Expecting a naive sum to equal the async one")
}
}
}

0 comments on commit a2a4ea5

Please sign in to comment.