Examples of Barrier


Examples of org.apache.derbyTesting.functionTests.util.Barrier

        commit();

        // Object used for synchronization between main thread and helper
        // thread. They should both wait for the other thread to reach the
        // barrier point before continuing.
        final Barrier barrier = new Barrier(2);

        // Hold a lock in a different thread to stop the index scan, then
        // split the first leaf (on which the scan is positioned) before the
        // lock is released.
        new AsyncThread(new AsyncTask() {
            public void doWork(Connection conn) throws Exception {
                conn.setAutoCommit(false);
                Statement s = conn.createStatement();
                s.executeUpdate("update t set x = x where x = 40");

                // Tell the main thread we have locked the row, and wait for
                // it to start the index scan.
                barrier.await();

                // Give the index scan time to get to the row we have locked.
                Thread.sleep(1000);

                // The index scan should be blocked now. Split the first leaf
                // by inserting more values just before the lowest key, so
                // that we can verify that the index scan is able to reposition
                // correctly after a page split.
                for (int i = 0; i < 300; i++) {
                    s.executeUpdate("insert into t values -1");
                }
                s.close();
                conn.commit();
            }
        });

        // Perform an index scan. Will be blocked for a while when fetching
        // the row where x=40, but should be able to resume the scan after
        // the helper thread commits and releases its locks.
        ResultSet rs = s.executeQuery(
                "select * from t --DERBY-PROPERTIES index=IDX");

        for (int i = 0; i < 300; i++) {
            assertTrue(rs.next());
            assertEquals(i, rs.getInt(1));

            // Once we have fetched the first row, tell the helper thread we
            // have started the index scan, and wait until it has locked the
            // row that should block the scan (x=40).
            if (i == 0) {
                barrier.await();
            }
        }
        assertFalse(rs.next());
        rs.close();
    }
View Full Code Here

Examples of org.apache.derbyTesting.functionTests.util.Barrier

        PreparedStatement ps1 = prepareStatement("select * from t where id=2");
        final PreparedStatement ps2 =
                c2.prepareStatement("select * from t where id=1");

        // Create a barrier for the two threads to synchronize.
        final Barrier barrier = new Barrier(2);

        final SQLException[] holder = new SQLException[2];
        final Throwable[] unexpected = new Throwable[1];
        Thread t = new Thread(new Runnable() {
                public void run() {
                    try {
                        // Let the main thread know the helper thread has
                        // started. The race for the locks can start.
                        barrier.await();

                        // This statement will be blocked because T1 holds
                        // an exclusive lock on the row we want.
                        JDBC.assertDrainResults(ps2.executeQuery());
                    } catch (SQLException e) {
                        holder[0] = e;
                    } catch (Throwable t) {
                        unexpected[0] = t;
                    }
                }
            });
        t.start();

        // Wait until the helper thread has started. Once the call returns,
        // both threads are ready, and the race for the locks can start.
        barrier.await();

        // This statement will be blocked because T2 holds an exclusive lock
        // on the row we want. So now we have T1 waiting for T2, and T2 waiting
        // for T1, and one of the transactions should be terminated because of
        // the deadlock.
View Full Code Here

