Skip to content

Commit

Permalink
fix: Setup async tests without Task
Browse files Browse the repository at this point in the history
Signed-off-by: Fabrizio Demaria <[email protected]>
  • Loading branch information
fabriziodemaria committed Jan 12, 2024
1 parent 64026c7 commit 223418c
Showing 1 changed file with 20 additions and 27 deletions.
47 changes: 20 additions & 27 deletions Tests/ConfidenceProviderTests/CacheDataInteractorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import XCTest
@testable import ConfidenceProvider

final class CacheDataInteractorTests: XCTestCase {
func testCacheDataInteractor_loadsEventsFromStorage() throws {
func testCacheDataInteractor_loadsEventsFromStorage() async throws {
// Given prefilled storage with 10 resolve events (20 apply events in each)
let prefilledCache = try CacheDataUtility.prefilledCacheData(
resolveEventCount: 10,
Expand All @@ -16,44 +16,37 @@ final class CacheDataInteractorTests: XCTestCase {
let cacheDataInteractor = CacheDataInteractor(cacheData: prefilledCache)

// Then cache data is loaded from storage
Task {
// Wrapped it in the Task in order to ensure that async code is completed before assertions
let cache = await cacheDataInteractor.cache
XCTAssertEqual(cache.resolveEvents.count, 10)
XCTAssertEqual(cache.resolveEvents.last?.events.count, 20)
}
let cache = await cacheDataInteractor.cache
XCTAssertEqual(cache.resolveEvents.count, 10)
XCTAssertEqual(cache.resolveEvents.last?.events.count, 20)
}

func testCacheDataInteractor_addEventToEmptyCache() async throws {
// Given cache data interactor with no previously stored data
let cacheDataInteractor = CacheDataInteractor(cacheData: .empty())
Task {
let cache = await cacheDataInteractor.cache
XCTAssertEqual(cache.resolveEvents.count, 0)
}

Task {
// When cache data add method is called
_ = await cacheDataInteractor.add(resolveToken: "token", flagName: "name", applyTime: Date())

// Then event is added with
let cache = await cacheDataInteractor.cache
XCTAssertEqual(cache.resolveEvents.count, 1)
}

let cache = await cacheDataInteractor.cache
XCTAssertEqual(cache.resolveEvents.count, 0)


// When cache data add method is called
_ = await cacheDataInteractor.add(resolveToken: "token", flagName: "name", applyTime: Date())

// Then event is added with
let cache2 = await cacheDataInteractor.cache
XCTAssertEqual(cache2.resolveEvents.count, 1)
}

func testCacheDataInteractor_addEventToPreFilledCache() async throws {
// Given cache data interactor with previously stored data (1 resolve token and 2 apply event)
let prefilledCacheData = try CacheDataUtility.prefilledCacheData(applyEventCount: 2)
let cacheDataInteractor = CacheDataInteractor(cacheData: prefilledCacheData)

Task {
// When cache data add method is called
_ = await cacheDataInteractor.add(resolveToken: "token", flagName: "name", applyTime: Date())
// When cache data add method is called
_ = await cacheDataInteractor.add(resolveToken: "token", flagName: "name", applyTime: Date())

// Then event is added with
let cache = await cacheDataInteractor.cache
XCTAssertEqual(cache.resolveEvents.count, 2)
}
// Then event is added with
let cache = await cacheDataInteractor.cache
XCTAssertEqual(cache.resolveEvents.count, 2)
}
}

0 comments on commit 223418c

Please sign in to comment.