|
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.
|
@@ -82,4 +82,66 @@ public void unsubscribe() {
|
82 | 82 | // we should still have unsubscribed to the second one
|
83 | 83 | assertEquals(1, counter.get());
|
84 | 84 | }
|
| 85 | + |
| 86 | + @Test |
| 87 | + public void testRemoveUnsubscribes() { |
| 88 | + BooleanSubscription s1 = new BooleanSubscription(); |
| 89 | + BooleanSubscription s2 = new BooleanSubscription(); |
| 90 | + |
| 91 | + CompositeSubscription s = new CompositeSubscription(); |
| 92 | + s.add(s1); |
| 93 | + s.add(s2); |
| 94 | + |
| 95 | + s.remove(s1); |
| 96 | + |
| 97 | + assertTrue(s1.isUnsubscribed()); |
| 98 | + assertFalse(s2.isUnsubscribed()); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testClear() { |
| 103 | + BooleanSubscription s1 = new BooleanSubscription(); |
| 104 | + BooleanSubscription s2 = new BooleanSubscription(); |
| 105 | + |
| 106 | + CompositeSubscription s = new CompositeSubscription(); |
| 107 | + s.add(s1); |
| 108 | + s.add(s2); |
| 109 | + |
| 110 | + assertFalse(s1.isUnsubscribed()); |
| 111 | + assertFalse(s2.isUnsubscribed()); |
| 112 | + |
| 113 | + s.clear(); |
| 114 | + |
| 115 | + assertTrue(s1.isUnsubscribed()); |
| 116 | + assertTrue(s1.isUnsubscribed()); |
| 117 | + assertFalse(s.isUnsubscribed()); |
| 118 | + |
| 119 | + BooleanSubscription s3 = new BooleanSubscription(); |
| 120 | + |
| 121 | + s.add(s3); |
| 122 | + s.unsubscribe(); |
| 123 | + |
| 124 | + assertTrue(s3.isUnsubscribed()); |
| 125 | + assertTrue(s.isUnsubscribed()); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testUnsubscribeIdempotence() { |
| 130 | + final AtomicInteger counter = new AtomicInteger(); |
| 131 | + CompositeSubscription s = new CompositeSubscription(); |
| 132 | + s.add(new Subscription() { |
| 133 | + |
| 134 | + @Override |
| 135 | + public void unsubscribe() { |
| 136 | + counter.incrementAndGet(); |
| 137 | + } |
| 138 | + }); |
| 139 | + |
| 140 | + s.unsubscribe(); |
| 141 | + s.unsubscribe(); |
| 142 | + s.unsubscribe(); |
| 143 | + |
| 144 | + // we should have only unsubscribed once |
| 145 | + assertEquals(1, counter.get()); |
| 146 | + } |
85 | 147 | }
|
0 commit comments