-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from deepueg/fix-request-handle-un-registration
Fix request handle un registration
- Loading branch information
Showing
7 changed files
with
218 additions
and
106 deletions.
There are no files selected for viewing
188 changes: 188 additions & 0 deletions
188
...src/androidTest/java/com/walmartlabs/electrode/reactnative/bridge/RequestHandleTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
package com.walmartlabs.electrode.reactnative.bridge; | ||
|
||
import android.support.annotation.NonNull; | ||
import android.support.annotation.Nullable; | ||
|
||
import com.walmartlabs.electrode.reactnative.sample.api.PersonApi; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
|
||
import static junit.framework.TestCase.assertEquals; | ||
import static junit.framework.TestCase.assertNotNull; | ||
import static junit.framework.TestCase.assertSame; | ||
import static junit.framework.TestCase.fail; | ||
|
||
public class RequestHandleTests extends BaseBridgeTestCase { | ||
|
||
@Test | ||
public void testRequestHandlerRemoval() { | ||
final CountDownLatch countDownLatch = new CountDownLatch(3); | ||
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); | ||
countDownLatch.countDown(); | ||
} | ||
}).execute(); | ||
|
||
|
||
new RequestProcessor<None, None>(requestName, null, None.class, new ElectrodeBridgeResponseListener<None>() { | ||
@Override | ||
public void onFailure(@NonNull FailureMessage failureMessage) { | ||
fail(); | ||
} | ||
|
||
@Override | ||
public void onSuccess(@Nullable None responseData) { | ||
assertSame(None.NONE, responseData); | ||
countDownLatch.countDown(); | ||
} | ||
}).execute(); | ||
|
||
requestHandle.unregister(); | ||
|
||
new RequestProcessor<None, None>(requestName, null, None.class, new ElectrodeBridgeResponseListener<None>() { | ||
@Override | ||
public void onFailure(@NonNull FailureMessage failureMessage) { | ||
failureMessage.getMessage(); | ||
countDownLatch.countDown(); | ||
} | ||
|
||
@Override | ||
public void onSuccess(@Nullable None responseData) { | ||
fail(); | ||
} | ||
}).execute(); | ||
|
||
waitForCountDownToFinishOrFail(countDownLatch); | ||
} | ||
|
||
@Test | ||
public void testRequestHandlerRemovalWithApi() { | ||
final CountDownLatch countDownLatch = new CountDownLatch(3); | ||
final int RESULT_AGE = 10; | ||
|
||
RequestHandlerHandle handlerHandle = PersonApi.requests().registerGetAgeRequestHandler(new ElectrodeBridgeRequestHandler<String, Integer>() { | ||
@Override | ||
public void onRequest(@Nullable String payload, @NonNull ElectrodeBridgeResponseListener<Integer> responseListener) { | ||
responseListener.onSuccess(RESULT_AGE); | ||
countDownLatch.countDown(); | ||
} | ||
}); | ||
|
||
PersonApi.requests().getAge("user", new ElectrodeBridgeResponseListener<Integer>() { | ||
@Override | ||
public void onSuccess(@Nullable Integer responseData) { | ||
assertNotNull(responseData); | ||
assertEquals(RESULT_AGE, responseData.intValue()); | ||
countDownLatch.countDown(); | ||
} | ||
|
||
@Override | ||
public void onFailure(@NonNull FailureMessage failureMessage) { | ||
fail(); | ||
} | ||
}); | ||
|
||
handlerHandle.unregister(); | ||
|
||
PersonApi.requests().getAge("user", new ElectrodeBridgeResponseListener<Integer>() { | ||
@Override | ||
public void onSuccess(@Nullable Integer responseData) { | ||
fail(); | ||
} | ||
|
||
@Override | ||
public void onFailure(@NonNull FailureMessage failureMessage) { | ||
assertNotNull(failureMessage); | ||
countDownLatch.countDown(); | ||
} | ||
}); | ||
waitForCountDownToFinishOrFail(countDownLatch); | ||
} | ||
|
||
@Test | ||
public void testMultipleRequestHandlers() { | ||
final CountDownLatch countDownLatch = new CountDownLatch(4); | ||
final int RESULT_AGE_FIRST = 10; | ||
final int RESULT_AGE_SECOND = 20; | ||
final int RESULT_AGE_THIRD = 30; | ||
|
||
RequestHandlerHandle handlerHandle = PersonApi.requests().registerGetAgeRequestHandler(new ElectrodeBridgeRequestHandler<String, Integer>() { | ||
@Override | ||
public void onRequest(@Nullable String payload, @NonNull ElectrodeBridgeResponseListener<Integer> responseListener) { | ||
responseListener.onSuccess(RESULT_AGE_FIRST); | ||
countDownLatch.countDown(); | ||
} | ||
}); | ||
|
||
RequestHandlerHandle handlerHandle1 = PersonApi.requests().registerGetAgeRequestHandler(new ElectrodeBridgeRequestHandler<String, Integer>() { | ||
@Override | ||
public void onRequest(@Nullable String payload, @NonNull ElectrodeBridgeResponseListener<Integer> responseListener) { | ||
responseListener.onSuccess(RESULT_AGE_SECOND); | ||
countDownLatch.countDown(); | ||
} | ||
}); | ||
|
||
RequestHandlerHandle handlerHandle2 = PersonApi.requests().registerGetAgeRequestHandler(new ElectrodeBridgeRequestHandler<String, Integer>() { | ||
@Override | ||
public void onRequest(@Nullable String payload, @NonNull ElectrodeBridgeResponseListener<Integer> responseListener) { | ||
responseListener.onSuccess(RESULT_AGE_THIRD); | ||
countDownLatch.countDown(); | ||
} | ||
}); | ||
|
||
PersonApi.requests().getAge("user", new ElectrodeBridgeResponseListener<Integer>() { | ||
@Override | ||
public void onSuccess(@Nullable Integer responseData) { | ||
assertNotNull(responseData); | ||
assertEquals(RESULT_AGE_THIRD, responseData.intValue()); | ||
countDownLatch.countDown(); | ||
} | ||
|
||
@Override | ||
public void onFailure(@NonNull FailureMessage failureMessage) { | ||
fail(); | ||
} | ||
}); | ||
|
||
handlerHandle.unregister(); | ||
handlerHandle1.unregister(); | ||
|
||
PersonApi.requests().getAge("user", new ElectrodeBridgeResponseListener<Integer>() { | ||
@Override | ||
public void onSuccess(@Nullable Integer responseData) { | ||
assertNotNull(responseData); | ||
assertEquals(RESULT_AGE_THIRD, responseData.intValue()); | ||
countDownLatch.countDown(); | ||
} | ||
|
||
@Override | ||
public void onFailure(@NonNull FailureMessage failureMessage) { | ||
fail(); | ||
} | ||
}); | ||
|
||
handlerHandle2.unregister(); | ||
|
||
PersonApi.requests().getAge("user", new ElectrodeBridgeResponseListener<Integer>() { | ||
@Override | ||
public void onSuccess(@Nullable Integer responseData) { | ||
fail(); | ||
} | ||
|
||
@Override | ||
public void onFailure(@NonNull FailureMessage failureMessage) { | ||
assertNotNull(failureMessage); | ||
countDownLatch.countDown(); | ||
} | ||
}); | ||
|
||
waitForCountDownToFinishOrFail(countDownLatch); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.