Skip to content

Commit

Permalink
Merge pull request #82 from deepueg/add-is-registered
Browse files Browse the repository at this point in the history
Add is registered
  • Loading branch information
deepueg authored Aug 27, 2020
2 parents 89b6b24 + 245a880 commit 5a3b301
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import java.util.concurrent.CountDownLatch;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertSame;
import static junit.framework.TestCase.assertTrue;
import static junit.framework.TestCase.fail;

public class RequestHandleTests extends BaseBridgeTestCase {
Expand Down Expand Up @@ -185,4 +187,45 @@ public void onFailure(@NonNull FailureMessage failureMessage) {

waitForCountDownToFinishOrFail(countDownLatch);
}

@Test
public void testRequestHandlerIsRegisteredOnUnregister() {
final String requestName = "testRequestHandlerRemoval";

RequestHandlerHandle requestHandle = new RequestHandlerProcessor<>(requestName, None.class, None.class, new ElectrodeBridgeRequestHandler<None, None>() {
@Override
public void onRequest(@Nullable None payload, @NonNull ElectrodeBridgeResponseListener<None> responseListener) {
assertSame(None.NONE, payload);
responseListener.onSuccess(null);
}
}).execute();
assertTrue(requestHandle.isRegistered());
requestHandle.unregister();
assertFalse(requestHandle.isRegistered());
}

@Test
public void testRequestHandlerIsRegisteredOnReRegister() {
final String requestName = "testRequestHandlerRemoval";

RequestHandlerHandle requestHandle = new RequestHandlerProcessor<>(requestName, None.class, None.class, new ElectrodeBridgeRequestHandler<None, None>() {
@Override
public void onRequest(@Nullable None payload, @NonNull ElectrodeBridgeResponseListener<None> responseListener) {
assertSame(None.NONE, payload);
responseListener.onSuccess(null);
}
}).execute();
assertTrue(requestHandle.isRegistered());
RequestHandlerHandle newRequestHandle = new RequestHandlerProcessor<>(requestName, None.class, None.class, new ElectrodeBridgeRequestHandler<None, None>() {
@Override
public void onRequest(@Nullable None payload, @NonNull ElectrodeBridgeResponseListener<None> responseListener) {
assertSame(None.NONE, payload);
responseListener.onSuccess(null);
}
}).execute();
//Ensure a new request handler registration removes the old one
assertFalse(requestHandle.isRegistered());

assertTrue(newRequestHandle.isRegistered());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,21 @@ public static ElectrodeBridgeRequestHandler<ElectrodeBridgeRequest, Object> unre
}
}

public static boolean isRegistered(@NonNull UUID requestHandlerUuid) {
if (!isReactNativeReady) {
synchronized (mQueuedRequestHandlersRegistration) {
for (Map.Entry<String, RequestHandlerPlaceholder> entry : mQueuedRequestHandlersRegistration.entrySet()) {
if (entry.getValue().getUUID() == requestHandlerUuid) {
return true;
}
}
}
} else {
return electrodeNativeBridge.isRegistered(requestHandlerUuid);
}
return false;
}

