Skip to content
This repository was archived by the owner on Jan 31, 2022. It is now read-only.

Commit 7962e57

Browse files
author
Clément Le Provost
committed
[offline][test] Test the various request strategies
1 parent 189e725 commit 7962e57

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed

algoliasearch/src/testOffline/java/com/algolia/search/saas/MirroredIndexTest.java

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.Arrays;
3838
import java.util.HashMap;
3939
import java.util.Map;
40+
import java.util.UUID;
4041
import java.util.concurrent.CountDownLatch;
4142
import java.util.concurrent.TimeUnit;
4243

@@ -480,6 +481,176 @@ public void doRequestCompleted(JSONObject content, AlgoliaException error) {
480481
});
481482
}
482483

484+
/**
485+
* Test the `ONLINE_ONLY` request strategy.
486+
*/
487+
@Test
488+
public void testRequestStrategyOnlineOnly() {
489+
final CountDownLatch signal = new CountDownLatch(1);
490+
491+
// Populate the online index & sync the offline mirror.
492+
final MirroredIndex index = client.getIndex(Helpers.safeIndexName(Helpers.getMethodName()));
493+
index.setRequestStrategy(MirroredIndex.Strategy.ONLINE_ONLY);
494+
sync(index, new SyncCompletionHandler() {
495+
@Override
496+
public void syncCompleted(@Nullable Throwable error) {
497+
assertNull(error);
498+
499+
// Test success.
500+
index.searchAsync(new Query(), new AssertCompletionHandler() {
501+
@Override
502+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
503+
assertNull(error);
504+
assertEquals(5, content.optInt("nbHits"));
505+
assertEquals("remote", content.optString("origin"));
506+
507+
// Test failure.
508+
client.setReadHosts("unknown.algolia.com");
509+
index.searchAsync(new Query(), new AssertCompletionHandler() {
510+
@Override
511+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
512+
assertNotNull(error);
513+
signal.countDown();
514+
}
515+
});
516+
}
517+
});
518+
}
519+
});
520+
}
521+
522+
/**
523+
* Test the `OFFLINE_ONLY` request strategy.
524+
*/
525+
@Test
526+
public void testRequestStrategyOfflineOnly() {
527+
final CountDownLatch signal = new CountDownLatch(1);
528+
529+
final MirroredIndex index = client.getIndex(Helpers.safeIndexName(Helpers.getMethodName()));
530+
index.setMirrored(true);
531+
index.setRequestStrategy(MirroredIndex.Strategy.OFFLINE_ONLY);
532+
533+
// Check that a request without local data fails.
534+
index.searchAsync(new Query(), new AssertCompletionHandler() {
535+
@Override
536+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
537+
assertNotNull(error);
538+
539+
// Populate the online index & sync the offline mirror.
540+
sync(index, new SyncCompletionHandler() {
541+
@Override
542+
public void syncCompleted(@Nullable Throwable error) {
543+
assertNull(error);
544+
545+
// Test success.
546+
index.searchAsync(new Query(), new AssertCompletionHandler() {
547+
@Override
548+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
549+
assertNull(error);
550+
assertEquals(3, content.optInt("nbHits"));
551+
assertEquals("local", content.optString("origin"));
552+
553+
signal.countDown();
554+
}
555+
});
556+
}
557+
});
558+
}
559+
});
560+
}
561+
562+
/**
563+
* Test the `FALLBACK_ON_FAILURE` request strategy.
564+
*/
565+
@Test
566+
public void testRequestStrategyFallbackOnFailure() {
567+
final CountDownLatch signal = new CountDownLatch(1);
568+
569+
// Populate the online index & sync the offline mirror.
570+
final MirroredIndex index = client.getIndex(Helpers.safeIndexName(Helpers.getMethodName()));
571+
index.setRequestStrategy(MirroredIndex.Strategy.FALLBACK_ON_FAILURE);
572+
// Populate the online index & sync the offline mirror.
573+
sync(index, new SyncCompletionHandler() {
574+
@Override
575+
public void syncCompleted(@Nullable Throwable error) {
576+
assertNull(error);
577+
578+
// Test online success.
579+
index.searchAsync(new Query(), new AssertCompletionHandler() {
580+
@Override
581+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
582+
assertNull(error);
583+
assertEquals(5, content.optInt("nbHits"));
584+
assertEquals("remote", content.optString("origin"));
585+
586+
// Test network failure.
587+
client.setReadHosts("unknown.algolia.com");
588+
index.searchAsync(new Query(), new AssertCompletionHandler() {
589+
@Override
590+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
591+
assertNull(error);
592+
assertEquals(3, content.optInt("nbHits"));
593+
assertEquals("local", content.optString("origin"));
594+
signal.countDown();
595+
}
596+
});
597+
}
598+
});
599+
}
600+
});
601+
}
602+
603+
/**
604+
* Test the `FALLBACK_ON_TIMEOUT` request strategy.
605+
*/
606+
@Test
607+
public void testRequestStrategyFallbackOnTimeout() {
608+
final CountDownLatch signal = new CountDownLatch(1);
609+
610+
// Populate the online index & sync the offline mirror.
611+
final MirroredIndex index = client.getIndex(Helpers.safeIndexName(Helpers.getMethodName()));
612+
index.setRequestStrategy(MirroredIndex.Strategy.FALLBACK_ON_TIMEOUT);
613+
// Populate the online index & sync the offline mirror.
614+
sync(index, new SyncCompletionHandler() {
615+
@Override
616+
public void syncCompleted(@Nullable Throwable error) {
617+
assertNull(error);
618+
619+
// Test online success.
620+
index.searchAsync(new Query(), new AssertCompletionHandler() {
621+
@Override
622+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
623+
assertNull(error);
624+
assertEquals(5, content.optInt("nbHits"));
625+
assertEquals("remote", content.optString("origin"));
626+
627+
// Test network failure.
628+
final String timeoutingHost = UUID.randomUUID().toString() + ".algolia.biz";
629+
client.setReadHosts(timeoutingHost);
630+
final long startTime = System.currentTimeMillis();
631+
index.searchAsync(new Query(), new AssertCompletionHandler() {
632+
@Override
633+
public void doRequestCompleted(JSONObject content, AlgoliaException error) {
634+
final long stopTime = System.currentTimeMillis();
635+
final long duration = stopTime - startTime;
636+
assertNull(error);
637+
assertEquals(3, content.optInt("nbHits"));
638+
assertEquals("local", content.optString("origin"));
639+
// Check that we hit the fallback time out, but not the complete online timeout.
640+
// NOTE: Those tests cannot be performed because of Robolectric's single-threaded model.
641+
if (false) {
642+
assertTrue(duration >= index.getOfflineFallbackTimeout());
643+
assertTrue(duration < Math.min(client.getSearchTimeout(), client.getReadTimeout()));
644+
}
645+
signal.countDown();
646+
}
647+
});
648+
}
649+
});
650+
}
651+
});
652+
}
653+
483654
/**
484655
* Test that a non-mirrored index behaves like a purely online index.
485656
*/

0 commit comments

Comments
 (0)