|
1 | 1 | /**
|
2 | 2 | * Copyright 2013 Netflix, Inc.
|
3 |
| - * |
| 3 | + * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
6 | 6 | * You may obtain a copy of the License at
|
7 |
| - * |
8 |
| - * http://www.apache.org/licenses/LICENSE-2.0 |
9 |
| - * |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
10 | 10 | * Unless required by applicable law or agreed to in writing, software
|
11 | 11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
12 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16 | 16 | package rx.operators;
|
17 | 17 |
|
18 | 18 | import static org.junit.Assert.*;
|
| 19 | +import static org.mockito.Matchers.*; |
19 | 20 | import static org.mockito.Mockito.*;
|
20 | 21 | import static rx.operators.OperationObserveOn.*;
|
21 | 22 |
|
|
30 | 31 | import rx.Observable;
|
31 | 32 | import rx.Observer;
|
32 | 33 | import rx.concurrency.Schedulers;
|
| 34 | +import rx.util.functions.Action1; |
33 | 35 |
|
34 | 36 | public class OperationObserveOnTest {
|
35 | 37 |
|
@@ -81,4 +83,53 @@ public Void answer(InvocationOnMock invocation) throws Throwable {
|
81 | 83 | inOrder.verify(observer, times(1)).onCompleted();
|
82 | 84 | inOrder.verifyNoMoreInteractions();
|
83 | 85 | }
|
| 86 | + |
| 87 | + @Test |
| 88 | + @SuppressWarnings("unchecked") |
| 89 | + public void testThreadName() throws InterruptedException { |
| 90 | + Observable<String> obs = Observable.from("one", null, "two", "three", "four"); |
| 91 | + |
| 92 | + Observer<String> observer = mock(Observer.class); |
| 93 | + |
| 94 | + InOrder inOrder = inOrder(observer); |
| 95 | + |
| 96 | + final CountDownLatch completedLatch = new CountDownLatch(1); |
| 97 | + doAnswer(new Answer<Void>() { |
| 98 | + |
| 99 | + @Override |
| 100 | + public Void answer(InvocationOnMock invocation) throws Throwable { |
| 101 | + completedLatch.countDown(); |
| 102 | + |
| 103 | + return null; |
| 104 | + } |
| 105 | + }).when(observer).onCompleted(); |
| 106 | + |
| 107 | + doAnswer(new Answer<Void>() { |
| 108 | + |
| 109 | + @Override |
| 110 | + public Void answer(InvocationOnMock invocation) throws Throwable { |
| 111 | + completedLatch.countDown(); |
| 112 | + |
| 113 | + return null; |
| 114 | + } |
| 115 | + }).when(observer).onError(any(Exception.class)); |
| 116 | + |
| 117 | + obs.observeOn(Schedulers.newThread()).doOnEach(new Action1<String>() { |
| 118 | + |
| 119 | + @Override |
| 120 | + public void call(String t1) { |
| 121 | + String threadName = Thread.currentThread().getName(); |
| 122 | + boolean correctThreadName = threadName.startsWith("RxNewThreadScheduler"); |
| 123 | + System.out.println("ThreadName: " + threadName + " Correct => " + correctThreadName); |
| 124 | + assertTrue(correctThreadName); |
| 125 | + } |
| 126 | + |
| 127 | + }).subscribe(observer); |
| 128 | + |
| 129 | + if (!completedLatch.await(1000, TimeUnit.MILLISECONDS)) { |
| 130 | + fail("timed out waiting"); |
| 131 | + } |
| 132 | + |
| 133 | + inOrder.verify(observer, times(1)).onCompleted(); |
| 134 | + } |
84 | 135 | }
|
0 commit comments