private static void registerQueuedRequestHandlers() {
for (Map.Entry<String, RequestHandlerPlaceholder> entry : mQueuedRequestHandlersRegistration.entrySet()) {
electrodeNativeBridge.registerRequestHandler(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ public UUID getRequestHandlerId(@NonNull String name) {
return sRequestRegistrar.getRequestHandlerId(name);
}

@Override
public boolean isRegistered(@NonNull UUID requestHandlerUuid) {
return sRequestRegistrar.isRegistered(requestHandlerUuid);
}

@NonNull
@Override
public UUID getEventListenerId(@NonNull ElectrodeBridgeEventListener<ElectrodeBridgeEvent> eventListener) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,32 @@ public interface ElectrodeNativeBridge {
*/
boolean registerRequestHandler(@NonNull String name, @NonNull ElectrodeBridgeRequestHandler<ElectrodeBridgeRequest, Object> requestHandler, UUID uuid);

/**
* Unregisters a request handler
*
* @param requestHandlerUuid {@link UUID} of registerRequestHandler
* @return registerRequestHandler unregistered
*/
@SuppressWarnings("unused")
ElectrodeBridgeRequestHandler<ElectrodeBridgeRequest, Object> unregisterRequestHandler(@NonNull UUID requestHandlerUuid);

/**
* Query UUID of the request handler
*
* @param name
* @return {@link UUID} of the request handler
*/
@Nullable
UUID getRequestHandlerId(@NonNull String name);

/**
* Validates if the request handler associated with the id is still in registered state.
*
* @param requestHandlerUuid requestHandlerId
* @return true | false
*/
boolean isRegistered(@NonNull UUID requestHandlerUuid);

/**
* Sends an event with some data to the all the even listeners.
*
Expand All @@ -61,15 +87,6 @@ public interface ElectrodeNativeBridge {
@NonNull
boolean addEventListener(@NonNull String name, @NonNull ElectrodeBridgeEventListener<ElectrodeBridgeEvent> eventListener, UUID uuid);

/**
* Query UUID of the request handler
*
* @param name
* @return {@link UUID} of the request handler
*/
@Nullable
UUID getRequestHandlerId(@NonNull String name);

/**
* Query UUID of the event listener
*
Expand All @@ -86,14 +103,4 @@ public interface ElectrodeNativeBridge {
* @return
*/
ElectrodeBridgeEventListener<ElectrodeBridgeEvent> removeEventListener(@NonNull UUID eventListenerUuid);

/**
* Unregisters a request handler
*
* @param requestHandlerUuid {@link UUID} of registerRequestHandler
* @return registerRequestHandler unregistered
*/
@SuppressWarnings("unused")
ElectrodeBridgeRequestHandler<ElectrodeBridgeRequest, Object> unregisterRequestHandler(@NonNull UUID requestHandlerUuid);

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@
*/
public interface RequestHandlerHandle {
/**
* Unregisters a request handler.
* Checks if the request handler associated to this handle is still the registered handler
*
* @return
* @return true | false
*/
boolean isRegistered();

/**
* Unregisters a request handler if present
*
* @return true if the request handler was unregistered. false if the handler was already removed
*/
boolean unregister();
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,14 @@ public void onSuccess(TResp obj) {
return this;
}

@Override
public boolean isRegistered() {
return ElectrodeBridgeHolder.isRegistered(id);
}

@Override
public boolean unregister() {
Logger.w(TAG, "Removing registered request handler %s with id %s", handler, id);
Logger.d(TAG, "Removing registered request handler %s with id %s", handler, id);
ElectrodeBridgeHolder.unregisterRequestHandler(id);
intermediateRequestHandler = null;
handler = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,12 @@ public interface RequestRegistrar<T> {
*/
@Nullable
UUID getRequestHandlerId(@NonNull String name);

/**
* Validates if the request handler is still registered
*
* @param requestHandlerUuid requestHandler id
* @return true | false
*/
boolean isRegistered(@NonNull UUID requestHandlerUuid);
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ public boolean registerRequestHandler(@NonNull String name, @NonNull T requestHa
}
mRequestHandlerByRequestName.put(name, requestHandler);
mRequestNameByUUID.put(requestHandlerUuid, name);
isRegistered = true;
Logger.d(TAG, "New request handler(id: %s) registered for request: %s", requestHandlerUuid, name);
return isRegistered;
return true;
}

/**
Expand Down Expand Up @@ -94,6 +93,11 @@ public UUID getRequestHandlerId(@NonNull String name) {
return null;
}

@Override
public boolean isRegistered(@NonNull UUID requestHandlerUuid) {
return mRequestNameByUUID.containsKey(requestHandlerUuid);
}

private void removeOldUuidMapping(@NonNull String name) {
UUID oldUuid = getRequestHandlerId(name);
if (oldUuid != null) {
Expand Down

0 comments on commit 5a3b301

Please sign in to comment.