|
21 | 21 | // THE SOFTWARE. |
22 | 22 | // |
23 | 23 |
|
24 | | -import AlgoliaSearchOffline |
| 24 | +@testable import AlgoliaSearchOffline |
25 | 25 | import XCTest |
26 | 26 |
|
27 | 27 |
|
@@ -353,4 +353,177 @@ class MirroredIndexTests: OfflineTestCase { |
353 | 353 | } |
354 | 354 | waitForExpectations(timeout: onlineExpectationTimeout, handler: nil) |
355 | 355 | } |
| 356 | + |
| 357 | + /// Test the `onlineOnly` request strategy. |
| 358 | + /// |
| 359 | + func testRequestStrategyOnlineOnly() { |
| 360 | + let expectation = self.expectation(description: #function) |
| 361 | + |
| 362 | + // Mock reachability. |
| 363 | + let reachability = MockNetworkReachability() |
| 364 | + client.reachability = reachability |
| 365 | + |
| 366 | + // Populate the online index & sync the offline mirror. |
| 367 | + let index: MirroredIndex = client.index(withName: safeIndexName(#function)) |
| 368 | + index.requestStrategy = .onlineOnly |
| 369 | + sync(index: index) { (error) in |
| 370 | + if let error = error { XCTFail("\(error)"); return } |
| 371 | + |
| 372 | + // Test success. |
| 373 | + index.search(Query()) { (content, error) in |
| 374 | + XCTAssertNil(error) |
| 375 | + XCTAssertEqual(5, content?["nbHits"] as? Int) |
| 376 | + XCTAssertEqual("remote", content?["origin"] as? String) |
| 377 | + |
| 378 | + // Test that reachability is observed. |
| 379 | + reachability.reachable = false |
| 380 | + let startTime = Date() |
| 381 | + index.search(Query()) { (content, error) in |
| 382 | + let stopTime = Date() |
| 383 | + let duration = stopTime.timeIntervalSince(startTime) |
| 384 | + guard let error = error as? NSError else { XCTFail("Request should have failed"); expectation.fulfill(); return } |
| 385 | + XCTAssertEqual(NSURLErrorDomain, error.domain) |
| 386 | + XCTAssertEqual(NSURLErrorNotConnectedToInternet, error.code) |
| 387 | + XCTAssert(duration < min(self.client.searchTimeout, self.client.timeout)) // check that we failed without waiting for the timeout |
| 388 | + |
| 389 | + // Test real network failure. |
| 390 | + reachability.reachable = true |
| 391 | + self.client.readHosts = [ "unknown.algolia.com" ] |
| 392 | + index.search(Query()) { (content, error) in |
| 393 | + guard let error = error as? NSError else { XCTFail("Request should have failed"); expectation.fulfill(); return } |
| 394 | + XCTAssertEqual(NSURLErrorDomain, error.domain) |
| 395 | + // Check that we failed with something else than a connectivity error caused by reachability. |
| 396 | + XCTAssertNotEqual(NSURLErrorNotConnectedToInternet, error.code) |
| 397 | + |
| 398 | + expectation.fulfill() |
| 399 | + } |
| 400 | + } |
| 401 | + } |
| 402 | + } |
| 403 | + waitForExpectations(timeout: onlineExpectationTimeout, handler: nil) |
| 404 | + } |
| 405 | + |
| 406 | + /// Test the `offlineOnly` request strategy. |
| 407 | + /// |
| 408 | + func testRequestStrategyOfflineOnly() { |
| 409 | + let expectation = self.expectation(description: #function) |
| 410 | + |
| 411 | + let index: MirroredIndex = client.index(withName: safeIndexName(#function)) |
| 412 | + index.mirrored = true |
| 413 | + index.requestStrategy = .offlineOnly |
| 414 | + |
| 415 | + // Check that a request without local data fails. |
| 416 | + index.search(Query()) { (content, error) in |
| 417 | + XCTAssertNotNil(error) |
| 418 | + |
| 419 | + // Populate the online index & sync the offline mirror. |
| 420 | + self.sync(index: index) { (error) in |
| 421 | + if let error = error { XCTFail("\(error)"); expectation.fulfill(); return } |
| 422 | + |
| 423 | + // Test success. |
| 424 | + index.search(Query()) { (content, error) in |
| 425 | + XCTAssertNil(error) |
| 426 | + XCTAssertEqual(3, content?["nbHits"] as? Int) |
| 427 | + XCTAssertEqual("local", content?["origin"] as? String) |
| 428 | + |
| 429 | + expectation.fulfill() |
| 430 | + } |
| 431 | + } |
| 432 | + } |
| 433 | + waitForExpectations(timeout: onlineExpectationTimeout, handler: nil) |
| 434 | + } |
| 435 | + |
| 436 | + /// Test the `fallbackOnFailure` request strategy. |
| 437 | + /// |
| 438 | + func testRequestStrategyFallbackOnFailure() { |
| 439 | + let expectation = self.expectation(description: #function) |
| 440 | + |
| 441 | + // Mock reachability. |
| 442 | + let reachability = MockNetworkReachability() |
| 443 | + client.reachability = reachability |
| 444 | + |
| 445 | + // Populate the online index & sync the offline mirror. |
| 446 | + let index: MirroredIndex = client.index(withName: safeIndexName(#function)) |
| 447 | + index.requestStrategy = .fallbackOnFailure |
| 448 | + sync(index: index) { (error) in |
| 449 | + if let error = error { XCTFail("\(error)"); return } |
| 450 | + |
| 451 | + // Test success. |
| 452 | + index.search(Query()) { (content, error) in |
| 453 | + XCTAssertNil(error) |
| 454 | + XCTAssertEqual(5, content?["nbHits"] as? Int) |
| 455 | + XCTAssertEqual("remote", content?["origin"] as? String) |
| 456 | + |
| 457 | + // Test that reachability is observed. |
| 458 | + reachability.reachable = false |
| 459 | + index.search(Query()) { (content, error) in |
| 460 | + XCTAssertNil(error) |
| 461 | + XCTAssertEqual(3, content?["nbHits"] as? Int) |
| 462 | + XCTAssertEqual("local", content?["origin"] as? String) |
| 463 | + |
| 464 | + // Test real network failure. |
| 465 | + reachability.reachable = true |
| 466 | + self.client.readHosts = [ "unknown.algolia.com" ] |
| 467 | + index.search(Query()) { (content, error) in |
| 468 | + XCTAssertNil(error) |
| 469 | + XCTAssertEqual(3, content?["nbHits"] as? Int) |
| 470 | + XCTAssertEqual("local", content?["origin"] as? String) |
| 471 | + |
| 472 | + expectation.fulfill() |
| 473 | + } |
| 474 | + } |
| 475 | + } |
| 476 | + } |
| 477 | + waitForExpectations(timeout: onlineExpectationTimeout, handler: nil) |
| 478 | + } |
| 479 | + |
| 480 | + /// Test the `fallbackOnTimeout` request strategy. |
| 481 | + /// |
| 482 | + func testRequestStrategyFallbackOnTimeout() { |
| 483 | + let expectation = self.expectation(description: #function) |
| 484 | + |
| 485 | + // Mock reachability. |
| 486 | + let reachability = MockNetworkReachability() |
| 487 | + client.reachability = reachability |
| 488 | + |
| 489 | + // Populate the online index & sync the offline mirror. |
| 490 | + let index: MirroredIndex = client.index(withName: safeIndexName(#function)) |
| 491 | + index.requestStrategy = .fallbackOnTimeout |
| 492 | + sync(index: index) { (error) in |
| 493 | + if let error = error { XCTFail("\(error)"); return } |
| 494 | + |
| 495 | + // Test success. |
| 496 | + index.search(Query()) { (content, error) in |
| 497 | + XCTAssertNil(error) |
| 498 | + XCTAssertEqual(5, content?["nbHits"] as? Int) |
| 499 | + XCTAssertEqual("remote", content?["origin"] as? String) |
| 500 | + |
| 501 | + // Test that reachability is observed. |
| 502 | + reachability.reachable = false |
| 503 | + index.search(Query()) { (content, error) in |
| 504 | + XCTAssertNil(error) |
| 505 | + XCTAssertEqual(3, content?["nbHits"] as? Int) |
| 506 | + XCTAssertEqual("local", content?["origin"] as? String) |
| 507 | + |
| 508 | + // Test real network failure. |
| 509 | + reachability.reachable = true |
| 510 | + self.client.readHosts = [ uniqueAlgoliaBizHost() ] |
| 511 | + let startTime = Date() |
| 512 | + index.search(Query()) { (content, error) in |
| 513 | + let stopTime = Date() |
| 514 | + let duration = stopTime.timeIntervalSince(startTime) |
| 515 | + XCTAssertNil(error) |
| 516 | + XCTAssertEqual(3, content?["nbHits"] as? Int) |
| 517 | + XCTAssertEqual("local", content?["origin"] as? String) |
| 518 | + // Check that we hit the fallback time out, but not the complete online timeout. |
| 519 | + XCTAssert(duration >= index.offlineFallbackTimeout) |
| 520 | + XCTAssert(duration < min(self.client.searchTimeout, self.client.timeout)) |
| 521 | + |
| 522 | + expectation.fulfill() |
| 523 | + } |
| 524 | + } |
| 525 | + } |
| 526 | + } |
| 527 | + waitForExpectations(timeout: onlineExpectationTimeout, handler: nil) |
| 528 | + } |
356 | 529 | } |
0 commit comments