/// <summary> /// Test that ScenarioArrays containing a single value are unwrapped when calling calculateAsync(). /// </summary> public virtual void unwrapScenarioResultsAsync() { ScenarioArray <string> scenarioResult = ScenarioArray.of("foo"); ScenarioResultFunction fn = new ScenarioResultFunction(TestingMeasures.PRESENT_VALUE, scenarioResult); CalculationTaskCell cell = CalculationTaskCell.of(0, 0, TestingMeasures.PRESENT_VALUE, NATURAL); CalculationTask task = CalculationTask.of(TARGET, fn, cell); Column column = Column.of(TestingMeasures.PRESENT_VALUE); CalculationTasks tasks = CalculationTasks.of(ImmutableList.of(task), ImmutableList.of(column)); // using the direct executor means there is no need to close/shutdown the runner CalculationTaskRunner test = CalculationTaskRunner.of(MoreExecutors.newDirectExecutorService()); Listener listener = new Listener(); MarketData marketData = MarketData.empty(VAL_DATE); test.calculateAsync(tasks, marketData, REF_DATA, listener); CalculationResult calculationResult1 = listener.result; //JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET: //ORIGINAL LINE: com.opengamma.strata.collect.result.Result<?> result1 = calculationResult1.getResult(); Result <object> result1 = calculationResult1.Result; // Check the result contains the string directly, not the result wrapping the string assertThat(result1).hasValue("foo"); test.calculateMultiScenarioAsync(tasks, ScenarioMarketData.of(1, marketData), REF_DATA, listener); CalculationResult calculationResult2 = listener.result; //JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET: //ORIGINAL LINE: com.opengamma.strata.collect.result.Result<?> result2 = calculationResult2.getResult(); Result <object> result2 = calculationResult2.Result; // Check the result contains the scenario result wrapping the string assertThat(result2).hasValue(scenarioResult); }
//------------------------------------------------------------------------- public virtual void test_requirements() { CalculationFunctions functions = CalculationFunctions.of(ImmutableMap.of(typeof(TestTarget), new TestFunction())); CalculationRules calculationRules = CalculationRules.of(functions, USD); IList <TestTarget> targets = ImmutableList.of(TARGET1); IList <Column> columns = ImmutableList.of(Column.of(TestingMeasures.PRESENT_VALUE)); CalculationTasks test = CalculationTasks.of(calculationRules, targets, columns); MarketDataRequirements requirements = test.requirements(REF_DATA); //JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET: //ORIGINAL LINE: java.util.Set<? extends com.opengamma.strata.data.MarketDataId<?>> nonObservables = requirements.getNonObservables(); ISet <MarketDataId <object> > nonObservables = requirements.NonObservables; //JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET: //ORIGINAL LINE: com.google.common.collect.ImmutableSet<? extends com.opengamma.strata.data.ObservableId> observables = requirements.getObservables(); ImmutableSet <ObservableId> observables = requirements.Observables; ImmutableSet <ObservableId> timeSeries = requirements.TimeSeries; assertThat(nonObservables).hasSize(1); assertThat(nonObservables.GetEnumerator().next()).isEqualTo(TestId.of("1")); //JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET: //ORIGINAL LINE: com.opengamma.strata.data.MarketDataId<?> observableId = com.opengamma.strata.calc.marketdata.TestObservableId.of("2", CalculationTaskTest.OBS_SOURCE); MarketDataId <object> observableId = TestObservableId.of("2", CalculationTaskTest.OBS_SOURCE); assertThat(observables).hasSize(1); assertThat(observables.GetEnumerator().next()).isEqualTo(observableId); //JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET: //ORIGINAL LINE: com.opengamma.strata.data.MarketDataId<?> timeSeriesId = com.opengamma.strata.calc.marketdata.TestObservableId.of("3", CalculationTaskTest.OBS_SOURCE); MarketDataId <object> timeSeriesId = TestObservableId.of("3", CalculationTaskTest.OBS_SOURCE); assertThat(timeSeries).hasSize(1); assertThat(timeSeries.GetEnumerator().next()).isEqualTo(timeSeriesId); }
//------------------------------------------------------------------------- public virtual void test_of() { CalculationFunctions functions = CalculationFunctions.of(ImmutableMap.of(typeof(TestTarget), new TestFunction())); IList <TestTarget> targets = ImmutableList.of(TARGET1, TARGET2); IList <Column> columns = ImmutableList.of(Column.of(TestingMeasures.PRESENT_VALUE), Column.of(TestingMeasures.PAR_RATE)); CalculationRules calculationRules = CalculationRules.of(functions, USD); CalculationTasks test = CalculationTasks.of(calculationRules, targets, columns); assertThat(test.Targets).hasSize(2); assertThat(test.Targets).containsExactly(TARGET1, TARGET2); assertThat(test.Columns).hasSize(2); assertThat(test.Columns).containsExactly(Column.of(TestingMeasures.PRESENT_VALUE), Column.of(TestingMeasures.PAR_RATE)); assertThat(test.Tasks).hasSize(2); assertThat(test.Tasks[0].Target).isEqualTo(TARGET1); assertThat(test.Tasks[0].Cells.size()).isEqualTo(2); assertThat(test.Tasks[0].Cells.get(0).RowIndex).isEqualTo(0); assertThat(test.Tasks[0].Cells.get(0).ColumnIndex).isEqualTo(0); assertThat(test.Tasks[0].Cells.get(0).Measure).isEqualTo(TestingMeasures.PRESENT_VALUE); assertThat(test.Tasks[0].Cells.get(1).RowIndex).isEqualTo(0); assertThat(test.Tasks[0].Cells.get(1).ColumnIndex).isEqualTo(1); assertThat(test.Tasks[0].Cells.get(1).Measure).isEqualTo(TestingMeasures.PAR_RATE); assertThat(test.Tasks[1].Target).isEqualTo(TARGET2); assertThat(test.Tasks[1].Cells.size()).isEqualTo(2); assertThat(test.Tasks[1].Cells.get(0).RowIndex).isEqualTo(1); assertThat(test.Tasks[1].Cells.get(0).ColumnIndex).isEqualTo(0); assertThat(test.Tasks[1].Cells.get(0).Measure).isEqualTo(TestingMeasures.PRESENT_VALUE); assertThat(test.Tasks[1].Cells.get(1).RowIndex).isEqualTo(1); assertThat(test.Tasks[1].Cells.get(1).ColumnIndex).isEqualTo(1); assertThat(test.Tasks[1].Cells.get(1).Measure).isEqualTo(TestingMeasures.PAR_RATE); coverImmutableBean(test); assertNotNull(CalculationTasks.meta()); }
//------------------------------------------------------------------------- public virtual void testToString() { IList <TestTarget> targets = ImmutableList.of(TARGET1, TARGET1); IList <Column> columns = ImmutableList.of(Column.of(TestingMeasures.PRESENT_VALUE), Column.of(TestingMeasures.PRESENT_VALUE), Column.of(TestingMeasures.PRESENT_VALUE)); CalculationRules rules = CalculationRules.of(CALC_FUNCTIONS, USD); CalculationTasks task = CalculationTasks.of(rules, targets, columns); assertThat(task.ToString()).isEqualTo("CalculationTasks[grid=2x3]"); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test(timeOut = 5000) public void interruptHangingResultsListener() throws InterruptedException //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: public virtual void interruptHangingResultsListener() { HangingFunction fn = new HangingFunction(); CalculationTaskCell cell = CalculationTaskCell.of(0, 0, TestingMeasures.PRESENT_VALUE, NATURAL); CalculationTask task = CalculationTask.of(TARGET, fn, cell); Column column = Column.of(TestingMeasures.PRESENT_VALUE); CalculationTasks tasks = CalculationTasks.of(ImmutableList.of(task), ImmutableList.of(column)); ExecutorService executor = Executors.newSingleThreadExecutor(); try { CalculationTaskRunner test = CalculationTaskRunner.of(executor); MarketData marketData = MarketData.empty(VAL_DATE); AtomicBoolean shouldNeverComplete = new AtomicBoolean(); AtomicBoolean interrupted = new AtomicBoolean(); AtomicReference <Exception> thrown = new AtomicReference <Exception>(); ResultsListener listener = new ResultsListener(); test.calculateAsync(tasks, marketData, REF_DATA, listener); System.Threading.CountdownEvent latch = new System.Threading.CountdownEvent(1); Thread thread = new Thread(() => { try { listener.result(); shouldNeverComplete.set(true); } catch (Exception ex) { interrupted.set(Thread.CurrentThread.Interrupted); thrown.set(ex); } latch.Signal(); }); // run the thread, wait until properly started, then interrupt, wait until properly handled thread.Start(); while (!fn.started) { } thread.Interrupt(); latch.await(); // asserts assertEquals(interrupted.get(), true); assertEquals(shouldNeverComplete.get(), false); assertEquals(thrown.get() is Exception, true); assertEquals(thrown.get().Cause is InterruptedException, true); } finally { executor.shutdownNow(); } }
//------------------------------------------------------------------------- //JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test(timeOut = 5000) public void interruptHangingCalculate() throws InterruptedException //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: public virtual void interruptHangingCalculate() { HangingFunction fn = new HangingFunction(); CalculationTaskCell cell = CalculationTaskCell.of(0, 0, TestingMeasures.PRESENT_VALUE, NATURAL); CalculationTask task = CalculationTask.of(TARGET, fn, cell); Column column = Column.of(TestingMeasures.PRESENT_VALUE); CalculationTasks tasks = CalculationTasks.of(ImmutableList.of(task), ImmutableList.of(column)); // using the direct executor means there is no need to close/shutdown the runner CalculationTaskRunner test = CalculationTaskRunner.of(MoreExecutors.newDirectExecutorService()); MarketData marketData = MarketData.empty(VAL_DATE); AtomicBoolean shouldNeverThrow = new AtomicBoolean(); AtomicBoolean interrupted = new AtomicBoolean(); AtomicReference <Results> results = new AtomicReference <Results>(); System.Threading.CountdownEvent latch = new System.Threading.CountdownEvent(1); Thread thread = new Thread(() => { try { Results result = test.calculate(tasks, marketData, REF_DATA); interrupted.set(Thread.CurrentThread.Interrupted); results.set(result); } catch (Exception) { shouldNeverThrow.set(true); } latch.Signal(); }); // run the thread, wait until properly started, then interrupt, wait until properly handled thread.Start(); while (!fn.started) { } thread.Interrupt(); latch.await(); // asserts assertEquals(interrupted.get(), true); assertEquals(shouldNeverThrow.get(), false); //JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET: //ORIGINAL LINE: com.opengamma.strata.collect.result.Result<?> result00 = results.get().get(0, 0); Result <object> result00 = results.get().get(0, 0); assertEquals(result00.Failure, true); assertEquals(result00.Failure.Reason, FailureReason.CALCULATION_FAILED); assertEquals(result00.Failure.Message.Contains("Runtime interrupted"), true); }
//------------------------------------------------------------------------- /// <summary> /// Tests that running an empty list of tasks completes and returns a set of results with zero rows. /// </summary> public virtual void runWithNoTasks() { Column column = Column.of(TestingMeasures.PRESENT_VALUE); CalculationTasks tasks = CalculationTasks.of(ImmutableList.of(), ImmutableList.of(column)); // using the direct executor means there is no need to close/shutdown the runner CalculationTaskRunner test = CalculationTaskRunner.of(MoreExecutors.newDirectExecutorService()); MarketData marketData = MarketData.empty(VAL_DATE); Results results = test.calculate(tasks, marketData, REF_DATA); assertThat(results.RowCount).isEqualTo(0); assertThat(results.ColumnCount).isEqualTo(1); assertThat(results.Columns.get(0).Measure).isEqualTo(TestingMeasures.PRESENT_VALUE); }
/// <summary> /// Test that ScenarioArrays containing multiple values are an error. /// </summary> public virtual void unwrapMultipleScenarioResults() { ScenarioArray <string> scenarioResult = ScenarioArray.of("foo", "bar"); ScenarioResultFunction fn = new ScenarioResultFunction(TestingMeasures.PAR_RATE, scenarioResult); CalculationTaskCell cell = CalculationTaskCell.of(0, 0, TestingMeasures.PAR_RATE, NATURAL); CalculationTask task = CalculationTask.of(TARGET, fn, cell); Column column = Column.of(TestingMeasures.PAR_RATE); CalculationTasks tasks = CalculationTasks.of(ImmutableList.of(task), ImmutableList.of(column)); // using the direct executor means there is no need to close/shutdown the runner CalculationTaskRunner test = CalculationTaskRunner.of(MoreExecutors.newDirectExecutorService()); MarketData marketData = MarketData.empty(VAL_DATE); assertThrowsIllegalArg(() => test.calculate(tasks, marketData, REF_DATA)); }
//------------------------------------------------------------------------- // Test that ScenarioArrays containing a single value are unwrapped. //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public void unwrapScenarioResults() throws Exception public virtual void unwrapScenarioResults() { ScenarioArray <string> scenarioResult = ScenarioArray.of("foo"); ScenarioResultFunction fn = new ScenarioResultFunction(TestingMeasures.PRESENT_VALUE, scenarioResult); CalculationTaskCell cell = CalculationTaskCell.of(0, 0, TestingMeasures.PRESENT_VALUE, NATURAL); CalculationTask task = CalculationTask.of(TARGET, fn, cell); Column column = Column.of(TestingMeasures.PRESENT_VALUE); CalculationTasks tasks = CalculationTasks.of(ImmutableList.of(task), ImmutableList.of(column)); // using the direct executor means there is no need to close/shutdown the runner CalculationTaskRunner test = CalculationTaskRunner.of(MoreExecutors.newDirectExecutorService()); MarketData marketData = MarketData.empty(VAL_DATE); Results results1 = test.calculate(tasks, marketData, REF_DATA); //JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET: //ORIGINAL LINE: com.opengamma.strata.collect.result.Result<?> result1 = results1.get(0, 0); Result <object> result1 = results1.get(0, 0); // Check the result contains the string directly, not the result wrapping the string assertThat(result1).hasValue("foo"); Results results2 = test.calculateMultiScenario(tasks, ScenarioMarketData.of(1, marketData), REF_DATA); //JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET: //ORIGINAL LINE: com.opengamma.strata.collect.result.Result<?> result2 = results2.get(0, 0); Result <object> result2 = results2.get(0, 0); // Check the result contains the scenario result wrapping the string assertThat(result2).hasValue(scenarioResult); ResultsListener resultsListener = new ResultsListener(); test.calculateAsync(tasks, marketData, REF_DATA, resultsListener); CompletableFuture <Results> future = resultsListener.Future; // The future is guaranteed to be done because everything is running on a single thread assertThat(future.Done).True; Results results3 = future.get(); //JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET: //ORIGINAL LINE: com.opengamma.strata.collect.result.Result<?> result3 = results3.get(0, 0); Result <object> result3 = results3.get(0, 0); // Check the result contains the string directly, not the result wrapping the string assertThat(result3).hasValue("foo"); }