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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import XCTest
@testable import NIO
import NIOConcurrencyHelpers
class NonBlockingFileIOTest: XCTestCase {
private var group: EventLoopGroup!
private var eventLoop: EventLoop!
private var allocator: ByteBufferAllocator!
private var fileIO: NonBlockingFileIO!
private var threadPool: NIOThreadPool!
override func setUp() {
super.setUp()
self.allocator = ByteBufferAllocator()
self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
self.threadPool = NIOThreadPool(numberOfThreads: 6)
self.threadPool.start()
self.fileIO = NonBlockingFileIO(threadPool: threadPool)
self.eventLoop = self.group.next()
}
override func tearDown() {
XCTAssertNoThrow(try self.group?.syncShutdownGracefully())
XCTAssertNoThrow(try self.threadPool?.syncShutdownGracefully())
self.group = nil
self.eventLoop = nil
self.allocator = nil
self.threadPool = nil
self.fileIO = nil
super.tearDown()
}
func testBasicFileIOWorks() throws {
let content = "hello"
try withTemporaryFile(content: content) { (fileHandle, _) -> Void in
let fr = FileRegion(fileHandle: fileHandle, readerIndex: 0, endIndex: 5)
var buf = try self.fileIO.read(fileRegion: fr,
allocator: self.allocator,
eventLoop: self.eventLoop).wait()
XCTAssertEqual(content.utf8.count, buf.readableBytes)
XCTAssertEqual(content, buf.readString(length: buf.readableBytes))
}
}
func testOffsetWorks() throws {
let content = "hello"
try withTemporaryFile(content: content) { (fileHandle, _) -> Void in
let fr = FileRegion(fileHandle: fileHandle, readerIndex: 3, endIndex: 5)
var buf = try self.fileIO.read(fileRegion: fr,
allocator: self.allocator,
eventLoop: self.eventLoop).wait()
XCTAssertEqual(2, buf.readableBytes)
XCTAssertEqual("lo", buf.readString(length: buf.readableBytes))
}
}
func testOffsetBeyondEOF() throws {
let content = "hello"
try withTemporaryFile(content: content) { (fileHandle, _) -> Void in
let fr = FileRegion(fileHandle: fileHandle, readerIndex: 3000, endIndex: 3001)
var buf = try self.fileIO.read(fileRegion: fr,
allocator: self.allocator,
eventLoop: self.eventLoop).wait()
XCTAssertEqual(0, buf.readableBytes)
XCTAssertEqual("", buf.readString(length: buf.readableBytes))
}
}
func testEmptyReadWorks() throws {
try withTemporaryFile { (fileHandle, _) -> Void in
let fr = FileRegion(fileHandle: fileHandle, readerIndex: 0, endIndex: 0)
let buf = try self.fileIO.read(fileRegion: fr,
allocator: self.allocator,
eventLoop: self.eventLoop).wait()
XCTAssertEqual(0, buf.readableBytes)
}
}
func testReadingShortWorks() throws {
let content = "hello"
try withTemporaryFile(content: "hello") { (fileHandle, _) -> Void in
let fr = FileRegion(fileHandle: fileHandle, readerIndex: 0, endIndex: 10)
var buf = try self.fileIO.read(fileRegion: fr,
allocator: self.allocator,
eventLoop: self.eventLoop).wait()
XCTAssertEqual(content.utf8.count, buf.readableBytes)
XCTAssertEqual(content, buf.readString(length: buf.readableBytes))
}
}
func testDoesNotBlockTheThreadOrEventLoop() throws {
var innerError: Error? = nil
try withPipe { readFH, writeFH in
let bufferFuture = self.fileIO.read(fileHandle: readFH,
byteCount: 10,
allocator: self.allocator,
eventLoop: self.eventLoop)
do {
try self.eventLoop.submit {
try writeFH.withUnsafeFileDescriptor { writeFD in
_ = try Posix.write(descriptor: writeFD, pointer: "X", size: 1)
}
try writeFH.close()
}.wait()
var buf = try bufferFuture.wait()
XCTAssertEqual(1, buf.readableBytes)
XCTAssertEqual("X", buf.readString(length: buf.readableBytes))
} catch {
innerError = error
}
return [readFH]
}
XCTAssertNil(innerError)
}
func testGettingErrorWhenEventLoopGroupIsShutdown() throws {
self.threadPool.shutdownGracefully(queue: .global()) { err in
XCTAssertNil(err)
}
try withPipe { readFH, writeFH in
XCTAssertThrowsError(try self.fileIO.read(fileHandle: readFH,
byteCount: 1,
allocator: self.allocator,
eventLoop: self.eventLoop).wait()) { error in
XCTAssertTrue(error is NIOThreadPoolError.ThreadPoolInactive)
}
return [readFH, writeFH]
}
}
func testChunkReadingWorks() throws {
let content = "hello"
let contentBytes = Array(content.utf8)
var numCalls = 0
try withTemporaryFile(content: content) { (fileHandle, path) -> Void in
let fr = FileRegion(fileHandle: fileHandle, readerIndex: 0, endIndex: 5)
try self.fileIO.readChunked(fileRegion: fr,
chunkSize: 1,
allocator: self.allocator,
eventLoop: self.eventLoop) { buf in
var buf = buf
XCTAssertTrue(self.eventLoop.inEventLoop)
XCTAssertEqual(1, buf.readableBytes)
XCTAssertEqual(contentBytes[numCalls], buf.readBytes(length: 1)?.first!)
numCalls += 1
return self.eventLoop.makeSucceededFuture(())
}.wait()
}
XCTAssertEqual(content.utf8.count, numCalls)
}
func testChunkReadingCanBeAborted() throws {
enum DummyError: Error { case dummy }
let content = "hello"
let contentBytes = Array(content.utf8)
var numCalls = 0
try withTemporaryFile(content: content) { (fileHandle, path) -> Void in
let fr = FileRegion(fileHandle: fileHandle, readerIndex: 0, endIndex: 5)
XCTAssertThrowsError(try self.fileIO.readChunked(fileRegion: fr,
chunkSize: 1,
allocator: self.allocator,
eventLoop: self.eventLoop) { buf in
var buf = buf
XCTAssertTrue(self.eventLoop.inEventLoop)
XCTAssertEqual(1, buf.readableBytes)
XCTAssertEqual(contentBytes[numCalls], buf.readBytes(length: 1)?.first!)
numCalls += 1
return self.eventLoop.makeFailedFuture(DummyError.dummy)
}.wait()) { error in
XCTAssertEqual(.dummy, error as? DummyError)
}
}
XCTAssertEqual(1, numCalls)
}
func testFailedIO() throws {
enum DummyError: Error { case dummy }
let unconnectedSockFH =
NIOFileHandle(descriptor: try! NIOBSDSocket.socket(domain: .unix, type: .stream, protocol: 0))
defer {
XCTAssertNoThrow(try unconnectedSockFH.close())
}
XCTAssertThrowsError(try self.fileIO.readChunked(fileHandle: unconnectedSockFH,
byteCount: 5,
chunkSize: 1,
allocator: self.allocator,
eventLoop: self.eventLoop) { buf in
XCTFail("shouldn't have been called")
return self.eventLoop.makeSucceededFuture(())
}.wait()) { error in
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
XCTAssertEqual(ENOTCONN, (error as? IOError)?.errnoCode)
#else
XCTAssertEqual(EINVAL, (error as? IOError)?.errnoCode)
#endif
}
}
func testChunkReadingWorksForIncrediblyLongChain() throws {
let content = String(repeating: "X", count: 20*1024)
var numCalls = 0
let expectedByte = content.utf8.first!
try withTemporaryFile(content: content) { (fileHandle, path) -> Void in
try self.fileIO.readChunked(fileHandle: fileHandle,
fromOffset: 0,
byteCount: content.utf8.count,
chunkSize: 1,
allocator: self.allocator,
eventLoop: self.eventLoop) { buf in
XCTAssertTrue(self.eventLoop.inEventLoop)
XCTAssertEqual(1, buf.readableBytes)
XCTAssertEqual(expectedByte, buf.readableBytesView.first)
numCalls += 1
return self.eventLoop.makeSucceededFuture(())
}.wait()
}
XCTAssertEqual(content.utf8.count, numCalls)
}
func testReadingDifferentChunkSize() throws {
let content = "0123456789"
var numCalls = 0
try withTemporaryFile(content: content) { (fileHandle, path) -> Void in
let fr = FileRegion(fileHandle: fileHandle, readerIndex: 0, endIndex: content.utf8.count)
try self.fileIO.readChunked(fileRegion: fr,
chunkSize: 2,
allocator: self.allocator,
eventLoop: self.eventLoop) { buf in
var buf = buf
XCTAssertTrue(self.eventLoop.inEventLoop)
XCTAssertEqual(2, buf.readableBytes)
XCTAssertEqual(Array("\(numCalls*2)\(numCalls*2 + 1)".utf8), buf.readBytes(length: 2)!)
numCalls += 1
return self.eventLoop.makeSucceededFuture(())
}.wait()
}
XCTAssertEqual(content.utf8.count/2, numCalls)
}
func testReadDoesNotReadShort() throws {
var innerError: Error? = nil
try withPipe { readFH, writeFH in
let bufferFuture = self.fileIO.read(fileHandle: readFH,
byteCount: 10,
allocator: self.allocator,
eventLoop: self.eventLoop)
do {
for i in 0..<10 {
// this construction will cause 'read' to repeatedly return with 1 byte read
try self.eventLoop.scheduleTask(in: .milliseconds(50)) {
try writeFH.withUnsafeFileDescriptor { writeFD in
_ = try Posix.write(descriptor: writeFD, pointer: "\(i)", size: 1)
}
}.futureResult.wait()
}
try writeFH.close()
var buf = try bufferFuture.wait()
XCTAssertEqual(10, buf.readableBytes)
XCTAssertEqual("0123456789", buf.readString(length: buf.readableBytes))
} catch {
innerError = error
}
return [readFH]
}
XCTAssertNil(innerError)
}
func testChunkReadingWhereByteCountIsNotAChunkSizeMultiplier() throws {
let content = "prefix-12345-suffix"
var allBytesActual = ""
let allBytesExpected = String(content.dropFirst(7).dropLast(7))
var numCalls = 0
try withTemporaryFile(content: content) { (fileHandle, path) -> Void in
let fr = FileRegion(fileHandle: fileHandle, readerIndex: 7, endIndex: 12)
try self.fileIO.readChunked(fileRegion: fr,
chunkSize: 3,
allocator: self.allocator,
eventLoop: self.eventLoop) { buf in
var buf = buf
XCTAssertTrue(self.eventLoop.inEventLoop)
allBytesActual += buf.readString(length: buf.readableBytes) ?? "WRONG"
numCalls += 1
return self.eventLoop.makeSucceededFuture(())
}.wait()
}
XCTAssertEqual(allBytesExpected, allBytesActual)
XCTAssertEqual(2, numCalls)
}
func testChunkedReadDoesNotReadShort() throws {
var innerError: Error? = nil
try withPipe { readFH, writeFH in
var allBytes = ""
let f = self.fileIO.readChunked(fileHandle: readFH,
byteCount: 10,
chunkSize: 3,
allocator: self.allocator,
eventLoop: self.eventLoop) { buf in
var buf = buf
if allBytes.utf8.count == 9 {
XCTAssertEqual(1, buf.readableBytes)
} else {
XCTAssertEqual(3, buf.readableBytes)
}
allBytes.append(buf.readString(length: buf.readableBytes) ?? "THIS IS WRONG")
return self.eventLoop.makeSucceededFuture(())
}
do {
for i in 0..<10 {
// this construction will cause 'read' to repeatedly return with 1 byte read
try self.eventLoop.scheduleTask(in: .milliseconds(50)) {
try writeFH.withUnsafeFileDescriptor { writeFD in
_ = try Posix.write(descriptor: writeFD, pointer: "\(i)", size: 1)
}
}.futureResult.wait()
}
try writeFH.close()
try f.wait()
XCTAssertEqual("0123456789", allBytes)
} catch {
innerError = error
}
return [readFH]
}
XCTAssertNil(innerError)
}
func testChunkSizeMoreThanTotal() throws {
let content = "0123456789"
var numCalls = 0
try withTemporaryFile(content: content) { (fileHandle, path) -> Void in
let fr = FileRegion(fileHandle: fileHandle, readerIndex: 0, endIndex: 5)
try self.fileIO.readChunked(fileRegion: fr,
chunkSize: 10,
allocator: self.allocator,
eventLoop: self.eventLoop) { buf in
var buf = buf
XCTAssertTrue(self.eventLoop.inEventLoop)
XCTAssertEqual(5, buf.readableBytes)
XCTAssertEqual("01234", buf.readString(length: buf.readableBytes) ?? "bad")
numCalls += 1
return self.eventLoop.makeSucceededFuture(())
}.wait()
}
XCTAssertEqual(1, numCalls)
}
func testFileRegionReadFromPipeFails() throws {
try withPipe { readFH, writeFH in
try! writeFH.withUnsafeFileDescriptor { writeFD in
_ = try! Posix.write(descriptor: writeFD, pointer: "ABC", size: 3)
}
let fr = FileRegion(fileHandle: readFH, readerIndex: 1, endIndex: 2)
XCTAssertThrowsError(try self.fileIO.readChunked(fileRegion: fr,
chunkSize: 10,
allocator: self.allocator,
eventLoop: self.eventLoop) { buf in
XCTFail("this shouldn't have been called")
return self.eventLoop.makeSucceededFuture(())
}.wait()) { error in
XCTAssertEqual(ESPIPE, (error as? IOError)?.errnoCode)
}
return [readFH, writeFH]
}
}
func testReadFromNonBlockingPipeFails() throws {
try withPipe { readFH, writeFH in
do {
try readFH.withUnsafeFileDescriptor { readFD in
let flags = try Posix.fcntl(descriptor: readFD, command: F_GETFL, value: 0)
let ret = try Posix.fcntl(descriptor: readFD, command: F_SETFL, value: flags | O_NONBLOCK)
assert(ret == 0, "unexpectedly, fcntl(\(readFD), F_SETFL, O_NONBLOCK) returned \(ret)")
}
try self.fileIO.readChunked(fileHandle: readFH,
byteCount: 10,
chunkSize: 10,
allocator: self.allocator,
eventLoop: self.eventLoop) { buf in
XCTFail("this shouldn't have been called")
return self.eventLoop.makeSucceededFuture(())
}.wait()
XCTFail("succeeded and shouldn't have")
} catch let e as NonBlockingFileIO.Error where e == NonBlockingFileIO.Error.descriptorSetToNonBlocking {
// OK
} catch {
XCTFail("wrong error \(error) caught")
}
return [readFH, writeFH]
}
}
func testSeekPointerIsSetToFront() throws {
let content = "0123456789"
var numCalls = 0
try withTemporaryFile(content: content) { (fileHandle, path) -> Void in
try self.fileIO.readChunked(fileHandle: fileHandle,
byteCount: content.utf8.count,
chunkSize: 9,
allocator: self.allocator,
eventLoop: self.eventLoop) { buf in
var buf = buf
numCalls += 1
XCTAssertTrue(self.eventLoop.inEventLoop)
if numCalls == 1 {
XCTAssertEqual(9, buf.readableBytes)
XCTAssertEqual("012345678", buf.readString(length: buf.readableBytes) ?? "bad")
} else {
XCTAssertEqual(1, buf.readableBytes)
XCTAssertEqual("9", buf.readString(length: buf.readableBytes) ?? "bad")
}
return self.eventLoop.makeSucceededFuture(())
}.wait()
}
XCTAssertEqual(2, numCalls)
}
func testReadingFileSize() throws {
try withTemporaryFile(content: "0123456789") { (fileHandle, _) -> Void in
let size = try self.fileIO.readFileSize(fileHandle: fileHandle,
eventLoop: eventLoop).wait()
XCTAssertEqual(size, 10)
}
}
func testChangeFileSizeShrink() throws {
try withTemporaryFile(content: "0123456789") { (fileHandle, _) -> Void in
try self.fileIO.changeFileSize(fileHandle: fileHandle,
size: 1,
eventLoop: eventLoop).wait()
let fileRegion = try FileRegion(fileHandle: fileHandle)
var buf = try self.fileIO.read(fileRegion: fileRegion,
allocator: allocator,
eventLoop: eventLoop).wait()
XCTAssertEqual("0", buf.readString(length: buf.readableBytes))
}
}
func testChangeFileSizeGrow() throws {
try withTemporaryFile(content: "0123456789") { (fileHandle, _) -> Void in
try self.fileIO.changeFileSize(fileHandle: fileHandle,
size: 100,
eventLoop: eventLoop).wait()
let fileRegion = try FileRegion(fileHandle: fileHandle)
var buf = try self.fileIO.read(fileRegion: fileRegion,
allocator: allocator,
eventLoop: eventLoop).wait()
let zeros = (1...90).map { _ in UInt8(0) }
guard let bytes = buf.readBytes(length: buf.readableBytes)?.suffix(from: 10) else {
XCTFail("readBytes(length:) should not be nil")
return
}
XCTAssertEqual(zeros, Array(bytes))
}
}
func testWriting() throws {
var buffer = allocator.buffer(capacity: 3)
buffer.writeStaticString("123")
try withTemporaryFile(content: "") { (fileHandle, path) in
try self.fileIO.write(fileHandle: fileHandle,
buffer: buffer,
eventLoop: self.eventLoop).wait()
let offset = try fileHandle.withUnsafeFileDescriptor {
try Posix.lseek(descriptor: $0, offset: 0, whence: SEEK_SET)
}
XCTAssertEqual(offset, 0)
let readBuffer = try self.fileIO.read(fileHandle: fileHandle,
byteCount: 3,
allocator: self.allocator,
eventLoop: self.eventLoop).wait()
XCTAssertEqual(readBuffer.getString(at: 0, length: 3), "123")
}
}
func testWriteMultipleTimes() throws {
var buffer = allocator.buffer(capacity: 3)
buffer.writeStaticString("xxx")
try withTemporaryFile(content: "AAA") { (fileHandle, path) in
for i in 0 ..< 3 {
buffer.writeString("\(i)")
try self.fileIO.write(fileHandle: fileHandle,
buffer: buffer,
eventLoop: self.eventLoop).wait()
}
let offset = try fileHandle.withUnsafeFileDescriptor {
try Posix.lseek(descriptor: $0, offset: 0, whence: SEEK_SET)
}
XCTAssertEqual(offset, 0)
let expectedOutput = "xxx0xxx01xxx012"
let readBuffer = try self.fileIO.read(fileHandle: fileHandle,
byteCount: expectedOutput.utf8.count,
allocator: self.allocator,
eventLoop: self.eventLoop).wait()
XCTAssertEqual(expectedOutput, String(decoding: readBuffer.readableBytesView, as: Unicode.UTF8.self))
}
}
func testWritingWithOffset() throws {
var buffer = allocator.buffer(capacity: 3)
buffer.writeStaticString("123")
try withTemporaryFile(content: "hello") { (fileHandle, _) -> Void in
try self.fileIO.write(fileHandle: fileHandle,
toOffset: 1,
buffer: buffer,
eventLoop: eventLoop).wait()
let offset = try fileHandle.withUnsafeFileDescriptor {
try Posix.lseek(descriptor: $0, offset: 0, whence: SEEK_SET)
}
XCTAssertEqual(offset, 0)
var readBuffer = try self.fileIO.read(fileHandle: fileHandle,
byteCount: 5,
allocator: self.allocator,
eventLoop: self.eventLoop).wait()
XCTAssertEqual(5, readBuffer.readableBytes)
XCTAssertEqual("h123o", readBuffer.readString(length: readBuffer.readableBytes))
}
}
// This is undefined behavior and may cause different
// results on other platforms. Please add #if:s according
// to platform requirements.
func testWritingBeyondEOF() throws {
var buffer = allocator.buffer(capacity: 3)
buffer.writeStaticString("123")
try withTemporaryFile(content: "hello") { (fileHandle, _) -> Void in
try self.fileIO.write(fileHandle: fileHandle,
toOffset: 6,
buffer: buffer,
eventLoop: eventLoop).wait()
let fileRegion = try FileRegion(fileHandle: fileHandle)
var buf = try self.fileIO.read(fileRegion: fileRegion,
allocator: self.allocator,
eventLoop: self.eventLoop).wait()
XCTAssertEqual(9, buf.readableBytes)
XCTAssertEqual("hello", buf.readString(length: 5))
XCTAssertEqual([ UInt8(0) ], buf.readBytes(length: 1))
XCTAssertEqual("123", buf.readString(length: buf.readableBytes))
}
}
func testFileOpenWorks() throws {
let content = "123"
try withTemporaryFile(content: content) { (fileHandle, path) -> Void in
let (fh, fr) = try self.fileIO.openFile(path: path, eventLoop: self.eventLoop).wait()
try fh.withUnsafeFileDescriptor { fd in
XCTAssertGreaterThanOrEqual(fd, 0)
}
XCTAssertTrue(fh.isOpen)
XCTAssertEqual(0, fr.readerIndex)
XCTAssertEqual(3, fr.endIndex)
try fh.close()
}
}
func testFileOpenWorksWithEmptyFile() throws {
let content = ""
try withTemporaryFile(content: content) { (fileHandle, path) -> Void in
let (fh, fr) = try self.fileIO.openFile(path: path, eventLoop: self.eventLoop).wait()
try fh.withUnsafeFileDescriptor { fd in
XCTAssertGreaterThanOrEqual(fd, 0)
}
XCTAssertTrue(fh.isOpen)
XCTAssertEqual(0, fr.readerIndex)
XCTAssertEqual(0, fr.endIndex)
try fh.close()
}
}
func testFileOpenFails() throws {
do {
_ = try self.fileIO.openFile(path: "/dev/null/this/does/not/exist", eventLoop: self.eventLoop).wait()
XCTFail("should've thrown")
} catch let e as IOError where e.errnoCode == ENOTDIR {
// OK
} catch {
XCTFail("wrong error: \(error)")
}
}
func testOpeningFilesForWriting() {
XCTAssertNoThrow(try withTemporaryDirectory { dir in
try self.fileIO!.openFile(path: "\(dir)/file",
mode: .write,
flags: .allowFileCreation(),
eventLoop: self.eventLoop).wait().close()
})
}
func testOpeningFilesForWritingFailsIfWeDontAllowItExplicitly() {
XCTAssertThrowsError(try withTemporaryDirectory { dir in
try self.fileIO!.openFile(path: "\(dir)/file",
mode: .write,
flags: .default,
eventLoop: self.eventLoop).wait().close()
}) { error in
XCTAssertEqual(ENOENT, (error as? IOError)?.errnoCode)
}
}
func testOpeningFilesForWritingDoesNotAllowReading() {
XCTAssertNoThrow(try withTemporaryDirectory { dir in
let fileHandle = try self.fileIO!.openFile(path: "\(dir)/file",
mode: .write,
flags: .allowFileCreation(),
eventLoop: self.eventLoop).wait()
defer {
try! fileHandle.close()
}
XCTAssertEqual(-1 /* read must fail */,
try fileHandle.withUnsafeFileDescriptor { fd -> ssize_t in
var data: UInt8 = 0
return withUnsafeMutableBytes(of: &data) { ptr in
read(fd, ptr.baseAddress, ptr.count)
}
})
})
}
func testOpeningFilesForWritingAndReading() {
XCTAssertNoThrow(try withTemporaryDirectory { dir in
let fileHandle = try self.fileIO!.openFile(path: "\(dir)/file",
mode: [.write, .read],
flags: .allowFileCreation(),
eventLoop: self.eventLoop).wait()
defer {
try! fileHandle.close()
}
XCTAssertEqual(0 /* read should read EOF */,
try fileHandle.withUnsafeFileDescriptor { fd -> ssize_t in
var data: UInt8 = 0
return withUnsafeMutableBytes(of: &data) { ptr in
read(fd, ptr.baseAddress, ptr.count)
}
})
})
}
func testOpeningFilesForWritingDoesNotImplyTruncation() {
XCTAssertNoThrow(try withTemporaryDirectory { dir in
// open 1 + write
try {
let fileHandle = try self.fileIO!.openFile(path: "\(dir)/file",
mode: [.write, .read],
flags: .allowFileCreation(),
eventLoop: self.eventLoop).wait()
defer {
try! fileHandle.close()
}
try fileHandle.withUnsafeFileDescriptor { fd in
var data = UInt8(ascii: "X")
XCTAssertEqual(IOResult<Int>.processed(1),
try withUnsafeBytes(of: &data) { ptr in
try Posix.write(descriptor: fd, pointer: ptr.baseAddress!, size: ptr.count)
})
}
}()
// open 2 + write again + read
try {
let fileHandle = try self.fileIO!.openFile(path: "\(dir)/file",
mode: [.write, .read],
flags: .default,
eventLoop: self.eventLoop).wait()
defer {
try! fileHandle.close()
}
try fileHandle.withUnsafeFileDescriptor { fd in
try Posix.lseek(descriptor: fd, offset: 0, whence: SEEK_END)
var data = UInt8(ascii: "Y")
XCTAssertEqual(IOResult<Int>.processed(1),
try withUnsafeBytes(of: &data) { ptr in
try Posix.write(descriptor: fd, pointer: ptr.baseAddress!, size: ptr.count)
})
}
XCTAssertEqual(2 /* both bytes */,
try fileHandle.withUnsafeFileDescriptor { fd -> ssize_t in
var data: UInt16 = 0
try Posix.lseek(descriptor: fd, offset: 0, whence: SEEK_SET)
let readReturn = withUnsafeMutableBytes(of: &data) { ptr in
read(fd, ptr.baseAddress, ptr.count)
}
XCTAssertEqual(UInt16(bigEndian: (UInt16(UInt8(ascii: "X")) << 8) | UInt16(UInt8(ascii: "Y"))),
data)
return readReturn
})
}()
})
}
func testOpeningFilesForWritingCanUseTruncation() {
XCTAssertNoThrow(try withTemporaryDirectory { dir in
// open 1 + write
try {
let fileHandle = try self.fileIO!.openFile(path: "\(dir)/file",
mode: [.write, .read],
flags: .allowFileCreation(),
eventLoop: self.eventLoop).wait()
defer {
try! fileHandle.close()
}
try fileHandle.withUnsafeFileDescriptor { fd in
var data = UInt8(ascii: "X")
XCTAssertEqual(IOResult<Int>.processed(1),
try withUnsafeBytes(of: &data) { ptr in
try Posix.write(descriptor: fd, pointer: ptr.baseAddress!, size: ptr.count)
})
}
}()
// open 2 (with truncation) + write again + read
try {
let fileHandle = try self.fileIO!.openFile(path: "\(dir)/file",
mode: [.write, .read],
flags: .posix(flags: O_TRUNC, mode: 0),
eventLoop: self.eventLoop).wait()
defer {
try! fileHandle.close()
}
try fileHandle.withUnsafeFileDescriptor { fd in
try Posix.lseek(descriptor: fd, offset: 0, whence: SEEK_END)
var data = UInt8(ascii: "Y")
XCTAssertEqual(IOResult<Int>.processed(1),
try withUnsafeBytes(of: &data) { ptr in
try Posix.write(descriptor: fd, pointer: ptr.baseAddress!, size: ptr.count)
})
}
XCTAssertEqual(1 /* read should read just one byte because we truncated the file */,
try fileHandle.withUnsafeFileDescriptor { fd -> ssize_t in
var data: UInt16 = 0
try Posix.lseek(descriptor: fd, offset: 0, whence: SEEK_SET)
let readReturn = withUnsafeMutableBytes(of: &data) { ptr in
read(fd, ptr.baseAddress, ptr.count)
}
XCTAssertEqual(UInt16(bigEndian: UInt16(UInt8(ascii: "Y")) << 8), data)
return readReturn
})
}()
})
}
func testReadFromOffset() {
XCTAssertNoThrow(try withTemporaryFile(content: "hello world") { (fileHandle, path) in
let buffer = self.fileIO.read(fileHandle: fileHandle,
fromOffset: 6,
byteCount: 5,
allocator: ByteBufferAllocator(),
eventLoop: self.eventLoop)
XCTAssertNoThrow(try XCTAssertEqual("world",
String(decoding: buffer.wait().readableBytesView,
as: Unicode.UTF8.self)))
})
}
func testReadChunkedFromOffset() {
XCTAssertNoThrow(try withTemporaryFile(content: "hello world") { (fileHandle, path) in
var numberOfCalls = 0
try self.fileIO.readChunked(fileHandle: fileHandle,
fromOffset: 6,
byteCount: 5,
chunkSize: 2,
allocator: .init(),
eventLoop: self.eventLoop) { buffer in
numberOfCalls += 1
switch numberOfCalls {
case 1:
XCTAssertEqual("wo", String(decoding: buffer.readableBytesView, as: Unicode.UTF8.self))
case 2:
XCTAssertEqual("rl", String(decoding: buffer.readableBytesView, as: Unicode.UTF8.self))
case 3:
XCTAssertEqual("d", String(decoding: buffer.readableBytesView, as: Unicode.UTF8.self))
default:
XCTFail()
}
return self.eventLoop.makeSucceededFuture(())
}.wait()
})
}
func testReadChunkedFromNegativeOffsetFails() {
XCTAssertThrowsError(try withTemporaryFile(content: "hello world") { (fileHandle, path) in
try self.fileIO.readChunked(fileHandle: fileHandle,
fromOffset: -1,
byteCount: 5,
chunkSize: 2,
allocator: .init(),
eventLoop: self.eventLoop) { _ in
XCTFail("shouldn't be called")
return self.eventLoop.makeSucceededFuture(())
}.wait()
}) { error in
XCTAssertEqual(EINVAL, (error as? IOError)?.errnoCode)
}
}
func testReadChunkedFromOffsetAfterEOFDeliversExactlyOneChunk() {
var numberOfCalls = 0
XCTAssertNoThrow(try withTemporaryFile(content: "hello world") { (fileHandle, path) in
try self.fileIO.readChunked(fileHandle: fileHandle,
fromOffset: 100,
byteCount: 5,
chunkSize: 2,
allocator: .init(),
eventLoop: self.eventLoop) { buffer in
numberOfCalls += 1
XCTAssertEqual(1, numberOfCalls)
XCTAssertEqual(0, buffer.readableBytes)
return self.eventLoop.makeSucceededFuture(())
}.wait()
})
}
func testReadChunkedFromEOFDeliversExactlyOneChunk() {
var numberOfCalls = 0
XCTAssertNoThrow(try withTemporaryFile(content: "") { (fileHandle, path) in
try self.fileIO.readChunked(fileHandle: fileHandle,
byteCount: 5,
chunkSize: 2,
allocator: .init(),
eventLoop: self.eventLoop) { buffer in
numberOfCalls += 1
XCTAssertEqual(1, numberOfCalls)
XCTAssertEqual(0, buffer.readableBytes)
return self.eventLoop.makeSucceededFuture(())
}.wait()
})
}
func testReadFromOffsetAfterEOFDeliversExactlyOneChunk() {
XCTAssertNoThrow(try withTemporaryFile(content: "hello world") { (fileHandle, path) in
XCTAssertEqual(0,
try self.fileIO.read(fileHandle: fileHandle,
fromOffset: 100,
byteCount: 5,
allocator: .init(),
eventLoop: self.eventLoop).wait().readableBytes)
})
}
func testReadFromEOFDeliversExactlyOneChunk() {
XCTAssertNoThrow(try withTemporaryFile(content: "") { (fileHandle, path) in
XCTAssertEqual(0,
try self.fileIO.read(fileHandle: fileHandle,
byteCount: 5,
allocator: .init(),
eventLoop: self.eventLoop).wait().readableBytes)
})
}
func testReadChunkedFromOffsetFileRegion() {
XCTAssertNoThrow(try withTemporaryFile(content: "hello world") { (fileHandle, path) in
var numberOfCalls = 0
let fileRegion = FileRegion(fileHandle: fileHandle, readerIndex: 6, endIndex: 11)
try self.fileIO.readChunked(fileRegion: fileRegion,
chunkSize: 2,
allocator: .init(),
eventLoop: self.eventLoop) { buffer in
numberOfCalls += 1
switch numberOfCalls {
case 1:
XCTAssertEqual("wo", String(decoding: buffer.readableBytesView, as: Unicode.UTF8.self))
case 2:
XCTAssertEqual("rl", String(decoding: buffer.readableBytesView, as: Unicode.UTF8.self))
case 3:
XCTAssertEqual("d", String(decoding: buffer.readableBytesView, as: Unicode.UTF8.self))
default:
XCTFail()
}
return self.eventLoop.makeSucceededFuture(())
}.wait()
})
}
func testReadManyChunks() {
let numberOfChunks = 2_000
XCTAssertNoThrow(try withTemporaryFile(content: String(repeating: "X",
count: numberOfChunks)) { (fileHandle, path) in
let numberOfCalls = NIOAtomic<Int>.makeAtomic(value: 0)
XCTAssertNoThrow(try self.fileIO.readChunked(fileHandle: fileHandle,
fromOffset: 0,
byteCount: numberOfChunks,
chunkSize: 1,
allocator: self.allocator,
eventLoop: self.eventLoop) { buffer in
_ = numberOfCalls.add(1)
XCTAssertEqual(1, buffer.readableBytes)
XCTAssertEqual(UInt8(ascii: "X"), buffer.readableBytesView.first)
return self.eventLoop.makeSucceededFuture(())
}.wait())
XCTAssertEqual(numberOfChunks, numberOfCalls.load())
})
}
func testThrowsErrorOnUnstartedPool() throws {
withTemporaryFile(content: "hello, world") { fileHandle, path in
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer {
XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully())
}
let expectation = XCTestExpectation(description: "Opened file")
let threadPool = NIOThreadPool(numberOfThreads: 1)
let fileIO = NonBlockingFileIO(threadPool: threadPool)
fileIO.openFile(path: path, eventLoop: eventLoopGroup.next()).whenFailure { (error) in
XCTAssertTrue(error is NIOThreadPoolError.ThreadPoolInactive)
expectation.fulfill()
}
self.wait(for: [expectation], timeout: 1.0)
}
}
}
|