View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.client.lib;
20  
21  import static org.junit.jupiter.api.Assertions.fail;
22  
23  import org.apache.commons.lang3.StringUtils;
24  import org.apache.syncope.common.rest.api.service.SyncopeService;
25  import org.junit.jupiter.api.Test;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  public class ConcurrencyTest {
30  
31      private static final Logger LOG = LoggerFactory.getLogger(ConcurrencyTest.class);
32  
33      private static final int THREAD_NUMBER = 1000;
34  
35      private static final SyncopeClient CLIENT = new SyncopeClientFactoryBean().
36              setAddress("http://url").create(new AuthenticationHandler() {
37      });
38  
39      @Test
40      public void multiThreadTest() throws InterruptedException {
41          for (int i = 0; i < THREAD_NUMBER; i++) {
42              Thread execution = new Thread("Th-" + StringUtils.leftPad(String.valueOf(i), 5, '0')) {
43  
44                  @Override
45                  public void run() {
46  
47                      try {
48                          CLIENT.getService(SyncopeService.class);
49  
50                          LOG.info(getName() + " completed successfully!");
51                      } catch (Exception e) {
52                          LOG.error(getName() + " did not complete", e);
53                      }
54                  }
55              };
56              try {
57                  execution.start();
58              } catch (OutOfMemoryError e) {
59                  // ignore
60              }
61          }
62  
63          Thread.sleep(THREAD_NUMBER);
64      }
65  
66      @Test
67      public void multiCallTest() {
68          try {
69              for (int i = 0; i < THREAD_NUMBER; i++) {
70                  CLIENT.getService(SyncopeService.class);
71              }
72          } catch (Exception e) {
73              fail(e::getMessage);
74          }
75      }
76  }