|
53 | 53 | import java.util.HashMap; |
54 | 54 | import java.util.List; |
55 | 55 | import java.util.Map; |
| 56 | +import java.util.concurrent.AbstractExecutorService; |
| 57 | +import java.util.concurrent.CountDownLatch; |
56 | 58 | import java.util.concurrent.ExecutorService; |
57 | 59 | import java.util.concurrent.Executors; |
| 60 | +import java.util.concurrent.RejectedExecutionException; |
58 | 61 | import java.util.concurrent.ThreadFactory; |
59 | 62 | import java.util.concurrent.TimeUnit; |
| 63 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 64 | +import java.util.concurrent.atomic.AtomicReference; |
60 | 65 | import org.junit.Before; |
61 | 66 | import org.junit.Test; |
62 | 67 |
|
@@ -776,4 +781,82 @@ public void testCustomThreadFactory() { |
776 | 781 |
|
777 | 782 | assertSame(customThreadFactory, clientWithThreadFactory.getThreadFactory()); |
778 | 783 | } |
| 784 | + |
| 785 | + @Test |
| 786 | + public void testDefaultExecutorUsesDaemonThreads() throws Exception { |
| 787 | + FirebaseMessagingClientImpl clientWithDefaultExecutor = |
| 788 | + FirebaseMessagingClientImpl.builder() |
| 789 | + .setProjectId("test-project") |
| 790 | + .setJsonFactory(ApiClientUtils.getDefaultJsonFactory()) |
| 791 | + .setRequestFactory(new MockHttpTransport().createRequestFactory()) |
| 792 | + .setChildRequestFactory(ApiClientUtils.getDefaultTransport().createRequestFactory()) |
| 793 | + .build(); |
| 794 | + |
| 795 | + final AtomicBoolean isDaemon = new AtomicBoolean(); |
| 796 | + final AtomicReference<String> threadName = new AtomicReference<>(); |
| 797 | + final CountDownLatch latch = new CountDownLatch(1); |
| 798 | + clientWithDefaultExecutor.getExecutor().execute(() -> { |
| 799 | + Thread current = Thread.currentThread(); |
| 800 | + isDaemon.set(current.isDaemon()); |
| 801 | + threadName.set(current.getName()); |
| 802 | + latch.countDown(); |
| 803 | + }); |
| 804 | + |
| 805 | + assertTrue(latch.await(5, TimeUnit.SECONDS)); |
| 806 | + assertTrue(isDaemon.get()); |
| 807 | + assertNotNull(threadName.get()); |
| 808 | + assertTrue(threadName.get().startsWith("firebase-messaging-topics-")); |
| 809 | + } |
| 810 | + |
| 811 | + @Test |
| 812 | + public void testTopicManagementRejectedExecution() throws Exception { |
| 813 | + ExecutorService rejectingExecutor = new AbstractExecutorService() { |
| 814 | + @Override |
| 815 | + public void shutdown() {} |
| 816 | + |
| 817 | + @Override |
| 818 | + public List<Runnable> shutdownNow() { |
| 819 | + return ImmutableList.of(); |
| 820 | + } |
| 821 | + |
| 822 | + @Override |
| 823 | + public boolean isShutdown() { |
| 824 | + return false; |
| 825 | + } |
| 826 | + |
| 827 | + @Override |
| 828 | + public boolean isTerminated() { |
| 829 | + return false; |
| 830 | + } |
| 831 | + |
| 832 | + @Override |
| 833 | + public boolean awaitTermination(long timeout, TimeUnit unit) { |
| 834 | + return false; |
| 835 | + } |
| 836 | + |
| 837 | + @Override |
| 838 | + public void execute(Runnable command) { |
| 839 | + throw new RejectedExecutionException("Task rejected"); |
| 840 | + } |
| 841 | + }; |
| 842 | + |
| 843 | + FirebaseMessagingClientImpl clientWithRejection = FirebaseMessagingClientImpl.builder() |
| 844 | + .setProjectId("test-project") |
| 845 | + .setJsonFactory(ApiClientUtils.getDefaultJsonFactory()) |
| 846 | + .setRequestFactory(new MockHttpTransport().createRequestFactory()) |
| 847 | + .setChildRequestFactory(ApiClientUtils.getDefaultTransport().createRequestFactory()) |
| 848 | + .setExecutor(rejectingExecutor) |
| 849 | + .build(); |
| 850 | + |
| 851 | + TopicManagementResponse result = clientWithRejection.subscribeToTopic( |
| 852 | + "test-topic", ImmutableList.of("id1", "id2")); |
| 853 | + |
| 854 | + assertEquals(0, result.getSuccessCount()); |
| 855 | + assertEquals(2, result.getFailureCount()); |
| 856 | + assertEquals(2, result.getErrors().size()); |
| 857 | + assertEquals(0, result.getErrors().get(0).getIndex()); |
| 858 | + assertEquals("rejected-by-executor", result.getErrors().get(0).getReason()); |
| 859 | + assertEquals(1, result.getErrors().get(1).getIndex()); |
| 860 | + assertEquals("rejected-by-executor", result.getErrors().get(1).getReason()); |
| 861 | + } |
779 | 862 | } |
0 commit comments