CompactionTests.swift 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2017 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. import XCTest
  19. import RealmSwift
  20. // MARK: Expected Sizes
  21. private var expectedTotalBytesBefore = 0
  22. private let expectedUsedBytesBeforeMin = 50000
  23. private var count = 1000
  24. // MARK: Helpers
  25. private func fileSize(path: String) -> Int {
  26. let attributes = try! FileManager.default.attributesOfItem(atPath: path)
  27. return attributes[.size] as! Int
  28. }
  29. // MARK: Tests
  30. class CompactionTests: TestCase {
  31. override func setUp() {
  32. super.setUp()
  33. autoreleasepool {
  34. // Make compactable Realm
  35. let realm = realmWithTestPath()
  36. let uuid = UUID().uuidString
  37. try! realm.write {
  38. realm.create(SwiftStringObject.self, value: ["A"])
  39. for _ in 0..<count {
  40. realm.create(SwiftStringObject.self, value: [uuid])
  41. }
  42. realm.create(SwiftStringObject.self, value: ["B"])
  43. }
  44. }
  45. expectedTotalBytesBefore = fileSize(path: testRealmURL().path)
  46. }
  47. func testSuccessfulCompactOnLaunch() {
  48. // Configure the Realm to compact on launch
  49. let config = Realm.Configuration(fileURL: testRealmURL(),
  50. shouldCompactOnLaunch: { totalBytes, usedBytes in
  51. // Confirm expected sizes
  52. XCTAssertEqual(totalBytes, expectedTotalBytesBefore)
  53. XCTAssert((usedBytes < totalBytes) && (usedBytes > expectedUsedBytesBeforeMin))
  54. // Compact if the file is over 500KB in size and less than 20% 'used'
  55. // In practice, users might want to use values closer to 100MB and 50%
  56. let fiveHundredKB = 500 * 1024
  57. return (totalBytes > fiveHundredKB) && (Double(usedBytes) / Double(totalBytes)) < 0.2
  58. })
  59. // Confirm expected sizes before and after opening the Realm
  60. XCTAssertEqual(fileSize(path: config.fileURL!.path), expectedTotalBytesBefore)
  61. let realm = try! Realm(configuration: config)
  62. XCTAssertLessThan(fileSize(path: config.fileURL!.path), expectedTotalBytesBefore)
  63. // Validate that the file still contains what it should
  64. XCTAssertEqual(realm.objects(SwiftStringObject.self).count, count + 2)
  65. XCTAssertEqual("A", realm.objects(SwiftStringObject.self).first?.stringCol)
  66. XCTAssertEqual("B", realm.objects(SwiftStringObject.self).last?.stringCol)
  67. }
  68. }