This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Distributed | |
public distributed actor BotPlayer: Identifiable { | |
typealias ActorSystem = LocalTestingDistributedActorSystem | |
var ai: RandomPlayerBotAI | |
var gameState: GameState | |
public init(team: CharacterTeam, actorSystem: ActorSystem) { | |
self.actorSystem = actorSystem // first, initialize the implicitly synthesized actor system property |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
func refreshBeers_tellsRepositoryToLoad() async throws { | |
sut = BeerViewModel(repository: mockBeerRepository) | |
mockBeerRepository.stubLoadBeersResponse = .success([]) | |
let exp = SwiftExpectation() | |
mockBeerRepository.didLoadBeers = { exp.fulfill() } | |
sut.refreshBeers() | |
try await exp.wait() | |
#expect(mockBeerRepository.loadBeersCallCount == 1) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func test_refresh_loadsDataFromAPI() { | |
// given | |
mockAPI.stubFetchResponse = .success([]) | |
let exp = expectation(description: #function) | |
// when | |
viewModel.refresh() | |
mockAPI.didFetchData = { exp.fulfill() } | |
// then |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class MockAPI: API { | |
// mock result | |
public var stubFetchResponse: Result<[Model], Error>? | |
// deferred closure | |
public var didFetchData: (() -> Void)? | |
// what we assert | |
public private(set) var fetchDataWasCalled = false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@MainActor | |
private func streamNotificationCount() async { | |
for await notificationsCount in combineLatest( | |
repo.chatNotificationsSequence, | |
repo.friendNotificationsSequence | |
).map({ | |
$0.0.count + $0.1.count | |
}) { | |
self.notificationCount = notificationsCount | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let taskA = Task { | |
Task { // task B | |
try Task.checkCancellation() // does NOT throw | |
} | |
try Task.checkCancellation() // throws | |
} | |
taskA.cancel() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var task: Task<Void, Error>? | |
task = Task { | |
async let name = fetchUserName() | |
async let avatar = fetchUserAvatar() | |
// this will throw if either child task checks cancellation | |
try await (name, avatar) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Task { // parent task 1 | |
// each of these async lets is a child task of parent task 1 | |
async let name = fetchUserName() | |
async let avatar = fetchUserAvatar() | |
try await (name, avatar) | |
} | |
Task { // parent task 2 | |
await withTaskGroup(of: Void.self) { group in | |
for url in urls { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Task { @concurrent in | |
await viewModel.processImage(self.selectedImage) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@concurrent | |
func process(image: UIImage) async -> [Objects]? { | |
let observations = visionModel.process(image) | |
let objects = objects(from: observations) | |
} |
NewerOlder