Package nexj.core.runtime

Examples of nexj.core.runtime.Instance


   }

   public void testWorkflowTrigger() throws Exception
   {
      Metaclass patientClass = getMetadata().getMetaclass("Patient");
      Instance parentPatient, childPatient;

      childPatient = (Instance)patientClass.invoke("new");
      childPatient.setValue("firstName", "child");
      parentPatient = (Instance)patientClass.invoke("new");
      parentPatient.setValue("firstName", "parent");
      ((InstanceArrayList)parentPatient.getValue("children")).add(childPatient);
      commit();

      getLogger().debug("Starting workflow on parent");
      parentPatient.invoke("triggerTestStart");
      commit();

      // Triggering child to wake from queue
      childPatient.invoke("triggerTest");
      commit();

      // Triggering child to wake from script
      childPatient.invoke("triggerTest");
      commit();

      // Triggering child, should not wake parent
      childPatient.invoke("triggerTest");
      commit();

      List workflowList = ((List)getMetadata().getMetaclass("SysWorkflow").invoke("forInstance",
         new Object[]{parentPatient}));

      assertEquals(1, workflowList.size());

      State state = (State)((Instance)workflowList.get(0)).getValue("state");

      assertEquals("6", state.toString());
      parentPatient.invoke("triggerTest");
      assertTrue(state.isFinal());
   }
View Full Code Here


    * a workflow try/catch.
    */
   public void testWorkflowTriggerException()
   {
      Metaclass patientClass = getMetadata().getMetaclass("Patient");
      Instance patient = (Instance)patientClass.invoke("new");

      patient.setValue("firstName", "John");
      commit();

      patient.invoke("triggerExceptionStart");
      commit();

      // Fire the trigger that throws an exception
      patient.invoke("triggerException");
      commit();
   }
