Skip to content

Instantly share code, notes, and snippets.

View jacobsapps's full-sized avatar

Jacob Bartlett jacobsapps

View GitHub Profile
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
@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)
}
func test_refresh_loadsDataFromAPI() {
// given
mockAPI.stubFetchResponse = .success([])
let exp = expectation(description: #function)
// when
viewModel.refresh()
mockAPI.didFetchData = { exp.fulfill() }
// then
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
@MainActor
private func streamNotificationCount() async {
for await notificationsCount in combineLatest(
repo.chatNotificationsSequence,
repo.friendNotificationsSequence
).map({
$0.0.count + $0.1.count
}) {
self.notificationCount = notificationsCount
}
let taskA = Task {
Task { // task B
try Task.checkCancellation() // does NOT throw
}
try Task.checkCancellation() // throws
}
taskA.cancel()
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)
}
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 {
Task { @concurrent in
await viewModel.processImage(self.selectedImage)
}
@concurrent
func process(image: UIImage) async -> [Objects]? {
let observations = visionModel.process(image)
let objects = objects(from: observations)
}