PageRenderTime 29ms CodeModel.GetById 18ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/ss7/mtp/mtp-impl/src/test/java/org/mobicents/protocols/ss7/mtp/MTPSchedulerTest.java

http://mobicents.googlecode.com/
Java | 120 lines | 57 code | 28 blank | 35 comment | 2 complexity | 33e3f382f678ca881fbb0fd39635b0ce MD5 | raw file
  1/*
  2 * JBoss, Home of Professional Open Source
  3 * Copyright 2011, Red Hat, Inc. and individual contributors
  4 * by the @authors tag. See the copyright.txt in the distribution for a
  5 * full listing of individual contributors.
  6 *
  7 * This is free software; you can redistribute it and/or modify it
  8 * under the terms of the GNU Lesser General Public License as
  9 * published by the Free Software Foundation; either version 2.1 of
 10 * the License, or (at your option) any later version.
 11 *
 12 * This software is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15 * Lesser General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU Lesser General Public
 18 * License along with this software; if not, write to the Free
 19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21 */
 22
 23/*
 24 * To change this template, choose Tools | Templates
 25 * and open the template in the editor.
 26 */
 27
 28package org.mobicents.protocols.ss7.mtp;
 29
 30import static org.testng.Assert.*;
 31
 32import org.testng.*;import org.testng.annotations.*;
 33
 34/**
 35 *
 36 * @author kulikov
 37 */
 38public class MTPSchedulerTest implements Runnable {
 39
 40    private MTPScheduler scheduler = new MTPScheduler();
 41    
 42    private boolean done;
 43    private boolean failed;
 44    
 45    private long stopTime;
 46    
 47    public MTPSchedulerTest() {
 48    }
 49
 50    @BeforeClass
 51    public static void setUpClass() throws Exception {
 52    }
 53
 54    @AfterClass
 55    public static void tearDownClass() throws Exception {
 56    }
 57
 58
 59
 60    /**
 61     * Test of schedule method, of class MTPScheduler.
 62     */
 63    @Test
 64    public void testSchedule() throws InterruptedException {
 65        stopTime = System.currentTimeMillis() + 5000;
 66        new Thread(this).start();
 67        
 68        Thread.currentThread().sleep(1000);
 69        
 70        MTPTask task = new TestTask();
 71        scheduler.schedule(task, 1000);
 72        
 73        Thread.currentThread().sleep(5000);
 74        assertTrue(done, "Task is not executed");
 75        assertFalse(failed, "Task executed more then one time");
 76    }
 77
 78
 79    /**
 80     * Test of schedule method, of class MTPScheduler.
 81     */
 82    @Test
 83    public void testCancel() throws InterruptedException {
 84        this.done = false;
 85        this.failed = false;
 86        
 87        stopTime = System.currentTimeMillis() + 5000;
 88        new Thread(this).start();
 89        
 90        Thread.currentThread().sleep(1000);
 91        
 92        MTPTask task = new TestTask();
 93        scheduler.schedule(task, 3000);
 94        
 95        Thread.currentThread().sleep(1000);
 96        task.cancel();
 97        
 98        Thread.currentThread().sleep(4000);
 99        assertFalse(done, "Task is  executed");
100        assertFalse(failed, "Task executed more then one time");
101    }
102    
103    public void run() {
104        while (System.currentTimeMillis() < stopTime) {
105            scheduler.tick();
106        }
107    }
108
109    private class TestTask extends MTPTask {
110
111        @Override
112        public void perform() {
113            if (done) {
114                failed = true;
115            }
116            done = true;
117        }
118        
119    }
120}