Search in sources :

Example 1 with AsyncCallbackService

use of org.apache.helix.messaging.handling.AsyncCallbackService in project helix by apache.

the class TestHelixTaskExecutor method testCMTaskExecutor.

@Test()
public void testCMTaskExecutor() throws Exception {
    System.out.println("START TestCMTaskExecutor");
    String msgId = "TestMessageId";
    Message message = new Message(MessageType.TASK_REPLY, msgId);
    message.setMsgId(msgId);
    message.setSrcName("cm-instance-0");
    message.setTgtName("cm-instance-1");
    message.setTgtSessionId("1234");
    message.setFromState("Offline");
    message.setToState("Slave");
    message.setPartitionName("TestDB_0");
    message.setResourceName("TestDB");
    message.setStateModelDef("MasterSlave");
    MockManager manager = new MockManager("clusterName");
    HelixDataAccessor accessor = manager.getHelixDataAccessor();
    StateModelDefinition stateModelDef = new StateModelDefinition(StateModelConfigGenerator.generateConfigForMasterSlave());
    Builder keyBuilder = accessor.keyBuilder();
    accessor.setProperty(keyBuilder.stateModelDef("MasterSlave"), stateModelDef);
    MockHelixTaskExecutor executor = new MockHelixTaskExecutor();
    MockMasterSlaveStateModel stateModel = new MockMasterSlaveStateModel();
    executor.registerMessageHandlerFactory(MessageType.TASK_REPLY.name(), new AsyncCallbackService());
    NotificationContext context = new NotificationContext(manager);
    CurrentState currentStateDelta = new CurrentState("TestDB");
    currentStateDelta.setState("TestDB_0", "OFFLINE");
    StateModelFactory<MockMasterSlaveStateModel> stateModelFactory = new StateModelFactory<MockMasterSlaveStateModel>() {

        @Override
        public MockMasterSlaveStateModel createNewStateModel(String resource, String partitionName) {
            // TODO Auto-generated method stub
            return new MockMasterSlaveStateModel();
        }
    };
    HelixStateTransitionHandler handler = new HelixStateTransitionHandler(stateModelFactory, stateModel, message, context, currentStateDelta);
    HelixTask task = new HelixTask(message, context, handler, executor);
    executor.scheduleTask(task);
    for (int i = 0; i < 10; i++) {
        if (!executor.isDone(task.getTaskId())) {
            Thread.sleep(500);
        }
    }
    AssertJUnit.assertTrue(stateModel.stateModelInvoked);
    System.out.println("END TestCMTaskExecutor");
}
Also used : Message(org.apache.helix.model.Message) HelixTask(org.apache.helix.messaging.handling.HelixTask) Builder(org.apache.helix.PropertyKey.Builder) AsyncCallbackService(org.apache.helix.messaging.handling.AsyncCallbackService) MockHelixTaskExecutor(org.apache.helix.mock.participant.MockHelixTaskExecutor) MockMasterSlaveStateModel(org.apache.helix.mock.statemodel.MockMasterSlaveStateModel) StateModelDefinition(org.apache.helix.model.StateModelDefinition) CurrentState(org.apache.helix.model.CurrentState) StateModelFactory(org.apache.helix.participant.statemachine.StateModelFactory) MockManager(org.apache.helix.mock.MockManager) HelixStateTransitionHandler(org.apache.helix.messaging.handling.HelixStateTransitionHandler) Test(org.testng.annotations.Test)

Example 2 with AsyncCallbackService

use of org.apache.helix.messaging.handling.AsyncCallbackService in project helix by apache.

the class TestAsyncCallbackSvc method testAsyncCallbackSvc.

@Test(groups = { "unitTest" })
public void testAsyncCallbackSvc() throws Exception {
    AsyncCallbackService svc = new AsyncCallbackService();
    HelixManager manager = new MockHelixManager();
    NotificationContext changeContext = new NotificationContext(manager);
    Message msg = new Message(svc.getMessageType(), UUID.randomUUID().toString());
    msg.setTgtSessionId(manager.getSessionId());
    try {
        MessageHandler aHandler = svc.createHandler(msg, changeContext);
    } catch (HelixException e) {
        AssertJUnit.assertTrue(e.getMessage().indexOf(msg.getMsgId()) != -1);
    }
    Message msg2 = new Message("RandomType", UUID.randomUUID().toString());
    msg2.setTgtSessionId(manager.getSessionId());
    try {
        MessageHandler aHandler = svc.createHandler(msg2, changeContext);
    } catch (HelixException e) {
        AssertJUnit.assertTrue(e.getMessage().indexOf(msg2.getMsgId()) != -1);
    }
    Message msg3 = new Message(svc.getMessageType(), UUID.randomUUID().toString());
    msg3.setTgtSessionId(manager.getSessionId());
    msg3.setCorrelationId("wfwegw");
    try {
        MessageHandler aHandler = svc.createHandler(msg3, changeContext);
    } catch (HelixException e) {
        AssertJUnit.assertTrue(e.getMessage().indexOf(msg3.getMsgId()) != -1);
    }
    TestAsyncCallback callback = new TestAsyncCallback();
    String corrId = UUID.randomUUID().toString();
    svc.registerAsyncCallback(corrId, new TestAsyncCallback());
    svc.registerAsyncCallback(corrId, callback);
    List<Message> msgSent = new ArrayList<Message>();
    msgSent.add(new Message("Test", UUID.randomUUID().toString()));
    callback.setMessagesSent(msgSent);
    msg = new Message(svc.getMessageType(), UUID.randomUUID().toString());
    msg.setTgtSessionId("*");
    msg.setCorrelationId(corrId);
    MessageHandler aHandler = svc.createHandler(msg, changeContext);
    Map<String, String> resultMap = new HashMap<String, String>();
    aHandler.handleMessage();
    AssertJUnit.assertTrue(callback.isDone());
    AssertJUnit.assertTrue(callback._repliedMessageId.contains(msg.getMsgId()));
}
Also used : HelixManager(org.apache.helix.HelixManager) Message(org.apache.helix.model.Message) MessageHandler(org.apache.helix.messaging.handling.MessageHandler) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AsyncCallbackService(org.apache.helix.messaging.handling.AsyncCallbackService) NotificationContext(org.apache.helix.NotificationContext) HelixException(org.apache.helix.HelixException) Test(org.testng.annotations.Test)

Aggregations

AsyncCallbackService (org.apache.helix.messaging.handling.AsyncCallbackService)2 Message (org.apache.helix.model.Message)2 Test (org.testng.annotations.Test)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HelixException (org.apache.helix.HelixException)1 HelixManager (org.apache.helix.HelixManager)1 NotificationContext (org.apache.helix.NotificationContext)1 Builder (org.apache.helix.PropertyKey.Builder)1 HelixStateTransitionHandler (org.apache.helix.messaging.handling.HelixStateTransitionHandler)1 HelixTask (org.apache.helix.messaging.handling.HelixTask)1 MessageHandler (org.apache.helix.messaging.handling.MessageHandler)1 MockManager (org.apache.helix.mock.MockManager)1 MockHelixTaskExecutor (org.apache.helix.mock.participant.MockHelixTaskExecutor)1 MockMasterSlaveStateModel (org.apache.helix.mock.statemodel.MockMasterSlaveStateModel)1 CurrentState (org.apache.helix.model.CurrentState)1 StateModelDefinition (org.apache.helix.model.StateModelDefinition)1 StateModelFactory (org.apache.helix.participant.statemachine.StateModelFactory)1