Commit Graph

2205 Commits

Author SHA1 Message Date
Cory Benfield bedb9fe613
Enhance and rename AsyncEmbeddedEventLoop (#2224)
* Enhance and rename AsyncEmbeddedEventLoop

Motivation

AsyncEmbeddedEventLoop is an important part of our ongoing testing story
for NIO. However, it suffers from two problems.

The first is a usability one. As we discovered during the original
implementation, following EmbeddedEventLoop's pattern of not having any
EventLoop "Tasks" execute until run() meant that simple constructs like
`EventLoopFuture.wait` and `EventLoopFuture.get` didn't work at all,
forcing us to add an annoying `awaitFuture` method.

When playing with implementing EmbeddedChannel, this got worse, as I/O
methods also don't run immediately if called from the testing thread. We
couldn't easily work around this issue, and it meant that common
patterns (like calling `channel.writeAndFlush`) would deadlock!

This is unacceptable, so a change had to be made.

While we're here, we received feedback that the name is unclear to
users. Given that this particular event loop is in no sense "embedded",
we no longer need the name, so we can take this opportunity to use a
better one.

Modifications

Changed `func execute` to immediately execute its task body, and to
dequeue all pending tasks at this time. Essentially, it's the equivalent
to run(). This is a major change in its behaviour.

Renamed the loop to `NIOAsyncTestingEventLoop`.

Result

Better names, easier to use.

* Make soundness happy

* Remove awaitFuture
2022-07-27 08:03:06 +01:00
Anish Aggarwal d05d2fd125
Added test for BufferView custom contains function (#2227)
* Added test for BufferView custom contains function

* Fixed test case

* testBufferViewContains fixed
2022-07-26 17:04:54 +01:00
Anish Aggarwal dd3b25449a
NIOCore: Implemented all three variants of _failEarlyRangeCheck methods for ByteBufferView (#2226)
* NIOCore: Implemented all three variants of _failEarlyRangeCheck methods for ByteBufferView

* _failEarlyRangeCheck is now no-op
2022-07-25 10:07:40 -07:00
David Nadoba b99da5d3fe
Workaround `Sendable` warning from Swift 5.7 (#2225)
Workaround for https://github.com/apple/swift/issues/59911
```swift
/src/Tests/NIOHTTP1Tests/HTTPTest.swift:148:21: warning: 'buf' mutated after capture by sendable closure
                buf.writeString("\(c)")
```
The variable `buf` is mutated but only before being capture by the sendable closure.

All other warnings/errors are fixed with the latest Swift 5.7 toolchain
2022-07-21 18:20:49 +01:00
Saleem Abdulrasool 48916a49af
NIOCore: handle error description on Windows (#2219)
We would previously attempt to convert an error to a description without
consideration for the provenance on Windows.  This would result in a
failure if the error code was not from `errno`.  Account for the source
of the error and translate it appropriately.  We can not retrieve
descriptions on errors that may originate from Windows or WinSock as
well as the C library.
2022-07-07 13:07:34 +01:00
David Nadoba 8b160c3681
Adopt `Sendable` for `NIOChatServer` and `MIOMuliticastChat` examples (#2220) 2022-07-06 16:30:24 +01:00
David Nadoba f1d62128c4
Adopt `Sendable` for Closures in `NIOWebSocket` (#2218)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-07-05 04:07:44 -07:00
David Nadoba fbe7ef484a
Adopt `Sendable` for Types in `NIOWebSocket` (#2217) 2022-07-05 11:50:55 +01:00
David Nadoba 927b91a1e7
Adopt `Sendable` for closures in `HTTPClientUpgradeHandler.swift` and `HTTPServerUpgradeHandler.swift` (#2216)
* Adopt `Sendable` for closures in `HTTPPipelineSetup.swift`

* make `UnsafeTransfer` and `UnsafeMutableTransferBox` available in Swift 5.4 too

* fix code duplication

* Copy `UnsafeTransfer` to `NIOHTTP1Tests` to be able to remove `@testable` from `NIOCore` import

* Adopt `Sendable` for closures in `HTTPServerUpgradeHandler.swift`

* Adopt `Sendable` for closures in `HTTPClientUpgradeHandler.swift`
2022-07-05 02:42:29 -07:00
David Nadoba 8c922223db
Adopt `Sendable` for closures in `HTTPPipelineSetup.swift` (#2214)
* Adopt `Sendable` for closures in `HTTPPipelineSetup.swift`

* make `UnsafeTransfer` and `UnsafeMutableTransferBox` available in Swift 5.4 too

* fix code duplication

* Copy `UnsafeTransfer` to `NIOHTTP1Tests` to be able to remove `@testable` from `NIOCore` import
2022-07-05 10:01:04 +01:00
David Nadoba 0abf7eb929
Adopt `Sendable` for types in `NIOHTTP1` (#2213) 2022-07-04 00:37:23 -07:00
Saleem Abdulrasool f4b0d661c1
NIOCore: add missing import on Windows (#2215)
We did not import `WinSDK` which provides the socketing interfaces.
This allows building `NIOCore` on Windows again.
2022-07-01 19:01:04 +01:00
David Nadoba 33fc191001
Adopt `Sendable` for `MultiThreadedEventLoopGroup` (#2211)
* Adopt `Sendable` for `MultiThreadedEventLoopGroup`

* remove `@Sendable` from `withCurrentThreadAsEventLoop(_:)`

* Remove trailing whitespace

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-07-01 06:05:54 -07:00
David Nadoba 6c54e11eaa
Adopt `Sendable` in `NonBlockingFileIO` (#2212)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-07-01 04:06:41 -07:00
David Nadoba d6f5e38af2
Adopt `Sendable` for Bootstraps (#2209)
* Adopt `Sendable` for Bootstraps

* fix swift nightly

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-07-01 03:45:07 -07:00
YR Chen a501353ef6
Deprecate `NIOAtomics` in favor of `Atomics` (#2204)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-07-01 02:31:14 -07:00
David Nadoba adda7374af
Adopt `Sendable` for `NIOThreadPool` (#2210)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-30 08:38:09 -07:00
David Nadoba 427d358d03
Adopt `Sendable` for types in `NIOPosix` (#2208) 2022-06-30 16:21:50 +01:00
David Nadoba e650ac1279
Adopt `noasync` for `ThreadSpecificVariable` (#2206) 2022-06-30 11:11:42 +02:00
David Nadoba e2c7fa4d4b
Adopt `Sendable` in `NIOTLS` (#2202) 2022-06-21 14:13:06 +01:00
David Nadoba 48f4e2978f
Adopt `Sendable` in `NIOFoundationCompat` (#2203) 2022-06-21 09:33:04 +01:00
David Nadoba b73fc4e90d
Adopt `Sendable` in `NIOTestUtils` (#2199)
* Adopting `Sendable` in `NIOTestUtils`

The parameter `decoderFactory` of the static methods `ByteToMessageDecoderVerifier.verifyDecoder` do not need to be `@escaping`. I have made them non-escaping as part of `Sendable` adoption because we would otherwise need to think about if they should be `@Sendable` too.

`VerificationError` is interesting, see the code comment for more information.

* Adopting `Sendable` in `NIOTestUtils`

The parameter `decoderFactory` of the static methods `ByteToMessageDecoderVerifier.verifyDecoder` do not need to be `@escaping`. I have made them non-escaping as part of `Sendable` adoption because we would otherwise need to think about if they should be `@Sendable` too.

`VerificationError` is interesting, see the code comment for more information.

* Clarify the reason `VerificationError` already conforms to `Sendable`
2022-06-20 13:23:14 +01:00
David Nadoba f554552edf
Adopt `Sendable` in `NIOEmbedded` (#2195)
`EmbeddedChannel` and `EmbeddedEventLoop` should not be `Sendable`. However, they inherit from `Channel` and `EventLoop` respectively. Therfore, they need to be `Sendable` (and are already implcitily `Sendable`. Given these constraints, we can’t mark `EmbeddedChannel` and `EmbeddedEventLoop` as non-Sendable.
2022-06-17 06:38:06 -07:00
Saleem Abdulrasool f7c3c45df4
NIOPosix: disable `timespec` extension on Windows (#2194)
This extension does not port cleanly to Windows as the time structures
on Windows are different.  This happens to be unused, so simply remove
the extension on Windows.
2022-06-15 10:37:30 +01:00
Si Beaumont 4588691a1a
Provide conversion APIs between TimeAmount and Swift.Duration (#2191)
* Provide conversion APIs between TimeAmount and Swift.Duration

Signed-off-by: Si Beaumont <beaumont@apple.com>

* fixup: Remove unnecessary overflow check

Signed-off-by: Si Beaumont <beaumont@apple.com>

* fixup: Move into new _NIOBeta57 module

Signed-off-by: Si Beaumont <beaumont@apple.com>

* fixup: Rename _NIOBeta57 module to _NIOBeta and move tests to own target

Signed-off-by: Si Beaumont <beaumont@apple.com>

* soundness: copyright years for some reason

Signed-off-by: Si Beaumont <beaumont@apple.com>
2022-06-14 14:38:17 +01:00
Si Beaumont 69bb75df32
Throw fatalError when scheduling on shutdown EL if SWIFTNIO_STRICT is set (#2190)
* Throw fatalError when scheduling on shutdown EL if SWIFTNIO_STRICT is set

Signed-off-by: Si Beaumont <beaumont@apple.com>

* Add CrashTest for SWIFTNIO_STRICT crash

Signed-off-by: Si Beaumont <beaumont@apple.com>

* fixup: Extract env var parsing to static let

Signed-off-by: Si Beaumont <beaumont@apple.com>

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-14 05:43:48 -07:00
Saleem Abdulrasool b393d39603
NIOPosix: add missing import for Windows (#2188)
Add an import of `NIOCore` on Windows which mirrors the other platforms.
This greatly reduces the noise in the error list.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-14 04:37:27 -07:00
Cory Benfield 7fbaed6f4a
Use correct alloc limits for 5.7 (#2193) 2022-06-14 11:57:26 +01:00
Saleem Abdulrasool 59cd8ff038
NIOPosix: import additional interfaces from WinSDK (#2189)
Import additional interfaces from WinSDK to enable additional paths to
build for Windows.
2022-06-14 10:56:44 +01:00
Stepan Ulyanin c0911e1b3c
Implement the _failEarlyRangeCheck methods as no-ops (#2161)
* implement the _failEarlyRangeCheck methods as no-ops

* mark _failEarlyRangeCheck with @inlinable

* add a comment on why the _failEarlyRangeCheck methods are implemented as no-ops

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-13 06:10:37 -07:00
Saleem Abdulrasool 3c3c2b8f41
NIOPosix: add missing `CNIOWindows` import (#2184)
Add a missing import to match the other platforms.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-13 05:53:07 -07:00
Cory Benfield ec4072c925
Use 5.7 nightlies (#2186) 2022-06-13 03:06:35 -07:00
Saleem Abdulrasool e0df87c5c1
NIOPosix: adjust precondition for Windows (#2183)
The member names are not identical across platforms.  Add a case to
handle the name difference on Windows.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-10 09:14:27 -07:00
Saleem Abdulrasool 01b13a5675
NIOPosix: reorganise packet info constants (#2182)
Co-locate the packet info constants and add Windows values.
2022-06-10 16:55:51 +01:00
Saleem Abdulrasool 03fa431216
NIOPosix: port file validation to Windows (#2179)
Use Win32 APIs to properly validate if a file is a pipe on Windows.
This enables providing the same semantics without leaking additional
Windows specifics.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-09 23:27:14 -07:00
Saleem Abdulrasool 57d41b502c
NIOPosix: reorganise ECN constants, define for Windows (#2181)
Reorganise the ECN constants to colocate the definitions for the
different platforms.  Define the constants for Windows as the platform
does not provide them in the system headers.
2022-06-10 07:12:26 +01:00
Saleem Abdulrasool 055043e151
NIOPosix: use internal enumeration for GAI resolver (#2177)
Replace the use of raw constants with the internal enumeration.  This
ensures that the constant names are uniform and don't leak structural
information from the underlying information.
2022-06-09 08:21:24 +01:00
Saleem Abdulrasool 2560725d71
NIOPosix: use typealias instead of C name (#2180)
Switch to the typealias rather than the raw name to support Windows and
make the code homogeneous with the rest of the codebase.
2022-06-08 10:07:32 -07:00
Saleem Abdulrasool e697fd315a
NIOPosix: silence some deprecation warnings on Windows (#2171)
Create a wrapper over `strerror` which is considered deprecated due to
the use of a shared global buffer.  Use `strerror_s` which requires a
buffer and size to prevent shared state or a buffer overrun.  This is
documented by Microsoft as requiring at most 94 characters, so we use a
buffer of size 95.
2022-06-08 10:20:52 +01:00
Saleem Abdulrasool 4448bb04aa
NIOPosix: remove unused alias for function (#2172)
This cleans up a reference to `freeifaddrs` as that is no longer
referenced in the code base.  This avoids an error on Windows as
a side-effect.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-07 10:48:29 -07:00
Saleem Abdulrasool cd70dd3ec1
NIOPosix: shuffle typealias location (#2174)
Move the `sockaddr` typealias for Windows into the `BSDSocketAPIWindows`
file which is a better home for the platform specific typealias.
2022-06-07 10:24:35 -07:00
Saleem Abdulrasool 4f20e48a78
NIOPosix: use wrapper function for duplicating file descriptor (#2173)
Use the wrapper function for duplicating the file descriptor.  This
allows us to swap out the implementation for different targets making
the code more portable.  It additionally adds additional error handling
and checking as the wrappers are intended to perform error handling.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-07 10:08:52 -07:00
Saleem Abdulrasool 65c3f576b1
NIOPosix: reduce imports for Windows (#2170)
Reduce unnecessary noise in the files after previous refactoring
shuffled the use of the imported interfaces.  We can centralise most of
the imports and internal typealiases which avoids cluttering the shared
interfaces with compatibility handling for Windows.
2022-06-07 09:37:11 -07:00
Saleem Abdulrasool 207e6e0f7e
NIOPosix: add additional imports and typealiases on Windows (#2168)
Extend more of the internal interfaces for the BSD API coverage on
Windows to get the same API surface when possible.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-07 07:38:50 -07:00
David Nadoba 0e2818bf86
Remove `@preconcurrency` from internal `EventLoopFuture` methods (#2135)
* add @preconcurrency again as it otherwise results in one more allcation in 1000_udpconnections
2022-06-07 07:09:44 -07:00
Saleem Abdulrasool fae6522f6d
NIOCore: adjust the BSDSocketAPI for Windows (#2164)
Windows does not provide a definition for `socklen_t`.  Create an
internal typealias for it to provide source stability to the internal
interfaces.

Co-authored-by: George Barnett <gbarnett@apple.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-07 06:39:48 -07:00
Saleem Abdulrasool b4e4f0211f
NIOCore: adjust system call helpers for Windows (#2155)
Windows does not provide a 3 parameter `open` and needs to be passed all
the parameters as `_open` is a variadic function (as per the
specification).  This provides a Windows specific path for `open` and
`read` as on Darwin and Linux, `read` uses a non-standard return type
(`ssize_t`).  As it happens, the `size` parameter on Windows also uses
`unsigned int` rather than `size_t` which would break on different
bitnesses.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-07 06:08:32 -07:00
Saleem Abdulrasool bb2aae798f
NIOCore: extract and modify POSIX flag constants (#2150)
Windows does not have the same set of POSIX flag constants.  In
particular, there is no equivalent to group permissions as the ACL model
on Windows is far richer and requires the full specification of the
DACLs.  Additionally, isolate `O_CLOEXEC` to aid Windows which does not
have this flag.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-07 05:44:36 -07:00
Saleem Abdulrasool a8bb008516
NIOPosix: use `errno` accessor on Windows (#2167)
`errno` is a thread local variable, and as such on many platforms is
accessed indirectly.  This is implemented as macro, but is considered
complex by the clang importer and cannot be directly used.  Resolve this
by using the accessor for the value on Windows.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-06-07 05:10:12 -07:00
Cory Benfield 67c0fd36d3
Add support for translating subnet prefixes to masks (#2169)
Motivation:

On Windows we have a need to transform a subnet prefix into a subnet
mask when enumerating devices. This is a straightforward transformation
that we can easily do in pure-Swift, so let's do that. Additionally, the
functions have small enough domains that we can just exhaustively test
them.

Modifications:

- Added functions to create SocketAddress objects from subnet prefixes.
- Added exhaustive tests for those functions.
- Use these functions in Windows code.

Result:

Correct transformations of prefixes into masks on Windows.
2022-06-07 04:49:19 -07:00