Search in sources :

Example 1 with Runtime

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"));
}
Also used : PersistenceService(com.walmartlabs.concord.runtime.v2.runner.PersistenceService) Runtime(com.walmartlabs.concord.svm.Runtime) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 2 with Runtime

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);
    });
}
Also used : Context(com.walmartlabs.concord.runtime.v2.sdk.Context) ParallelBlock(com.walmartlabs.concord.runtime.v2.model.ParallelBlock) java.util(java.util) ParallelBlockOptions(com.walmartlabs.concord.runtime.v2.model.ParallelBlockOptions) Runtime(com.walmartlabs.concord.svm.Runtime) com.walmartlabs.concord.svm(com.walmartlabs.concord.svm) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) EvalContextFactory(com.walmartlabs.concord.runtime.v2.runner.el.EvalContextFactory) Collectors(java.util.stream.Collectors) ExpressionEvaluator(com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator) Serializable(java.io.Serializable) ParallelBlockOptions(com.walmartlabs.concord.runtime.v2.model.ParallelBlockOptions) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

Runtime (com.walmartlabs.concord.svm.Runtime)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ParallelBlock (com.walmartlabs.concord.runtime.v2.model.ParallelBlock)1 ParallelBlockOptions (com.walmartlabs.concord.runtime.v2.model.ParallelBlockOptions)1 PersistenceService (com.walmartlabs.concord.runtime.v2.runner.PersistenceService)1 EvalContextFactory (com.walmartlabs.concord.runtime.v2.runner.el.EvalContextFactory)1 ExpressionEvaluator (com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator)1 Context (com.walmartlabs.concord.runtime.v2.sdk.Context)1 com.walmartlabs.concord.svm (com.walmartlabs.concord.svm)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Serializable (java.io.Serializable)1 java.util (java.util)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Collectors (java.util.stream.Collectors)1 Test (org.junit.jupiter.api.Test)1