Search in sources :

Example 1 with PersistenceService

use of com.walmartlabs.concord.runtime.v2.runner.PersistenceService in project concord by walmartlabs.

the class SaveLastErrorCommand method eval.

@Override
public void eval(Runtime runtime, State state, ThreadId threadId) throws Exception {
    Frame frame = state.peekFrame(threadId);
    frame.pop();
    Exception e = VMUtils.assertLocal(state, threadId, Frame.LAST_EXCEPTION_KEY);
    ObjectMapper om = runtime.getService(ObjectMapper.class);
    PersistenceService persistenceService = runtime.getService(PersistenceService.class);
    Map<String, Object> currentOut = persistenceService.loadPersistedFile(Constants.Files.OUT_VALUES_FILE_NAME, in -> om.readValue(in, MAP_TYPE));
    Map<String, Object> outValues = new HashMap<>(currentOut != null ? currentOut : Collections.emptyMap());
    outValues.put(Constants.Context.LAST_ERROR_KEY, serialize(e));
    persistenceService.persistFile(Constants.Files.OUT_VALUES_FILE_NAME, out -> om.writeValue(out, outValues));
    throw e;
}
Also used : PersistenceService(com.walmartlabs.concord.runtime.v2.runner.PersistenceService) HashMap(java.util.HashMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with PersistenceService

use of com.walmartlabs.concord.runtime.v2.runner.PersistenceService 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)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 PersistenceService (com.walmartlabs.concord.runtime.v2.runner.PersistenceService)2 Runtime (com.walmartlabs.concord.svm.Runtime)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 HashMap (java.util.HashMap)1 Test (org.junit.jupiter.api.Test)1