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

Commit 08ef007

Browse files
authored
fix(AbstractClient): Don't add existing userAgents
* fix(AbstractClient): Don't add existing userAgents fixes algolia/instantsearch-android#45 * test(AbstractClient): Don't add existing userAgents * style(AbstractClient): Spaces, indent
1 parent de2b848 commit 08ef007

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

algoliasearch/src/main/java/com/algolia/search/saas/AbstractClient.java

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,10 @@ public LibraryVersion(@NonNull String name, @NonNull String version) {
7878

7979
@Override
8080
public boolean equals(Object object) {
81-
if (!(object instanceof LibraryVersion))
81+
if (!(object instanceof LibraryVersion)) {
8282
return false;
83-
LibraryVersion other = (LibraryVersion)object;
83+
}
84+
LibraryVersion other = (LibraryVersion) object;
8485
return this.name.equals(other.name) && this.version.equals(other.version);
8586
}
8687

@@ -150,7 +151,8 @@ private static class HostStatus {
150151
protected ExecutorService searchExecutorService = Executors.newFixedThreadPool(4);
151152

152153
/** Executor used to run completion handlers. By default, runs on the main thread. */
153-
protected @NonNull Executor completionExecutor = new HandlerExecutor(new Handler(Looper.getMainLooper()));
154+
protected @NonNull
155+
Executor completionExecutor = new HandlerExecutor(new Handler(Looper.getMainLooper()));
154156

155157
protected Map<String, WeakReference<Object>> indices = new HashMap<>();
156158

@@ -162,19 +164,21 @@ private static class HostStatus {
162164
* Create a new client.
163165
*
164166
* @param applicationID [optional] The application ID.
165-
* @param apiKey [optional] A valid API key for the service.
166-
* @param readHosts List of hosts for read operations.
167-
* @param writeHosts List of hosts for write operations.
167+
* @param apiKey [optional] A valid API key for the service.
168+
* @param readHosts List of hosts for read operations.
169+
* @param writeHosts List of hosts for write operations.
168170
*/
169171
protected AbstractClient(@Nullable String applicationID, @Nullable String apiKey, @Nullable String[] readHosts, @Nullable String[] writeHosts) {
170172
this.applicationID = applicationID;
171173
this.apiKey = apiKey;
172174
this.addUserAgent(new LibraryVersion("Algolia for Android", version));
173175
this.addUserAgent(new LibraryVersion("Android", Build.VERSION.RELEASE));
174-
if (readHosts != null)
176+
if (readHosts != null) {
175177
setReadHosts(readHosts);
176-
if (writeHosts != null)
178+
}
179+
if (writeHosts != null) {
177180
setWriteHosts(writeHosts);
181+
}
178182
}
179183

180184
// ----------------------------------------------------------------------
@@ -322,7 +326,9 @@ public void setHostDownDelay(int hostDownDelay) {
322326
* @param userAgent The library to add.
323327
*/
324328
public void addUserAgent(@NonNull LibraryVersion userAgent) {
325-
userAgents.add(userAgent);
329+
if (!userAgents.contains(userAgent)) {
330+
userAgents.add(userAgent);
331+
}
326332
updateUserAgents();
327333
}
328334

@@ -444,6 +450,7 @@ private static String _toCharArray(InputStream stream) throws IOException {
444450

445451
/**
446452
* Reads the InputStream into a byte array
453+
*
447454
* @param stream the InputStream to read
448455
* @return the stream's content as a byte[]
449456
* @throws AlgoliaException if the stream can't be read or flushed
@@ -638,12 +645,10 @@ private byte[] _requestRaw(@NonNull Method m, @NonNull String url, @Nullable Map
638645
}
639646
return rawResponse;
640647

641-
}
642-
catch (JSONException e) { // fatal
648+
} catch (JSONException e) { // fatal
643649
consumeQuietly(hostConnection);
644650
throw new AlgoliaException("Invalid JSON returned by server", e);
645-
}
646-
catch (UnsupportedEncodingException e) { // fatal
651+
} catch (UnsupportedEncodingException e) { // fatal
647652
consumeQuietly(hostConnection);
648653
throw new AlgoliaException("Invalid encoding returned by server", e);
649654
} catch (IOException e) { // host error, continue on the next host
@@ -697,6 +702,7 @@ private void checkTimeout(int connectTimeout) {
697702

698703
/**
699704
* Get the hosts that are not considered down in a given list.
705+
*
700706
* @param hosts a list of hosts whose {@link HostStatus} will be checked.
701707
* @return the hosts considered up, or all hosts if none is known to be reachable.
702708
*/
@@ -727,7 +733,7 @@ abstract protected class AsyncTaskRequest extends FutureRequest {
727733
* Construct a new request with the specified completion handler, executing on the client's search executor,
728734
* and calling the completion handler on the client's completion executor.
729735
*
730-
* @param completionHandler The completion handler to be notified of results. May be null if the caller omitted it.
736+
* @param completionHandler The completion handler to be notified of results. May be null if the caller omitted it.
731737
*/
732738
protected AsyncTaskRequest(@Nullable CompletionHandler completionHandler) {
733739
this(completionHandler, searchExecutorService);
@@ -737,8 +743,8 @@ protected AsyncTaskRequest(@Nullable CompletionHandler completionHandler) {
737743
* Construct a new request with the specified completion handler, executing on the specified executor, and
738744
* calling the completion handler on the client's completion executor.
739745
*
740-
* @param completionHandler The completion handler to be notified of results. May be null if the caller omitted it.
741-
* @param requestExecutor Executor on which to execute the request.
746+
* @param completionHandler The completion handler to be notified of results. May be null if the caller omitted it.
747+
* @param requestExecutor Executor on which to execute the request.
742748
*/
743749
protected AsyncTaskRequest(@Nullable CompletionHandler completionHandler, @NonNull Executor requestExecutor) {
744750
super(completionHandler, requestExecutor, completionExecutor);

algoliasearch/src/test/java/com/algolia/search/saas/ClientTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void tearDown() throws Exception {
5757

5858
@Test
5959
public void testIndexReuse() throws Exception {
60-
Map<String, WeakReference<Index>> indices = (Map<String, WeakReference<Index>>)Whitebox.getInternalState(client, "indices");
60+
Map<String, WeakReference<Index>> indices = (Map<String, WeakReference<Index>>) Whitebox.getInternalState(client, "indices");
6161
final String indexName = "name";
6262

6363
// Ask for the same index twice and check that it is re-used.
@@ -78,4 +78,18 @@ public void testIndexReuse() throws Exception {
7878
assertNull(indices.get(indexName).get());
7979
*/
8080
}
81+
82+
@Test
83+
public void testUniqueAgent() {
84+
client.addUserAgent(new AbstractClient.LibraryVersion("foo", "bar"));
85+
client.addUserAgent(new AbstractClient.LibraryVersion("foo", "bar"));
86+
final AbstractClient.LibraryVersion[] userAgents = client.getUserAgents();
87+
int found = 0;
88+
for (int i = 0; i < userAgents.length; i++) {
89+
if ("foo".equals(userAgents[i].name)) {
90+
found++;
91+
}
92+
}
93+
assertEquals("There should be only one foo user agent.", 1, found);
94+
}
8195
}

0 commit comments

Comments
 (0)