Skip to main content
summaryrefslogtreecommitdiffstats
blob: a51ae5616023c193c7fa12f3fb56ddb29fd87f18 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.client.integration.tests.integration.skynet.core;

import static org.eclipse.osee.client.demo.DemoChoice.OSEE_CLIENT_DEMO;
import static org.eclipse.osee.framework.core.enums.CoreBranches.COMMON;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.osee.client.integration.tests.integration.skynet.core.utils.TestUtil;
import org.eclipse.osee.client.test.framework.OseeClientIntegrationRule;
import org.eclipse.osee.client.test.framework.OseeLogMonitorRule;
import org.eclipse.osee.client.test.framework.TestInfo;
import org.eclipse.osee.framework.core.data.BranchId;
import org.eclipse.osee.framework.core.enums.CoreArtifactTypes;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.core.enums.CoreBranches;
import org.eclipse.osee.framework.core.enums.DeletionFlag;
import org.eclipse.osee.framework.core.enums.LoadLevel;
import org.eclipse.osee.framework.core.operation.IOperation;
import org.eclipse.osee.framework.core.operation.OperationLogger;
import org.eclipse.osee.framework.core.operation.Operations;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.OseeStateException;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactCache;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactLoader;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactPersistenceManager;
import org.eclipse.osee.framework.skynet.core.artifact.LoadType;
import org.eclipse.osee.framework.skynet.core.artifact.search.ArtifactQuery;
import org.eclipse.osee.framework.skynet.core.transaction.SkynetTransaction;
import org.eclipse.osee.framework.skynet.core.transaction.TransactionManager;
import org.junit.After;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;

/**
 * @author Donald G. Dunne
 */
public class ArtifactLoaderTest {

   @Rule
   public OseeClientIntegrationRule integration = new OseeClientIntegrationRule(OSEE_CLIENT_DEMO);

   @Rule
   public OseeLogMonitorRule monitorRule = new OseeLogMonitorRule();

   @Rule
   public TestInfo testInfo = new TestInfo();

   private static final int TOTAL_THREADS = 7;
   private static final int NUM_ARTIFACTS = 100;
   private static final String ATTRIBUTE_VALUE = "now is the time";
   private static final BranchId branch = CoreBranches.COMMON;

   @After
   public void tearDown() throws Exception {
      SkynetTransaction transaction = TransactionManager.createTransaction(branch, testInfo.getQualifiedTestName());
      List<Artifact> artifacts =
         ArtifactQuery.getArtifactListFromName(testInfo.getQualifiedTestName(), branch, DeletionFlag.EXCLUDE_DELETED);
      ArtifactPersistenceManager.deleteArtifactCollection(transaction, false, artifacts);
      transaction.execute();
   }

   /**
    * This test was created because artifact loading is loading multiple attribute instances for the same attribute in
    * the database. This will create 100 artifacts with an attribute that should only be a singleton, decache the
    * artifacts so they are reloaded and then reload them in 8 different threads. Due to the intermittent nature, it may
    * need to be run multiple times.<br>
    * <br>
    * Another interesting side-effect, is this test also gives a repeatable case for duplicate relation loading. These
    * are caught by the SevereLoggingMonitor.
    */
   @Test(timeout = 10000)
   public void testThreadSafeLoading() throws Exception {
      // Create some software artifacts
      SkynetTransaction transaction = TransactionManager.createTransaction(COMMON, "ArtifactLoaderTest");
      Collection<Artifact> artifacts = TestUtil.createSimpleArtifacts(CoreArtifactTypes.GlobalPreferences,
         NUM_ARTIFACTS, "ArtifactLoaderTest", COMMON);
      for (Artifact artifact : artifacts) {
         artifact.setName("ArtifactLoaderTest");
         artifact.addAttribute(CoreAttributeTypes.DefaultMailServer, ATTRIBUTE_VALUE);
         artifact.persist(transaction);
      }
      transaction.execute();

      // now, de-cache them
      for (Artifact artifact : artifacts) {
         ArtifactCache.deCache(artifact);
      }

      Collection<LoadArtifacts> tasks = new ArrayList<>();
      for (int x = 1; x <= TOTAL_THREADS; x++) {
         tasks.add(new LoadArtifacts());
      }

      ExecutorService executor = Executors.newFixedThreadPool(TOTAL_THREADS, new LoadThreadFactory());
      List<Future<List<Artifact>>> futures = executor.invokeAll(tasks, 81, TimeUnit.SECONDS);
      int completed = 0;
      int cancelled = 0;
      for (Future<?> future : futures) {
         if (future.isCancelled()) {
            cancelled++;
         }
         if (future.isDone()) {
            completed++;
         }
      }
      executor.shutdownNow();
      String message = String.format("Hit timeout value before threads were completed - completed[%s] cancelled[%s]",
         completed, cancelled);
      Assert.assertEquals(message, TOTAL_THREADS, completed);

      // Load and check artifacts
      artifacts = ArtifactQuery.getArtifactListFromName("ArtifactLoaderTest", COMMON, DeletionFlag.EXCLUDE_DELETED);
      Assert.assertEquals(NUM_ARTIFACTS, artifacts.size());
      for (Artifact artifact : artifacts) {
         Assert.assertEquals(ATTRIBUTE_VALUE, artifact.getSoleAttributeValue(CoreAttributeTypes.DefaultMailServer));
         Assert.assertEquals(1, artifact.getAttributesToStringList(CoreAttributeTypes.DefaultMailServer).size());
      }
   }

