@Override public void doRun() { //pick a random node in the tree, starting from root //add a new child node to this node Scope scope = NodeUtil.findRandomNode(rootScopeName, ACCEPTANCE_THRESHOLD); if (scope == null) { setIsSuccessful(true); return; } //remove any node except root if (scope.getParentScope() == null) { setIsSuccessful(true); return; } Object scopeName = scope.getName(); Toothpick.closeScope(scopeName); setIsSuccessful(true); } }
@Test public void concurrentScopeRemovals_shouldNotCrash() throws InterruptedException { //GIVEN final int removalNodeThreadCount = STANDARD_THREAD_COUNT; List<TestableThread> threadList = new ArrayList<>(); //WHEN for (int indexThread = 0; indexThread < removalNodeThreadCount; indexThread++) { RemoveNodeThread runnable = new RemoveNodeThread(ROOT_SCOPE); threadList.add(runnable); ThreadTestUtil.submit(runnable); } //THEN //we simply should not have crashed when all threads are done ThreadTestUtil.shutdown(); for (TestableThread thread : threadList) { assertTrue(String.format("test of thread %s failed", thread.getName()), thread.isSuccessful()); } }
@Test public void concurrentScopeRemovals_shouldNotCrash() throws InterruptedException { //GIVEN final int removalNodeThreadCount = STANDARD_THREAD_COUNT; List<TestableThread> threadList = new ArrayList<>(); //WHEN for (int indexThread = 0; indexThread < removalNodeThreadCount; indexThread++) { RemoveNodeThread runnable = new RemoveNodeThread(ROOT_SCOPE); threadList.add(runnable); ThreadTestUtil.submit(runnable); } //THEN //we simply should not have crashed when all threads are done ThreadTestUtil.shutdown(); for (TestableThread thread : threadList) { assertTrue(String.format("test of thread %s failed", thread.getName()), thread.isSuccessful()); } }
@Test public void concurrentScopeAdditionsAndRemovals_shouldNotCrash() throws InterruptedException { //GIVEN final int removalNodeThreadCount = STANDARD_THREAD_COUNT / 2; final int addNodeThreadCount = STANDARD_THREAD_COUNT / 2; List<TestableThread> threadList = new ArrayList<>(); final Random random = new Random(); //WHEN for (int indexThread = 0; indexThread < addNodeThreadCount + removalNodeThreadCount; indexThread++) { final TestableThread runnable; if (random.nextInt(100) < 50) { runnable = new RemoveNodeThread(ROOT_SCOPE); } else { runnable = new AddNodeThread(ROOT_SCOPE); } threadList.add(runnable); ThreadTestUtil.submit(runnable); } //THEN //we simply should not have crashed when all threads are done ThreadTestUtil.shutdown(); for (TestableThread thread : threadList) { assertTrue(String.format("test of thread %s failed", thread.getName()), thread.isSuccessful()); } } }
@Test public void concurrentScopeAdditionsAndRemovals_shouldNotCrash() throws InterruptedException { //GIVEN final int removalNodeThreadCount = STANDARD_THREAD_COUNT / 2; final int addNodeThreadCount = STANDARD_THREAD_COUNT / 2; List<TestableThread> threadList = new ArrayList<>(); final Random random = new Random(); //WHEN for (int indexThread = 0; indexThread < addNodeThreadCount + removalNodeThreadCount; indexThread++) { final TestableThread runnable; if (random.nextInt(100) < 50) { runnable = new RemoveNodeThread(ROOT_SCOPE); } else { runnable = new AddNodeThread(ROOT_SCOPE); } threadList.add(runnable); ThreadTestUtil.submit(runnable); } //THEN //we simply should not have crashed when all threads are done ThreadTestUtil.shutdown(); for (TestableThread thread : threadList) { assertTrue(String.format("test of thread %s failed", thread.getName()), thread.isSuccessful()); } } }