Correctly implement SelectedSelectionKeySet iterator(), contains(...) and remove(...) (#8244)
Motivation: Our SelectedSelectionKeySet does not correctly implement various methods which can be done without any performance overhead. Modifications: Implement iterator(), contains(...) and remove(...) Result: Related to https://github.com/netty/netty/issues/8242.
This commit is contained in:
parent
9d8846cfce
commit
c1a335446d
@ -19,6 +19,7 @@ import java.nio.channels.SelectionKey;
|
||||
import java.util.AbstractSet;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
final class SelectedSelectionKeySet extends AbstractSet<SelectionKey> {
|
||||
|
||||
@ -48,19 +49,24 @@ final class SelectedSelectionKeySet extends AbstractSet<SelectionKey> {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Object o) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object o) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<SelectionKey> iterator() {
|
||||
throw new UnsupportedOperationException();
|
||||
return new Iterator<SelectionKey>() {
|
||||
private int idx;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return idx < size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SelectionKey next() {
|
||||
if (!hasNext()) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
return keys[idx++];
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
void reset() {
|
||||
|
@ -21,12 +21,10 @@ import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import java.nio.channels.SelectionKey;
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class SelectedSelectionKeySetTest {
|
||||
@Mock
|
||||
@ -34,6 +32,9 @@ public class SelectedSelectionKeySetTest {
|
||||
@Mock
|
||||
private SelectionKey mockKey2;
|
||||
|
||||
@Mock
|
||||
private SelectionKey mockKey3;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
@ -63,4 +64,54 @@ public class SelectedSelectionKeySetTest {
|
||||
assertEquals(0, set.size());
|
||||
assertTrue(set.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void iterator() {
|
||||
SelectedSelectionKeySet set = new SelectedSelectionKeySet();
|
||||
assertTrue(set.add(mockKey));
|
||||
assertTrue(set.add(mockKey2));
|
||||
Iterator<SelectionKey> keys = set.iterator();
|
||||
assertTrue(keys.hasNext());
|
||||
assertSame(mockKey, keys.next());
|
||||
assertTrue(keys.hasNext());
|
||||
assertSame(mockKey2, keys.next());
|
||||
assertFalse(keys.hasNext());
|
||||
|
||||
try {
|
||||
keys.next();
|
||||
fail();
|
||||
} catch (NoSuchElementException expected) {
|
||||
// expected
|
||||
}
|
||||
|
||||
try {
|
||||
keys.remove();
|
||||
fail();
|
||||
} catch (UnsupportedOperationException expected) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contains() {
|
||||
SelectedSelectionKeySet set = new SelectedSelectionKeySet();
|
||||
assertTrue(set.add(mockKey));
|
||||
assertTrue(set.add(mockKey2));
|
||||
assertTrue(set.contains(mockKey));
|
||||
assertTrue(set.contains(mockKey2));
|
||||
assertFalse(set.contains(mockKey3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void remove() {
|
||||
SelectedSelectionKeySet set = new SelectedSelectionKeySet();
|
||||
assertTrue(set.add(mockKey));
|
||||
assertFalse(set.remove(mockKey2));
|
||||
try {
|
||||
set.remove(mockKey);
|
||||
fail();
|
||||
} catch (UnsupportedOperationException expected) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user