Fixes to tests and test properties.
Project: http://git-wip-us.apache.org/repos/asf/usergrid/repo Commit: http://git-wip-us.apache.org/repos/asf/usergrid/commit/917f0e32 Tree: http://git-wip-us.apache.org/repos/asf/usergrid/tree/917f0e32 Diff: http://git-wip-us.apache.org/repos/asf/usergrid/diff/917f0e32 Branch: refs/heads/release-2.1.1 Commit: 917f0e32ea72734b98902ef041a6fe5302495d4b Parents: 996bf09 Author: Dave Johnson <[email protected]> Authored: Thu Jul 7 12:10:01 2016 -0400 Committer: Dave Johnson <[email protected]> Committed: Thu Jul 7 12:10:01 2016 -0400 ---------------------------------------------------------------------- .../UniqueValuesServiceDeleteTest.java | 114 +++++++------------ 1 file changed, 39 insertions(+), 75 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/usergrid/blob/917f0e32/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/uniquevalues/UniqueValuesServiceDeleteTest.java ---------------------------------------------------------------------- diff --git a/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/uniquevalues/UniqueValuesServiceDeleteTest.java b/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/uniquevalues/UniqueValuesServiceDeleteTest.java index 3f58834..397ec78 100644 --- a/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/uniquevalues/UniqueValuesServiceDeleteTest.java +++ b/stack/corepersistence/collection/src/test/java/org/apache/usergrid/persistence/collection/uniquevalues/UniqueValuesServiceDeleteTest.java @@ -22,6 +22,7 @@ import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Multimap; import com.google.common.collect.Multimaps; import com.google.inject.Inject; +import org.apache.commons.lang.RandomStringUtils; import org.apache.usergrid.persistence.actorsystem.ActorSystemFig; import org.apache.usergrid.persistence.actorsystem.ActorSystemManager; import org.apache.usergrid.persistence.collection.AbstractUniqueValueTest; @@ -52,6 +53,8 @@ import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; +import static junit.framework.TestCase.fail; + /** * Test the unique values service. @@ -86,7 +89,7 @@ public class UniqueValuesServiceDeleteTest extends AbstractUniqueValueTest { @Before public void initAkka() { // each test class needs unique port number - initAkka( 2555, actorSystemManager, uniqueValuesService ); + initAkka( 2559, actorSystemManager, uniqueValuesService ); } @@ -94,92 +97,53 @@ public class UniqueValuesServiceDeleteTest extends AbstractUniqueValueTest { * Use multiple threads to attempt to create entities with duplicate usernames. */ @Test - public void testDuplicatePrevention() throws Exception { + public void testUniqueValueCleanup() throws Exception { initAkka(); - final AtomicInteger successCounter = new AtomicInteger( 0 ); - final AtomicInteger errorCounter = new AtomicInteger( 0 ); - - Multimap<String, Entity> usersCreated = - generateDuplicateUsers( numUsers, successCounter, errorCounter ); - - int userCount = 0; - int usernamesWithDuplicates = 0; - for ( String username : usersCreated.keySet() ) { - Collection<Entity> users = usersCreated.get( username ); - if ( users.size() > 1 ) { - usernamesWithDuplicates++; - } - userCount++; - } - - Assert.assertEquals( 0, usernamesWithDuplicates ); - - Assert.assertEquals( numUsers, successCounter.get() ); - Assert.assertEquals( 0, errorCounter.get() ); - Assert.assertEquals( numUsers, usersCreated.size() ); - Assert.assertEquals( numUsers, userCount ); - } - - - private Multimap<String, Entity> generateDuplicateUsers( - int numUsers, AtomicInteger successCounter, AtomicInteger errorCounter ) { - ApplicationScope context = new ApplicationScopeImpl( new SimpleId( "organization" ) ); EntityCollectionManager manager = factory.createCollectionManager( context ); - Multimap<String, Entity> usersCreated = - Multimaps.synchronizedListMultimap( ArrayListMultimap.create() ); - - ExecutorService execService = Executors.newFixedThreadPool( poolSize ); + String username = RandomStringUtils.randomAlphanumeric( 20 ); - for (int i = 0; i < numUsers; i++) { - - // multiple threads simultaneously trying to create a user with the same propertyName - for (int j = 0; j < numThreads; j++) { - String username = "user_" + i; - - execService.submit( () -> { - - try { - - // give entity two unqiue fields username and email - Entity newEntity = new Entity( new SimpleId( "user" ) ); - newEntity.setField( new StringField( "username", username, true ) ); - newEntity.setField( new StringField( "email", username + "@example.org", true ) ); - - Observable<Entity> observable = manager.write( newEntity, null ); - Entity returned = observable.toBlocking().lastOrDefault( null ); - - usersCreated.put( username, newEntity ); - successCounter.incrementAndGet(); - - logger.debug("Created user {}", username); - - } catch ( Throwable t ) { - if ( t instanceof WriteUniqueVerifyException) { - // we expect lots of these - } else { - errorCounter.incrementAndGet(); - logger.error( "Error creating user " + username, t ); - } - } + // create user + Entity originalUser = null; + { + Entity newEntity = new Entity( new SimpleId( "user" ) ); + newEntity.setField( new StringField( "username", username, true ) ); + newEntity.setField( new StringField( "email", username + "@example.org", true ) ); + Observable<Entity> observable = manager.write( newEntity, null ); + originalUser = observable.toBlocking().lastOrDefault( null ); + } - } ); - } + // cannot create another user with same name + { + Entity newEntity = new Entity( new SimpleId( "user" ) ); + newEntity.setField( new StringField( "username", username, true ) ); + newEntity.setField( new StringField( "email", username + "@example.org", true ) ); + try { + Observable<Entity> observable = manager.write( newEntity, null ); + Entity returned = observable.toBlocking().lastOrDefault( null ); + fail("Should not have created dupliate user"); + } catch ( WriteUniqueVerifyException expected ) {} } - execService.shutdown(); - try { - while (!execService.awaitTermination( 60, TimeUnit.SECONDS )) { - System.out.println( "Waiting..." ); + // delete user + manager.mark( originalUser.getId(), null ).toBlocking().firstOrDefault( null ); + + // now we can create another user with same name + { + Entity newEntity = new Entity( new SimpleId( "user" ) ); + newEntity.setField( new StringField( "username", username, true ) ); + newEntity.setField( new StringField( "email", username + "@example.org", true ) ); + try { + Observable<Entity> observable = manager.write( newEntity, null ); + Entity returned = observable.toBlocking().lastOrDefault( null ); + } catch ( WriteUniqueVerifyException unexpected ) { + logger.error("Error creating user", unexpected); + fail("Still cannot create new user after delete"); } - } catch (InterruptedException e) { - e.printStackTrace(); } - - return usersCreated; } }
