How do I use the unit-test framework in parallel with shared Simulink data dictionaries?

How do I use the unit-test framework in parallel with shared Simulink data dictionaries? I'm calling "runInParallel" to perform some tests on Simulink models with a shared data dictionary, and the tests fail because the data dictionary cannot be accessed concurrently by all parallel workers.

 Accepted Answer

The "runInParallel" function does not support all kinds of data access due to its multi-threaded nature. However, Simulink has the "Simulink.data.dictionary.setupWorkerCache" function which can be ran before the data dictionary is modified to ensure each parallel worker is only modifying their copy of the data dictionary.
To embed this function into the test suite, first create a TestRunnerPlugin that calls this function and then calls the superclass to run the actual tests. This way we ensure that the parallel pool has been initialized, the "setupWorkerCache" function called, and then the actual tests are ran. Save the below code in a file called "CacheSetupPlugin.m":
classdef CacheSetupPlugin < matlab.unittest.plugins.TestRunnerPlugin
methods (Access = protected)
%%overload the superclass' method
function runTestSuite(plugin, pluginData)
Simulink.data.dictionary.setupWorkerCache
% Once setup is complete, invoke super class method to run the test suite
runTestSuite@matlab.unittest.plugins.TestRunnerPlugin(plugin, pluginData);
Simulink.data.dictionary.cleanupWorkerCache
end
end
methods (Hidden)
function bool = supportsParallel(~)
bool = true;
end
end
end
Now run the test suite like so:
>> suite = matlab.unittest.TestSuite.fromClass(?testDataDictionaryInteractions)
>> runner = matlab.unittest.TestRunner.withTextOutput()
>> runner.addPlugin(CacheSetupPlugin)
>> result = runInParallel(runner,suite)
See the following links for added information:

More Answers (0)

Categories

Find more on Test Model Components in Help Center and File Exchange

Products

Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!