Skip to content

Commit

Permalink
Fix tests for 32-bit platforms, tested for Android armv7 (#1877)
Browse files Browse the repository at this point in the history
Motivation:

Get the tests passing on 32-bit platforms again.

Modifications:

- Change the way _UInt56.max is initialized.
- Set the proper max capacity for AdaptiveRecvByteBufferAllocator
  and change the test to match it.
- Add a cmsghdrExample for Android armv7.

Result:

All the same tests pass on Android armv7 as Android AArch64.
  • Loading branch information
finagolfin committed Jun 11, 2021
1 parent 67f0843 commit a1f4052
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Sources/NIO/IntegerTypes.swift
Expand Up @@ -75,7 +75,8 @@ struct _UInt56 {

static let bitWidth: Int = 56

static let max: _UInt56 = .init((1 << 56) - 1)
private static let initializeUInt64 : UInt64 = (1 << 56) - 1
static let max: _UInt56 = .init(initializeUInt64)
static let min: _UInt56 = .init(0)
}

Expand Down
7 changes: 4 additions & 3 deletions Sources/NIO/RecvByteBufferAllocator.swift
Expand Up @@ -55,7 +55,7 @@ public struct AdaptiveRecvByteBufferAllocator: RecvByteBufferAllocator {
private var nextReceiveBufferSize: Int
private var decreaseNow: Bool

private static let maximumAllocationSize = 1 << 31
private static let maximumAllocationSize = 1 << 30

public init() {
self.init(minimum: 64, initial: 2048, maximum: 65536)
Expand Down Expand Up @@ -93,7 +93,7 @@ public struct AdaptiveRecvByteBufferAllocator: RecvByteBufferAllocator {
// Here we need to be careful with 32-bit systems: if maximum is too large then any shift or multiply will overflow, which
// we don't want. Instead we check, and clamp to this current value if we overflow.
let upperBoundCandidate = Int(truncatingIfNeeded: Int64(self.nextReceiveBufferSize) &<< 1)
let upperBound = upperBoundCandidate == 0 ? self.nextReceiveBufferSize : upperBoundCandidate
let upperBound = upperBoundCandidate <= 0 ? self.nextReceiveBufferSize : upperBoundCandidate

if actualReadBytes <= lowerBound && lowerBound >= self.minimum {
if self.decreaseNow {
Expand All @@ -102,7 +102,8 @@ public struct AdaptiveRecvByteBufferAllocator: RecvByteBufferAllocator {
} else {
self.decreaseNow = true
}
} else if actualReadBytes >= self.nextReceiveBufferSize && upperBound <= self.maximum {
} else if actualReadBytes >= self.nextReceiveBufferSize && upperBound <= self.maximum &&
self.nextReceiveBufferSize != upperBound {
self.nextReceiveBufferSize = upperBound
self.decreaseNow = false
mayGrow = true
Expand Down
10 changes: 5 additions & 5 deletions Tests/NIOTests/RecvByteBufAllocatorTest.swift
Expand Up @@ -117,8 +117,8 @@ final class AdaptiveRecvByteBufferAllocatorTest: XCTestCase {
}

let buffer = self.adaptive.buffer(allocator: self.allocator)
XCTAssertEqual(buffer.capacity, 1 << 31)
XCTAssertEqual(self.adaptive.maximum, 1 << 31)
XCTAssertEqual(buffer.capacity, 1 << 30)
XCTAssertEqual(self.adaptive.maximum, 1 << 30)
XCTAssertEqual(self.adaptive.minimum, 0)
}

Expand All @@ -136,8 +136,8 @@ final class AdaptiveRecvByteBufferAllocatorTest: XCTestCase {
}

let adaptive = AdaptiveRecvByteBufferAllocator(minimum: targetValue, initial: targetValue + 1, maximum: targetValue + 2)
XCTAssertEqual(adaptive.minimum, 1 << 31)
XCTAssertEqual(adaptive.maximum, 1 << 31)
XCTAssertEqual(adaptive.initial, 1 << 31)
XCTAssertEqual(adaptive.minimum, 1 << 30)
XCTAssertEqual(adaptive.maximum, 1 << 30)
XCTAssertEqual(adaptive.initial, 1 << 30)
}
}
15 changes: 15 additions & 0 deletions Tests/NIOTests/SystemTest.swift
Expand Up @@ -62,6 +62,21 @@ class SystemTest: XCTestCase {
private static let cmsghdr_secondDataCount = 1
private static let cmsghdr_firstType = IP_RECVDSTADDR
private static let cmsghdr_secondType = IP_RECVTOS
#elseif os(Android) && arch(arm)
private static let cmsghdrExample: [UInt8] = [0x10, 0x00, 0x00, 0x00, // Length 16 including header
0x00, 0x00, 0x00, 0x00, // IPPROTO_IP
0x08, 0x00, 0x00, 0x00, // IP_PKTINFO
0x7F, 0x00, 0x00, 0x01, // 127.0.0.1
0x0D, 0x00, 0x00, 0x00, // Length 13 including header
0x00, 0x00, 0x00, 0x00, // IPPROTO_IP
0x01, 0x00, 0x00, 0x00, // IP_TOS
0x01, 0x00, 0x00, 0x00] // ECT-1 (1 byte)
private static let cmsghdr_secondStartPosition = 16
private static let cmsghdr_firstDataStart = 12
private static let cmsghdr_firstDataCount = 4
private static let cmsghdr_secondDataCount = 1
private static let cmsghdr_firstType = IP_PKTINFO
private static let cmsghdr_secondType = IP_TOS
#elseif os(Linux) || os(Android)
// Example twin data options captured on Linux
private static let cmsghdrExample: [UInt8] = [
Expand Down

0 comments on commit a1f4052

Please sign in to comment.