|
| 1 | +/** |
| 2 | +* Copyright IBM Corporation 2016, 2018 |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +**/ |
| 16 | + |
| 17 | +import XCTest |
| 18 | +#if os(Linux) |
| 19 | + import Glibc |
| 20 | +#else |
| 21 | + import Darwin |
| 22 | +#endif |
| 23 | +import Foundation |
| 24 | + |
| 25 | +@testable import CouchDB |
| 26 | + |
| 27 | +class CouchDBTest: XCTestCase { |
| 28 | + |
| 29 | + // MARK: - Database connection properties |
| 30 | + |
| 31 | + // The database name should be defined in an environment variable TESTDB_NAME |
| 32 | + // in Travis, to allow each Travis build to use a separate database. |
| 33 | + let dbName = ProcessInfo.processInfo.environment["TESTDB_NAME"] ?? "Error-TESTDB_NAME-not-set" |
| 34 | + |
| 35 | + let couchDBClient: CouchDBClient! = { |
| 36 | + let credentials = Utils.readCredentials() |
| 37 | + |
| 38 | + // Connection properties for testing Cloudant or CouchDB instance |
| 39 | + let connProperties = ConnectionProperties(host: credentials.host, |
| 40 | + port: credentials.port, |
| 41 | + secured: true, |
| 42 | + username: credentials.username, |
| 43 | + password: credentials.password) |
| 44 | + |
| 45 | + // Create couchDBClient instance using connection properties |
| 46 | + let client = CouchDBClient(connectionProperties: connProperties) |
| 47 | + |
| 48 | + print("Hostname is: \(client.connProperties.host)") |
| 49 | + |
| 50 | + return client |
| 51 | + }() |
| 52 | + |
| 53 | + var database: Database? |
| 54 | + |
| 55 | + // MARK: - Initializers and test set-up and tear-down |
| 56 | + |
| 57 | + override func setUp() { |
| 58 | + delay(dropDatabaseIfExists) |
| 59 | + } |
| 60 | + |
| 61 | + override func tearDown() { |
| 62 | + delay(dropDatabaseIfExists) |
| 63 | + } |
| 64 | + |
| 65 | + /// Drop the test database, if it exists. |
| 66 | + /// |
| 67 | + func dropDatabaseIfExists() { |
| 68 | + // Check if DB exists |
| 69 | + couchDBClient.dbExists(dbName) { exists, error in |
| 70 | + if error != nil { |
| 71 | + XCTFail("Failed checking existence of database \(self.dbName). Error=\(error!.localizedDescription)") |
| 72 | + } else { |
| 73 | + if exists { |
| 74 | + self.dropDatabase() |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /// Create the test database, failing the test if it already exists, or if there |
| 81 | + /// is a connectivity error. |
| 82 | + /// |
| 83 | + func createDatabase() { |
| 84 | + delay(delayedCreateDatabase) |
| 85 | + } |
| 86 | + |
| 87 | + /// Drop the test database, failing the test if it does not exist, or if there |
| 88 | + /// is a connectivity error. |
| 89 | + /// |
| 90 | + func dropDatabase() { |
| 91 | + delay(delayedDropDatabase) |
| 92 | + } |
| 93 | + |
| 94 | + /// Delay an action by a specified amount of time (default 1 second). The purpose |
| 95 | + /// of delaying actions in CouchDB tests is to avoid exceeding the API limit for |
| 96 | + /// the test database provider while running multiple executions via Travis. |
| 97 | + /// |
| 98 | + func delay(time: Double = 1.0, _ work: @escaping () -> Void) { |
| 99 | + let start = DispatchSemaphore(value: 0) |
| 100 | + let end = DispatchSemaphore(value: 0) |
| 101 | + DispatchQueue.global().asyncAfter(deadline: .now() + time) { |
| 102 | + start.wait() |
| 103 | + work() |
| 104 | + end.signal() |
| 105 | + } |
| 106 | + start.signal() |
| 107 | + end.wait() |
| 108 | + } |
| 109 | + |
| 110 | + fileprivate func delayedCreateDatabase() { |
| 111 | + couchDBClient.createDB(dbName) { database, error in |
| 112 | + if let error = error { |
| 113 | + XCTFail("DB creation error: \(error.code) \(error.localizedDescription)") |
| 114 | + return |
| 115 | + } |
| 116 | + if database == nil { |
| 117 | + XCTFail("Created database is nil") |
| 118 | + return |
| 119 | + } |
| 120 | + print("Database \"\(self.dbName)\" successfully created") |
| 121 | + self.database = database |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + fileprivate func delayedDropDatabase() { |
| 126 | + // Retrieve and delete test database |
| 127 | + couchDBClient.deleteDB(dbName) { error in |
| 128 | + if let error = error { |
| 129 | + XCTFail("DB deletion error: \(error.code) \(error.localizedDescription)") |
| 130 | + return |
| 131 | + } |
| 132 | + print("Database \"\(self.dbName)\" successfully deleted") |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | +} |
| 137 | + |
0 commit comments