Files
async-http-client/Tests/AsyncHTTPClientTests/SSLContextCacheTests.swift
T
Johannes Weiss 8ccba7328d SSLContextCache: use DispatchQueue instead of NIOThreadPool (#368)
Motivation:

In the vast majority of cases, we'll only ever create one and only one
`NIOSSLContext`. It's therefore wasteful to keep around a whole thread
doing nothing just for that. A `DispatchQueue` is absolutely fine here.

Modification:

Run the `NIOSSLContext` creation on a `DispatchQueue` instead.

Result:

Fewer threads hanging around.
2021-05-13 15:11:49 +01:00

80 lines
3.3 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the AsyncHTTPClient open source project
//
// Copyright (c) 2021 Apple Inc. and the AsyncHTTPClient project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
@testable import AsyncHTTPClient
import NIO
import NIOSSL
import XCTest
final class SSLContextCacheTests: XCTestCase {
func testRequestingSSLContextWorks() {
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let eventLoop = group.next()
let cache = SSLContextCache()
defer {
XCTAssertNoThrow(try group.syncShutdownGracefully())
}
XCTAssertNoThrow(try cache.sslContext(tlsConfiguration: .forClient(),
eventLoop: eventLoop,
logger: HTTPClient.loggingDisabled).wait())
}
func testCacheWorks() {
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let eventLoop = group.next()
let cache = SSLContextCache()
defer {
XCTAssertNoThrow(try group.syncShutdownGracefully())
}
var firstContext: NIOSSLContext?
var secondContext: NIOSSLContext?
XCTAssertNoThrow(firstContext = try cache.sslContext(tlsConfiguration: .forClient(),
eventLoop: eventLoop,
logger: HTTPClient.loggingDisabled).wait())
XCTAssertNoThrow(secondContext = try cache.sslContext(tlsConfiguration: .forClient(),
eventLoop: eventLoop,
logger: HTTPClient.loggingDisabled).wait())
XCTAssertNotNil(firstContext)
XCTAssertNotNil(secondContext)
XCTAssert(firstContext === secondContext)
}
func testCacheDoesNotReturnWrongEntry() {
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let eventLoop = group.next()
let cache = SSLContextCache()
defer {
XCTAssertNoThrow(try group.syncShutdownGracefully())
}
var firstContext: NIOSSLContext?
var secondContext: NIOSSLContext?
XCTAssertNoThrow(firstContext = try cache.sslContext(tlsConfiguration: .forClient(),
eventLoop: eventLoop,
logger: HTTPClient.loggingDisabled).wait())
// Second one has a _different_ TLSConfiguration.
XCTAssertNoThrow(secondContext = try cache.sslContext(tlsConfiguration: .forClient(certificateVerification: .none),
eventLoop: eventLoop,
logger: HTTPClient.loggingDisabled).wait())
XCTAssertNotNil(firstContext)
XCTAssertNotNil(secondContext)
XCTAssert(firstContext !== secondContext)
}
}