Examples of org.apache.derbyTesting.functionTests.util.Barrier

        // helper thread. The main thread uses it to tell the helper thread
        // that it has started the scan. The helper thread uses it
        // to tell the main thread that it is ready.
        // Both threads should wait until the other thread
        // has reached the barrier before continuing.
        final Barrier barrier = new Barrier(2);
       
        // start the second thread and make it do the same update
        new AsyncThread(new AsyncTask() {
            public void doWork(Connection conn) throws Exception {
                conn.setAutoCommit(false);
                Statement s = conn.createStatement();
                // note: asserts in this inner class do not make the test fail
                // so, just executing it here
                s.executeUpdate("update account set b = b + 11");
                s.close();
               
                // Tell the main thread that we've locked the row
                barrier.await();
               
                // The main thread now can continue - give it a
                // second to do its stuff
                //Thread.sleep(1000L);
                // we check that the 'wait' state is gone at the main thread,
                // it would not cause the test to fail if we checked it here.
            }
        });
       
        // now select from syscs_diag.lock_table, don't wait more than minute.
        int totalWait = 0;
        boolean found=false;
        do {
            totalWait += 500;
            Thread.sleep(500);
            // we want to look for 'WAIT' state. There will also
            // be one of more 'GRANT' state locks, likely background threads,
            // but we're not interested in those here.
            found=getWaitState();
        } while (!found && totalWait < 6000);
        // defer the assert until we've alerted the async thread
        // commit will release the lock
        commit();
       
        // set the timeout back so things can timeout.
        s.executeUpdate("call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY" +
                "('derby.locks.waitTimeout','5')");
        commit();
       
        // Now that we've found the wait state, tell the helper thread we
        // are done
        barrier.await();
       
        // now that we've released the helper thread, we can safely let
        // the test fail if the results of the earlier check were bad
        assertTrue("expected to find a 'WAIT' state, but did not", found);
       
View Full Code Here

Examples of org.apache.sling.event.impl.Barrier

     * Test simple job execution.
     * The job is executed once and finished successfully.
     */
    @Test(timeout = DEFAULT_TEST_TIMEOUT)
    public void testSimpleJobExecutionUsingBridge() throws Exception {
        final Barrier cb = new Barrier(2);

        final ServiceRegistration reg = this.registerEventHandler(TOPIC,
                new EventHandler() {
                    @Override
                    public void handleEvent(Event event) {
                        JobUtil.acknowledgeJob(event);
                        JobUtil.finishedJob(event);
                        cb.block();
                    }

                 });

        try {
            this.eventAdmin.sendEvent(getJobEvent(null));
            assertTrue("No event received in the given time.", cb.block(5));
            cb.reset();
            assertFalse("Unexpected event received in the given time.", cb.block(5));
        } finally {
            reg.unregister();
        }
    }
View Full Code Here

Examples of org.apache.sling.event.impl.Barrier

     * Test simple job execution with job id.
     * The job is executed once and finished successfully.
     */
    @Test(timeout = DEFAULT_TEST_TIMEOUT)
    public void testSimpleJobWithIdExecution() throws Exception {
        final Barrier cb = new Barrier(2);
        final ServiceRegistration jcReg = this.registerJobConsumer(TOPIC,
                new JobConsumer() {

                    @Override
                    public JobResult process(Job job) {
                        cb.block();
                        return JobResult.OK;
                    }
                });
        try {
            final JobManager jobManager = this.getJobManager();
            jobManager.addJob(TOPIC, "myid1", null);
            assertTrue("No event received in the given time.", cb.block(5));
            cb.reset();
            assertFalse("Unexpected event received in the given time.", cb.block(5));
        } finally {
            jcReg.unregister();
        }
    }
View Full Code Here

