/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. * To change this template, choose Tools | Templates
  24. * and open the template in the editor.
  25. */
  26. package org.mobicents.protocols.ss7.mtp;
  27. import static org.testng.Assert.*;
  28. import org.testng.*;import org.testng.annotations.*;
  29. /**
  30. *
  31. * @author kulikov
  32. */
  33. public class MTPSchedulerTest implements Runnable {
  34. private MTPScheduler scheduler = new MTPScheduler();
  35. private boolean done;
  36. private boolean failed;
  37. private long stopTime;
  38. public MTPSchedulerTest() {
  39. }
  40. @BeforeClass
  41. public static void setUpClass() throws Exception {
  42. }
  43. @AfterClass
  44. public static void tearDownClass() throws Exception {
  45. }
  46. /**
  47. * Test of schedule method, of class MTPScheduler.
  48. */
  49. @Test
  50. public void testSchedule() throws InterruptedException {
  51. stopTime = System.currentTimeMillis() + 5000;
  52. new Thread(this).start();
  53. Thread.currentThread().sleep(1000);
  54. MTPTask task = new TestTask();
  55. scheduler.schedule(task, 1000);
  56. Thread.currentThread().sleep(5000);
  57. assertTrue(done, "Task is not executed");
  58. assertFalse(failed, "Task executed more then one time");
  59. }
  60. /**
  61. * Test of schedule method, of class MTPScheduler.
  62. */
  63. @Test
  64. public void testCancel() throws InterruptedException {
  65. this.done = false;
  66. this.failed = false;
  67. stopTime = System.currentTimeMillis() + 5000;
  68. new Thread(this).start();
  69. Thread.currentThread().sleep(1000);
  70. MTPTask task = new TestTask();
  71. scheduler.schedule(task, 3000);
  72. Thread.currentThread().sleep(1000);
  73. task.cancel();
  74. Thread.currentThread().sleep(4000);
  75. assertFalse(done, "Task is executed");
  76. assertFalse(failed, "Task executed more then one time");
  77. }
  78. public void run() {
  79. while (System.currentTimeMillis() < stopTime) {
  80. scheduler.tick();
  81. }
  82. }
  83. private class TestTask extends MTPTask {
  84. @Override
  85. public void perform() {
  86. if (done) {
  87. failed = true;
  88. }
  89. done = true;
  90. }
  91. }
  92. }