forked from apple/swift-log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMDCTest.swift
54 lines (52 loc) · 2 KB
/
MDCTest.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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Logging API open source project
//
// Copyright (c) 2018-2019 Apple Inc. and the Swift Logging API project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift Logging API project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Dispatch
@testable import Logging
import XCTest
class MDCTest: XCTestCase {
func test1() throws {
// bootstrap with our test logger
let logging = TestLogging()
LoggingSystem.bootstrapInternal { logging.make(label: $0) }
// run the program
MDC.global["foo"] = "bar"
let group = DispatchGroup()
for r in 5 ... 10 {
group.enter()
DispatchQueue(label: "mdc-test-queue-\(r)").async {
let add = Int.random(in: 10 ... 1000)
let remove = Int.random(in: 0 ... add - 1)
for i in 0 ... add {
MDC.global["key-\(i)"] = "value-\(i)"
}
for i in 0 ... remove {
MDC.global["key-\(i)"] = nil
}
XCTAssertEqual(add - remove, MDC.global.metadata.count, "expected number of entries to match")
for i in remove + 1 ... add {
XCTAssertNotNil(MDC.global["key-\(i)"], "expecting value for key-\(i)")
}
for i in 0 ... remove {
XCTAssertNil(MDC.global["key-\(i)"], "not expecting value for key-\(i)")
}
MDC.global.clear()
group.leave()
}
}
group.wait()
XCTAssertEqual(MDC.global["foo"], "bar", "expecting to find top items")
MDC.global["foo"] = nil
XCTAssertTrue(MDC.global.metadata.isEmpty, "MDC should be empty")
}
}