   @Test(timeout = 5000)
   public void testThreadSafeLoadingSameArtifact() throws Exception {
      // Create some software artifacts
      SkynetTransaction transaction = TransactionManager.createTransaction(COMMON, "ArtifactLoaderTest");
      Artifact testArt =
         TestUtil.createSimpleArtifact(CoreArtifactTypes.GlobalPreferences, "ArtifactLoaderTest", COMMON);
      testArt.setName("ArtifactLoaderTest");
      testArt.addAttribute(CoreAttributeTypes.DefaultMailServer, ATTRIBUTE_VALUE);
      testArt.persist(transaction);
      transaction.execute();

      final String guid = testArt.getGuid();

      // now, de-cache them
      ArtifactCache.deCache(testArt);

      List<Callable<String>> callables = new LinkedList<>();

      int size = 4;
      //create 4 threads to load the same artifact
      for (int i = 0; i < size; i++) {
         MultiThreadCallable mtc = new MultiThreadCallable(guid);
         callables.add(mtc);
      }

      ExecutorService executor = Executors.newFixedThreadPool(size);
      for (Future<String> future : executor.invokeAll(callables, 81, TimeUnit.SECONDS)) {
         Assert.assertEquals(ATTRIBUTE_VALUE, future.get());
      }

      //double check
      ArtifactCache.deCache(testArt);
      for (Future<String> future : executor.invokeAll(callables, 81, TimeUnit.SECONDS)) {
         Assert.assertEquals(ATTRIBUTE_VALUE, future.get());
      }

   }

   @Test(timeout = 5000)
   public void testLoadingNonExistingArtifactMultipleTimes() throws InterruptedException {
      final List<Integer> artIds = new LinkedList<>();
      artIds.add(Integer.MAX_VALUE);
      artIds.add(Integer.MIN_VALUE);
      Job job1 = Operations.executeAsJob(new IOperation() {

         @Override
         public String getName() {
            return "Job1";
         }

         @Override
         public IStatus run(SubMonitor subMonitor) {
            List<Artifact> artifacts;
            try {
               artifacts = ArtifactLoader.loadArtifactIds(artIds, CoreBranches.COMMON, LoadLevel.ALL,
                  LoadType.RELOAD_CACHE, DeletionFlag.EXCLUDE_DELETED);
               Assert.assertTrue(artifacts.isEmpty());
            } catch (OseeCoreException ex) {
               // do nothing
            }

            return Status.OK_STATUS;
         }

         @Override
         public OperationLogger getLogger() {
            return null;
         }
      }, true);

      Job job2 = Operations.executeAsJob(new IOperation() {

         @Override
         public String getName() {
            return "Job2";
         }

         @Override
         public IStatus run(SubMonitor subMonitor) {
            List<Artifact> artifacts;
            try {
               artifacts = ArtifactLoader.loadArtifactIds(artIds, CoreBranches.COMMON, LoadLevel.ALL,
                  LoadType.RELOAD_CACHE, DeletionFlag.EXCLUDE_DELETED);
               Assert.assertTrue(artifacts.isEmpty());
            } catch (OseeCoreException ex) {
               // do nothing
            }

            return Status.OK_STATUS;
         }

         @Override
         public OperationLogger getLogger() {
            return null;
         }
      }, true);

      job1.join();
      job2.join();

   }

   private final class MultiThreadCallable implements Callable<String> {

      private final String guid;

      public MultiThreadCallable(String guid) {
         this.guid = guid;
      }

      @Override
      public String call() throws Exception {
         Artifact art = ArtifactQuery.getArtifactFromId(guid, CoreBranches.COMMON);
         return art.getSoleAttributeValueAsString(CoreAttributeTypes.DefaultMailServer, "");
      }

   }

   private static final class LoadArtifacts implements Callable<List<Artifact>> {

      @Override
      public List<Artifact> call() throws Exception {
         List<Artifact> artifacts =
            ArtifactQuery.getArtifactListFromName("ArtifactLoaderTest", COMMON, DeletionFlag.EXCLUDE_DELETED);
         if (artifacts.size() != NUM_ARTIFACTS) {
            throw new OseeStateException("Should have loaded %d not %d", NUM_ARTIFACTS, artifacts.size());
         }
         return artifacts;
      }
   };

   private static final class LoadThreadFactory implements ThreadFactory {
      private static int threadCount = 0;

      @Override
      public Thread newThread(Runnable target) {
         return new Thread(target, "Loading Thread: " + ++threadCount);
      }
   }
}

Back to the top