LocalContext { public void registerNew(String workflowIdentifier, long version, String description,String correlationId, String clientElbId) { if (this.stateMachineDefinition.get() != null) { throw new IllegalStateException("A single thread cannot execute more than one workflow"); } stateMachineDefinition.set(new StateMachineDefinition(description,workflowIdentifier, version, new HashSet<>(), new HashSet<>(), correlationId, clientElbId)); tlUniqueEventCount.set(new MutableInt(0)); this.eventNames.set(new IdentityHashMap<>()); } LocalContext();   LocalContext(ThreadLocal<StateMachineDefinition> stateMachineDefinition, ThreadLocal<MutableInt> tlUniqueEventCount, ThreadLocal<IdentityHashMap<Event, String>> eventNames); void registerNew(String workflowIdentifier, long version, String description,String correlationId,
                            String clientElbId); void registerNewState(Long version,
                                 String name, String description,
                                 String hookIdentifier, String taskIdentifier,
                                 Long retryCount, Long timeout,
                                 List<EventDefinition> dependencySet, EventDefinition outputEvent
    ); void reset(); StateMachineDefinition getStateMachineDef(); boolean isWorkflowInterception(); void addEvents(EventData ...events); String generateEventName(Event event); EventDefinition checkExistingDefinition(final EventDefinition givenDefinition);  }
@Test public void testRegisterNew_shouldCreateALocalRegistration() throws Exception { localContext.registerNew("fooBar", 1, "someDescription", "someContextId", "defaultElbId" ); assertThat(tlStateMachineDef.get()).isEqualTo(new StateMachineDefinition("someDescription", "fooBar", 1l, new HashSet<>(), new HashSet<>(), "someContextId", "defaultElbId")); assertThat(tlMutableInteger.get().intValue()).isEqualTo(0); }
@Test(expected = IllegalStateException.class) public void shouldDisallowDuplicateRegistrations() throws Exception { localContext.registerNew("fooBar", 1, "someDescription","someContextId", "defaultElbId"); localContext.registerNew("fooBar", 1, "someOtherDescription", "someOtherContextId", "defaultElbId"); }
@Test public void shouldAllowSameMethodRegistrationFromDifferentThreads() throws Exception { final MutableObject<StateMachineDefinition> definitionOne = new MutableObject<>(null); final MutableObject<StateMachineDefinition> definitionTwo = new MutableObject<>(null); final Thread thread1 = new Thread(() -> { localContext.registerNew("fooBar", 1, "someDescription", "someContext", "defaultElbId"); definitionOne.setValue(tlStateMachineDef.get()); }); final Thread thread2 = new Thread(() -> { localContext.registerNew("fooBar", 1, "someDescription", "someContext", "defaultElbId"); definitionTwo.setValue(tlStateMachineDef.get()); }); thread1.start(); thread2.start(); thread1.join(); thread2.join(); assertThat(definitionOne.getValue()).isNotNull().isEqualTo(definitionTwo.getValue()).isEqualTo(new StateMachineDefinition( "someDescription", "fooBar", 1l, new HashSet<>(), new HashSet<>(), "someContext", "defaultElbId")); }