forked from swift-server/swift-aws-lambda-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDetachedTasksTests.swift
80 lines (69 loc) · 2.69 KB
/
DetachedTasksTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftAWSLambdaRuntime project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
@testable import AWSLambdaRuntimeCore
import NIO
import XCTest
import Logging
class DetachedTasksTest: XCTestCase {
actor Expectation {
var isFulfilled = false
func fulfill() {
isFulfilled = true
}
}
func testAwaitTasks() async throws {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) }
let context = DetachedTasksContainer.Context(
eventLoop: eventLoopGroup.next(),
logger: Logger(label: "test")
)
let expectation = Expectation()
let container = DetachedTasksContainer(context: context)
await container.detached {
try! await Task.sleep(for: .milliseconds(200))
await expectation.fulfill()
}
try await container.awaitAll().get()
let isFulfilled = await expectation.isFulfilled
XCTAssert(isFulfilled)
}
func testAwaitChildrenTasks() async throws {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) }
let context = DetachedTasksContainer.Context(
eventLoop: eventLoopGroup.next(),
logger: Logger(label: "test")
)
let expectation1 = Expectation()
let expectation2 = Expectation()
let container = DetachedTasksContainer(context: context)
await container.detached {
await container.detached {
try! await Task.sleep(for: .milliseconds(300))
await expectation1.fulfill()
}
try! await Task.sleep(for: .milliseconds(200))
await container.detached {
try! await Task.sleep(for: .milliseconds(100))
await expectation2.fulfill()
}
}
try await container.awaitAll().get()
let isFulfilled1 = await expectation1.isFulfilled
let isFulfilled2 = await expectation2.isFulfilled
XCTAssert(isFulfilled1)
XCTAssert(isFulfilled2)
}
}