View Full Code Here

         return;
      }

      Metaclass metaclass = getMetadata().getMetaclass("Principal");
      OID oid = new OID(new Object[]{Binary.parse("00000000000000000000000000000004")});
      Instance principal = m_context.findInstance(metaclass, oid);

      if (principal == null)
      {
         principal = new Instance(metaclass, m_context);
         principal.cache(oid);
      }

      Instance contact = new Instance(getMetadata().getMetaclass("Contact"), Instance.NEW, m_context);

      contact.setValue("firstName", FIRST_NAME);
      contact.setValue("lastName", LAST_NAME);
      contact.setValue("readPrincipal", principal);
      contact.setValue("version", Primitive.ZERO_INTEGER);
      commit();

      InstanceList resultList;

      resultList = Query.createRead(getMetadata().getMetaclass("Contact"), null,
View Full Code Here

   public void testWorkflowTryFinallyInFork() throws Exception
   {
      Metaclass assignmentClass = getMetadata().getMetaclass("SysWorkflowAssignment");
      InstanceList assignmentList;
      Metaclass primaryClass = getMetadata().getMetaclass("WorkflowTrace");
      Instance primary = (Instance)primaryClass.invoke("new");

      // Test 1: No exception
      primary.setValue("trace", null);
      primary.setValue("throwUncaughtEx", Boolean.FALSE);
      primary.setValue("throwInnerEx", Boolean.FALSE);
      primary.invoke("goForkTry");
      assignmentList = Query.createRead(assignmentClass, null, Boolean.TRUE, null, -1, 0, false, Query.SEC_NONE, m_context).read();
      assertEquals(1, assignmentList.size());
      primary.invoke("resumeForkTry");
      commit();
      assertTrue("scrStart;scrLeft1;scrLeft2;scrLeft3;scrRight1;scrLeft4;scrRight2;scrRight3;scrInnerFinally;scrOuterFinally;scrFinish;".equals(primary.getValue("trace")) ||
                 "scrStart;scrLeft1;scrLeft2;scrLeft3;scrLeft4;scrRight1;scrRight2;scrRight3;scrInnerFinally;scrOuterFinally;scrFinish;".equals(primary.getValue("trace")));
      assignmentList = Query.createRead(assignmentClass, null, Boolean.TRUE, null, -1, 0, false, Query.SEC_NONE, m_context).read();
      assertEquals(0, assignmentList.size());


      // Test 2: Caught Exception
      primary.setValue("trace", null);
      primary.setValue("throwUncaughtEx", Boolean.FALSE);
      primary.setValue("throwInnerEx", Boolean.TRUE);
      primary.invoke("goForkTry");
      commit();
     
      // If DEBUG log mode, then State gets sorted so that left branch of Fork/Join is preferred. This prevents
      // entry to right branch's Try block. Thus the right hand (inner) Finally may not be executed.
      assertTrue("scrStart;scrLeft1;scrLeft2;scrLeft3;scrRight1;scrLeft4;scrInnerFinally;scrCatch;scrOuterFinally;scrFinish;".equals(primary.getValue("trace")) ||
                 "scrStart;scrLeft1;scrLeft2;scrLeft3;scrLeft4;scrCatch;scrOuterFinally;scrFinish;".equals(primary.getValue("trace")));
      assignmentList = Query.createRead(assignmentClass, null, Boolean.TRUE, null, -1, 0, false, Query.SEC_NONE, m_context).read();
      assertEquals(0, assignmentList.size());

      // Test 3: Uncaught Exception
      primary.setValue("trace", null);
      primary.setValue("throwUncaughtEx", Boolean.TRUE);
      primary.setValue("throwInnerEx", Boolean.FALSE);

      try
      {
         primary.invoke("goForkTry");
         fail("Exception not thrown");
      }
      catch (ScriptingException ex)
      {
        
          // If DEBUG log mode, then State gets sorted so that left branch of Fork/Join is preferred. This prevents
          // entry to right branch's Try block. Thus the right hand (inner) Finally may not be executed.
         assertTrue("scrStart;scrLeft1;scrLeft2;scrLeft3;scrRight1;scrLeft4;scrInnerFinally;scrOuterFinally;".equals(primary.getValue("trace")) ||
                    "scrStart;scrLeft1;scrLeft2;scrLeft3;scrLeft4;scrOuterFinally;".equals(primary.getValue("trace")));
         assignmentList = Query.createRead(assignmentClass, null, Boolean.TRUE, null, -1, 0, false, Query.SEC_NONE, m_context).read();
         assertEquals(0, assignmentList.size());
         rollback();
      }
   }
View Full Code Here

    * Tests the Finally part of a Workflow Try block.
    */
   public void testWorkflowTryFinally() throws Exception
   {
      Metaclass primaryClass = getMetadata().getMetaclass("WorkflowTrace");
      Instance primary = (Instance)primaryClass.invoke("new");

      primary.invoke("go");
      commit();
      assertEquals("THROWBLOCK;END_THROWBLOCK;F_INNER;F_OUTER;", primary.getValue("trace"));


      primary.setValue("throwInnerEx", Boolean.TRUE);
      primary.setValue("trace", null);
      primary.invoke("go");
      commit();
      assertEquals("THROWBLOCK;CAUGHT_INNER;F_INNER;F_OUTER;", primary.getValue("trace"));


      primary.setValue("throwInnerEx", Boolean.FALSE);
      primary.setValue("throwOuterEx", Boolean.TRUE);
      primary.setValue("trace", null);
      primary.invoke("go");
      commit();
      assertEquals("THROWBLOCK;F_INNER;CAUGHT_OUTER;F_OUTER;", primary.getValue("trace"));


      primary.setValue("throwOuterEx", Boolean.FALSE);
      primary.setValue("throwUncaughtEx", Boolean.TRUE);
      primary.setValue("trace", null);

      try
      {
         primary.invoke("go");
         fail();
      }
      catch (ScriptingException ex)
      {
         assertEquals("THROWBLOCK;F_INNER;F_OUTER;", primary.getValue("trace"));
         rollback();
      }


      // An exception whose handler re-enters a try block
      primary.setValue("throwUncaughtEx", Boolean.FALSE);
      primary.setValue("throwOuterEx", Boolean.FALSE);
      primary.setValue("throwOuter2Ex", Boolean.TRUE);
      primary.setValue("trace", null);
      primary.invoke("go");
      commit();
      assertEquals("THROWBLOCK;F_INNER;CAUGHT_OUTER2;RUNNING_IN_OUTER_TRY;END_IN_OUTER_TRY;F_OUTER;", primary.getValue("trace"));
   }
View Full Code Here

   public void testWorkflowSemaphoreFlowControl() throws Exception
   {
      Metaclass workflowClass = getMetadata().getMetaclass(Metadata.WORKFLOW_CLASS_NAME);
      Metaclass primaryClass = getMetadata().getMetaclass("WorkflowTrace");
      Metaclass schedulerClass = getMetadata().getMetaclass("TestSysWorkflowSchedulerBatchJob");
      Instance primary = (Instance)primaryClass.invoke("new");
      InstanceList workflowList, assignmentList;
      Instance workflow, assignment;

      // Set scheduler time outside of the queue time window
      schedulerClass.setValue("time", Primitive.toTimestamp(Primitive.createInteger(15000)));


      // Test: no exception
      primary.setValue("trace", null);
      primary.setValue("throwOuterEx", Boolean.FALSE);
      primary.setValue("throwUncaughtEx", Boolean.FALSE);
      primary.invoke("goSemaphore");
      assertEquals("BEFORE;ASSIGNMENT_CREATE;", primary.getValue("trace"));
      workflowList = (InstanceList)workflowClass.invoke("forInstance",
         new Object[]{primary});
      assertEquals(1, workflowList.size());
      workflow = workflowList.getInstance(0);
      assignmentList = (InstanceList)workflow.getValue("assignments");
      assertEquals(1, assignmentList.size());
      assignment = assignmentList.getInstance(0);
      assertEquals(Boolean.TRUE, assignment.getValue("semaphore"));
      assertEquals(Primitive.ZERO_INTEGER, assignment.getValue("status"));
      commit();
      assignment.invoke("schedulerRun");
      assertEquals(Instance.DELETED, assignment.getState());
      assertEquals(Instance.DELETED, workflow.getState());
      assertEquals("BEFORE;ASSIGNMENT_CREATE;EXECUTE_SEMAPHORE;END_SEMAPHORE;ASSIGNMENT_DELETE;AFTER;", primary.getValue("trace"));
      commit();
     


      // Test: caught exception
      primary.setValue("throwOuterEx", Boolean.TRUE);
      primary.setValue("throwUncaughtEx", Boolean.FALSE);
      primary.setValue("trace", null);
      primary.invoke("goSemaphore");
      assertEquals("BEFORE;ASSIGNMENT_CREATE;", primary.getValue("trace"));
      workflowList = (InstanceList)workflowClass.invoke("forInstance",
         new Object[]{primary});
      assertEquals(1, workflowList.size());
      workflow = workflowList.getInstance(0);
      assignmentList = (InstanceList)workflow.getValue("assignments");
      assertEquals(1, assignmentList.size());
      assignment = assignmentList.getInstance(0);
      assertEquals(Boolean.TRUE, assignment.getValue("semaphore"));
      assertEquals(Primitive.ZERO_INTEGER, assignment.getValue("status"));
      commit();
      assignment.invoke("schedulerRun");
      assertEquals(Instance.DELETED, assignment.getState());
      assertEquals(Instance.DELETED, workflow.getState());
      assertEquals(Primitive.createInteger(2), assignment.getValue("status"));
      assertEquals("BEFORE;ASSIGNMENT_CREATE;EXECUTE_SEMAPHORE;ASSIGNMENT_DELETE;CAUGHT_OUTER;", primary.getValue("trace"));
      commit();


      // Test: uncaught exception
      primary.setValue("throwOuterEx", Boolean.FALSE);
      primary.setValue("throwUncaughtEx", Boolean.TRUE);
      primary.setValue("trace", null);
      primary.invoke("goSemaphore");
      assertEquals("BEFORE;ASSIGNMENT_CREATE;", primary.getValue("trace"));
      workflowList = (InstanceList)workflowClass.invoke("forInstance",
         new Object[]{primary});
      assertEquals(1, workflowList.size());
      workflow = workflowList.getInstance(0);
      assignmentList = (InstanceList)workflow.getValue("assignments");
      assertEquals(1, assignmentList.size());
      assignment = assignmentList.getInstance(0);
      assertEquals(Boolean.TRUE, assignment.getValue("semaphore"));
      assertEquals(Primitive.ZERO_INTEGER, assignment.getValue("status"));
      commit();

      try
      {
         assignment.invoke("schedulerRun");
         fail();
      }
      catch (ScriptingException ex)
      {
         assertEquals(Instance.DELETED, assignment.getState());
         assertEquals(Primitive.createInteger(2), assignment.getValue("status"));
         assertEquals("BEFORE;ASSIGNMENT_CREATE;EXECUTE_SEMAPHORE;ASSIGNMENT_DELETE;", primary.getValue("trace"));
         workflow.invoke("delete");
         commit();
      }
   }
View Full Code Here

   public void testWorkflowSemaphoresInFork() throws Exception
   {
      Metaclass workflowClass = getMetadata().getMetaclass(Metadata.WORKFLOW_CLASS_NAME);
      Metaclass primaryClass = getMetadata().getMetaclass("WorkflowTrace");
      Metaclass schedulerClass = getMetadata().getMetaclass("TestSysWorkflowSchedulerBatchJob");
      Instance primary = (Instance)primaryClass.invoke("new");
      InstanceList workflowList, assignmentList;
      Instance workflow, assignment1, assignment2;
      List invocationList;

      // Initialization
      primary.setValue("trace", null);
      primary.setValue("throwOuterEx", Boolean.FALSE);
      primary.setValue("throwUncaughtEx", Boolean.FALSE);


      /*
       * Test: Two semaphores in a fork.
       */
      schedulerClass.setValue("time", Primitive.toTimestamp(Primitive.createInteger(1000)));
      primary.setValue("trace", null);
      primary.setValue("throwInnerEx", Boolean.FALSE);
      primary.setValue("throwOuterEx", Boolean.FALSE);
      primary.invoke("goForkSemaphore");
      workflowList = (InstanceList)workflowClass.invoke("forInstance",
         new Object[]{primary});
      assertEquals(1, workflowList.size());
      workflow = workflowList.getInstance(0);
      assignmentList = (InstanceList)workflow.getValue("assignments");
      assertEquals(2, assignmentList.size());
      assignment1 = assignmentList.getInstance(0);
      assignment2 = assignmentList.getInstance(1);

      if (assignment1.getValue("caption").equals("semaphoreRight:2"))
      {
         Instance temp = assignment1;

         assignment1 = assignment2;
         assignment2 = temp;
      }

      assertEquals("semaphoreLeftCaption", assignment1.getValue("caption"));
      assertEquals("semaphoreRight:2", assignment2.getValue("caption"));
      assertTrue(
         (assignment1.getValue("status").equals(Primitive.ONE_INTEGER) &&
         assignment2.getValue("status").equals(Primitive.ZERO_INTEGER)) ||
         (assignment1.getValue("status").equals(Primitive.ZERO_INTEGER) &&
         assignment2.getValue("status").equals(Primitive.ONE_INTEGER))
      );
      assertEquals(Boolean.TRUE, assignment1.getValue("semaphore"));
      assertEquals(Primitive.ONE_INTEGER, assignment1.getValue("status"));
      assertEquals(Boolean.TRUE, assignment2.getValue("semaphore"));
      assertEquals(Primitive.ZERO_INTEGER, assignment2.getValue("status"));
      invocationList = (List)schedulerClass.getValue("asyncInvokeList");
      assertEquals(1, invocationList.size());
      assertEquals(assignment1.getOID(), invocationList.get(0));

      // Simulate run of left semaphore
      assignment1.invoke("schedulerRun");
      assertEquals(Instance.DELETED, assignment1.getState());
      assertEquals(Instance.DIRTY, workflow.getState());
      assertEquals("scrStart;scrLeft1;", primary.getValue("trace"));

      // Left semaphore done, right semaphore should be running
      assertEquals(Boolean.TRUE, assignment2.getValue("semaphore"));
      assertEquals(Primitive.ONE_INTEGER, assignment2.getValue("status"));
      assertEquals(2, invocationList.size());
      assertEquals(assignment2.getOID(), invocationList.get(1));

      // Simulate run of right semaphore
      assignment2.invoke("schedulerRun");
      assertEquals(Instance.DELETED, assignment2.getState());
      assertEquals(Instance.DELETED, workflow.getState());
      assertEquals("scrStart;scrLeft1;scrRight1;scrFinish;", primary.getValue("trace"));
      commit();



      /*
       * Test: Two semaphores in a fork, left Semaphore throws exception
       */
      ((List)schedulerClass.getValue("asyncInvokeList")).clear();
      schedulerClass.setValue("time", Primitive.toTimestamp(Primitive.createInteger(1000)));
      primary.setValue("trace", null);
      primary.setValue("throwInnerEx", Boolean.TRUE);
      primary.setValue("throwOuterEx", Boolean.FALSE);
      primary.invoke("goForkSemaphore");
      workflowList = (InstanceList)workflowClass.invoke("forInstance",
         new Object[]{primary});
      assertEquals(1, workflowList.size());
      workflow = workflowList.getInstance(0);
      assignmentList = (InstanceList)workflow.getValue("assignments");
      assertEquals(2, assignmentList.size());
      assignment1 = assignmentList.getInstance(0);
      assignment2 = assignmentList.getInstance(1);

      if (assignment1.getValue("caption").equals("semaphoreRight:2"))
      {
         Instance temp = assignment1;

         assignment1 = assignment2;
         assignment2 = temp;
      }

      assertEquals("semaphoreLeftCaption", assignment1.getValue("caption"));
      assertEquals("semaphoreRight:2", assignment2.getValue("caption"));
      assertTrue(
         (assignment1.getValue("status").equals(Primitive.ONE_INTEGER) &&
         assignment2.getValue("status").equals(Primitive.ZERO_INTEGER)) ||
         (assignment1.getValue("status").equals(Primitive.ZERO_INTEGER) &&
         assignment2.getValue("status").equals(Primitive.ONE_INTEGER))
      );
      assertEquals(Boolean.TRUE, assignment1.getValue("semaphore"));
      assertEquals(Primitive.ONE_INTEGER, assignment1.getValue("status"));
      assertEquals(Boolean.TRUE, assignment2.getValue("semaphore"));
      assertEquals(Primitive.ZERO_INTEGER, assignment2.getValue("status"));
      invocationList = (List)schedulerClass.getValue("asyncInvokeList");
      assertEquals(1, invocationList.size());
      assertEquals(assignment1.getOID(), invocationList.get(0));

      // Simulate run of left semaphore
      try
      {
         assignment1.invoke("schedulerRun");
         fail("No exception thrown");
      }
      catch (RuntimeException ex)
      {
         assertEquals(Instance.DELETED, assignment1.getState());
         assertEquals(Instance.DELETED, assignment2.getState());
         assertEquals("scrStart;scrLeft1;", primary.getValue("trace"));
         workflow.invoke("delete");
      }

      commit();



      /*
       * Test: Two semaphores in a fork, right (2nd) Semaphore throws exception
       */
      ((List)schedulerClass.getValue("asyncInvokeList")).clear();
      schedulerClass.setValue("time", Primitive.toTimestamp(Primitive.createInteger(1000)));
      primary.setValue("trace", null);
      primary.setValue("throwInnerEx", Boolean.FALSE);
      primary.setValue("throwOuterEx", Boolean.TRUE);
      primary.invoke("goForkSemaphore");
      workflowList = (InstanceList)workflowClass.invoke("forInstance",
         new Object[]{primary});
      assertEquals(1, workflowList.size());
      workflow = workflowList.getInstance(0);
      assignmentList = (InstanceList)workflow.getValue("assignments");
      assertEquals(2, assignmentList.size());
      assignment1 = assignmentList.getInstance(0);
      assignment2 = assignmentList.getInstance(1);

      if (assignment1.getValue("caption").equals("semaphoreRight:2"))
      {
         Instance temp = assignment1;

         assignment1 = assignment2;
         assignment2 = temp;
      }

View Full Code Here

   public void testWorkflowSemaphoreScheduler() throws Exception
   {
      Metaclass workflowClass = getMetadata().getMetaclass(Metadata.WORKFLOW_CLASS_NAME);
      Metaclass primaryClass = getMetadata().getMetaclass("WorkflowTrace");
      Metaclass schedulerClass = getMetadata().getMetaclass("TestSysWorkflowSchedulerBatchJob");
      Instance primary = (Instance)primaryClass.invoke("new");
      Instance primary2 = (Instance)primaryClass.invoke("new");
      Instance primary3 = (Instance)primaryClass.invoke("new");
      Instance primary4 = (Instance)primaryClass.invoke("new");
      Instance scheduler = (Instance)schedulerClass.getValue("instance");
      InstanceList workflowList, assignmentList;
      Instance workflow, assignment;
      List invocationList;

      // Initialization
      primary.setValue("trace", null);
      primary.setValue("throwOuterEx", Boolean.FALSE);
      primary.setValue("throwUncaughtEx", Boolean.FALSE);
      primary2.setValue("trace", null);
      primary2.setValue("throwOuterEx", Boolean.FALSE);
      primary2.setValue("throwUncaughtEx", Boolean.FALSE);
      primary3.setValue("trace", null);
      primary3.setValue("throwOuterEx", Boolean.FALSE);
      primary3.setValue("throwUncaughtEx", Boolean.FALSE);
      primary4.setValue("trace", null);
      primary4.setValue("throwOuterEx", Boolean.FALSE);
      primary4.setValue("throwUncaughtEx", Boolean.FALSE);


      /*
       * Test: One job, inside time window
       */
      schedulerClass.setValue("time", Primitive.toTimestamp(Primitive.createInteger(1000)));
      primary.setValue("trace", null);
      primary.setValue("queueName", "Semaphore");
      primary.invoke("goSemaphore");
      workflowList = (InstanceList)workflowClass.invoke("forInstance",
         new Object[]{primary});
      assertEquals(1, workflowList.size());
      workflow = workflowList.getInstance(0);
      assignmentList = (InstanceList)workflow.getValue("assignments");
      assertEquals(1, assignmentList.size());
      assignment = assignmentList.getInstance(0);
      assertEquals(Boolean.TRUE, assignment.getValue("semaphore"));
      assertEquals(Primitive.ONE_INTEGER, assignment.getValue("status"));
      invocationList = (List)schedulerClass.getValue("asyncInvokeList");
      assertEquals(1, invocationList.size());
      assertEquals(assignment.getOID(), invocationList.get(0));

      // Simulate run of Job
      assignment.invoke("schedulerRun");
      assertEquals(Instance.DELETED, assignment.getState());
      assertEquals(Instance.DELETED, workflow.getState());
      assertEquals("BEFORE;ASSIGNMENT_CREATE;EXECUTE_SEMAPHORE;END_SEMAPHORE;ASSIGNMENT_DELETE;AFTER;", primary.getValue("trace"));
      commit();


      /*
       * Test: Two jobs, queue concurrency = 1
       */
      ((List)schedulerClass.getValue("asyncInvokeList")).clear();
      primary.setValue("queueName", "SemaphoreLowConcurrency");
      primary2.setValue("queueName", "SemaphoreLowConcurrency");
      primary.setValue("trace", null);
      primary2.setValue("trace", null);
      primary.invoke("goSemaphore");
      Thread.sleep(50);
      primary2.invoke("goSemaphore");

      // Job 2: NOT RUNNING
      workflowList = (InstanceList)workflowClass.invoke("forInstance",
         new Object[]{primary2});
      assertEquals(1, workflowList.size());
      workflow = workflowList.getInstance(0);
      assignmentList = (InstanceList)workflow.getValue("assignments");
      assertEquals(1, assignmentList.size());
      assignment = assignmentList.getInstance(0);
      assertEquals(Primitive.ZERO_INTEGER, assignment.getValue("status"));
      assertEquals("BEFORE;ASSIGNMENT_CREATE;", primary2.getValue("trace"));

      // Job 1: RUNNING
      workflowList = (InstanceList)workflowClass.invoke("forInstance",
         new Object[]{primary});
      assertEquals(1, workflowList.size());
      workflow = workflowList.getInstance(0);
      assignmentList = (InstanceList)workflow.getValue("assignments");
      assertEquals(1, assignmentList.size());
      assignment = assignmentList.getInstance(0);
      assertEquals(Primitive.ONE_INTEGER, assignment.getValue("status"));
      invocationList = (List)schedulerClass.getValue("asyncInvokeList");
      assertEquals(1, invocationList.size());
      assertEquals(assignment.getOID(), invocationList.get(0));

      // Simulate run of Job 1
      assignment.invoke("schedulerRun");
      assertEquals(Instance.DELETED, assignment.getState());
      assertEquals(Instance.DELETED, workflow.getState());
      assertEquals("BEFORE;ASSIGNMENT_CREATE;EXECUTE_SEMAPHORE;END_SEMAPHORE;ASSIGNMENT_DELETE;AFTER;", primary.getValue("trace"));

      // Job 1 finished, Job 2 should have been run
      workflowList = (InstanceList)workflowClass.invoke("forInstance",
         new Object[]{primary2});
      assertEquals(1, workflowList.size());
      workflow = workflowList.getInstance(0);
      assignmentList = (InstanceList)workflow.getValue("assignments");
      assertEquals(1, assignmentList.size());
      assignment = assignmentList.getInstance(0);
      assertEquals(Primitive.ONE_INTEGER, assignment.getValue("status"));
      invocationList = (List)schedulerClass.getValue("asyncInvokeList");
      assertEquals(2, invocationList.size());
      assertEquals(assignment.getOID(), invocationList.get(1));

      // Simulate run of Job 2
      assignment.invoke("schedulerRun");
      assertEquals(Instance.DELETED, assignment.getState());
      assertEquals(Instance.DELETED, workflow.getState());
      assertEquals("BEFORE;ASSIGNMENT_CREATE;EXECUTE_SEMAPHORE;END_SEMAPHORE;ASSIGNMENT_DELETE;AFTER;", primary2.getValue("trace"));
      commit();


      /*
       * Test: Four jobs. System concurrency: 2.
       * Job 1: Low concurrency queue    RUN...FINISH
       * Job 2: Low concurrency queue    WAIT       |           RUN...FINISH
       * Job 3: High concurrency queue   RUN........|.....FINISH^
       * Job 4: High concurrency queue   WAIT       RUN...........FINISH
       */
      ((List)schedulerClass.getValue("asyncInvokeList")).clear();
      primary.setValue("queueName", "SemaphoreLowConcurrency");
      primary2.setValue("queueName", "SemaphoreLowConcurrency");
      primary3.setValue("queueName", "Semaphore");
      primary4.setValue("queueName", "Semaphore");
      primary.setValue("trace", null);
      primary2.setValue("trace", null);
      primary3.setValue("trace", null);
      primary4.setValue("trace", null);
      primary.invoke("goSemaphore");
      Thread.sleep(50);
      primary2.invoke("goSemaphore");
      Thread.sleep(50);
      primary3.invoke("goSemaphore");
      Thread.sleep(50);
      primary4.invoke("goSemaphore");


      invocationList = (List)schedulerClass.getValue("asyncInvokeList");
      assertEquals(2, invocationList.size());
     

      // Job 4: NOT RUNNING
      workflowList = (InstanceList)workflowClass.invoke("forInstance",
         new Object[]{primary4});
      assertEquals(1, workflowList.size());
      workflow = workflowList.getInstance(0);
      assignmentList = (InstanceList)workflow.getValue("assignments");
      assertEquals(1, assignmentList.size());
      assignment = assignmentList.getInstance(0);
      assertEquals(Primitive.ZERO_INTEGER, assignment.getValue("status"));
      assertEquals("BEFORE;ASSIGNMENT_CREATE;", primary4.getValue("trace"));

      // Job 2: NOT RUNNING
      workflowList = (InstanceList)workflowClass.invoke("forInstance",
         new Object[]{primary2});
      assertEquals(1, workflowList.size());
      workflow = workflowList.getInstance(0);
      assignmentList = (InstanceList)workflow.getValue("assignments");
      assertEquals(1, assignmentList.size());
      assignment = assignmentList.getInstance(0);
      assertEquals(Primitive.ZERO_INTEGER, assignment.getValue("status"));
      assertEquals("BEFORE;ASSIGNMENT_CREATE;", primary2.getValue("trace"));

      // Job 3: RUNNING
      workflowList = (InstanceList)workflowClass.invoke("forInstance",
         new Object[]{primary3});
      assertEquals(1, workflowList.size());
      workflow = workflowList.getInstance(0);
      assignmentList = (InstanceList)workflow.getValue("assignments");
      assertEquals(1, assignmentList.size());
      assignment = assignmentList.getInstance(0);
      assertEquals(Primitive.ONE_INTEGER, assignment.getValue("status"));
      assertEquals(assignment.getOID(), invocationList.get(1));

      // Job 1: RUNNING
      workflowList = (InstanceList)workflowClass.invoke("forInstance",
         new Object[]{primary});
      assertEquals(1, workflowList.size());
      workflow = workflowList.getInstance(0);
      assignmentList = (InstanceList)workflow.getValue("assignments");
      assertEquals(1, assignmentList.size());
      assignment = assignmentList.getInstance(0);
      assertEquals(Primitive.ONE_INTEGER, assignment.getValue("status"));
      assertEquals(assignment.getOID(), invocationList.get(0));

      // Simulate run/finish of Job 1
      assignment.invoke("schedulerRun");
      assertEquals(Instance.DELETED, assignment.getState());
      assertEquals(Instance.DELETED, workflow.getState());
      assertEquals("BEFORE;ASSIGNMENT_CREATE;EXECUTE_SEMAPHORE;END_SEMAPHORE;ASSIGNMENT_DELETE;AFTER;", primary.getValue("trace"));
      commit();

      // Job 2 or 4 could run: 4 runs because its queue has higher priority
      assertEquals(3, invocationList.size());

      // Job 4: RUNNING
      workflowList = (InstanceList)workflowClass.invoke("forInstance",
         new Object[]{primary4});
      assertEquals(1, workflowList.size());
      workflow = workflowList.getInstance(0);
      assignmentList = (InstanceList)workflow.getValue("assignments");
      assertEquals(1, assignmentList.size());
      assignment = assignmentList.getInstance(0);
      assertEquals(Primitive.ONE_INTEGER, assignment.getValue("status"));
      assertEquals(assignment.getOID(), invocationList.get(2));

      // Job 2: NOT RUNNING
      workflowList = (InstanceList)workflowClass.invoke("forInstance",
         new Object[]{primary2});
      assertEquals(1, workflowList.size());
      workflow = workflowList.getInstance(0);
      assignmentList = (InstanceList)workflow.getValue("assignments");
      assertEquals(1, assignmentList.size());
      assignment = assignmentList.getInstance(0);
      assertEquals(Primitive.ZERO_INTEGER, assignment.getValue("status"));
      assertEquals("BEFORE;ASSIGNMENT_CREATE;", primary2.getValue("trace"));
     
      // Job 3: RUNNING
      workflowList = (InstanceList)workflowClass.invoke("forInstance",
         new Object[]{primary3});
      assertEquals(1, workflowList.size());
      workflow = workflowList.getInstance(0);
      assignmentList = (InstanceList)workflow.getValue("assignments");
      assertEquals(1, assignmentList.size());
      assignment = assignmentList.getInstance(0);
      assertEquals(Primitive.ONE_INTEGER, assignment.getValue("status"));
      assertEquals(assignment.getOID(), invocationList.get(1));

      // Simulate run/finish of Job 3
      assignment.invoke("schedulerRun");
      assertEquals(Instance.DELETED, assignment.getState());
      assertEquals(Instance.DELETED, workflow.getState());
      assertEquals("BEFORE;ASSIGNMENT_CREATE;EXECUTE_SEMAPHORE;END_SEMAPHORE;ASSIGNMENT_DELETE;AFTER;", primary3.getValue("trace"));
      commit();

      // Job 2 runs
      assertEquals(4, invocationList.size());

      // Job 2: RUNNING
      workflowList = (InstanceList)workflowClass.invoke("forInstance",
         new Object[]{primary2});
      assertEquals(1, workflowList.size());
      workflow = workflowList.getInstance(0);
      assignmentList = (InstanceList)workflow.getValue("assignments");
      assertEquals(1, assignmentList.size());
      assignment = assignmentList.getInstance(0);
      assertEquals(Primitive.ONE_INTEGER, assignment.getValue("status"));
      assertEquals(assignment.getOID(), invocationList.get(3));
     
      // Job 4: RUNNING
      workflowList = (InstanceList)workflowClass.invoke("forInstance",
         new Object[]{primary4});
      assertEquals(1, workflowList.size());
      workflow = workflowList.getInstance(0);
      assignmentList = (InstanceList)workflow.getValue("assignments");
      assertEquals(1, assignmentList.size());
      assignment = assignmentList.getInstance(0);
      assertEquals(Primitive.ONE_INTEGER, assignment.getValue("status"));
      assertEquals(assignment.getOID(), invocationList.get(2));

      // Simulate run/finish of Job 4
      assignment.invoke("schedulerRun");
      assertEquals(Instance.DELETED, assignment.getState());
      assertEquals(Instance.DELETED, workflow.getState());
      assertEquals("BEFORE;ASSIGNMENT_CREATE;EXECUTE_SEMAPHORE;END_SEMAPHORE;ASSIGNMENT_DELETE;AFTER;", primary4.getValue("trace"));
      assertEquals(4, invocationList.size());

      // Job 2: RUNNING
      workflowList = (InstanceList)workflowClass.invoke("forInstance",
         new Object[]{primary2});
      assertEquals(1, workflowList.size());
      workflow = workflowList.getInstance(0);
      assignmentList = (InstanceList)workflow.getValue("assignments");
      assertEquals(1, assignmentList.size());
      assignment = assignmentList.getInstance(0);
      assertEquals(Primitive.ONE_INTEGER, assignment.getValue("status"));
      assertEquals(assignment.getOID(), invocationList.get(3));

      // Simulate run/finish of Job 2
      assignment.invoke("schedulerRun");
      assertEquals(Instance.DELETED, assignment.getState());
      assertEquals(Instance.DELETED, workflow.getState());
      assertEquals("BEFORE;ASSIGNMENT_CREATE;EXECUTE_SEMAPHORE;END_SEMAPHORE;ASSIGNMENT_DELETE;AFTER;", primary2.getValue("trace"));
      assertEquals(4, invocationList.size());


      /*
       * Test: One job, outside of time window.
       */
      ((List)schedulerClass.getValue("asyncInvokeList")).clear();
      schedulerClass.setValue("time", Primitive.toTimestamp(Primitive.createInteger(15000)));
      primary.setValue("trace", null);
      primary.setValue("queueName", "Semaphore");
      primary.invoke("goSemaphore");
      workflowList = (InstanceList)workflowClass.invoke("forInstance",
         new Object[]{primary});
      assertEquals(1, workflowList.size());
      workflow = workflowList.getInstance(0);
      assignmentList = (InstanceList)workflow.getValue("assignments");
      assertEquals(1, assignmentList.size());
      assignment = assignmentList.getInstance(0);
      assertEquals(Boolean.TRUE, assignment.getValue("semaphore"));
      assertEquals(Primitive.ZERO_INTEGER, assignment.getValue("status"));
      invocationList = (List)schedulerClass.getValue("asyncInvokeList");
      assertEquals(0, invocationList.size());

      // Beginning of next time window is at 60 seconds.
      assertEquals(Primitive.toTimestamp(Primitive.createLong(60000)), scheduler.getValue("start"));
      assertEquals(Primitive.createLong(45000), scheduler.getValue("period"));

      // Simulate timer tick at beginning of next window
      schedulerClass.setValue("time", Primitive.toTimestamp(Primitive.createInteger(60000)));
      m_context.setSecure(false);
      scheduler.invoke("run");
      m_context.setSecure(true);

      assertEquals(Primitive.ONE_INTEGER, assignment.getValue("status"));
      assertEquals(1, invocationList.size());
      assertEquals(assignment.getOID(), invocationList.get(0));

      // Simulate run of Job
      assignment.invoke("schedulerRun");
      assertEquals(Instance.DELETED, assignment.getState());
      assertEquals(Instance.DELETED, workflow.getState());
      assertEquals("BEFORE;ASSIGNMENT_CREATE;EXECUTE_SEMAPHORE;END_SEMAPHORE;ASSIGNMENT_DELETE;AFTER;", primary.getValue("trace"));
      commit();
   }
View Full Code Here

   {
      Metaclass workflowClass = getMetadata().getMetaclass(Metadata.WORKFLOW_CLASS_NAME);
      Metaclass primaryClass = getMetadata().getMetaclass("WorkflowTrace");
      Metaclass schedulerClass = getMetadata().getMetaclass("TestSysWorkflowSchedulerBatchJob");
      Metaclass throttleCounterBatchJobClass = getMetadata().getMetaclass("SysQueueThrottleCounterBatchJob");
      Instance primary = (Instance)primaryClass.invoke("new");
      Instance primary2 = (Instance)primaryClass.invoke("new");
      Instance primary3 = (Instance)primaryClass.invoke("new");
      Instance scheduler = (Instance)schedulerClass.getValue("instance");
      Instance throttleCounterBatchJob = (Instance)throttleCounterBatchJobClass.getValue("instance");
      InstanceList workflowList, assignmentList;
      Instance workflow, assignment;
      List invocationList;

      // Initialization
      primary.setValue("trace", null);
      primary.setValue("throwOuterEx", Boolean.FALSE);
      primary.setValue("throwUncaughtEx", Boolean.FALSE);
      primary2.setValue("trace", null);
      primary2.setValue("throwOuterEx", Boolean.FALSE);
      primary2.setValue("throwUncaughtEx", Boolean.FALSE);
      primary3.setValue("trace", null);
      primary3.setValue("throwOuterEx", Boolean.FALSE);
      primary3.setValue("throwUncaughtEx", Boolean.FALSE);


      /*
       * Test: 3 jobs:
       * Job 1: started at 0 seconds, runs immediately (in window)
       * Job 2: started at 8 seconds, runs immediately (in window)
       * Job 3: started at 9 seconds, doesn't run (throttle exceeded)
       *
       * @ 60 seconds: Timer tick, job counter reset, job 3 resumed.
       */
      schedulerClass.setValue("time", Primitive.toTimestamp(Primitive.ZERO_INTEGER));
      ((List)schedulerClass.getValue("asyncInvokeList")).clear();
      primary.setValue("queueName", "SemaphoreThrottled");
      primary2.setValue("queueName", "SemaphoreThrottled");
      primary3.setValue("queueName", "SemaphoreThrottled");
      primary.setValue("trace", null);
      primary2.setValue("trace", null);
      primary3.setValue("trace", null);
      primary.invoke("goSemaphore");

      invocationList = (List)schedulerClass.getValue("asyncInvokeList");
      assertEquals(1, invocationList.size());

      // Job 1: RUNNING
      workflowList = (InstanceList)workflowClass.invoke("forInstance",
         new Object[]{primary});
      assertEquals(1, workflowList.size());
      workflow = workflowList.getInstance(0);
      assignmentList = (InstanceList)workflow.getValue("assignments");
      assertEquals(1, assignmentList.size());
      assignment = assignmentList.getInstance(0);
      assertEquals(Primitive.ONE_INTEGER, assignment.getValue("status"));
      assertEquals(assignment.getOID(), invocationList.get(0));

      // Simulate run/finish of Job 1
      assignment.invoke("schedulerRun");
      assertEquals(Instance.DELETED, assignment.getState());
      assertEquals(Instance.DELETED, workflow.getState());
      assertEquals("BEFORE;ASSIGNMENT_CREATE;EXECUTE_SEMAPHORE;END_SEMAPHORE;ASSIGNMENT_DELETE;AFTER;", primary.getValue("trace"));
      commit();


      // @ 8 seconds
      schedulerClass.setValue("time", Primitive.toTimestamp(Primitive.createInteger(8000)));
      primary2.invoke("goSemaphore");

      // Job 2: RUNNING
      workflowList = (InstanceList)workflowClass.invoke("forInstance",
         new Object[]{primary2});
      assertEquals(1, workflowList.size());
      workflow = workflowList.getInstance(0);
      assignmentList = (InstanceList)workflow.getValue("assignments");
      assertEquals(1, assignmentList.size());
      assignment = assignmentList.getInstance(0);
      assertEquals(Primitive.ONE_INTEGER, assignment.getValue("status"));
      assertEquals(2, invocationList.size());
      assertEquals(assignment.getOID(), invocationList.get(1));

      // Simulate run/finish of Job 2
      assignment.invoke("schedulerRun");
      assertEquals(Instance.DELETED, assignment.getState());
      assertEquals(Instance.DELETED, workflow.getState());
      assertEquals("BEFORE;ASSIGNMENT_CREATE;EXECUTE_SEMAPHORE;END_SEMAPHORE;ASSIGNMENT_DELETE;AFTER;", primary2.getValue("trace"));
      commit();

      // @ 9 seconds
      schedulerClass.setValue("time", Primitive.toTimestamp(Primitive.createInteger(9000)));
      primary3.invoke("goSemaphore");

      // Job 3: NOT RUNNING
      workflowList = (InstanceList)workflowClass.invoke("forInstance",
         new Object[]{primary3});
      assertEquals(1, workflowList.size());
      workflow = workflowList.getInstance(0);
      assignmentList = (InstanceList)workflow.getValue("assignments");
      assertEquals(1, assignmentList.size());
      assignment = assignmentList.getInstance(0);
      assertEquals(Primitive.ZERO_INTEGER, assignment.getValue("status"));
      assertEquals("BEFORE;ASSIGNMENT_CREATE;", primary3.getValue("trace"));
      assertEquals(2, invocationList.size());


      // @ 60 seconds
      m_context.setSecure(false);
      throttleCounterBatchJob.invoke("run");
      m_context.setSecure(true);
      schedulerClass.setValue("time", Primitive.toTimestamp(Primitive.createInteger(60000)));
      scheduler.invoke("schedule");
      assertEquals(3, invocationList.size());
      workflowList = (InstanceList)workflowClass.invoke("forInstance",
         new Object[]{primary3});
      assertEquals(1, workflowList.size());
      workflow = workflowList.getInstance(0);
      assignmentList = (InstanceList)workflow.getValue("assignments");
      assertEquals(1, assignmentList.size());
      assignment = assignmentList.getInstance(0);
      assertEquals(Primitive.ONE_INTEGER, assignment.getValue("status"));
      assertEquals(assignment.getOID(), invocationList.get(2));

      // Simulate run/finish of Job 3
      assignment.invoke("schedulerRun");
      assertEquals(Instance.DELETED, assignment.getState());
      assertEquals(Instance.DELETED, workflow.getState());
      assertEquals("BEFORE;ASSIGNMENT_CREATE;EXECUTE_SEMAPHORE;END_SEMAPHORE;ASSIGNMENT_DELETE;AFTER;", primary3.getValue("trace"));
      commit();
   }
View Full Code Here

   public void testWorkflowProcessEvent() throws Exception
   {
      Metaclass workflowClass = getMetadata().getMetaclass(Metadata.WORKFLOW_CLASS_NAME);
      Metaclass primaryClass = getMetadata().getMetaclass("WorkflowTrace");
      Metaclass schedulerClass = getMetadata().getMetaclass("TestSysWorkflowSchedulerBatchJob");
      Instance primary = (Instance)primaryClass.invoke("new");
      InstanceList workflowList, assignmentList, timerList;
      Instance workflow, assignment, timeoutTimer;
      List invocationList;

      // Initialization
      primary.setValue("trace", null);
      primary.setValue("throwOuterEx", Boolean.FALSE);
      primary.setValue("throwUncaughtEx", Boolean.FALSE);

      /*
       * Test: One job, inside time window.
       */
      schedulerClass.setValue("time", Primitive.toTimestamp(Primitive.createInteger(1000)));
      primary.setValue("trace", null);
      primary.setValue("queueName", "Semaphore");
      timerList = getNonInitialTimers();
      assertEquals(0, timerList.size());
      primary.invoke("goProcessEvent");
      workflowList = (InstanceList)workflowClass.invoke("forInstance",
         new Object[]{primary});
      assertEquals(1, workflowList.size());
      workflow = workflowList.getInstance(0);
      assignmentList = (InstanceList)workflow.getValue("assignments");
      assertEquals(1, assignmentList.size());
      assignment = assignmentList.getInstance(0);
      assertEquals(Boolean.TRUE, assignment.getValue("semaphore"));
      assertEquals(Primitive.ONE_INTEGER, assignment.getValue("status"));
      invocationList = (List)schedulerClass.getValue("asyncInvokeList");
      assertEquals(1, invocationList.size());
      assertEquals(assignment.getOID(), invocationList.get(0));
      assertEquals("BEGIN;", primary.getValue("trace"));
      timerList = getNonInitialTimers();
      assertEquals(0, timerList.size());


      // Simulate run
      assignment.invoke("schedulerRun");
      assertEquals(Instance.DELETED, assignment.getState());
      assertEquals(Instance.DELETED, workflow.getState());
      assertEquals("BEGIN;PROCESS_EVENT;PROCESS_EVENT_DONE;END;", primary.getValue("trace"));
      timerList = getNonInitialTimers();
      assertEquals(0, timerList.size());
      commit();


      /*
       * Test 2: One job, outside time window, canceled by class event.
       */
      schedulerClass.setValue("time", Primitive.toTimestamp(Primitive.createInteger(30000*3)));
      ((List)schedulerClass.getValue("asyncInvokeList")).clear();
      primary.setValue("trace", null);
      primary.setValue("queueName", "Semaphore");
      timerList = getNonInitialTimers();
      assertEquals(0, timerList.size());
      primary.invoke("goProcessEvent");
      workflowList = (InstanceList)workflowClass.invoke("forInstance",
         new Object[]{primary});
      assertEquals(1, workflowList.size());
      workflow = workflowList.getInstance(0);
      assignmentList = (InstanceList)workflow.getValue("assignments");
      assertEquals(1, assignmentList.size());
      assignment = assignmentList.getInstance(0);
      assertEquals(Boolean.TRUE, assignment.getValue("semaphore"));
      assertEquals(Primitive.ZERO_INTEGER, assignment.getValue("status"));
      invocationList = (List)schedulerClass.getValue("asyncInvokeList");
      assertEquals(0, invocationList.size());
      assertEquals("BEGIN;", primary.getValue("trace"));

      // Scheduler runs
      commit();
      timerList = getNonInitialTimers();
      assertEquals(2, timerList.size())// Scheduler timer and the TimerEvent timer
      assertEquals(Instance.CLEAN, assignment.getState());
      assertEquals(Instance.CLEAN, workflow.getState());
      assertEquals("BEGIN;", primary.getValue("trace"));

      // Cancel event
      primary.invoke("resumeProcessEvent");
      assertEquals(Instance.DELETED, assignment.getState());
      assertEquals(Instance.DELETED, workflow.getState());
      assertEquals("BEGIN;CLASS_EVENT;END;", primary.getValue("trace"));
      timerList = getNonInitialTimers();
      assertEquals(1, timerList.size())// The scheduler timer remains
      timerList.getInstance(0).delete();
      commit();


      /*
       * Test 3: One job, outside time window, times out by Timer Event.
       */
      schedulerClass.setValue("time", Primitive.toTimestamp(Primitive.createInteger(30000*1)));
      ((List)schedulerClass.getValue("asyncInvokeList")).clear();
      primary.setValue("trace", null);
      primary.setValue("queueName", "Semaphore");
      timerList = getNonInitialTimers();
      assertEquals(0, timerList.size());
      primary.invoke("goProcessEvent");
      workflowList = (InstanceList)workflowClass.invoke("forInstance",
         new Object[]{primary});
      assertEquals(1, workflowList.size());
      workflow = workflowList.getInstance(0);
      assignmentList = (InstanceList)workflow.getValue("assignments");
      assertEquals(1, assignmentList.size());
      assignment = assignmentList.getInstance(0);
      assertEquals(Boolean.TRUE, assignment.getValue("semaphore"));
      assertEquals(Primitive.ZERO_INTEGER, assignment.getValue("status"));
      invocationList = (List)schedulerClass.getValue("asyncInvokeList");
      assertEquals(0, invocationList.size());
      assertEquals("BEGIN;", primary.getValue("trace"));

      // Scheduler runs
      commit();
      timerList = getNonInitialTimers();
      assertEquals(2, timerList.size())// Scheduler timer and the TimerEvent timer
      assertEquals(Instance.CLEAN, assignment.getState());
      assertEquals(Instance.CLEAN, workflow.getState());
      assertEquals("BEGIN;", primary.getValue("trace"));

      // Timeout event
      timeoutTimer = getTimeoutTimer(workflow);
      assertNotNull(timeoutTimer);
      workflow.invoke("timeout", new Object[] {
         timeoutTimer.getValue("ordinal")
      });
      assertEquals(Instance.DELETED, assignment.getState());
      assertEquals(Instance.DELETED, workflow.getState());
      assertEquals("BEGIN;TIMED_OUT;END;", primary.getValue("trace"));
      timeoutTimer.delete()// Simulated timeout must remove timer manually
      commit();
      timerList = getNonInitialTimers();
      assertEquals(1, timerList.size())// The scheduler timer remains
View Full Code Here

TOP

Related Classes of nexj.core.runtime.Instance

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.