use of com.walmartlabs.concord.svm.Runtime in project concord by walmartlabs.
the class SaveLastErrorCommandTest method test.
@Test
public void test() throws Exception {
ObjectMapper om = new ObjectMapper();
PersistenceService persistenceService = mock(PersistenceService.class);
Runtime runtime = mock(Runtime.class);
when(runtime.getService(eq(ObjectMapper.class))).thenReturn(om);
when(runtime.getService(eq(PersistenceService.class))).thenReturn(persistenceService);
MyException error = new MyException("BOOM1");
error.setSomeCyclicField(error);
Frame rootFrame = Frame.builder().root().locals(Collections.singletonMap(Frame.LAST_EXCEPTION_KEY, error)).build();
State state = new InMemoryState(rootFrame);
ThreadId threadId = state.getRootThreadId();
// ---
SaveLastErrorCommand cmd = new SaveLastErrorCommand();
rootFrame.push(new SaveLastErrorCommand());
try {
cmd.eval(runtime, state, threadId);
} catch (MyException e) {
assertEquals(error, e);
}
ArgumentCaptor<PersistenceService.Writer> writerCaptor = ArgumentCaptor.forClass(PersistenceService.Writer.class);
verify(persistenceService, times(1)).persistFile(eq(Constants.Files.OUT_VALUES_FILE_NAME), writerCaptor.capture());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
writerCaptor.getValue().write(bos);
assertThat(bos.toString(), containsString("\"message\":\"BOOM1\""));
assertThat(bos.toString(), containsString("\"someCyclicField\":1"));
assertThat(bos.toString(), containsString("\"@id\":1"));
}
use of com.walmartlabs.concord.svm.Runtime in project concord by walmartlabs.
the class ParallelCommand method execute.
@Override
protected void execute(Runtime runtime, State state, ThreadId threadId) {
Frame frame = state.peekFrame(threadId);
frame.pop();
// parallel execution consist of "forks" for each command running in separate threads
// and a combined "join" executing in the parent (current) thread
List<Map.Entry<ThreadId, Command>> forks = commands.stream().map(e -> new AbstractMap.SimpleEntry<>(state.nextThreadId(), e)).collect(Collectors.toList());
ParallelBlockOptions opts = Objects.requireNonNull(getStep().getOptions());
Command outVarsCommand;
if (!opts.outExpr().isEmpty()) {
Map<String, Object> accumulator = new ConcurrentHashMap<>();
outVarsCommand = new CollectVariablesCommand(accumulator);
frame.push(new EvalVariablesCommand(runtime.getService(Context.class), accumulator, opts.outExpr(), frame));
} else {
outVarsCommand = new CopyVariablesCommand(opts.out(), State::peekFrame, frame);
}
Collection<ThreadId> forkIds = forks.stream().map(Map.Entry::getKey).collect(Collectors.toSet());
frame.push(new JoinCommand(forkIds));
Collections.reverse(forks);
forks.forEach(f -> {
Command cmd = new ForkCommand(f.getKey(), outVarsCommand, f.getValue());
frame.push(cmd);
});
}
Aggregations