Examples of org.apache.sling.event.impl.Barrier

     * Test force canceling a job
     * The job execution always fails
     */
    @Test(timeout = DEFAULT_TEST_TIMEOUT)
    public void testForceCancelJob() throws Exception {
        final Barrier cb = new Barrier(2);
        final ServiceRegistration jcReg = this.registerJobConsumer(TOPIC,
                new JobConsumer() {

                    @Override
                    public JobResult process(Job job) {
                        cb.block();
                        sleep(1000);
                        return JobResult.FAILED;
                    }
                });
        try {
            final JobManager jobManager = this.getJobManager();
            jobManager.addJob(TOPIC, "myid3", null);
            cb.block();

            assertEquals(1, jobManager.findJobs(JobManager.QueryType.ALL, "sling/test", -1, (Map<String, Object>[])null).size());
            // job is currently sleeping, but force cancel always waits!
            final Event e = jobManager.findJob("sling/test", Collections.singletonMap(JobUtil.PROPERTY_JOB_NAME, (Object)"myid3"));
            assertNotNull(e);
View Full Code Here

Examples of org.apache.sling.event.impl.Barrier

     * Test simple job execution.
     * The job is executed once and finished successfully.
     */
    @Test(timeout = DEFAULT_TEST_TIMEOUT)
    public void testSimpleJobExecutionUsingJobConsumer() throws Exception {
        final Barrier cb = new Barrier(2);

        final ServiceRegistration reg = this.registerJobConsumer(TOPIC,
                new JobConsumer() {

            @Override
                    public JobResult process(final Job job) {
                        cb.block();
                        return JobResult.OK;
                    }
                 });

        try {
            this.getJobManager().addJob(TOPIC, null);
            assertTrue("No event received in the given time.", cb.block(5));
            cb.reset();
            assertFalse("Unexpected event received in the given time.", cb.block(5));
        } finally {
            reg.unregister();
        }
    }
View Full Code Here

Examples of org.apache.sling.event.impl.Barrier

     * Test simple job execution.
     * The job is executed once and finished successfully.
     */
    @Test(timeout = DEFAULT_TEST_TIMEOUT)
    public void testSimpleJobExecutionUsingJobExecutor() throws Exception {
        final Barrier cb = new Barrier(2);

        final ServiceRegistration reg = this.registerJobExecutor(TOPIC,
                new JobExecutor() {

                    @Override
                    public JobExecutionResult process(final Job job, final JobExecutionContext context) {
                        cb.block();
                        return context.result().succeeded();
                    }
                });

        try {
            this.getJobManager().addJob(TOPIC, null);
            assertTrue("No event received in the given time.", cb.block(5));
            cb.reset();
            assertFalse("Unexpected event received in the given time.", cb.block(5));
        } finally {
            reg.unregister();
        }
    }
View Full Code Here

Examples of org.apache.sling.event.impl.Barrier

     * Test canceling a job
     * The job execution always fails
     */
    @Test(timeout = DEFAULT_TEST_TIMEOUT)
    public void testCancelJob() throws Exception {
        final Barrier cb = new Barrier(2);
        final Barrier cb2 = new Barrier(2);
        final ServiceRegistration jcReg = this.registerJobConsumer(TOPIC,
                new JobConsumer() {

                    @Override
                    public JobResult process(Job job) {
                        cb.block();
                        cb2.block();
                        return JobResult.FAILED;
                    }
                });
        try {
            final JobManager jobManager = this.getJobManager();
            jobManager.addJob(TOPIC, Collections.singletonMap("id", (Object)"myid2"));
            cb.block();

            assertEquals(1, jobManager.findJobs(JobManager.QueryType.ALL, TOPIC, -1, (Map<String, Object>[])null).size());
            // job is currently waiting, therefore cancel fails
            final Job e1 = jobManager.getJob(TOPIC, Collections.singletonMap("id", (Object)"myid2"));
            assertNotNull(e1);
            cb2.block(); // and continue job

            sleep(200);

            // the job is now in the queue again
            final Job e2 = jobManager.getJob(TOPIC, Collections.singletonMap("id", (Object)"myid2"));
View Full Code Here

Examples of org.apache.sling.event.impl.Barrier

    /**
     * Test get a job
     */
    @Test(timeout = DEFAULT_TEST_TIMEOUT)
    public void testGetJob() throws Exception {
        final Barrier cb = new Barrier(2);
        final Barrier cb2 = new Barrier(2);
        final ServiceRegistration jcReg = this.registerJobConsumer(TOPIC,
                new JobConsumer() {

                    @Override
                    public JobResult process(Job job) {
                        cb.block();
                        cb2.block();
                        return JobResult.OK;
                    }
                });
        try {
            final JobManager jobManager = this.getJobManager();
            final Job j = jobManager.addJob(TOPIC, null);
            cb.block();

            assertNotNull(jobManager.getJob(TOPIC, null));

            cb2.block(); // and continue job

            jobManager.removeJobById(j.getId());
        } finally {
            jcReg.unregister();
        }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.