Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Feb 20, 2018
2 parents 1b881f8 + 41da93f commit 017b690
Showing 1 changed file with 67 additions and 3 deletions.
70 changes: 67 additions & 3 deletions src/test/java/org/cactoos/iterator/SyncIteratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@
import java.util.Arrays;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.cactoos.list.ListOf;
import org.cactoos.matchers.RunsInThreads;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;

// @todo #482:30min add multi-threaded tests which test that the lock syncs the
// access to the next method against next and hasNext calls and calls to the
// hasNext method against next calls.
/**
* Test for {@link SyncIterator}.
*
Expand Down Expand Up @@ -73,4 +71,70 @@ public void syncIteratorReturnsCorrectValuesWithInternalLock() {
);
}

@Test
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
public void correctValuesForConcurrentNextNext() {
for (int iter = 0; iter < 5000; iter += 1) {
MatcherAssert.assertThat(
"",
map -> {
MatcherAssert.assertThat(
map.next(),
Matchers.anyOf(
Matchers.equalTo("a"),
Matchers.equalTo("b")
)
);
return true;
},
new RunsInThreads<>(
new SyncIterator<>(
Arrays.asList("a", "b").iterator()
),
2
)
);
}
}

@Test
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
public void correctValuesForConcurrentNextHasNext() {
for (int iter = 0; iter < 5000; iter += 1) {
MatcherAssert.assertThat(
"",
map -> {
MatcherAssert.assertThat(
map.hasNext(),
Matchers.anyOf(
Matchers.equalTo(true),
Matchers.equalTo(true)
)
);
MatcherAssert.assertThat(
map.next(),
Matchers.anyOf(
Matchers.equalTo("a"),
Matchers.equalTo("b")
)
);
MatcherAssert.assertThat(
map.hasNext(),
Matchers.anyOf(
Matchers.equalTo(true),
Matchers.equalTo(false)
)
);
return true;
},
new RunsInThreads<>(
new SyncIterator<>(
Arrays.asList("a", "b").iterator()
),
2
)
);
}
}

}

1 comment on commit 017b690

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 017b690 Feb 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 482-5994e3af disappeared from src/test/java/org/cactoos/iterator/SyncIteratorTest.java, that's why I closed #585. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.