SignupController extends BaseController { @RequestMapping(value = "/signup.html", method = RequestMethod.POST) public void signup( HttpServletRequest request, HttpServletResponse response ) throws Exception { JSONUtil jsonUtil = new JSONUtil( request, response ); String jsonText = null; String password = request.getParameter( "password" ); String cPass = request.getParameter( "passwordConfirm" ); if ( reCaptcha.isPrivateKeySet() ) { if ( !reCaptcha.validateRequest( request ).isValid() ) { jsonText = "{success:false,message:'Captcha was not entered correctly.'}"; jsonUtil.writeToResponse( jsonText ); return; } } else { log.warn( "No recaptcha private key is configured, skipping validation" ); } if ( password.length() < UserFormMultiActionController.MIN_PASSWORD_LENGTH || !password.equals( cPass ) ) { jsonText = "{success:false,message:'Password was not valid or didn't match'}"; jsonUtil.writeToResponse( jsonText ); return; } String username = request.getParameter( "username" ); String encodedPassword = passwordEncoder.encodePassword( password, username ); String email = request.getParameter( "email" ); String cEmail = request.getParameter( "emailConfirm" ); if ( !email.matches( "^(\\w+)([-+.][\\w]+)*@(\\w[-\\w]*\\.){1,5}([A-Za-z]){2,4}$" ) || !email .equals( cEmail ) ) { jsonText = "{success:false,message:'Email was not valid or didn't match'}"; jsonUtil.writeToResponse( jsonText ); return; } String key = userManager.generateSignupToken( username ); Date now = new Date(); UserDetailsImpl u = new UserDetailsImpl( encodedPassword, username, false, null, email, key, now ); try { userManager.createUser( u ); sendSignupConfirmationEmail( request, u ); jsonText = "{success:true}"; } catch ( Exception e ) { log.error( e, e ); jsonText = jsonUtil.getJSONErrorMessage( e ); log.info( jsonText ); } finally { jsonUtil.writeToResponse( jsonText ); } }  @RequestMapping(value = "/ajaxLoginCheck.html") void ajaxLoginCheck( HttpServletRequest request, HttpServletResponse response ); @RequestMapping("/confirmRegistration.html") void confirmRegistration( HttpServletRequest request, HttpServletResponse response ); LoginDetailsValueObject loginCheck(); void setPasswordEncoder( PasswordEncoder passwordEncoder ); void setUserManager( UserManager userManager ); @RequestMapping(value = "/signup.html", method = RequestMethod.POST) void signup( HttpServletRequest request, HttpServletResponse response ); @RequestMapping(value = "/signup.html", method = RequestMethod.GET) String signupForm(); void setRecaptchaTester( ReCaptcha reCaptcha );  }
@SuppressWarnings("Duplicates") @Test public void testSignup() throws Exception { int numThreads = 10; final int numsignupsperthread = 20; final Random random = new Random(); final AtomicInteger c = new AtomicInteger( 0 ); final AtomicBoolean failed = new AtomicBoolean( false ); Collection<Thread> threads = new HashSet<>(); for ( int i = 0; i < numThreads; i++ ) { Thread k = new Thread( new Runnable() { @Override public void run() { try { for ( int j = 0; j < numsignupsperthread; j++ ) { MockHttpServletRequest req; Thread.sleep( random.nextInt( 50 ) ); req = new MockHttpServletRequest( "POST", "/signup.html" ); String uname = RandomStringUtils.randomAlphabetic( 10 ); String password = RandomStringUtils.randomAlphabetic( 40 ); req.addParameter( "password", password ); req.addParameter( "passwordConfirm", password ); req.addParameter( "username", uname ); String email = "foo@" + RandomStringUtils.randomAlphabetic( 10 ) + ".edu"; req.addParameter( "email", email ); req.addParameter( "emailConfirm", email ); suc.signup( req, new MockHttpServletResponse() ); ExpressionExperiment ee = ExpressionExperiment.Factory.newInstance(); ee.setDescription( "From test" ); ee.setName( RandomStringUtils.randomAlphabetic( 20 ) ); ee.setShortName( RandomStringUtils.randomAlphabetic( 20 ) ); expressionExperimentService.create( ee ); c.incrementAndGet(); } } catch ( Exception e ) { failed.set( true ); log.error( "!!!!!!!!!!!!!!!!!!!!!! FAILED: " + e.getMessage() ); log.debug( e, e ); throw new RuntimeException( e ); } log.debug( "Thread done." ); } } ); threads.add( k ); k.start(); } int waits = 0; int maxWaits = 20; int expectedEventCount = numThreads * numsignupsperthread; while ( c.get() < expectedEventCount && !failed.get() ) { Thread.sleep( 3000 ); log.info( "Waiting ... C=" + +c.get() ); if ( ++waits > maxWaits ) { for ( Thread t : threads ) { if ( t.isAlive() ) t.interrupt(); } fail( "Multithreaded failure: timed out." ); } } log.debug( " &&&&& DONE &&&&&" ); for ( Thread thread : threads ) { if ( thread.isAlive() ) thread.interrupt(); } if ( failed.get() || c.get() != expectedEventCount ) { fail( "Multithreaded loading failure: check logs for failure to recover from deadlock?" ); } else { log.info( "TORTURE TEST PASSED!" ); } }