PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/drools-core/src/test/java/org/drools/core/time/impl/JDKTimerServiceTest.java

https://github.com/esteban-aliverti/drools
Java | 175 lines | 126 code | 32 blank | 17 comment | 2 complexity | 385f074b20f3756a561479732c4062e7 MD5 | raw file
  1. /*
  2. * Copyright 2010 JBoss Inc
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.drools.core.time.impl;
  17. import java.io.IOException;
  18. import java.io.ObjectInput;
  19. import java.io.ObjectOutput;
  20. import java.util.ArrayList;
  21. import java.util.Date;
  22. import java.util.List;
  23. import java.util.Stack;
  24. import org.drools.core.ClockType;
  25. import org.drools.core.SessionConfiguration;
  26. import org.junit.Test;
  27. import static org.junit.Assert.*;
  28. import org.drools.core.time.Job;
  29. import org.drools.core.time.JobContext;
  30. import org.drools.core.time.JobHandle;
  31. import org.drools.core.time.TimerService;
  32. import org.drools.core.time.TimerServiceFactory;
  33. import org.drools.core.time.Trigger;
  34. public class JDKTimerServiceTest {
  35. @Test
  36. public void testSingleExecutionJob() throws Exception {
  37. SessionConfiguration config = new SessionConfiguration();
  38. config.setClockType(ClockType.REALTIME_CLOCK);
  39. TimerService timeService = TimerServiceFactory.getTimerService( config );
  40. Trigger trigger = new DelayedTrigger( 100 );
  41. HelloWorldJobContext ctx = new HelloWorldJobContext( "hello world", timeService);
  42. timeService.scheduleJob( new HelloWorldJob(), ctx, trigger);
  43. Thread.sleep( 500 );
  44. timeService.shutdown();
  45. assertEquals( 1, ctx.getList().size() );
  46. }
  47. @Test
  48. public void testRepeatedExecutionJob() throws Exception {
  49. SessionConfiguration config = new SessionConfiguration();
  50. config.setClockType(ClockType.REALTIME_CLOCK);
  51. TimerService timeService = TimerServiceFactory.getTimerService( config );
  52. Trigger trigger = new DelayedTrigger( new long[] { 100, 100, 100} );
  53. HelloWorldJobContext ctx = new HelloWorldJobContext( "hello world", timeService);
  54. timeService.scheduleJob( new HelloWorldJob(), ctx, trigger);
  55. Thread.sleep( 500 );
  56. timeService.shutdown();
  57. assertEquals( 3, ctx.getList().size() );
  58. }
  59. @Test
  60. public void testRepeatedExecutionJobWithRemove() throws Exception {
  61. SessionConfiguration config = new SessionConfiguration();
  62. config.setClockType(ClockType.REALTIME_CLOCK);
  63. TimerService timeService = TimerServiceFactory.getTimerService( config );
  64. Trigger trigger = new DelayedTrigger( new long[] {100, 100, 100, 100, 100, 100, 100, 100} );
  65. HelloWorldJobContext ctx = new HelloWorldJobContext( "hello world", timeService);
  66. ctx.setLimit( 3 );
  67. timeService.scheduleJob( new HelloWorldJob(), ctx, trigger);
  68. Thread.sleep( 1000 );
  69. timeService.shutdown();
  70. assertEquals( 5, ctx.getList().size() );
  71. }
  72. public static class HelloWorldJob implements Job {
  73. public void execute(JobContext c) {
  74. HelloWorldJobContext ctx = (HelloWorldJobContext) c;
  75. int counter = ctx.increaseCounter();
  76. if ( counter > 3 ) {
  77. ctx.timeService.removeJob( ctx.getJobHandle() );
  78. }
  79. ctx.getList().add( ((HelloWorldJobContext)ctx).getMessage() + " : " + counter);
  80. }
  81. }
  82. public static class HelloWorldJobContext implements JobContext {
  83. private String message;
  84. private TimerService timeService;
  85. private JobHandle jobHandle;
  86. private List list;
  87. private int counter;
  88. private int limit;
  89. public HelloWorldJobContext(String message, TimerService timeService) {
  90. this.message = message;
  91. this.timeService = timeService;
  92. this.list = new ArrayList();
  93. }
  94. public String getMessage() {
  95. return this.message;
  96. }
  97. public int increaseCounter() {
  98. return this.counter++;
  99. }
  100. public JobHandle getJobHandle() {
  101. return this.jobHandle;
  102. }
  103. public void setJobHandle(JobHandle jobHandle) {
  104. this.jobHandle = jobHandle;
  105. }
  106. public int getLimit() {
  107. return limit;
  108. }
  109. public void setLimit(int limit) {
  110. this.limit = limit;
  111. }
  112. public List getList() {
  113. return list;
  114. }
  115. }
  116. public static class DelayedTrigger implements Trigger {
  117. private Stack<Date> stack;
  118. public DelayedTrigger(long delay) {
  119. this( new long[] { delay } );
  120. }
  121. public DelayedTrigger(long[] delay) {
  122. this.stack = new Stack<Date>();
  123. for( int i = delay.length-1; i >= 0; i-- ) {
  124. this.stack.push( new Date( new Date().getTime() + delay[i] ) );
  125. }
  126. }
  127. public Date hasNextFireTime() {
  128. return this.stack.isEmpty() ? null : this.stack.peek();
  129. }
  130. public Date nextFireTime() {
  131. return this.stack.isEmpty() ? null : this.stack.pop();
  132. }
  133. public void readExternal(ObjectInput in) throws IOException,
  134. ClassNotFoundException {
  135. // FIXME : not safe, since timestamps will be wrong
  136. this.stack = (Stack<Date>) in.readObject();
  137. }
  138. public void writeExternal(ObjectOutput out) throws IOException {
  139. // FIXME : not safe, since timestamps will be wrong
  140. out.writeObject( stack );
  141. }
  142. }
  143. }